Completed
Push — develop ( da7b37...33756a )
by
unknown
06:46
created

Plugin::is_compatible_php()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
	 * Register hooks that are fired when the plugin is activated and deactivated.
78
	 *
79
	 * @return void
80
	 */
81
	public function register_activation_hooks() {
82
		register_activation_hook( $this->dir( 'gravityview.php' ), array( $this, 'activate' ) );
83
		register_deactivation_hook( $this->dir( 'gravityview.php' ), array( $this, 'deactivate' ) );
84
	}
85
86
	/**
87
	 * Plugin activation function.
88
	 *
89
	 * @internal
90
	 * @return void
91
	 */
92 1
	public function activate() {
93
		/** Register the gravityview post type upon WordPress core init. */
94 1
		require_once $this->dir( 'future/includes/class-gv-view.php' );
95 1
		View::register_post_type();
96
97
		/** Add the entry rewrite endpoint. */
98 1
		require_once $this->dir( 'future/includes/class-gv-entry.php' );
99 1
		Entry::add_rewrite_endpoint();
100
101
		/** Flush all URL rewrites. */
102 1
		flush_rewrite_rules();
103
104 1
		update_option( 'gv_version', \GravityView_Plugin::version );
105 1
	}
106
107
	/**
108
	 * Plugin deactivation function.
109
	 *
110
	 * @internal
111
	 * @return void
112
	 */
113
	public function deactivate() {
114
		flush_rewrite_rules();
115
	}
116
117
	/**
118
	 * Retrieve an absolute  path within the Gravity Forms plugin directory.
119
	 *
120
	 * @api
121
	 * @since future
122
	 *
123
	 * @param string $path Optional. Append this extra path component.
124
	 * @return string The absolute path to the plugin directory.
125
	 */
126 1
	public function dir( $path = '' ) {
127 1
		return GRAVITYVIEW_DIR . ltrim( $path, '/' );
128
	}
129
130
	/**
131
	 * Retrieve a URL within the Gravity Forms plugin directory.
132
	 *
133
	 * @api
134
	 * @since future
135
	 *
136
	 * @param string $path Optional. Extra path appended to the URL.
137
	 * @return The URL to this plugin, with trailing slash.
138
	 */
139 1
	public function url( $path = '/' ) {
140 1
		return plugins_url( $path, $this->dir( 'gravityview.php' ) );
141
	}
142
143
	/**
144
	 * Is everything compatible with this version of GravityView?
145
	 *
146
	 * @api
147
	 * @since future
148
	 *
149
	 * @return bool
150
	 */
151 1
	public function is_compatible() {
152
		return
153 1
			$this->is_compatible_php()
154 1
			&& $this->is_compatible_wordpress()
155 1
			&& $this->is_compatible_gravityforms();
156
	}
157
158
	/**
159
	 * Is this version of GravityView compatible with the current version of PHP?
160
	 *
161
	 * @api
162
	 * @since future
163
	 *
164
	 * @return bool true if compatible, false otherwise.
165
	 */
166 1
	public function is_compatible_php() {
167 1
		return version_compare( $this->get_php_version(), self::$min_php_version, '>=' );
168
	}
169
170
	/**
171
	 * Is this version of GravityView compatible with the current version of WordPress?
172
	 *
173
	 * @api
174
	 * @since future
175
	 *
176
	 * @return bool true if compatible, false otherwise.
177
	 */
178 1
	public function is_compatible_wordpress() {
179 1
		return version_compare( $this->get_wordpress_version(), self::$min_wp_version, '>=' );
180
	}
181
182
	/**
183
	 * Is this version of GravityView compatible with the current version of Gravity Forms?
184
	 *
185
	 * @api
186
	 * @since future
187
	 *
188
	 * @return bool true if compatible, false otherwise (or not active/installed).
189
	 */
190 1
	public function is_compatible_gravityforms() {
191
		try {
192 1
			return version_compare( $this->get_gravityforms_version(), self::$min_gf_version, '>=' );
193 1
		} catch ( \ErrorException $e ) {
194 1
			return false;
195
		}
196
	}
197
198
	/**
199
	 * Retrieve the current PHP version.
200
	 *
201
	 * Overridable with GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE during testing.
202
	 *
203
	 * @return string The version of PHP.
204
	 */
205
	private function get_php_version() {
206
		return !empty( $GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] ) ?
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
207
			$GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] : phpversion();
208
	}
209
210
	/**
211
	 * Retrieve the current WordPress version.
212
	 *
213
	 * Overridable with GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE during testing.
214
	 *
215
	 * @return string The version of WordPress.
216
	 */
217
	private function get_wordpress_version() {
218
		return !empty( $GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] ) ?
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
219
			$GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] : $GLOBALS['wp_version'];
220
	}
221
222
	/**
223
	 * Retrieve the current Gravity Forms version.
224
	 *
225
	 * Overridable with GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE during testing.
226
	 *
227
	 * @throws \ErrorException If the Gravity Forms plugin is not active or installed (E_ERROR severity)
228
	 * @return string The version of Gravity Forms.
229
	 */
230
	private function get_gravityforms_version() {
231
		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...
232
			throw new \ErrorException( 'Gravity Forms is inactive or not installed.' );
233
234
		return !empty( $GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] ) ?
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
235
			$GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] : \GFCommon::$version;
236
	}
237
238
	private function __clone() { }
239
240
	private function __wakeup() { }
241
}
242