activation-actions.php
4 years ago
build-inline-scripts.php
3 years ago
build-modules.php
4 years ago
build-widgets.php
4 years ago
config-list.php
4 years ago
handler-api.php
4 years ago
handler-widget.php
4 years ago
handler-api.php
46 lines
| 1 | <?php |
| 2 | namespace ElementsKit_Lite\Core; |
| 3 | |
| 4 | class Handler_Api { |
| 5 | |
| 6 | public $prefix = ''; |
| 7 | public $param = ''; |
| 8 | public $request = null; |
| 9 | |
| 10 | public function __construct() { |
| 11 | $this->config(); |
| 12 | $this->init(); |
| 13 | } |
| 14 | |
| 15 | public function config() { |
| 16 | } |
| 17 | |
| 18 | public function init() { |
| 19 | add_action( |
| 20 | 'rest_api_init', |
| 21 | function () { |
| 22 | register_rest_route( |
| 23 | untrailingslashit( 'elementskit/v1/' . $this->prefix ), |
| 24 | '/(?P<action>\w+)/' . ltrim( $this->param, '/' ), |
| 25 | array( |
| 26 | 'methods' => \WP_REST_Server::ALLMETHODS, |
| 27 | 'callback' => array( $this, 'callback' ), |
| 28 | 'permission_callback' => '__return_true', |
| 29 | // all permissions are implimented inside the callback action |
| 30 | ) |
| 31 | ); |
| 32 | } |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | public function callback( $request ) { |
| 37 | $this->request = $request; |
| 38 | $action_class = strtolower( $this->request->get_method() ) . '_' . $this->request['action']; |
| 39 | |
| 40 | if ( method_exists( $this, $action_class ) ) { |
| 41 | return $this->{$action_class}(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | } |
| 46 |