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