balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

ApiRouter.php (3315B)


      1 <?php
      2 /**
      3  * API router
      4  */
      5 
      6 namespace Extendify\ExtendifySdk;
      7 
      8 use Extendify\ExtendifySdk\App;
      9 use Extendify\ExtendifySdk\Http;
     10 
     11 /**
     12  * Simple router for the REST Endpoints
     13  */
     14 class ApiRouter extends \WP_REST_Controller
     15 {
     16 
     17     /**
     18      * The class instance.
     19      *
     20      * @var $instance
     21      */
     22     protected static $instance = null;
     23 
     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      * Check the authorization of the request
     56      *
     57      * @return boolean
     58      */
     59     public function checkPermission()
     60     {
     61         // 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);
     64         }
     65 
     66         return false;
     67     }
     68 
     69     /**
     70      * Register dynamic routes
     71      *
     72      * @param string   $namespace - The api name space.
     73      * @param string   $endpoint  - The endpoint.
     74      * @param function $callback  - The callback to run.
     75      *
     76      * @return void
     77      */
     78     public function getHandler($namespace, $endpoint, $callback)
     79     {
     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         );
     92     }
     93 
     94     /**
     95      * The post handler
     96      *
     97      * @param string $namespace - The api name space.
     98      * @param string $endpoint  - The endpoint.
     99      * @param string $callback  - The callback to run.
    100      *
    101      * @return void
    102      */
    103     public function postHandler($namespace, $endpoint, $callback)
    104     {
    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         );
    117     }
    118 
    119     /**
    120      * The caller
    121      *
    122      * @param string $name      - The name of the method to call.
    123      * @param array  $arguments - The arguments to pass in.
    124      *
    125      * @return mixed
    126      */
    127     public static function __callStatic($name, array $arguments)
    128     {
    129         $name = "{$name}Handler";
    130         if (is_null(self::$instance)) {
    131             self::$instance = new static();
    132         }
    133 
    134         $r = self::$instance;
    135         return $r->$name(APP::$slug . '/' . APP::$apiVersion, ...$arguments);
    136     }
    137 }