1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\Pollux; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use GeminiLabs\Pollux\Config\Config; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
|
9
|
|
|
class GateKeeper |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* [plugin_file_path] => [plugin_name]|[plugin_version]|[plugin_url] |
13
|
|
|
*/ |
14
|
|
|
const DEPENDENCIES = [ |
15
|
|
|
'meta-box/meta-box.php' => 'Meta Box|4.11|https://wordpress.org/plugins/meta-box/', |
16
|
|
|
]; |
17
|
|
|
const MIN_PHP_VERSION = '5.6.0'; |
18
|
|
|
const MIN_WORDPRESS_VERSION = '4.7'; |
19
|
|
|
|
20
|
|
|
public $errors = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Application |
24
|
|
|
*/ |
25
|
|
|
protected $app; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Notice |
29
|
|
|
*/ |
30
|
|
|
protected $notice; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $plugin; |
36
|
|
|
|
37
|
|
|
public function __construct( $plugin ) |
38
|
|
|
{ |
39
|
|
|
$this->plugin = $plugin; |
40
|
|
|
|
41
|
|
|
if( $this->canActivate() ) { |
42
|
|
|
add_action( 'admin_init', array( $this, 'init' )); |
43
|
|
|
} |
44
|
|
|
else { |
45
|
|
|
add_action( 'activated_plugin', array( $this, 'deactivate' )); |
46
|
|
|
add_action( 'admin_notices', array( $this, 'deactivate' )); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function init() |
51
|
|
|
{ |
52
|
|
|
$this->app = pollux_app(); |
53
|
|
|
$this->notice = pollux_app()->make( 'Notice' ); |
54
|
|
|
|
55
|
|
|
add_action( 'current_screen', array( $this, 'activatePlugin' )); |
56
|
|
|
add_action( 'wp_ajax_pollux/dependency/activate_url', array( $this, 'ajaxActivatePluginLink' )); |
57
|
|
|
add_action( 'admin_notices', array( $this, 'printNotices' )); |
58
|
|
|
add_action( 'current_screen', array( $this, 'setDependencyNotice' )); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function activatePlugin() |
65
|
|
|
{ |
66
|
|
|
$screen = ( new Helper )->getCurrentScreen(); |
67
|
|
|
$plugin = filter_input( INPUT_GET, 'plugin' ); |
68
|
|
|
if( $screen->id != 'settings_page_pollux' |
69
|
|
|
|| $screen->pagenow != 'options-general.php' |
70
|
|
|
|| filter_input( INPUT_GET, 'action' ) != 'activate' |
71
|
|
|
)return; |
72
|
|
|
check_admin_referer( 'activate-plugin_' . $plugin ); |
73
|
|
|
$result = activate_plugin( $plugin, null, is_network_admin(), true ); |
74
|
|
|
if( is_wp_error( $result )) { |
75
|
|
|
wp_die( $result->get_error_message() ); |
76
|
|
|
} |
77
|
|
|
wp_safe_redirect( wp_get_referer() ); |
78
|
|
|
exit; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function ajaxActivatePluginLink() |
85
|
|
|
{ |
86
|
|
|
check_ajax_referer( 'updates' ); |
87
|
|
|
$plugin = filter_input( INPUT_POST, 'plugin' ); |
88
|
|
|
if( !$this->isPluginDependency( $plugin )) { |
89
|
|
|
wp_send_json_error(); |
90
|
|
|
} |
91
|
|
|
$actionUrl = self_admin_url( sprintf( 'options-general.php?page=%s&action=activate&plugin=%s', $this->app->id, $plugin )); |
92
|
|
|
wp_send_json_success([ |
93
|
|
|
'activate_url' => wp_nonce_url( $actionUrl, sprintf( 'activate-plugin_%s', $plugin )), |
94
|
|
|
filter_input( INPUT_POST, 'type' ) => $plugin, |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function canActivate() |
102
|
|
|
{ |
103
|
|
|
return $this->hasValidPHPVersion() && $this->hasValidWPVersion(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return void |
108
|
|
|
* @action activated_plugin |
109
|
|
|
* @action admin_notices |
110
|
|
|
*/ |
111
|
|
|
public function deactivate( $plugin ) |
112
|
|
|
{ |
113
|
|
|
if( $plugin == $this->plugin ) { |
114
|
|
|
$this->redirect(); |
115
|
|
|
} |
116
|
|
|
deactivate_plugins( $this->plugin ); |
117
|
|
|
$addNotice = $this->hasValidPHPVersion() |
118
|
|
|
? 'addInvalidWPVersionNotice' |
119
|
|
|
: 'addInvalidPHPVersionNotice'; |
120
|
|
|
$this->$addNotice(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return void|null |
125
|
|
|
*/ |
126
|
|
|
public function getDependencyAction() |
127
|
|
|
{ |
128
|
|
|
if( get_current_screen()->id != 'settings_page_pollux' )return; |
129
|
|
|
|
130
|
|
|
$action = filter_input( INPUT_GET, 'action' ); |
131
|
|
|
$plugin = filter_input( INPUT_GET, 'plugin' ); |
132
|
|
|
|
133
|
|
|
if( $action == 'activate' ) { |
134
|
|
|
$this->activatePlugin( $plugin ); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
else if( $action == 'install' ) { |
137
|
|
|
$this->installPlugin( $plugin ); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
else if( filter_input( INPUT_GET, '_error_nonce' )) { |
140
|
|
|
wp_safe_redirect( remove_query_arg( '_error_nonce' )); |
141
|
|
|
exit; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
public function hasDependency( $plugin ) |
149
|
|
|
{ |
150
|
|
|
if( !$this->isPluginDependency( $plugin )) { |
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
return $this->isPluginInstalled( $plugin ) && $this->isPluginValid( $plugin ); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function hasValidPHPVersion() |
160
|
|
|
{ |
161
|
|
|
return version_compare( PHP_VERSION, self::MIN_PHP_VERSION, '>=' ); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function hasValidWPVersion() |
168
|
|
|
{ |
169
|
|
|
global $wp_version; |
170
|
|
|
return version_compare( $wp_version, self::MIN_WORDPRESS_VERSION, '>=' ); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
public function isPluginActive( $plugin ) |
177
|
|
|
{ |
178
|
|
|
return $this->catchError( $plugin, 'inactive', |
179
|
|
|
is_plugin_active( $plugin ) || array_key_exists( $plugin, $this->getMustUsePlugins() ) |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
public function isPluginDependency( $plugin ) |
187
|
|
|
{ |
188
|
|
|
return array_key_exists( $plugin, static::DEPENDENCIES ); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return bool |
193
|
|
|
*/ |
194
|
|
|
public function isPluginInstalled( $plugin ) |
195
|
|
|
{ |
196
|
|
|
return $this->catchError( $plugin, 'not_found', |
197
|
|
|
array_key_exists( $plugin, $this->getAllPlugins() ) |
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return bool |
203
|
|
|
*/ |
204
|
|
|
public function isPluginValid( $plugin ) |
205
|
|
|
{ |
206
|
|
|
return $this->isPluginActive( $plugin ) && $this->isPluginVersionValid( $plugin ); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
|
|
public function isPluginVersionValid( $plugin ) |
213
|
|
|
{ |
214
|
|
|
if( !$this->isPluginDependency( $plugin )) { |
215
|
|
|
return true; |
216
|
|
|
} |
217
|
|
|
if( !$this->isPluginInstalled( $plugin )) { |
218
|
|
|
return false; |
219
|
|
|
} |
220
|
|
|
return $this->catchError( $plugin, 'wrong_version', version_compare( |
221
|
|
|
$this->getPluginRequirements( $plugin, 'version' ), |
222
|
|
|
$this->getAllPlugins()[$plugin]['Version'], |
223
|
|
|
'<=' |
224
|
|
|
)); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return void |
229
|
|
|
*/ |
230
|
|
|
public function printNotices() |
231
|
|
|
{ |
232
|
|
|
foreach( $this->notice->all as $notice ) { |
|
|
|
|
233
|
|
|
echo $this->notice->generate( $notice ); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return bool |
239
|
|
|
*/ |
240
|
|
|
public function hasPendingDependencies() |
241
|
|
|
{ |
242
|
|
|
foreach( static::DEPENDENCIES as $plugin => $data ) { |
243
|
|
|
if( !$this->isPluginDependency( $plugin ))continue; |
244
|
|
|
$this->isPluginActive( $plugin ); |
245
|
|
|
$this->isPluginVersionValid( $plugin ); |
246
|
|
|
} |
247
|
|
|
return !empty( $this->errors ); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return void|null |
252
|
|
|
*/ |
253
|
|
|
public function setDependencyNotice() |
254
|
|
|
{ |
255
|
|
|
if( get_current_screen()->id != 'settings_page_pollux' |
256
|
|
|
|| $this->app->config->disable_config |
257
|
|
|
|| !$this->hasPendingDependencies() |
258
|
|
|
)return; |
259
|
|
|
|
260
|
|
|
$plugins = ''; |
261
|
|
|
$actions = ''; |
262
|
|
|
|
263
|
|
|
foreach( $this->errors as $plugin => $errors ) { |
264
|
|
|
$plugins .= $this->getPluginLink( $plugin ); |
265
|
|
|
if( in_array( 'not_found', $errors ) && current_user_can( 'install_plugins' )) { |
266
|
|
|
$actions .= $this->notice->installButton( $this->getPluginRequirements( $plugin )); |
|
|
|
|
267
|
|
|
} |
268
|
|
|
else if( in_array( 'wrong_version', $errors ) && current_user_can( 'update_plugins' )) { |
269
|
|
|
$actions .= $this->notice->updateButton( $this->getPluginInformation( $plugin )); |
|
|
|
|
270
|
|
|
} |
271
|
|
|
else if( in_array( 'inactive', $errors ) && current_user_can( 'activate_plugins' )) { |
272
|
|
|
$actions .= $this->notice->activateButton( $this->getPluginInformation( $plugin )); |
|
|
|
|
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
$this->notice->addWarning([ |
|
|
|
|
276
|
|
|
sprintf( '<strong>%s</strong> %s', __( 'Pollux requires the latest version of the following plugins:', 'pollux' ), $plugins ), |
277
|
|
|
$actions, |
278
|
|
|
]); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @return void |
283
|
|
|
*/ |
284
|
|
View Code Duplication |
protected function addInvalidPHPVersionNotice() |
|
|
|
|
285
|
|
|
{ |
286
|
|
|
$this->notice->addError([ |
|
|
|
|
287
|
|
|
$this->notice->title( __( 'The Pollux plugin was deactivated.', 'pollux' )), |
288
|
|
|
sprintf( __( 'Sorry, Pollux requires PHP %s or greater in order to work properly (your server is running PHP %s).', 'pollux' ), self::MIN_PHP_VERSION, PHP_VERSION ), |
289
|
|
|
__( 'Please contact your hosting provider or server administrator to upgrade the version of PHP running on your server, or find an alternate plugin.', 'pollux' ), |
290
|
|
|
]); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @return void |
295
|
|
|
*/ |
296
|
|
View Code Duplication |
protected function addInvalidWPVersionNotice() |
|
|
|
|
297
|
|
|
{ |
298
|
|
|
$this->notice->addError([ |
|
|
|
|
299
|
|
|
$this->notice->title( __( 'The Pollux plugin was deactivated.', 'pollux' )), |
300
|
|
|
sprintf( __( 'Sorry, Pollux requires WordPress %s or greater in order to work properly.', 'pollux' ), self::MIN_WORDPRESS_VERSION ), |
301
|
|
|
$this->notice->button( __( 'Update WordPress', 'pollux' ), self_admin_url( 'update-core.php' )), |
302
|
|
|
]); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param string $error |
307
|
|
|
* @param bool $bool |
|
|
|
|
308
|
|
|
* @return bool |
309
|
|
|
*/ |
310
|
|
|
protected function catchError( $plugin, $error, $isValid ) |
311
|
|
|
{ |
312
|
|
|
if( !$isValid ) { |
313
|
|
|
if( !isset( $this->errors[$plugin] )) { |
314
|
|
|
$this->errors[$plugin] = []; |
315
|
|
|
} |
316
|
|
|
$this->errors[$plugin] = array_keys( array_flip( |
317
|
|
|
array_merge( $this->errors[$plugin], [$error] ) |
318
|
|
|
)); |
319
|
|
|
} |
320
|
|
|
return $isValid; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @return array |
325
|
|
|
*/ |
326
|
|
|
protected function getAllPlugins() |
327
|
|
|
{ |
328
|
|
|
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
329
|
|
|
return array_merge( get_plugins(), $this->getMustUsePlugins() ); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @return array |
334
|
|
|
*/ |
335
|
|
|
protected function getMustUsePlugins() |
336
|
|
|
{ |
337
|
|
|
$plugins = get_mu_plugins(); |
338
|
|
|
if( in_array( 'Bedrock Autoloader', array_column( $plugins, 'Name' ))) { |
339
|
|
|
$autoloadedPlugins = get_site_option( 'bedrock_autoloader' ); |
340
|
|
|
if( !empty( $autoloadedPlugins['plugins'] )) { |
341
|
|
|
return array_merge( $plugins, $autoloadedPlugins['plugins'] ); |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
return $plugins; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @return array|false |
349
|
|
|
*/ |
350
|
|
|
protected function getPlugin( $plugin ) |
351
|
|
|
{ |
352
|
|
|
if( $this->isPluginInstalled( $plugin )) { |
353
|
|
|
return $this->getAllPlugins()[$plugin]; |
354
|
|
|
} |
355
|
|
|
return false; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @return array|string |
360
|
|
|
*/ |
361
|
|
|
protected function getPluginData( $plugin, array $data, $key = null ) |
362
|
|
|
{ |
363
|
|
|
$data['plugin'] = $plugin; |
364
|
|
|
$data['slug'] = $this->getPluginSlug( $plugin ); |
365
|
|
|
$data = array_change_key_case( $data ); |
366
|
|
|
if( is_null( $key )) { |
367
|
|
|
return $data; |
368
|
|
|
} |
369
|
|
|
$key = strtolower( $key ); |
370
|
|
|
return isset( $data[$key] ) |
371
|
|
|
? $data[$key] |
372
|
|
|
: ''; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @return array|string |
377
|
|
|
*/ |
378
|
|
|
protected function getPluginInformation( $plugin, $key = null ) |
379
|
|
|
{ |
380
|
|
|
$data = $this->getPlugin( $plugin ); |
381
|
|
|
if( is_array( $data )) { |
382
|
|
|
return $this->getPluginData( $plugin, $data, $key ); |
383
|
|
|
} |
384
|
|
|
throw new Exception( sprintf( 'Plugin information not found for: %s', $plugin )); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @return string |
389
|
|
|
*/ |
390
|
|
|
protected function getPluginLink( $plugin ) |
391
|
|
|
{ |
392
|
|
|
try { |
393
|
|
|
$data = $this->getPluginInformation( $plugin ); |
394
|
|
|
} |
395
|
|
|
catch( Exception $e ) { |
396
|
|
|
$data = $this->getPluginRequirements( $plugin ); |
397
|
|
|
} |
398
|
|
|
return sprintf( '<span class="plugin-%s"><a href="%s">%s</a></span>', |
399
|
|
|
$data['slug'], |
400
|
|
|
$data['pluginuri'], |
401
|
|
|
$data['name'] |
402
|
|
|
); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @return array|string |
407
|
|
|
*/ |
408
|
|
|
protected function getPluginRequirements( $plugin, $key = null ) |
409
|
|
|
{ |
410
|
|
|
$keys = ['Name', 'Version', 'PluginURI']; |
411
|
|
|
$index = array_search( $key, $keys, true ); |
|
|
|
|
412
|
|
|
$requirements = $this->isPluginDependency( $plugin ) |
413
|
|
|
? array_pad( explode( '|', static::DEPENDENCIES[$plugin] ), 3, '' ) |
414
|
|
|
: array_fill( 0, 3, '' ); |
415
|
|
|
return $this->getPluginData( $plugin, array_combine( $keys, $requirements ), $key ); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* @return string |
420
|
|
|
*/ |
421
|
|
|
protected function getPluginSlug( $plugin ) |
422
|
|
|
{ |
423
|
|
|
return substr( $plugin, 0, strrpos( $plugin, '/' )); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* @return void |
428
|
|
|
*/ |
429
|
|
|
protected function redirect() |
430
|
|
|
{ |
431
|
|
|
wp_safe_redirect( self_admin_url( sprintf( 'plugins.php?plugin_status=%s&paged=%s&s=%s', |
432
|
|
|
filter_input( INPUT_GET, 'plugin_status' ), |
433
|
|
|
filter_input( INPUT_GET, 'paged' ), |
434
|
|
|
filter_input( INPUT_GET, 's' ) |
435
|
|
|
))); |
436
|
|
|
exit; |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.