| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sets up the CLI command early in the WordPress load process. |
| 4 |
* |
| 5 |
* This is necessary to setup the environment to perform runtime checks. |
| 6 |
* |
| 7 |
* @package plugin-check |
| 8 |
* @since 1.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
use WordPress\Plugin_Check\Checker\CLI_Runner; |
| 12 |
use WordPress\Plugin_Check\CLI\Plugin_Check_Command; |
| 13 |
use WordPress\Plugin_Check\Plugin_Context; |
| 14 |
|
| 15 |
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { |
| 16 |
return; |
| 17 |
} |
| 18 |
|
| 19 |
// Check if the plugin autoloading is set up. |
| 20 |
if ( ! class_exists( 'WordPress\Plugin_Check\CLI\Plugin_Check_Command' ) ) { |
| 21 |
// Check the autoload file exists. |
| 22 |
if ( ! file_exists( __DIR__ . '/vendor/autoload.php' ) ) { |
| 23 |
WP_CLI::error( 'Plugin Check autoloader not found.' ); |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
// Load the Composer autoloader. |
| 28 |
require_once __DIR__ . '/vendor/autoload.php'; |
| 29 |
} |
| 30 |
|
| 31 |
if ( ! isset( $context ) ) { |
| 32 |
$context = new Plugin_Context( __DIR__ . '/plugin.php' ); |
| 33 |
} |
| 34 |
|
| 35 |
// Create the CLI command instance and add to WP CLI. |
| 36 |
$plugin_command = new Plugin_Check_Command( $context ); |
| 37 |
WP_CLI::add_command( |
| 38 |
'plugin', |
| 39 |
$plugin_command, |
| 40 |
array( |
| 41 |
/** |
| 42 |
* This is a cleanup for the below hook which adds the object-cache.php drop-in. |
| 43 |
* |
| 44 |
* It is executed right after the command ran. |
| 45 |
* |
| 46 |
* Since the drop-in could be from somewhere else, a check of its contents is necessary |
| 47 |
* to verify it is the one that was added below. |
| 48 |
*/ |
| 49 |
'after_invoke' => function () { |
| 50 |
if ( |
| 51 |
file_exists( ABSPATH . 'wp-content/object-cache.php' ) && |
| 52 |
false !== strpos( file_get_contents( ABSPATH . 'wp-content/object-cache.php' ), 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) |
| 53 |
) { |
| 54 |
unlink( ABSPATH . 'wp-content/object-cache.php' ); |
| 55 |
} |
| 56 |
}, |
| 57 |
) |
| 58 |
); |
| 59 |
|
| 60 |
/* |
| 61 |
* Add hook to set up the object-cache.php drop-in file. |
| 62 |
* |
| 63 |
* Runs after wp-config.php is loaded and thus ABSPATH is defined, |
| 64 |
* but before any plugins are actually loaded. |
| 65 |
*/ |
| 66 |
WP_CLI::add_hook( |
| 67 |
'after_wp_config_load', |
| 68 |
function () { |
| 69 |
if ( CLI_Runner::is_plugin_check() ) { |
| 70 |
if ( ! file_exists( ABSPATH . 'wp-content/object-cache.php' ) ) { |
| 71 |
if ( ! copy( __DIR__ . '/drop-ins/object-cache.copy.php', ABSPATH . 'wp-content/object-cache.php' ) ) { |
| 72 |
WP_CLI::error( 'Unable to copy object-cache.php file.' ); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
); |
| 78 |
|