| @@ -1,58 +1,28 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | /** |
| 3 | 4 | * API router |
| 4 | 5 | */ |
| 5 | 6 | |
| 6 | -namespace Extendify\Library; | |
| 7 | +namespace Extendify; | |
| 7 | 8 | |
| 8 | -use Extendify\Library\App; | |
| 9 | -use Extendify\Library\Http; | |
| 9 | +defined('ABSPATH') || die('No direct access.'); | |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | 12 | * Simple router for the REST Endpoints |
| 13 | 13 | */ |
| 14 | + | |
| 14 | 15 | class ApiRouter extends \WP_REST_Controller |
| 15 | 16 | { |
| 16 | - | |
| 17 | 17 | /** |
| 18 | 18 | * The class instance. |
| 19 | 19 | * |
| 20 | - * @var $instance | |
| 20 | + * @var self|null | |
| 21 | 21 | */ |
| 22 | 22 | protected static $instance = null; |
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | - * The capablity required for access. | |
| 26 | - * | |
| 27 | - * @var $capability | |
| 28 | - */ | |
| 29 | - protected $capability; | |
| 30 | - | |
| 31 | - | |
| 32 | - /** | |
| 33 | - * The constructor | |
| 34 | - */ | |
| 35 | - public function __construct() | |
| 36 | - { | |
| 37 | - $this->capability = App::$requiredCapability; | |
| 38 | - add_filter( | |
| 39 | - 'rest_request_before_callbacks', | |
| 40 | - // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed | |
| 41 | - function ($response, $handler, $request) { | |
| 42 | - // Add the request to our helper class. | |
| 43 | - if ($request->get_header('x_extendify')) { | |
| 44 | - Http::init($request); | |
| 45 | - } | |
| 46 | - | |
| 47 | - return $response; | |
| 48 | - }, | |
| 49 | - 10, | |
| 50 | - 3 | |
| 51 | - ); | |
| 52 | - } | |
| 53 | - | |
| 54 | - /** | |
| 55 | 25 | * Check the authorization of the request |
| 56 | 26 | * |
| 57 | 27 | * @return boolean |
| 58 | 28 | */ |
| @@ -58,10 +28,13 @@ | ||
| 58 | 28 | */ |
| 59 | 29 | public function checkPermission() |
| 60 | 30 | { |
| 61 | 31 | // Check for the nonce on the server (used by WP REST). |
| 62 | - if (isset($_SERVER['HTTP_X_WP_NONCE']) && \wp_verify_nonce(sanitize_text_field(wp_unslash($_SERVER['HTTP_X_WP_NONCE'])), 'wp_rest')) { | |
| 63 | - return \current_user_can($this->capability); | |
| 32 | + if ( | |
| 33 | + isset($_SERVER['HTTP_X_WP_NONCE']) | |
| 34 | + && \wp_verify_nonce(sanitize_text_field(wp_unslash($_SERVER['HTTP_X_WP_NONCE'])), 'wp_rest') | |
| 35 | + ) { | |
| 36 | + return \current_user_can(Config::$requiredCapability); | |
| 64 | 37 | } |
| 65 | 38 | |
| 66 | 39 | return false; |
| 67 | 40 | } |
| @@ -76,20 +49,13 @@ | ||
| 76 | 49 | * @return void |
| 77 | 50 | */ |
| 78 | 51 | public function getHandler($namespace, $endpoint, $callback) |
| 79 | 52 | { |
| 80 | - \register_rest_route( | |
| 81 | - $namespace, | |
| 82 | - $endpoint, | |
| 83 | - [ | |
| 84 | - 'methods' => 'GET', | |
| 85 | - 'callback' => $callback, | |
| 86 | - 'permission_callback' => [ | |
| 87 | - $this, | |
| 88 | - 'checkPermission', | |
| 89 | - ], | |
| 90 | - ] | |
| 91 | - ); | |
| 53 | + \register_rest_route($namespace, $endpoint, [ | |
| 54 | + 'methods' => 'GET', | |
| 55 | + 'callback' => $callback, | |
| 56 | + 'permission_callback' => [$this, 'checkPermission'], | |
| 57 | + ]); | |
| 92 | 58 | } |
| 93 | 59 | |
| 94 | 60 | /** |
| 95 | 61 | * The post handler |
| @@ -101,20 +67,13 @@ | ||
| 101 | 67 | * @return void |
| 102 | 68 | */ |
| 103 | 69 | public function postHandler($namespace, $endpoint, $callback) |
| 104 | 70 | { |
| 105 | - \register_rest_route( | |
| 106 | - $namespace, | |
| 107 | - $endpoint, | |
| 108 | - [ | |
| 109 | - 'methods' => 'POST', | |
| 110 | - 'callback' => $callback, | |
| 111 | - 'permission_callback' => [ | |
| 112 | - $this, | |
| 113 | - 'checkPermission', | |
| 114 | - ], | |
| 115 | - ] | |
| 116 | - ); | |
| 71 | + \register_rest_route($namespace, $endpoint, [ | |
| 72 | + 'methods' => 'POST', | |
| 73 | + 'callback' => $callback, | |
| 74 | + 'permission_callback' => [$this, 'checkPermission'], | |
| 75 | + ]); | |
| 117 | 76 | } |
| 118 | 77 | |
| 119 | 78 | /** |
| 120 | 79 | * The caller |
| @@ -131,7 +90,7 @@ | ||
| 131 | 90 | self::$instance = new static(); |
| 132 | 91 | } |
| 133 | 92 | |
| 134 | 93 | $r = self::$instance; |
| 135 | - return $r->$name(APP::$slug . '/' . APP::$apiVersion, ...$arguments); | |
| 94 | + return $r->$name(Config::$slug . '/' . Config::$apiVersion, ...$arguments); | |
| 136 | 95 | } |
| 137 | 96 | } |