| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class WordPress\Plugin_Check\Checker\CLI_Runner |
| 4 |
* |
| 5 |
* @package plugin-check |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WordPress\Plugin_Check\Checker; |
| 9 |
|
| 10 |
use Exception; |
| 11 |
|
| 12 |
/** |
| 13 |
* CLI Runner class. |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
class CLI_Runner extends Abstract_Check_Runner { |
| 18 |
|
| 19 |
/** |
| 20 |
* An instance of the Checks class. |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
* @var Checks |
| 24 |
*/ |
| 25 |
protected $checks; |
| 26 |
|
| 27 |
/** |
| 28 |
* Checks if the current request is a CLI request for the Plugin Checker. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
* |
| 32 |
* @return bool Returns true if is an CLI request for the plugin check else false. |
| 33 |
*/ |
| 34 |
public static function is_plugin_check() { |
| 35 |
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
if ( empty( $_SERVER['argv'] ) || 3 > count( $_SERVER['argv'] ) ) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
|
| 43 |
if ( |
| 44 |
'plugin' === $_SERVER['argv'][1] && |
| 45 |
'check' === $_SERVER['argv'][2] |
| 46 |
) { |
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns the plugin parameter based on the request. |
| 55 |
* |
| 56 |
* @since 1.0.0 |
| 57 |
* |
| 58 |
* @return string The plugin parameter. |
| 59 |
* |
| 60 |
* @throws Exception Thrown if the plugin parameter is empty. |
| 61 |
*/ |
| 62 |
protected function get_plugin_param() { |
| 63 |
// Exclude first three reserved elements. |
| 64 |
$params = array_slice( $_SERVER['argv'], 3 ); |
| 65 |
|
| 66 |
// Remove associative arguments. |
| 67 |
$params = array_filter( |
| 68 |
$params, |
| 69 |
static function ( $val ) { |
| 70 |
return ! str_starts_with( $val, '--' ); |
| 71 |
} |
| 72 |
); |
| 73 |
|
| 74 |
// Use only first element. We don't support checking multiple plugins at once yet! |
| 75 |
$plugin = count( $params ) > 0 ? reset( $params ) : ''; |
| 76 |
|
| 77 |
if ( empty( $plugin ) ) { |
| 78 |
throw new Exception( |
| 79 |
__( 'Invalid plugin: Plugin parameter must not be empty.', 'plugin-check' ) |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
return $plugin; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns an array of Check slugs to run based on the request. |
| 88 |
* |
| 89 |
* @since 1.0.0 |
| 90 |
* |
| 91 |
* @return array An array of Check slugs to run. |
| 92 |
*/ |
| 93 |
protected function get_check_slugs_param() { |
| 94 |
$checks = array(); |
| 95 |
|
| 96 |
foreach ( $_SERVER['argv'] as $value ) { |
| 97 |
if ( false !== strpos( $value, '--checks=' ) ) { |
| 98 |
$checks = wp_parse_list( str_replace( '--checks=', '', $value ) ); |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return $checks; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Returns an array of Check slugs to exclude based on the request. |
| 108 |
* |
| 109 |
* @since 1.0.0 |
| 110 |
* |
| 111 |
* @return array An array of Check slugs to run. |
| 112 |
*/ |
| 113 |
protected function get_check_exclude_slugs_param() { |
| 114 |
$checks = array(); |
| 115 |
|
| 116 |
foreach ( $_SERVER['argv'] as $value ) { |
| 117 |
if ( false !== strpos( $value, '--exclude-checks=' ) ) { |
| 118 |
$checks = wp_parse_list( str_replace( '--exclude-checks=', '', $value ) ); |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return $checks; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Returns the include experimental parameter based on the request. |
| 128 |
* |
| 129 |
* @since 1.0.0 |
| 130 |
* |
| 131 |
* @return bool Returns true to include experimental checks else false. |
| 132 |
*/ |
| 133 |
protected function get_include_experimental_param() { |
| 134 |
if ( in_array( '--include-experimental', $_SERVER['argv'], true ) ) { |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Returns an array of categories for filtering the checks. |
| 143 |
* |
| 144 |
* @since 1.0.0 |
| 145 |
* |
| 146 |
* @return array An array of categories. |
| 147 |
*/ |
| 148 |
protected function get_categories_param() { |
| 149 |
$categories = array(); |
| 150 |
|
| 151 |
foreach ( $_SERVER['argv'] as $value ) { |
| 152 |
if ( false !== strpos( $value, '--categories=' ) ) { |
| 153 |
$categories = wp_parse_list( str_replace( '--categories=', '', $value ) ); |
| 154 |
break; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return $categories; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Returns plugin slug parameter. |
| 163 |
* |
| 164 |
* @since 1.2.0 |
| 165 |
* |
| 166 |
* @return string Plugin slug parameter. |
| 167 |
*/ |
| 168 |
protected function get_slug_param() { |
| 169 |
$slug = ''; |
| 170 |
|
| 171 |
foreach ( $_SERVER['argv'] as $value ) { |
| 172 |
if ( false !== strpos( $value, '--slug=' ) ) { |
| 173 |
$slug = str_replace( '--slug=', '', $value ); |
| 174 |
break; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return $slug; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Initializes the runtime environment so that runtime checks can be run against a separate set of database tables. |
| 183 |
* |
| 184 |
* @since 1.3.0 |
| 185 |
* |
| 186 |
* @return callable[] Array of cleanup functions to run after the process has completed. |
| 187 |
*/ |
| 188 |
protected function initialize_runtime(): array { |
| 189 |
/* |
| 190 |
* Since for WP-CLI all checks are run in a single process, we should set up the runtime environment (i.e. |
| 191 |
* install the separate database tables) as part of this step. |
| 192 |
* This way it runs before the regular runtime preparations, just like it does for the AJAX based flow, where |
| 193 |
* they are invoked in a separate request prior to the requests performing actual checks. |
| 194 |
*/ |
| 195 |
$runtime_setup = new Runtime_Environment_Setup(); |
| 196 |
$runtime_setup->set_up(); |
| 197 |
|
| 198 |
$cleanup_functions = parent::initialize_runtime(); |
| 199 |
$cleanup_functions[] = function () use ( $runtime_setup ) { |
| 200 |
$runtime_setup->clean_up(); |
| 201 |
}; |
| 202 |
|
| 203 |
return $cleanup_functions; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Checks whether the current environment allows for runtime checks to be used. |
| 208 |
* |
| 209 |
* @since 1.2.0 |
| 210 |
* |
| 211 |
* @return bool True if runtime checks are allowed, false otherwise. |
| 212 |
*/ |
| 213 |
protected function allow_runtime_checks(): bool { |
| 214 |
/* |
| 215 |
* For WP-CLI, everything happens in one request. So if the runner was not initialized early, we won't be |
| 216 |
* able to set that up, since the object-cache.php drop-in would only become effective in subsequent requests. |
| 217 |
*/ |
| 218 |
if ( ! $this->initialized_early ) { |
| 219 |
return false; |
| 220 |
} |
| 221 |
|
| 222 |
return parent::allow_runtime_checks(); |
| 223 |
} |
| 224 |
} |
| 225 |
|