Completed
Push — master ( 39734d...9bfb60 )
by Zack
05:51 queued 02:37
created

Plugin::url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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