Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
61.90% covered (warning)
61.90%
39 / 63
62.50% covered (warning)
62.50%
5 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_REST_API_V2_Endpoint_Launchpad_Navigator
62.90% covered (warning)
62.90%
39 / 62
62.50% covered (warning)
62.50%
5 / 8
21.63
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 register_routes
100.00% covered (success)
100.00%
28 / 28
100.00% covered (success)
100.00%
1 / 1
1
 validate_checklist_slug_param
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 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
 update_navigator_options
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
20
 get_navigator_data
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 can_access
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Launchpad API endpoint
4 *
5 * @package automattic/jetpack-mu-wpcom
6 * @since 4.9.0
7 */
8
9/**
10 * Fetches Launchpad Navigator-related data for the site.
11 *
12 * @since 4.9.0
13 */
14class WPCOM_REST_API_V2_Endpoint_Launchpad_Navigator extends WP_REST_Controller {
15
16    /**
17     * Class constructor
18     */
19    public function __construct() {
20        $this->namespace = 'wpcom/v2';
21        $this->rest_base = 'launchpad/navigator';
22
23        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
24    }
25
26    /**
27     * Register our routes.
28     */
29    public function register_routes() {
30        // Register rest route for getting a list of available checklists and the currently active checklist.
31        register_rest_route(
32            $this->namespace,
33            $this->rest_base,
34            array(
35                array(
36                    'methods'             => WP_REST_Server::READABLE,
37                    'callback'            => array( $this, 'get_navigator_data' ),
38                    'permission_callback' => array( $this, 'can_access' ),
39                ),
40                array(
41                    'methods'             => WP_REST_Server::EDITABLE,
42                    'callback'            => array( $this, 'update_navigator_options' ),
43                    'permission_callback' => array( $this, 'can_access' ),
44                    'args'                => array(
45                        'active_checklist_slug' => array(
46                            'description'       => 'The slug of the checklist to set as active.',
47                            'type'              => array( 'null', 'string' ),
48                            'validate_callback' => array( $this, 'validate_checklist_slug_param' ),
49                        ),
50                        'remove_checklist_slug' => array(
51                            'description' => 'The slug of the checklist to remove from the active list.',
52                            'type'        => 'string',
53                            'enum'        => $this->get_readable_checklist_slug_enums(),
54                        ),
55                    ),
56                ),
57            )
58        );
59    }
60
61    /**
62     * Validates that the argument sent to the active_checklist_slug parameter is a valid checklist slug or empty.
63     *
64     * @param string|null $value The value of the active_checklist_slug parameter.
65     * @return bool
66     */
67    public function validate_checklist_slug_param( $value ) {
68        if ( $value === null ) {
69            return true;
70        }
71
72        return is_string( $value ) && in_array( $value, $this->get_checklist_slug_enums(), true );
73    }
74
75    /**
76     * Returns all available checklist slugs.
77     * TODO: This function is used by both endpoints, we should move it somewhere common.
78     *
79     * @return array Array of checklist slugs.
80     */
81    public function get_checklist_slug_enums() {
82        $checklists = wpcom_launchpad_checklists()->get_all_task_lists();
83        return array_keys( $checklists );
84    }
85
86    /**
87     * Returns the checklist slugs a read or removal request may reference.
88     *
89     * Retired flow slugs are included so a stale client can still read or remove
90     * one. Setting a retired slug as active still uses the registered slugs from
91     * get_checklist_slug_enums(), so it is rejected at the boundary.
92     *
93     * @return array Array of checklist slugs.
94     */
95    public function get_readable_checklist_slug_enums() {
96        return array_values(
97            array_unique( array_merge( $this->get_checklist_slug_enums(), wpcom_launchpad_get_retired_site_intents() ) )
98        );
99    }
100
101    /**
102     * Updates Launchpad navigator-related options and returns the result
103     *
104     * @param WP_REST_Request $request Request object.
105     */
106    public function update_navigator_options( $request ) {
107        $updated               = array();
108        $input                 = $request->get_json_params();
109        $extra_response_params = array();
110
111        foreach ( $input as $key => $value ) {
112            switch ( $key ) {
113                case 'active_checklist_slug':
114                    $updated[ $key ] = wpcom_launchpad_set_current_active_checklist( $input['active_checklist_slug'] );
115                    break;
116                case 'remove_checklist_slug':
117                    $removal_result  = wpcom_launchpad_navigator_remove_checklist( $input['remove_checklist_slug'] );
118                    $updated[ $key ] = $removal_result['updated'];
119
120                    $extra_response_params['new_active_checklist'] = $removal_result['new_active_checklist'];
121                    break;
122            }
123        }
124
125        return array_merge(
126            array(
127                'updated' => $updated,
128            ),
129            $extra_response_params
130        );
131    }
132
133    /**
134     * Returns a list of available checklists and the currently active checklist.
135     *
136     * @return array Array with two keys: `checklists` and `active_checklist`.
137     */
138    public function get_navigator_data() {
139        return array(
140            'available_checklists' => wpcom_launchpad_navigator_get_checklists(),
141            'current_checklist'    => wpcom_launchpad_get_active_checklist(),
142        );
143    }
144
145    /**
146     * Permission callback for the REST route.
147     *
148     * @return boolean
149     */
150    public function can_access() {
151        return current_user_can( 'manage_options' );
152    }
153}
154
155wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Launchpad_Navigator' );