Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 60 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| WPCOM_REST_API_V2_Endpoint_Launchpad_Navigator | |
0.00% |
0 / 59 |
|
0.00% |
0 / 7 |
156 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| register_routes | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
2 | |||
| validate_checklist_slug_param | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| get_checklist_slug_enums | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| update_navigator_options | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
| get_navigator_data | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| can_access | |
0.00% |
0 / 1 |
|
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 | */ |
| 14 | class 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_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 $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 | * Updates Launchpad navigator-related options and returns the result |
| 88 | * |
| 89 | * @param WP_REST_Request $request Request object. |
| 90 | */ |
| 91 | public function update_navigator_options( $request ) { |
| 92 | $updated = array(); |
| 93 | $input = $request->get_json_params(); |
| 94 | $extra_response_params = array(); |
| 95 | |
| 96 | foreach ( $input as $key => $value ) { |
| 97 | switch ( $key ) { |
| 98 | case 'active_checklist_slug': |
| 99 | $updated[ $key ] = wpcom_launchpad_set_current_active_checklist( $input['active_checklist_slug'] ); |
| 100 | break; |
| 101 | case 'remove_checklist_slug': |
| 102 | $removal_result = wpcom_launchpad_navigator_remove_checklist( $input['remove_checklist_slug'] ); |
| 103 | $updated[ $key ] = $removal_result['updated']; |
| 104 | |
| 105 | $extra_response_params['new_active_checklist'] = $removal_result['new_active_checklist']; |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return array_merge( |
| 111 | array( |
| 112 | 'updated' => $updated, |
| 113 | ), |
| 114 | $extra_response_params |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns a list of available checklists and the currently active checklist. |
| 120 | * |
| 121 | * @return array Array with two keys: `checklists` and `active_checklist`. |
| 122 | */ |
| 123 | public function get_navigator_data() { |
| 124 | return array( |
| 125 | 'available_checklists' => wpcom_launchpad_navigator_get_checklists(), |
| 126 | 'current_checklist' => wpcom_launchpad_get_active_checklist(), |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Permission callback for the REST route. |
| 132 | * |
| 133 | * @return boolean |
| 134 | */ |
| 135 | public function can_access() { |
| 136 | return current_user_can( 'manage_options' ); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Launchpad_Navigator' ); |