-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathAbstract_Check_Runner.php
615 lines (540 loc) · 15.6 KB
/
Abstract_Check_Runner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<?php
/**
* Class WordPress\Plugin_Check\Checker\Abstract_Check_runner
*
* @package plugin-check
*/
namespace WordPress\Plugin_Check\Checker;
use Exception;
use WordPress\Plugin_Check\Checker\Exception\Invalid_Check_Slug_Exception;
use WordPress\Plugin_Check\Checker\Preparations\Universal_Runtime_Preparation;
use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility;
/**
* Abstract Check Runner class.
*
* @since 1.0.0
*
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
abstract class Abstract_Check_Runner implements Check_Runner {
/**
* True if the class was initialized early in the WordPress load process.
*
* @since 1.0.0
* @var bool
*/
protected $initialized_early;
/**
* The check slugs to run.
*
* @since 1.0.0
* @var array
*/
protected $check_slugs;
/**
* The check slugs to exclude.
*
* @since 1.0.0
* @var array
*/
protected $check_exclude_slugs;
/**
* The plugin parameter.
*
* @since 1.0.0
* @var string
*/
protected $plugin;
/**
* An instance of the Checks class.
*
* @since 1.0.0
* @var Checks
*/
protected $checks;
/**
* The plugin basename to check.
*
* @since 1.0.0
* @var string
*/
protected $plugin_basename;
/**
* Whether to delete the plugin folder during cleanup.
*
* Used when downloading a plugin from a URL.
*
* @since 1.1.0
* @var bool
*/
private $delete_plugin_folder = false;
/**
* An instance of the Check_Repository.
*
* @since 1.0.0
* @var Check_Repository
*/
private $check_repository;
/**
* Runtime environment.
*
* @since 1.0.0
* @var Runtime_Environment_Setup
*/
protected $runtime_environment;
/**
* Whether to include experimental checks.
*
* @since 1.0.0
* @var bool
*/
protected $include_experimental;
/**
* Checks category for the filter.
*
* @since 1.0.0
* @var array
*/
protected $check_categories;
/**
* Returns the plugin parameter based on the request.
*
* @since 1.0.0
*
* @return string The plugin parameter from the request.
*/
abstract protected function get_plugin_param();
/**
* Returns an array of Check slugs to run based on the request.
*
* @since 1.0.0
*
* @return array An array of Check slugs.
*/
abstract protected function get_check_slugs_param();
/**
* Returns an array of Check slugs to exclude based on the request.
*
* @since 1.0.0
*
* @return array An array of Check slugs.
*/
abstract protected function get_check_exclude_slugs_param();
/**
* Returns the include experimental parameter based on the request.
*
* @since 1.0.0
*
* @return bool Returns true to include experimental checks else false.
*/
abstract protected function get_include_experimental_param();
/**
* Returns an array of categories for filtering the checks.
*
* @since 1.0.0
*
* @return array An array of categories.
*/
abstract protected function get_categories_param();
/**
* Sets whether the runner class was initialized early.
*
* @since 1.0.0
*/
final public function __construct() {
$this->initialized_early = ! did_action( 'muplugins_loaded' );
$this->check_repository = new Default_Check_Repository();
$this->runtime_environment = new Runtime_Environment_Setup();
}
/**
* Sets the check slugs to be run.
*
* @since 1.0.0
*
* @param array $check_slugs An array of check slugs to be run.
*
* @throws Exception Thrown if the checks do not match those in the original request.
*/
final public function set_check_slugs( array $check_slugs ) {
if ( $this->initialized_early ) {
// Compare the check slugs to see if there was an error.
if ( $check_slugs !== $this->get_check_slugs_param() ) {
throw new Exception(
__( 'Invalid checks: The checks to run do not match the original request.', 'plugin-check' )
);
}
}
$this->check_slugs = $check_slugs;
}
/**
* Sets the check slugs to be excluded.
*
* @since 1.0.0
*
* @param array $check_slugs An array of check slugs to be excluded.
*
* @throws Exception Thrown if the checks do not match those in the original request.
*/
final public function set_check_exclude_slugs( array $check_slugs ) {
if ( $this->initialized_early ) {
// Compare the check slugs to see if there was an error.
if ( $check_slugs !== $this->get_check_exclude_slugs_param() ) {
throw new Exception(
__( 'Invalid checks: The checks to exclude do not match the original request.', 'plugin-check' )
);
}
}
$this->check_exclude_slugs = $check_slugs;
}
/**
* Sets the plugin slug or basename to be checked.
*
* @since 1.0.0
*
* @param string $plugin The plugin slug or basename to be checked.
*
* @throws Exception Thrown if the plugin set does not match the original request parameter.
*/
final public function set_plugin( $plugin ) {
if ( $this->initialized_early ) {
// Compare the plugin parameter to see if there was an error.
if ( $plugin !== $this->get_plugin_param() ) {
throw new Exception(
__( 'Invalid plugin: The plugin set does not match the original request parameter.', 'plugin-check' )
);
}
}
$this->plugin = $plugin;
}
/**
* Sets whether to include experimental checks in the process.
*
* @since 1.0.0
*
* @param bool $include_experimental True to include experimental checks. False to exclude.
*
* @throws Exception Thrown if the flag set does not match the original request parameter.
*/
final public function set_experimental_flag( $include_experimental ) {
if ( $this->initialized_early ) {
if ( $include_experimental !== $this->get_include_experimental_param() ) {
throw new Exception(
sprintf(
/* translators: %s: include-experimental */
__( 'Invalid flag: The %s value does not match the original request parameter.', 'plugin-check' ),
'include-experimental'
)
);
}
}
$this->include_experimental = $include_experimental;
}
/**
* Sets categories for filtering the checks.
*
* @since 1.0.0
*
* @param array $categories An array of categories for filtering.
*
* @throws Exception Thrown if the categories does not match the original request parameter.
*/
final public function set_categories( $categories ) {
if ( $this->initialized_early ) {
if ( $categories !== $this->get_categories_param() ) {
throw new Exception(
sprintf(
/* translators: %s: categories */
__( 'Invalid categories: The %s value does not match the original request parameter.', 'plugin-check' ),
'categories'
)
);
}
}
$this->check_categories = $categories;
}
/**
* Prepares the environment for running the requested checks.
*
* @since 1.0.0
*
* @return callable Cleanup function to revert any changes made here.
*
* @throws Exception Thrown exception when preparation fails.
*/
final public function prepare() {
$cleanup_functions = array();
if ( $this->initialized_early ) {
/*
* When initialized early, plugins are not loaded yet when this method is called.
* Therefore it could be that check slugs provided refer to addon checks that are not loaded yet.
* In that case, the only reliable option is to assume that it refers to an addon check and that the addon
* check is a runtime check. We don't know, but better to have the runtime preparations initialize
* unnecessarily rather than not having them when needed.
*
* The actual checks to run are retrieved later (once plugins are loaded), so if one of the provided slugs
* is actually invalid, the exception will still be thrown at that point.
*/
try {
$checks = $this->get_checks_to_run();
$initialize_runtime = $this->has_runtime_check( $checks );
} catch ( Invalid_Check_Slug_Exception $e ) {
$initialize_runtime = true;
}
} else {
// When not initialized early, all checks are loaded, so we can simply see if there are runtime checks.
$initialize_runtime = $this->has_runtime_check( $this->get_checks_to_run() );
}
if ( $initialize_runtime ) {
$preparation = new Universal_Runtime_Preparation( $this->get_check_context() );
$cleanup_functions[] = $preparation->prepare();
}
if ( $this->delete_plugin_folder ) {
$cleanup_functions = function () {
// It must be a directory at this point, but double check just in case.
if ( is_dir( $this->plugin_basename ) ) {
rmdir( $this->plugin_basename );
}
};
}
return function () use ( $cleanup_functions ) {
foreach ( $cleanup_functions as $cleanup_function ) {
$cleanup_function();
}
};
}
/**
* Runs the checks against the plugin.
*
* @since 1.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
* @global string $table_prefix The database table prefix.
*
* @return Check_Result An object containing all check results.
*/
final public function run() {
global $wpdb, $table_prefix;
$checks = $this->get_checks_to_run();
$preparations = $this->get_shared_preparations( $checks );
$cleanups = array();
$old_prefix = null;
// Set the correct test database prefix if required.
if ( $this->has_runtime_check( $checks ) ) {
$old_prefix = $wpdb->set_prefix( $table_prefix . 'pc_' );
}
// Prepare all shared preparations.
foreach ( $preparations as $preparation ) {
$instance = new $preparation['class']( ...$preparation['args'] );
$cleanups[] = $instance->prepare();
}
$results = $this->get_checks_instance()->run_checks( $this->get_check_context(), $checks );
if ( ! empty( $cleanups ) ) {
foreach ( $cleanups as $cleanup ) {
$cleanup();
}
}
// Restore the old prefix.
if ( $old_prefix ) {
$wpdb->set_prefix( $old_prefix );
}
return $results;
}
/**
* Determines if any of the checks are a runtime check.
*
* @since 1.0.0
*
* @param array $checks An array of check instances to run.
* @return bool Returns true if one or more checks is a runtime check.
*/
private function has_runtime_check( array $checks ) {
foreach ( $checks as $check ) {
if ( $check instanceof Runtime_Check ) {
return true;
}
}
return false;
}
/**
* Returns all shared preparations used by the checks to run.
*
* @since 1.0.0
*
* @param array $checks An array of Check instances to run.
* @return array An array of Preparations to run where each item is an array with keys `class` and `args`.
*/
private function get_shared_preparations( array $checks ) {
$shared_preparations = array();
foreach ( $checks as $check ) {
if ( ! $check instanceof With_Shared_Preparations ) {
continue;
}
$preparations = $check->get_shared_preparations();
foreach ( $preparations as $class => $args ) {
$key = $class . '::' . md5( json_encode( $args ) );
if ( ! isset( $shared_preparations[ $key ] ) ) {
$shared_preparations[ $key ] = array(
'class' => $class,
'args' => $args,
);
}
}
}
return array_values( $shared_preparations );
}
/**
* Returns the Check instances to run.
*
* @since 1.0.0
*
* @return array An array map of check slugs to Check instances.
*
* @throws Exception Thrown when invalid flag is passed, or Check slug does not exist.
*/
final public function get_checks_to_run() {
$check_slugs = $this->get_check_slugs();
$check_flags = Check_Repository::TYPE_STATIC;
// Check if conditions are met in order to perform Runtime Checks.
if ( $this->allow_runtime_checks() ) {
$check_flags = Check_Repository::TYPE_ALL;
}
// Check whether to include experimental checks.
if ( $this->get_include_experimental() ) {
$check_flags = $check_flags | Check_Repository::INCLUDE_EXPERIMENTAL;
}
$excluded_checks = $this->get_check_exclude_slugs();
$collection = $this->check_repository->get_checks( $check_flags )
->require( $check_slugs ) // Ensures all of the given slugs are valid.
->include( $check_slugs ) // Ensures only the checks with the given slugs are included.
->exclude( $excluded_checks ); // Exclude provided checks from list.
// Filters the checks by specific categories.
$categories = $this->get_categories();
if ( $categories ) {
$collection = Check_Categories::filter_checks_by_categories( $collection, $categories );
}
return $collection->to_map();
}
/**
* Checks whether the current environment allows for runtime checks to be used.
*
* @since n.e.x.t
*
* @return bool True if runtime checks are allowed, false otherwise.
*/
protected function allow_runtime_checks(): bool {
// Ensure that is_plugin_active() is available.
require_once ABSPATH . 'wp-admin/includes/plugin.php';
return ( $this->initialized_early || $this->runtime_environment->can_set_up() )
&& is_plugin_active( $this->get_plugin_basename() );
}
/**
* Creates and returns the Check instance.
*
* @since 1.0.0
*
* @return Checks An instance of the Checks class.
*
* @throws Exception Thrown if the plugin slug is invalid.
*/
protected function get_checks_instance() {
if ( null !== $this->checks ) {
return $this->checks;
}
$this->checks = new Checks();
return $this->checks;
}
/**
* Returns the check slugs to run.
*
* @since 1.0.0
*
* @return array An array of check slugs to run.
*/
private function get_check_slugs() {
if ( null !== $this->check_slugs ) {
return $this->check_slugs;
}
return $this->get_check_slugs_param();
}
/**
* Returns the check slugs to exclude.
*
* @since 1.0.0
*
* @return array An array of check slugs to exclude.
*/
private function get_check_exclude_slugs() {
if ( null !== $this->check_exclude_slugs ) {
return $this->check_exclude_slugs;
}
return $this->get_check_exclude_slugs_param();
}
/**
* Returns the plugin basename.
*
* @since 1.0.0
*
* @return string The plugin basename to check.
*/
final public function get_plugin_basename() {
if ( null === $this->plugin_basename ) {
$plugin = null !== $this->plugin ? $this->plugin : $this->get_plugin_param();
if ( filter_var( $plugin, FILTER_VALIDATE_URL ) ) {
$this->plugin_basename = Plugin_Request_Utility::download_plugin( $plugin );
$this->delete_plugin_folder = true;
} elseif ( Plugin_Request_Utility::is_directory_valid_plugin( $plugin ) ) {
$this->plugin_basename = $plugin;
} else {
$this->plugin_basename = Plugin_Request_Utility::get_plugin_basename_from_input( $plugin );
}
}
return $this->plugin_basename;
}
/**
* Returns the value for the include experimental flag.
*
* @since 1.0.0
*
* @return bool True if experimental checks are included. False if not.
*/
final protected function get_include_experimental() {
if ( null !== $this->include_experimental ) {
return $this->include_experimental;
}
return $this->get_include_experimental_param();
}
/**
* Returns an array of categories for filtering the checks.
*
* @since 1.0.0
*
* @return array An array of categories.
*/
final protected function get_categories() {
if ( null !== $this->check_categories ) {
return $this->check_categories;
}
return $this->get_categories_param();
}
/** Gets the Check_Context for the plugin.
*
* @since 1.0.0
*
* @return Check_Context The check context for the plugin file.
*/
private function get_check_context() {
$plugin_basename = $this->get_plugin_basename();
$plugin_path = is_dir( $plugin_basename ) ? $plugin_basename : WP_PLUGIN_DIR . '/' . $plugin_basename;
return new Check_Context( $plugin_path );
}
/**
* Sets the runtime environment setup.
*
* @since 1.0.0
*
* @param Runtime_Environment_Setup $runtime_environment_setup Runtime environment instance.
*/
final public function set_runtime_environment_setup( $runtime_environment_setup ) {
$this->runtime_environment = $runtime_environment_setup;
}
}