Completed
Push — develop ( 5b8f3d...365b4b )
by Zack
11s
created

Plugin::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 5.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
6
	die();
7
8
/**
9
 * The GravityView WordPress plugin class.
10
 *
11
 * Contains functionality related to GravityView being
12
 * a WordPress plugin and doing WordPress pluginy things.
13
 *
14
 * Accessible via gravityview()->plugin
15
 */
16
final class Plugin {
17
	/**
18
	 * @var string The plugin version.
19
	 *
20
	 * @api
21
	 * @since future
22
	 */
23
	public $version = 'future';
24
25
	/**
26
	 * @var string Minimum WordPress version.
27
	 *
28
	 * GravityView requires at least this version of WordPress to function properly.
29
	 */
30
	private static $min_wp_version = '4.0';
31
32
	/**
33
	 * @var string Minimum Gravity Forms version.
34
	 *
35
	 * GravityView requires at least this version of Gravity Forms to function properly.
36
	 */
37
	private static $min_gf_version = '1.9.14';
38
39
	/**
40
	 * @var string Minimum PHP version.
41
	 *
42
	 * GravityView requires at least this version of PHP to function properly.
43
	 */
44
	private static $min_php_version = '5.3.0';
45
46
	/**
47
	 * @var string|bool Minimum future PHP version.
48
	 *
49
	 * GravityView will require this version of PHP soon. False if no future PHP version changes are planned.
50
	 */
51
	private static $future_min_php_version = false;
0 ignored issues
show
Unused Code introduced by
The property $future_min_php_version is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
52
53
	/**
54
	 * @var string|bool Minimum future Gravity Forms version.
55
	 *
56
	 * GravityView will require this version of Gravity Forms soon. False if no future Gravity Forms version changes are planned.
57
	 */
58
	private static $future_min_gf_version = false;
0 ignored issues
show
Unused Code introduced by
The property $future_min_gf_version is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
59
60
	/**
61
	 * @var \GV\Plugin The \GV\Plugin static instance.
62
	 */
63
	private static $__instance = null;
64
65
	/**
66
	 * Get the global instance of \GV\Plugin.
67
	 *
68
	 * @return \GV\Plugin The global instance of GravityView Plugin.
69
	 */
70
	public static function get() {
71
		if ( ! self::$__instance instanceof self )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
72
			self::$__instance = new self;
73
		return self::$__instance;
74
	}
75
76
	/**
77
	 * Bootstrap.
78
	 *
79
	 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
80
	 */
81
	private function __construct() {
82
		$this->init();
83
	}
84
85
	/**
86
	 * Initialize all plugin hooks, constants, settings, etc.
87
	 *
88
	 * @return void
89
	 */
90
	private function init() {
91
		/**
92
		 * Stop all further functionality from loading if the WordPress
93
		 * plugin is incompatible with the current environment.
94
		 */
95
		if ( ! $this->is_compatible() )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
96
			return;
97
98
		/** Register hooks that are fired when the plugin is activated and deactivated. */
99
		register_activation_hook( $this->dir( 'gravityview.php' ), array( $this, 'activate' ) );
100
		register_deactivation_hook( $this->dir( 'gravityview.php' ), array( $this, 'deactivate' ) );
101
	}
102
103
	/**
104
	 * Plugin activation function.
105
	 *
106
	 * @internal
107
	 * @return void
108
	 */
109
	public function activate() {
110
		flush_rewrite_rules();
111
112
		update_option( 'gv_version', \GravityView_Plugin::version );
113
	}
114
115
	/**
116
	 * Plugin deactivation function.
117
	 *
118
	 * @internal
119
	 * @return void
120
	 */
121
	public function deactivate() {
122
		flush_rewrite_rules();
123
	}
124
125
	/**
126
	 * Retrieve an absolute  path within the Gravity Forms plugin directory.
127
	 *
128
	 * @api
129
	 * @since future
130
	 *
131
	 * @param string $path Optional. Append this extra path component.
132
	 * @return string The absolute path to the plugin directory.
133
	 */
134
	public function dir( $path = '' ) {
135
		return GRAVITYVIEW_DIR . ltrim( $path, '/' );
136
	}
137
138
	/**
139
	 * Retrieve a URL within the Gravity Forms plugin directory.
140
	 *
141
	 * @api
142
	 * @since future
143
	 *
144
	 * @param string $path Optional. Extra path appended to the URL.
145
	 * @return The URL to this plugin, with trailing slash.
146
	 */
147
	public function url( $path = '/' ) {
148
		return plugins_url( $path, $this->dir( 'gravityview.php' ) );
149
	}
150
151
	/**
152
	 * Is everything compatible with this version of GravityView?
153
	 *
154
	 * @api
155
	 * @since future
156
	 *
157
	 * @return bool
158
	 */
159
	public function is_compatible() {
160
		return
161
			$this->is_compatible_php()
162
			&& $this->is_compatible_wordpress()
163
			&& $this->is_compatible_gravityforms();
164
	}
165
166
	/**
167
	 * Is this version of GravityView compatible with the current version of PHP?
168
	 *
169
	 * @api
170
	 * @since future
171
	 *
172
	 * @return bool true if compatible, false otherwise.
173
	 */
174
	public function is_compatible_php() {
175
		return version_compare( $this->get_php_version(), self::$min_php_version, '>=' );
176
	}
177
178
	/**
179
	 * Is this version of GravityView compatible with the current version of WordPress?
180
	 *
181
	 * @api
182
	 * @since future
183
	 *
184
	 * @return bool true if compatible, false otherwise.
185
	 */
186
	public function is_compatible_wordpress() {
187
		return version_compare( $this->get_wordpress_version(), self::$min_wp_version, '>=' );
188
	}
189
190
	/**
191
	 * Is this version of GravityView compatible with the current version of Gravity Forms?
192
	 *
193
	 * @api
194
	 * @since future
195
	 *
196
	 * @return bool true if compatible, false otherwise (or not active/installed).
197
	 */
198
	public function is_compatible_gravityforms() {
199
		try {
200
			return version_compare( $this->get_gravityforms_version(), self::$min_gf_version, '>=' );
201
		} catch ( \ErrorException $e ) {
202
			return false;
203
		}
204
	}
205
206
	/**
207
	 * Retrieve the current PHP version.
208
	 *
209
	 * Overridable with GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE during testing.
210
	 *
211
	 * @return string The version of PHP.
212
	 */
213
	private function get_php_version() {
214
		return !empty( $GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] ) ?
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
215
			$GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] : phpversion();
216
	}
217
218
	/**
219
	 * Retrieve the current WordPress version.
220
	 *
221
	 * Overridable with GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE during testing.
222
	 *
223
	 * @return string The version of WordPress.
224
	 */
225
	private function get_wordpress_version() {
226
		return !empty( $GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] ) ?
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
227
			$GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] : $GLOBALS['wp_version'];
228
	}
229
230
	/**
231
	 * Retrieve the current Gravity Forms version.
232
	 *
233
	 * Overridable with GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE during testing.
234
	 *
235
	 * @throws \ErrorException If the Gravity Forms plugin is not active or installed (E_ERROR severity)
236
	 * @return string The version of Gravity Forms.
237
	 */
238
	private function get_gravityforms_version() {
239
		if ( !class_exists( '\GFCommon' ) || !empty( $GLOBALS['GRAVITYVIEW_TESTS_GF_INACTIVE_OVERRIDE'] ) )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
240
			throw new \ErrorException( __( 'Gravity Forms is inactive or not installed.', 'gravityview' ) );
241
242
		return !empty( $GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] ) ?
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
243
			$GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] : \GFCommon::$version;
244
	}
245
246
	private function __clone() { }
247
248
	private function __wakeup() { }
249
}
250