Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.31% |
36 / 39 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| WPCOM_REST_API_V2_Endpoint_Newsletter_Task_Lists | |
100.00% |
36 / 36 |
|
100.00% |
5 / 5 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| register_routes | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
2 | |||
| permission_check | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_task_list | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| skip_task | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * REST API endpoint for the Newsletter task lists (e.g. onboarding). |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | * @since 16.4 |
| 7 | */ |
| 8 | |
| 9 | use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request; |
| 10 | use Automattic\Jetpack\Status\Host; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit( 0 ); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Class WPCOM_REST_API_V2_Endpoint_Newsletter_Task_Lists |
| 18 | * |
| 19 | * Proxies `GET /sites/{blog_id}/newsletter/task-lists/{list_id}` and |
| 20 | * `POST /sites/{blog_id}/newsletter/task-lists/{list_id}/tasks/{task_id}/skip` so the Newsletter |
| 21 | * dashboard can show its checklist and skip its tasks. WP.com owns the implementation, including which |
| 22 | * lists and tasks exist and how each task's completion is checked and stored, but flags it as a |
| 23 | * site-specific, WP.com-only endpoint — meaning it only exists under `/wpcom/v2/sites/{blog_id}/…` |
| 24 | * on public-api and is unreachable from a Jetpack site's own REST API without this proxy. |
| 25 | */ |
| 26 | class WPCOM_REST_API_V2_Endpoint_Newsletter_Task_Lists extends WP_REST_Controller { |
| 27 | use WPCOM_REST_API_Proxy_Request; |
| 28 | |
| 29 | /** |
| 30 | * Constructor. |
| 31 | */ |
| 32 | public function __construct() { |
| 33 | $this->wpcom_is_wpcom_only_endpoint = true; |
| 34 | $this->wpcom_is_site_specific_endpoint = true; |
| 35 | $this->base_api_path = 'wpcom'; |
| 36 | $this->version = 'v2'; |
| 37 | $this->namespace = $this->base_api_path . '/' . $this->version; |
| 38 | $this->rest_base = '/newsletter/task-lists'; |
| 39 | |
| 40 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Register routes. |
| 45 | * |
| 46 | * Not registered on WP.com Simple: WP.com already registers its own implementation at these |
| 47 | * exact paths there, and a second registration would shadow it. Unknown list and task ids are |
| 48 | * left for WP.com to reject, so the routes only constrain their shape. |
| 49 | */ |
| 50 | public function register_routes() { |
| 51 | if ( ( new Host() )->is_wpcom_simple() ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | $list_route = $this->rest_base . '/(?P<list_id>[a-z_-]+)'; |
| 56 | |
| 57 | register_rest_route( |
| 58 | $this->namespace, |
| 59 | $list_route, |
| 60 | array( |
| 61 | 'show_in_index' => true, |
| 62 | 'methods' => WP_REST_Server::READABLE, |
| 63 | 'callback' => array( $this, 'get_task_list' ), |
| 64 | 'permission_callback' => array( $this, 'permission_check' ), |
| 65 | ) |
| 66 | ); |
| 67 | |
| 68 | register_rest_route( |
| 69 | $this->namespace, |
| 70 | $list_route . '/tasks/(?P<task_id>[a-z_]+)/skip', |
| 71 | array( |
| 72 | 'show_in_index' => true, |
| 73 | 'methods' => WP_REST_Server::CREATABLE, |
| 74 | 'callback' => array( $this, 'skip_task' ), |
| 75 | 'permission_callback' => array( $this, 'permission_check' ), |
| 76 | ) |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Only site administrators can read the Newsletter task lists and skip their tasks. |
| 82 | * |
| 83 | * @return bool |
| 84 | */ |
| 85 | public function permission_check() { |
| 86 | return current_user_can( 'manage_options' ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Proxy the task list read to WP.com, forwarding the list id as a path segment. |
| 91 | * |
| 92 | * @param WP_REST_Request $request Request object. |
| 93 | * @return mixed|WP_Error Response from WP.com, or an error. |
| 94 | */ |
| 95 | public function get_task_list( $request ) { |
| 96 | return $this->proxy_request_to_wpcom_as_user( $request, $request->get_param( 'list_id' ) ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Proxy a task skip to WP.com, forwarding the list and task ids as path segments. |
| 101 | * |
| 102 | * @param WP_REST_Request $request Request object. |
| 103 | * @return mixed|WP_Error Response from WP.com, or an error. |
| 104 | */ |
| 105 | public function skip_task( $request ) { |
| 106 | return $this->proxy_request_to_wpcom_as_user( |
| 107 | $request, |
| 108 | $request->get_param( 'list_id' ) . '/tasks/' . $request->get_param( 'task_id' ) . '/skip' |
| 109 | ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Newsletter_Task_Lists' ); |