1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace GV; |
3
|
|
|
|
4
|
|
|
/** If this file is called directly, abort. */ |
5
|
|
|
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
6
|
|
|
die(); |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The GravityView WordPress plugin class. |
11
|
|
|
* |
12
|
|
|
* Contains functionality related to GravityView being |
13
|
|
|
* a WordPress plugin and doing WordPress pluginy things. |
14
|
|
|
* |
15
|
|
|
* Accessible via gravityview()->plugin |
16
|
|
|
*/ |
17
|
|
|
final class Plugin { |
18
|
|
|
/** |
19
|
|
|
* @var string The plugin version. |
20
|
|
|
* |
21
|
|
|
* @api |
22
|
|
|
* @since future |
23
|
|
|
*/ |
24
|
|
|
public $version = 'future'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string Minimum WordPress version. |
28
|
|
|
* |
29
|
|
|
* GravityView requires at least this version of WordPress to function properly. |
30
|
|
|
*/ |
31
|
|
|
private static $min_wp_version = '4.0'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string Minimum Gravity Forms version. |
35
|
|
|
* |
36
|
|
|
* GravityView requires at least this version of Gravity Forms to function properly. |
37
|
|
|
*/ |
38
|
|
|
private static $min_gf_version = '1.9.14'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string Minimum PHP version. |
42
|
|
|
* |
43
|
|
|
* GravityView requires at least this version of PHP to function properly. |
44
|
|
|
*/ |
45
|
|
|
private static $min_php_version = '5.3.0'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string|bool Minimum future PHP version. |
49
|
|
|
* |
50
|
|
|
* GravityView will require this version of PHP soon. False if no future PHP version changes are planned. |
51
|
|
|
*/ |
52
|
|
|
private static $future_min_php_version = false; |
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string|bool Minimum future Gravity Forms version. |
56
|
|
|
* |
57
|
|
|
* GravityView will require this version of Gravity Forms soon. False if no future Gravity Forms version changes are planned. |
58
|
|
|
*/ |
59
|
|
|
private static $future_min_gf_version = false; |
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var \GV\Plugin The \GV\Plugin static instance. |
63
|
|
|
*/ |
64
|
|
|
private static $__instance = null; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the global instance of \GV\Plugin. |
68
|
|
|
* |
69
|
|
|
* @return \GV\Plugin The global instance of GravityView Plugin. |
70
|
|
|
*/ |
71
|
|
|
public static function get() { |
72
|
|
|
if ( ! self::$__instance instanceof self ) { |
73
|
|
|
self::$__instance = new self; |
74
|
|
|
} |
75
|
|
|
return self::$__instance; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Register hooks that are fired when the plugin is activated and deactivated. |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public function register_activation_hooks() { |
84
|
|
|
register_activation_hook( $this->dir( 'gravityview.php' ), array( $this, 'activate' ) ); |
85
|
|
|
register_deactivation_hook( $this->dir( 'gravityview.php' ), array( $this, 'deactivate' ) ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Plugin activation function. |
90
|
|
|
* |
91
|
|
|
* @internal |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
1 |
|
public function activate() { |
95
|
|
|
/** Register the gravityview post type upon WordPress core init. */ |
96
|
1 |
|
require_once $this->dir( 'future/includes/class-gv-view.php' ); |
97
|
1 |
|
View::register_post_type(); |
98
|
|
|
|
99
|
|
|
/** Add the entry rewrite endpoint. */ |
100
|
1 |
|
require_once $this->dir( 'future/includes/class-gv-entry.php' ); |
101
|
1 |
|
Entry::add_rewrite_endpoint(); |
102
|
|
|
|
103
|
|
|
/** Flush all URL rewrites. */ |
104
|
1 |
|
flush_rewrite_rules(); |
105
|
|
|
|
106
|
1 |
|
update_option( 'gv_version', \GravityView_Plugin::version ); |
107
|
1 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Plugin deactivation function. |
111
|
|
|
* |
112
|
|
|
* @internal |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function deactivate() { |
116
|
|
|
flush_rewrite_rules(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Retrieve an absolute path within the Gravity Forms plugin directory. |
121
|
|
|
* |
122
|
|
|
* @api |
123
|
|
|
* @since future |
124
|
|
|
* |
125
|
|
|
* @param string $path Optional. Append this extra path component. |
126
|
|
|
* @return string The absolute path to the plugin directory. |
127
|
|
|
*/ |
128
|
1 |
|
public function dir( $path = '' ) { |
129
|
1 |
|
return GRAVITYVIEW_DIR . ltrim( $path, '/' ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Retrieve a URL within the Gravity Forms plugin directory. |
134
|
|
|
* |
135
|
|
|
* @api |
136
|
|
|
* @since future |
137
|
|
|
* |
138
|
|
|
* @param string $path Optional. Extra path appended to the URL. |
139
|
|
|
* @return The URL to this plugin, with trailing slash. |
140
|
|
|
*/ |
141
|
1 |
|
public function url( $path = '/' ) { |
142
|
1 |
|
return plugins_url( $path, $this->dir( 'gravityview.php' ) ); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Is everything compatible with this version of GravityView? |
147
|
|
|
* |
148
|
|
|
* @api |
149
|
|
|
* @since future |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
1 |
|
public function is_compatible() { |
154
|
|
|
return |
155
|
1 |
|
$this->is_compatible_php() |
156
|
1 |
|
&& $this->is_compatible_wordpress() |
157
|
1 |
|
&& $this->is_compatible_gravityforms(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Is this version of GravityView compatible with the current version of PHP? |
162
|
|
|
* |
163
|
|
|
* @api |
164
|
|
|
* @since future |
165
|
|
|
* |
166
|
|
|
* @return bool true if compatible, false otherwise. |
167
|
|
|
*/ |
168
|
1 |
|
public function is_compatible_php() { |
169
|
1 |
|
return version_compare( $this->get_php_version(), self::$min_php_version, '>=' ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Is this version of GravityView compatible with the current version of WordPress? |
174
|
|
|
* |
175
|
|
|
* @api |
176
|
|
|
* @since future |
177
|
|
|
* |
178
|
|
|
* @return bool true if compatible, false otherwise. |
179
|
|
|
*/ |
180
|
1 |
|
public function is_compatible_wordpress() { |
181
|
1 |
|
return version_compare( $this->get_wordpress_version(), self::$min_wp_version, '>=' ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Is this version of GravityView compatible with the current version of Gravity Forms? |
186
|
|
|
* |
187
|
|
|
* @api |
188
|
|
|
* @since future |
189
|
|
|
* |
190
|
|
|
* @return bool true if compatible, false otherwise (or not active/installed). |
191
|
|
|
*/ |
192
|
1 |
|
public function is_compatible_gravityforms() { |
193
|
1 |
|
$version = $this->get_gravityforms_version(); |
194
|
1 |
|
return $version ? version_compare( $version, self::$min_gf_version, '>=' ) : false; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Retrieve the current PHP version. |
199
|
|
|
* |
200
|
|
|
* Overridable with GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE during testing. |
201
|
|
|
* |
202
|
|
|
* @return string The version of PHP. |
203
|
|
|
*/ |
204
|
|
|
private function get_php_version() { |
205
|
|
|
return ! empty( $GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] ) ? |
206
|
|
|
$GLOBALS['GRAVITYVIEW_TESTS_PHP_VERSION_OVERRIDE'] : phpversion(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Retrieve the current WordPress version. |
211
|
|
|
* |
212
|
|
|
* Overridable with GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE during testing. |
213
|
|
|
* |
214
|
|
|
* @return string The version of WordPress. |
215
|
|
|
*/ |
216
|
|
|
private function get_wordpress_version() { |
217
|
|
|
return ! empty( $GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] ) ? |
218
|
|
|
$GLOBALS['GRAVITYVIEW_TESTS_WP_VERSION_OVERRIDE'] : $GLOBALS['wp_version']; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Retrieve the current Gravity Forms version. |
223
|
|
|
* |
224
|
|
|
* Overridable with GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE during testing. |
225
|
|
|
* |
226
|
|
|
* @return string|null The version of Gravity Forms or null if inactive. |
227
|
|
|
*/ |
228
|
|
|
private function get_gravityforms_version() { |
229
|
|
|
if ( ! class_exists( '\GFCommon' ) || ! empty( $GLOBALS['GRAVITYVIEW_TESTS_GF_INACTIVE_OVERRIDE'] ) ) { |
230
|
|
|
gravityview()->log->error( 'Gravity Forms is inactive or not installed.' ); |
231
|
|
|
return null; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return ! empty( $GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] ) ? |
235
|
|
|
$GLOBALS['GRAVITYVIEW_TESTS_GF_VERSION_OVERRIDE'] : \GFCommon::$version; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
private function __clone() { } |
239
|
|
|
|
240
|
|
|
private function __wakeup() { } |
241
|
|
|
} |
242
|
|
|
|
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.