traits
4 years ago
dynamic-rest-url-trait.php
4 years ago
rest-api-controller-base.php
4 years ago
rest-api-endpoint-base.php
4 years ago
rest-response.php
4 years ago
rest-api-controller-base.php
45 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Rest_Api; |
| 5 | |
| 6 | abstract class Rest_Api_Controller_Base { |
| 7 | |
| 8 | public function rest_api_init() { |
| 9 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * @return Rest_Api_Endpoint_Base[] |
| 14 | */ |
| 15 | abstract public function routes(): array; |
| 16 | |
| 17 | public function register_routes() { |
| 18 | foreach ( $this->routes() as $route ) { |
| 19 | $endpoint_args = $route->get_overridden_args() |
| 20 | ?: array( |
| 21 | 'methods' => $route::get_methods(), |
| 22 | 'callback' => array( $route, 'run_callback' ), |
| 23 | 'permission_callback' => array( $route, 'check_permission' ), |
| 24 | 'args' => $route->get_common_args(), |
| 25 | ); |
| 26 | |
| 27 | $result = register_rest_route( |
| 28 | $route::get_namespace(), |
| 29 | "/{$route::get_rest_base()}", |
| 30 | $endpoint_args, |
| 31 | $route->get_override() |
| 32 | ); |
| 33 | |
| 34 | if ( ! $result ) { |
| 35 | _doing_it_wrong( |
| 36 | __METHOD__, |
| 37 | "Error on register REST API route: {$route::get_namespace()}/{$route::get_rest_base()}", |
| 38 | '1.4.0' |
| 39 | ); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | } |
| 45 |