| 1 |
<?php |
| 2 |
defined('ABSPATH') or die; |
| 3 |
|
| 4 |
if (!class_exists('CBPRouter')) { |
| 5 |
class CBPRouter extends \WP_REST_Controller |
| 6 |
{ |
| 7 |
protected static $instance = null; |
| 8 |
protected $namespace = 'code-block-pro/v1'; |
| 9 |
protected $capability = 'edit_posts'; |
| 10 |
public function getHandler($namespace, $endpoint, $callback) |
| 11 |
{ |
| 12 |
\register_rest_route( |
| 13 |
$namespace, |
| 14 |
$endpoint, |
| 15 |
[ |
| 16 |
'methods' => 'GET', |
| 17 |
'callback' => $callback, |
| 18 |
'permission_callback' => function () { |
| 19 |
return \current_user_can($this->capability); |
| 20 |
}, |
| 21 |
] |
| 22 |
); |
| 23 |
} |
| 24 |
public function postHandler($namespace, $endpoint, $callback) |
| 25 |
{ |
| 26 |
\register_rest_route( |
| 27 |
$namespace, |
| 28 |
$endpoint, |
| 29 |
[ |
| 30 |
'methods' => 'POST', |
| 31 |
'callback' => $callback, |
| 32 |
'permission_callback' => function () { |
| 33 |
return \current_user_can($this->capability); |
| 34 |
}, |
| 35 |
] |
| 36 |
); |
| 37 |
} |
| 38 |
|
| 39 |
public static function __callStatic($name, array $arguments) |
| 40 |
{ |
| 41 |
$name = "{$name}Handler"; |
| 42 |
if (is_null(self::$instance)) { |
| 43 |
self::$instance = new static(); |
| 44 |
} |
| 45 |
$r = self::$instance; |
| 46 |
return $r->$name($r->namespace, ...$arguments); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|