| @@ -1,67 +1,67 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Class LP_Abstract_REST_Controller | |
| 5 | - */ | |
| 6 | -class LP_Abstract_REST_Controller extends WP_REST_Controller { | |
| 7 | - | |
| 8 | - /** | |
| 9 | - * @var string | |
| 10 | - */ | |
| 11 | - public $namespace = 'lp/v1'; | |
| 12 | - | |
| 13 | - /** | |
| 14 | - * @var string | |
| 15 | - */ | |
| 16 | - public $rest_base = ''; | |
| 17 | - | |
| 18 | - /** | |
| 19 | - * @var array | |
| 20 | - */ | |
| 21 | - public $routes = array(); | |
| 22 | - | |
| 23 | - public function __construct() { | |
| 24 | - | |
| 25 | - } | |
| 26 | - | |
| 27 | - /** | |
| 28 | - * Register routes for controller. | |
| 29 | - */ | |
| 30 | - public function register_routes() { | |
| 31 | - | |
| 32 | - if ( ! $this->routes ) { | |
| 33 | - return; | |
| 34 | - } | |
| 35 | - | |
| 36 | - foreach ( $this->routes as $key => $args ) { | |
| 37 | - $rest_base = $this->rest_base; | |
| 38 | - $override = false; | |
| 39 | - | |
| 40 | - if ( is_bool( end( $args ) ) ) { | |
| 41 | - $override = array_pop( $args ); | |
| 42 | - } | |
| 43 | - | |
| 44 | - if ( ! is_numeric( $key ) ) { | |
| 45 | - $rest_base = "{$rest_base}/{$key}"; | |
| 46 | - } | |
| 47 | - | |
| 48 | - register_rest_route( $this->namespace, '/' . $rest_base, $args, $override ); | |
| 49 | - } | |
| 50 | - } | |
| 51 | - | |
| 52 | - public function ensure_response( $data ) { | |
| 53 | - add_filter( 'rest_pre_serve_request', array( $this, 'print_response' ), 10, 4 ); | |
| 54 | - | |
| 55 | - return rest_ensure_response( $data ); | |
| 56 | - } | |
| 57 | - | |
| 58 | - /** | |
| 59 | - * @param boolean $false | |
| 60 | - * @param WP_REST_Response $result | |
| 61 | - * @param WP_REST_Request $request | |
| 62 | - * @param WP_REST_Server $server | |
| 63 | - */ | |
| 64 | - public function print_response( $false, $result, $request, $server ) { | |
| 65 | - learn_press_send_json( $result->get_data() ); | |
| 66 | - } | |
| 67 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Class LP_Abstract_REST_Controller | |
| 5 | + */ | |
| 6 | +class LP_Abstract_REST_Controller extends WP_REST_Controller { | |
| 7 | + | |
| 8 | + /** | |
| 9 | + * @var string | |
| 10 | + */ | |
| 11 | + public $namespace = 'lp/v1'; | |
| 12 | + | |
| 13 | + /** | |
| 14 | + * @var string | |
| 15 | + */ | |
| 16 | + public $rest_base = ''; | |
| 17 | + | |
| 18 | + /** | |
| 19 | + * @var array | |
| 20 | + */ | |
| 21 | + public $routes = array(); | |
| 22 | + | |
| 23 | + public function __construct() { | |
| 24 | + | |
| 25 | + } | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * Register routes for controller. | |
| 29 | + */ | |
| 30 | + public function register_routes() { | |
| 31 | + | |
| 32 | + if ( ! $this->routes ) { | |
| 33 | + return; | |
| 34 | + } | |
| 35 | + | |
| 36 | + foreach ( $this->routes as $key => $args ) { | |
| 37 | + $rest_base = $this->rest_base; | |
| 38 | + $override = false; | |
| 39 | + | |
| 40 | + if ( is_bool( end( $args ) ) ) { | |
| 41 | + $override = array_pop( $args ); | |
| 42 | + } | |
| 43 | + | |
| 44 | + if ( ! is_numeric( $key ) ) { | |
| 45 | + $rest_base = "{$rest_base}/{$key}"; | |
| 46 | + } | |
| 47 | + | |
| 48 | + register_rest_route( $this->namespace, '/' . $rest_base, $args, $override ); | |
| 49 | + } | |
| 50 | + } | |
| 51 | + | |
| 52 | + public function ensure_response( $data ) { | |
| 53 | + add_filter( 'rest_pre_serve_request', array( $this, 'print_response' ), 10, 4 ); | |
| 54 | + | |
| 55 | + return rest_ensure_response( $data ); | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * @param boolean $false | |
| 60 | + * @param WP_REST_Response $result | |
| 61 | + * @param WP_REST_Request $request | |
| 62 | + * @param WP_REST_Server $server | |
| 63 | + */ | |
| 64 | + public function print_response( $false, $result, $request, $server ) { | |
| 65 | + learn_press_send_json( $result->get_data() ); | |
| 66 | + } | |
| 67 | +} | |