Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 61 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| RestApiController | |
0.00% |
0 / 61 |
|
0.00% |
0 / 6 |
110 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| checkPermission | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| get_handler | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
| post_handler | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
| delete_handler | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
| __callStatic | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace NewfoldLabs\WP\Module\Patterns\Api\Controllers; |
| 3 | |
| 4 | use NewfoldLabs\WP\Module\Patterns\Api\RemoteRequest; |
| 5 | use NewfoldLabs\WP\Module\Patterns\Permissions; |
| 6 | |
| 7 | /** |
| 8 | * RestApiController class. |
| 9 | */ |
| 10 | class RestApiController extends \WP_REST_Controller { |
| 11 | |
| 12 | /** |
| 13 | * The class instance. |
| 14 | * |
| 15 | * @var $instance |
| 16 | */ |
| 17 | protected static $instance = null; |
| 18 | |
| 19 | /** |
| 20 | * The constructor |
| 21 | */ |
| 22 | public function __construct() { |
| 23 | $this->namespace = 'nfd-wonder-blocks/v1'; |
| 24 | |
| 25 | \add_filter( |
| 26 | 'rest_request_before_callbacks', |
| 27 | function ( $response, $handler, $request ) { |
| 28 | |
| 29 | if ( $request->get_header( 'x_nfd_wonder_blocks' ) ) { |
| 30 | RemoteRequest::init( $request ); |
| 31 | } |
| 32 | |
| 33 | return $response; |
| 34 | }, |
| 35 | 10, |
| 36 | 3 |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Check the authorization of the request |
| 42 | * |
| 43 | * @return boolean |
| 44 | */ |
| 45 | public function checkPermission() { |
| 46 | |
| 47 | // Check for the nonce on the server (used by WP REST). |
| 48 | if ( isset( $_SERVER['HTTP_X_WP_NONCE'] ) && \wp_verify_nonce( \sanitize_text_field( \wp_unslash( $_SERVER['HTTP_X_WP_NONCE'] ) ), 'wp_rest' ) ) { |
| 49 | return Permissions::is_editor(); |
| 50 | } |
| 51 | |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Handle GET request. |
| 57 | * |
| 58 | * @param string $namespace - The api name space. |
| 59 | * @param string $endpoint - The endpoint. |
| 60 | * @param function $callback - The callback to run. |
| 61 | * @param array $args - The arguments to pass in. |
| 62 | * |
| 63 | * @return void |
| 64 | */ |
| 65 | public function get_handler( $namespace, $endpoint, $callback, $args = array() ) { |
| 66 | \register_rest_route( |
| 67 | $namespace, |
| 68 | $endpoint, |
| 69 | array( |
| 70 | 'methods' => \WP_REST_Server::READABLE, |
| 71 | 'callback' => $callback, |
| 72 | 'permission_callback' => array( |
| 73 | $this, |
| 74 | 'checkPermission', |
| 75 | ), |
| 76 | 'args' => $args, |
| 77 | ) |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Handle POST request. |
| 83 | * |
| 84 | * @param string $namespace - The api name space. |
| 85 | * @param string $endpoint - The endpoint. |
| 86 | * @param string $callback - The callback to run. |
| 87 | * @param array $args - The arguments to pass in. |
| 88 | * |
| 89 | * @return void |
| 90 | */ |
| 91 | public function post_handler( $namespace, $endpoint, $callback, $args = array() ) { |
| 92 | \register_rest_route( |
| 93 | $namespace, |
| 94 | $endpoint, |
| 95 | array( |
| 96 | 'methods' => \WP_REST_Server::CREATABLE, |
| 97 | 'callback' => $callback, |
| 98 | 'permission_callback' => array( |
| 99 | $this, |
| 100 | 'checkPermission', |
| 101 | ), |
| 102 | 'args' => $args, |
| 103 | ) |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Handle DELETE request. |
| 109 | * |
| 110 | * @param string $namespace - The api name space. |
| 111 | * @param string $endpoint - The endpoint. |
| 112 | * @param string $callback - The callback to run. |
| 113 | * @param array $args - The arguments to pass in. |
| 114 | * |
| 115 | * @return void |
| 116 | */ |
| 117 | public function delete_handler( $namespace, $endpoint, $callback, $args = array() ) { |
| 118 | \register_rest_route( |
| 119 | $namespace, |
| 120 | $endpoint, |
| 121 | array( |
| 122 | 'methods' => 'DELETE', |
| 123 | 'callback' => $callback, |
| 124 | 'permission_callback' => array( |
| 125 | $this, |
| 126 | 'checkPermission', |
| 127 | ), |
| 128 | 'args' => $args, |
| 129 | ) |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * The caller |
| 135 | * |
| 136 | * @param string $name - The name of the method to call. |
| 137 | * @param array $arguments - The arguments to pass in. |
| 138 | * |
| 139 | * @return mixed |
| 140 | */ |
| 141 | public static function __callStatic( $name, array $arguments ) { |
| 142 | $name = "{$name}_handler"; |
| 143 | |
| 144 | if ( is_null( self::$instance ) ) { |
| 145 | self::$instance = new static(); |
| 146 | } |
| 147 | |
| 148 | $r = self::$instance; |
| 149 | return $r->$name( $r->namespace, ...$arguments ); |
| 150 | } |
| 151 | } |