Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Endpoint | |
0.00% |
0 / 31 |
|
0.00% |
0 / 8 |
132 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| register_rest_route | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| handler | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| handle_get | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handle_post | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| handle_delete | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| create_nonce | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| permissions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack\Packages\Async_Option; |
| 4 | |
| 5 | class Endpoint { |
| 6 | |
| 7 | /** |
| 8 | * @var Async_Option $option |
| 9 | */ |
| 10 | private $option; |
| 11 | |
| 12 | /** |
| 13 | * @var string $rest_namespace |
| 14 | */ |
| 15 | private $rest_namespace; |
| 16 | |
| 17 | /** |
| 18 | * @var string $route |
| 19 | */ |
| 20 | private $route; |
| 21 | |
| 22 | /** |
| 23 | * @var Authenticated_Nonce |
| 24 | */ |
| 25 | private $nonce; |
| 26 | |
| 27 | /** |
| 28 | * @param string $namespace |
| 29 | * @param Async_Option $option |
| 30 | */ |
| 31 | public function __construct( $namespace, $route, Async_Option $option ) { |
| 32 | $this->option = $option; |
| 33 | $this->rest_namespace = $namespace; |
| 34 | $this->route = $route; |
| 35 | $this->nonce = new Authenticated_Nonce( "{$namespace}_{$option->key()}" ); |
| 36 | } |
| 37 | |
| 38 | public function register_rest_route() { |
| 39 | register_rest_route( |
| 40 | $this->rest_namespace, |
| 41 | $this->route, |
| 42 | array( |
| 43 | 'methods' => \WP_REST_Server::ALLMETHODS, |
| 44 | 'callback' => array( $this, 'handler' ), |
| 45 | 'permission_callback' => array( $this, 'permissions' ), |
| 46 | ) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Route the request to the apropriate handler. |
| 52 | * |
| 53 | * @param \WP_REST_Request $request |
| 54 | */ |
| 55 | public function handler( $request ) { |
| 56 | $methods = array( |
| 57 | 'GET' => 'handle_get', |
| 58 | 'POST' => 'handle_post', |
| 59 | 'DELETE' => 'handle_delete', |
| 60 | ); |
| 61 | |
| 62 | if ( ! isset( $methods[ $request->get_method() ] ) ) { |
| 63 | return new \WP_Error( 'invalid_method', 'Invalid method.', array( 'status' => 400 ) ); |
| 64 | } |
| 65 | |
| 66 | $method = $methods[ $request->get_method() ]; |
| 67 | |
| 68 | return rest_ensure_response( $this->$method( $request ) ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Handle GET Requests |
| 73 | * |
| 74 | * @param \WP_REST_Request $request Currently unused. |
| 75 | */ |
| 76 | public function handle_get( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 77 | return $this->option->get(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Handle POST Requests |
| 82 | * |
| 83 | * @param \WP_REST_Request $request |
| 84 | */ |
| 85 | public function handle_post( $request ) { |
| 86 | $this->option->set( $request->get_body() ); |
| 87 | if ( $this->option->has_errors() ) { |
| 88 | return new \WP_Error( 400, $this->option->get_errors(), array( 'status' => 400 ) ); |
| 89 | } |
| 90 | return $this->option->get(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Handle DELETE Requests. |
| 95 | */ |
| 96 | public function handle_delete() { |
| 97 | $this->option->delete(); |
| 98 | return $this->option->get(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Create a nonce for this endpoint |
| 103 | * |
| 104 | * @return false|string |
| 105 | */ |
| 106 | public function create_nonce() { |
| 107 | return $this->nonce->create(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @param \WP_REST_Request $request |
| 112 | */ |
| 113 | public function permissions( $request ) { |
| 114 | return current_user_can( 'manage_options' ) && $this->nonce->verify( $request->get_header( 'X-Async-Options-Nonce' ) ); |
| 115 | } |
| 116 | } |