Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.68% covered (success)
95.68%
155 / 162
77.78% covered (warning)
77.78%
7 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_REST_API_V2_Endpoint_Launchpad
96.88% covered (success)
96.88%
155 / 160
77.78% covered (warning)
77.78%
7 / 9
26
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 register_routes
100.00% covered (success)
100.00%
80 / 80
100.00% covered (success)
100.00%
1 / 1
1
 get_checklist_slug_enums
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get_readable_checklist_slug_enums
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 get_checklist_statuses_properties
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
4
 can_access
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_data
91.30% covered (success)
91.30%
21 / 23
0.00% covered (danger)
0.00%
0 / 1
5.02
 parse_relative_date
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 update_site_options
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
1 / 1
11
1<?php
2/**
3 * Launchpad API endpoint
4 *
5 * @package automattic/jetpack-mu-wpcom
6 * @since 1.1.0
7 */
8
9use Automattic\Jetpack\Launchpad;
10
11require_once __DIR__ . '/../launchpad/goal-launchpad-task-lists.php';
12
13/**
14 * Fetches Launchpad-related data for the site.
15 *
16 * @since 1.1.0
17 */
18class WPCOM_REST_API_V2_Endpoint_Launchpad extends WP_REST_Controller {
19
20    /**
21     * Class constructor
22     */
23    public function __construct() {
24        $this->namespace = 'wpcom/v2';
25        $this->rest_base = 'launchpad';
26
27        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
28    }
29
30    /**
31     * Register our routes.
32     */
33    public function register_routes() {
34        register_rest_route(
35            $this->namespace,
36            $this->rest_base,
37            array(
38                array(
39                    'methods'             => WP_REST_Server::READABLE,
40                    'callback'            => array( $this, 'get_data' ),
41                    'permission_callback' => array( $this, 'can_access' ),
42                    'args'                => array(
43                        'checklist_slug'             => array(
44                            'description' => 'Checklist slug',
45                            'type'        => 'string',
46                            'enum'        => $this->get_readable_checklist_slug_enums(),
47                        ),
48                        'launchpad_context'          => array(
49                            'description' => 'Screen where Launchpand instance is loaded.',
50                            'type'        => 'string',
51                        ),
52                        'use_goals'                  => array(
53                            'description' => 'Should the launchpad data use site goals or intent.',
54                            'type'        => 'boolean',
55                            'required'    => false,
56                            'default'     => false,
57                        ),
58                        'enable_checklist_for_goals' => array(
59                            'description' => 'Used by the client to signal to Jetpack which launchpad goals have been enabled (e.g. via feature flags)',
60                            'type'        => 'array',
61                            'items'       => array( 'type' => 'string' ),
62                            'required'    => false,
63                            'default'     => array(),
64                        ),
65                    ),
66                ),
67                array(
68                    'methods'             => WP_REST_Server::EDITABLE,
69                    'callback'            => array( $this, 'update_site_options' ),
70                    'permission_callback' => array( $this, 'can_access' ),
71                    'args'                => array(
72                        'checklist_statuses'        => array(
73                            'description'          => 'Launchpad statuses',
74                            'type'                 => 'object',
75                            'properties'           => $this->get_checklist_statuses_properties(),
76                            'additionalProperties' => false,
77                        ),
78                        'launchpad_screen'          => array(
79                            'description' => 'Launchpad screen',
80                            'type'        => 'string',
81                            'enum'        => array( 'off', 'minimized', 'full', 'skipped' ),
82                        ),
83                        'is_checklist_dismissed'    => array(
84                            'description'          => 'Marks a checklist as dismissed by the user',
85                            'type'                 => 'object',
86                            'properties'           => array(
87                                'slug'         => array(
88                                    'description' => 'Checklist slug',
89                                    'type'        => 'string',
90                                    'enum'        => $this->get_checklist_slug_enums(),
91                                    'required'    => true,
92                                ),
93                                'is_dismissed' => array(
94                                    'type'     => 'boolean',
95                                    'required' => false,
96                                    'default'  => false,
97                                ),
98                                'dismiss_by'   => array(
99                                    'description' => 'Timestamp of when the checklist should be shown again',
100                                    'type'        => 'string',
101                                    'enum'        => array( '+ 1 day', '+ 1 week' ),
102                                ),
103                            ),
104                            'additionalProperties' => false,
105                        ),
106                        'hide_fse_next_steps_modal' => array(
107                            'description' => 'Controls whether we show or hide the next steps modal in the full site editor',
108                            'type'        => 'boolean',
109                        ),
110                    ),
111                ),
112            )
113        );
114    }
115
116    /**
117     * Returns all available checklist slugs.
118     *
119     * @return array Array of checklist slugs.
120     */
121    public function get_checklist_slug_enums() {
122        $checklists = wpcom_launchpad_checklists()->get_all_task_lists();
123        return array_keys( $checklists );
124    }
125
126    /**
127     * Returns the checklist slugs a read request may reference.
128     *
129     * Retired flow slugs are included so a stale client request degrades to an
130     * empty checklist rather than a 400. Read-only: mutations use the registered
131     * slugs from get_checklist_slug_enums() so they cannot act on a retired slug.
132     *
133     * @return array Array of checklist slugs.
134     */
135    public function get_readable_checklist_slug_enums() {
136        return array_values(
137            array_unique( array_merge( $this->get_checklist_slug_enums(), wpcom_launchpad_get_retired_site_intents() ) )
138        );
139    }
140
141    /**
142     * Returns all registered checklist statuses.
143     *
144     * @return array Associative array of checklist status properties for the REST API.
145     */
146    public function get_checklist_statuses_properties() {
147        $tasks            = wpcom_launchpad_checklists()->get_all_tasks();
148        $allowed_task_ids = array();
149        foreach ( $tasks as $task ) {
150            $allowed_task_ids[] = $task['id'];
151            if ( isset( $task['id_map'] ) ) {
152                $allowed_task_ids[] = $task['id_map'];
153            }
154        }
155        $allowed_task_ids = array_unique( $allowed_task_ids );
156        $properties       = array();
157        foreach ( $allowed_task_ids as $task_id ) {
158            $properties[ $task_id ] = array(
159                'type' => 'boolean',
160            );
161        }
162        return $properties;
163    }
164
165    /**
166     * Permission callback for the REST route.
167     *
168     * @return boolean
169     */
170    public function can_access() {
171        return current_user_can( 'manage_options' );
172    }
173
174    /**
175     * Returns Launchpad-related options.
176     *
177     * @param WP_REST_Request $request Full data about the request.
178     *
179     * @return array Associative array with `site_intent`, `launchpad_screen`,
180     *               `launchpad_checklist_tasks_statuses` as `checklist_statuses`,
181     *               and `checklist`.
182     */
183    public function get_data( $request ) {
184        // Handle translations for Atomic sites.
185        $switched_locale = false;
186        $locale          = $request['_locale'] ? get_user_locale() : null;
187
188        if ( $locale ) {
189            $switched_locale = switch_to_locale( $locale );
190        }
191
192        $checklist_slug = $request['checklist_slug'] ?? get_option( 'site_intent' );
193        $use_goals      = $request['use_goals'] ?? false;
194
195        $launchpad_context = $request['launchpad_context'] ?? null;
196
197        if ( $use_goals ) {
198            // The user must be part of a cohort which should deterine which checklist to show soley on
199            // goal selection, not the "intent".
200            $site_goals     = get_option( 'site_goals', array() );
201            $checklist_slug = Launchpad\get_checklist_slug_by_goals( $site_goals, $request['enable_checklist_for_goals'] );
202        }
203
204        $response = array(
205            'site_intent'        => get_option( 'site_intent' ),
206            'launchpad_screen'   => get_option( 'launchpad_screen' ),
207            'checklist_statuses' => get_option( 'launchpad_checklist_tasks_statuses', array() ),
208            'checklist'          => wpcom_get_launchpad_checklist_by_checklist_slug( $checklist_slug, $launchpad_context ),
209            'is_enabled'         => (bool) wpcom_get_launchpad_task_list_is_enabled( $checklist_slug ),
210            'is_dismissed'       => wpcom_launchpad_is_task_list_dismissed( $checklist_slug ),
211            'is_dismissible'     => wpcom_launchpad_is_task_list_dismissible( $checklist_slug ),
212            'title'              => wpcom_get_launchpad_checklist_title_by_checklist_slug( $checklist_slug ),
213        );
214
215        if ( $switched_locale ) {
216            restore_previous_locale();
217        }
218
219        return $response;
220    }
221
222    /**
223     * Parses the relative date string and returns the timestamp
224     *
225     * @param string $relative_date The string to parse.
226     *
227     * @return int The timestamp of when the checklist should be shown again.
228     */
229    public function parse_relative_date( $relative_date ) {
230        $date = new \DateTime( 'now', new \DateTimeZone( 'UTC' ) );
231
232        return $date->modify( $relative_date )->getTimestamp();
233    }
234
235    /**
236     * Updates Launchpad-related options and returns the result
237     *
238     * @param WP_REST_Request $request Request object.
239     * @return array|WP_Error Associative array with updated site options, or an
240     *                        error if the request has a key the route doesn't declare.
241     */
242    public function update_site_options( $request ) {
243        $updated = array();
244        $input   = $request->get_json_params();
245        $input   = is_array( $input ) ? $input : array();
246
247        $undeclared_keys = array_keys( array_diff_key( $input, $request->get_attributes()['args'] ?? array() ) );
248        if ( $undeclared_keys ) {
249            return new WP_Error(
250                'rest_invalid_param',
251                /* translators: %s: List of invalid parameters. */
252                sprintf( __( 'Invalid parameter(s): %s', 'jetpack-mu-wpcom' ), implode( ', ', $undeclared_keys ) ),
253                array( 'status' => 400 )
254            );
255        }
256
257        foreach ( $input as $key => $value ) {
258            switch ( $key ) {
259                case 'launchpad_screen':
260                    if ( update_option( 'launchpad_screen', $value ) ) {
261                        $updated[ $key ] = $value;
262                    }
263                    break;
264
265                case 'checklist_statuses':
266                    $updated[ $key ] = wpcom_launchpad_update_task_status( $value );
267
268                    // This will check if we have completed all the tasks and disable Launchpad if so.
269                    wpcom_launchpad_checklists()->maybe_disable_fullscreen_launchpad();
270                    break;
271
272                case 'is_checklist_dismissed':
273                    $checklist_slug  = $value['slug'];
274                    $is_dismissed    = $value['is_dismissed'] ?? false;
275                    $dismissed_until = isset( $value['dismiss_by'] ) ? $this->parse_relative_date( $value['dismiss_by'] ) : null;
276
277                    wpcom_launchpad_set_task_list_dismissed( $checklist_slug, $is_dismissed, $dismissed_until );
278                    break;
279
280                case 'hide_fse_next_steps_modal':
281                    $value = (bool) $value;
282                    if ( wpcom_launchpad_set_fse_next_steps_modal_hidden( $value ) ) {
283                        $updated[ $key ] = $value;
284                    }
285                    break;
286            }
287        }
288
289        return array(
290            'updated' => $updated,
291        );
292    }
293}
294
295wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Launchpad' );