Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 69 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| CLI | |
0.00% |
0 / 69 |
|
0.00% |
0 / 6 |
702 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| module | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
56 | |||
| getting_started | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| set_module_status | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
42 | |||
| connection | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
72 | |||
| reset | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * CLI commands for Jetpack Boost. |
| 4 | * |
| 5 | * @link https://automattic.com |
| 6 | * @since 1.0.0 |
| 7 | * @package automattic/jetpack-boost |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack_Boost\Lib; |
| 11 | |
| 12 | use Automattic\Jetpack_Boost\Data_Sync\Getting_Started_Entry; |
| 13 | use Automattic\Jetpack_Boost\Modules\Module; |
| 14 | use Automattic\Jetpack_Boost\Modules\Modules_Setup; |
| 15 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Garbage_Collection; |
| 16 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Page_Cache_Setup; |
| 17 | use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Boost_Cache_Settings; |
| 18 | |
| 19 | /** |
| 20 | * Control your local Jetpack Boost installation. |
| 21 | */ |
| 22 | class CLI { |
| 23 | |
| 24 | /** |
| 25 | * Jetpack Boost plugin. |
| 26 | * |
| 27 | * @var \Automattic\Jetpack_Boost\Jetpack_Boost $jetpack_boost |
| 28 | */ |
| 29 | private $jetpack_boost; |
| 30 | |
| 31 | const MAKE_E2E_TESTS_WORK_MODULES = array( 'critical_css', 'speculation_rules', 'render_blocking_js', 'page_cache', 'lcp', 'minify_js', 'minify_css', 'image_cdn', 'image_guide' ); |
| 32 | |
| 33 | /** |
| 34 | * CLI constructor. |
| 35 | * |
| 36 | * @param \Automattic\Jetpack_Boost\Jetpack_Boost $jetpack_boost Jetpack Boost plugin. |
| 37 | */ |
| 38 | public function __construct( $jetpack_boost ) { |
| 39 | $this->jetpack_boost = $jetpack_boost; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Manage Jetpack Boost Modules |
| 44 | * |
| 45 | * ## OPTIONS |
| 46 | * |
| 47 | * <activate|deactivate> |
| 48 | * : The action to take. |
| 49 | * --- |
| 50 | * options: |
| 51 | * - activate |
| 52 | * - deactivate |
| 53 | * --- |
| 54 | * |
| 55 | * [<module_slug>] |
| 56 | * : The slug of the module to perform an action on. |
| 57 | * |
| 58 | * ## EXAMPLES |
| 59 | * |
| 60 | * wp jetpack-boost module activate critical_css |
| 61 | * wp jetpack-boost module deactivate critical_css |
| 62 | * |
| 63 | * @param array $args Command arguments. |
| 64 | */ |
| 65 | public function module( $args ) { |
| 66 | $action = isset( $args[0] ) ? $args[0] : null; |
| 67 | $module_slug = isset( $args[1] ) ? $args[1] : null; |
| 68 | |
| 69 | if ( $module_slug === null ) { |
| 70 | /* translators: Placeholder is list of available modules. */ |
| 71 | \WP_CLI::error( sprintf( __( 'Please specify a valid module. It should be one of %s', 'jetpack-boost' ), wp_json_encode( self::MAKE_E2E_TESTS_WORK_MODULES, JSON_UNESCAPED_SLASHES ) ) ); |
| 72 | } |
| 73 | |
| 74 | if ( ! in_array( $module_slug, self::MAKE_E2E_TESTS_WORK_MODULES, true ) ) { |
| 75 | \WP_CLI::error( |
| 76 | /* translators: %1$s refers to the module slug like 'critical-css', %2$s is the list of available modules. */ |
| 77 | sprintf( __( "The '%1\$s' module slug is invalid. It should be one of %2\$s", 'jetpack-boost' ), $module_slug, wp_json_encode( self::MAKE_E2E_TESTS_WORK_MODULES, JSON_UNESCAPED_SLASHES ) ) |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | switch ( $action ) { |
| 82 | case 'activate': |
| 83 | $this->set_module_status( $module_slug, true ); |
| 84 | break; |
| 85 | case 'deactivate': |
| 86 | $this->set_module_status( $module_slug, false ); |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public function getting_started( $args ) { |
| 92 | $status = isset( $args[0] ) ? $args[0] : null; |
| 93 | |
| 94 | if ( ! in_array( $status, array( 'true', 'false' ), true ) ) { |
| 95 | \WP_CLI::error( |
| 96 | /* translators: %s refers to the module slug like 'critical-css' */ |
| 97 | sprintf( __( "The '%s' status is invalid", 'jetpack-boost' ), $status ) |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | ( new Getting_Started_Entry() )->set( 'true' === $status ); |
| 102 | |
| 103 | \WP_CLI::success( |
| 104 | /* translators: %s refers to 'true' or 'false' */ |
| 105 | sprintf( __( 'Getting started is set to %s', 'jetpack-boost' ), $status ) |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Set a module status. |
| 111 | * |
| 112 | * @param string $module_slug Module slug. |
| 113 | * @param string $status Module status. |
| 114 | */ |
| 115 | private function set_module_status( $module_slug, $status ) { |
| 116 | $modules = ( new Modules_Setup() )->get_available_modules_and_submodules(); |
| 117 | $module = $modules[ $module_slug ] ?? false; |
| 118 | if ( ! ( $module instanceof Module ) ) { |
| 119 | \WP_CLI::error( |
| 120 | /* translators: %s refers to the module slug like 'critical-css' */ |
| 121 | sprintf( __( "The '%s' module slug is invalid", 'jetpack-boost' ), $module_slug ) |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | $module->update( $status ); |
| 126 | |
| 127 | if ( $module_slug === 'page_cache' && $status ) { |
| 128 | $setup_result = Page_Cache_Setup::run_setup(); |
| 129 | if ( is_wp_error( $setup_result ) ) { |
| 130 | \WP_CLI::error( |
| 131 | sprintf( |
| 132 | /* translators: %s refers to the error code */ |
| 133 | __( 'Setup: %s', 'jetpack-boost' ), |
| 134 | $setup_result->get_error_code() |
| 135 | ) |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | Garbage_Collection::activate(); |
| 140 | Boost_Cache_Settings::get_instance()->set( array( 'enabled' => true ) ); |
| 141 | } |
| 142 | |
| 143 | $status_label = $status ? __( 'activated', 'jetpack-boost' ) : __( 'deactivated', 'jetpack-boost' ); |
| 144 | |
| 145 | /* translators: The %1$s refers to the module slug, %2$s refers to the module state (either activated or deactivated)*/ |
| 146 | \WP_CLI::success( |
| 147 | sprintf( __( "'%1\$s' has been %2\$s.", 'jetpack-boost' ), $module_slug, $status_label ) |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Manage Jetpack Boost connection |
| 153 | * |
| 154 | * ## OPTIONS |
| 155 | * |
| 156 | * <activate|deactivate|status> |
| 157 | * : The action to take. |
| 158 | * --- |
| 159 | * options: |
| 160 | * - activate |
| 161 | * - deactivate |
| 162 | * - status |
| 163 | * --- |
| 164 | * |
| 165 | * ## EXAMPLES |
| 166 | * |
| 167 | * wp jetpack-boost connection activate |
| 168 | * wp jetpack-boost connection deactivate |
| 169 | * wp jetpack-boost connection status |
| 170 | * |
| 171 | * @param array $args Command arguments. |
| 172 | */ |
| 173 | public function connection( $args ) { |
| 174 | $action = isset( $args[0] ) ? $args[0] : null; |
| 175 | |
| 176 | switch ( $action ) { |
| 177 | case 'activate': |
| 178 | $result = $this->jetpack_boost->connection->register(); |
| 179 | if ( true === $result ) { |
| 180 | \WP_CLI::success( __( 'Boost is connected to WP.com', 'jetpack-boost' ) ); |
| 181 | } else { |
| 182 | \WP_CLI::error( __( 'Boost could not be connected to WP.com', 'jetpack-boost' ) ); |
| 183 | } |
| 184 | break; |
| 185 | case 'deactivate': |
| 186 | require_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 187 | |
| 188 | if ( is_plugin_active_for_network( JETPACK_BOOST_PATH ) ) { |
| 189 | $this->jetpack_boost->connection->deactivate_disconnect_network(); |
| 190 | } else { |
| 191 | $this->jetpack_boost->connection->disconnect(); |
| 192 | } |
| 193 | |
| 194 | \WP_CLI::success( __( 'Boost is disconnected from WP.com', 'jetpack-boost' ) ); |
| 195 | break; |
| 196 | case 'status': |
| 197 | $is_connected = $this->jetpack_boost->connection->is_connected(); |
| 198 | if ( $is_connected ) { |
| 199 | \WP_CLI::line( 'connected' ); |
| 200 | } else { |
| 201 | \WP_CLI::line( 'disconnected' ); |
| 202 | } |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Reset Jetpack Boost |
| 209 | * |
| 210 | * ## EXAMPLE |
| 211 | * |
| 212 | * wp jetpack-boost reset |
| 213 | */ |
| 214 | public function reset() { |
| 215 | $this->jetpack_boost->deactivate(); |
| 216 | $this->jetpack_boost->uninstall(); |
| 217 | \WP_CLI::success( 'Reset successfully' ); |
| 218 | } |
| 219 | } |