← All changes
|
inc/rest-api/v1/frontend/class-lp-rest-ajax-controller.php
+110
-110
4.3.8
→
4.4.9
View file →
| @@ -1,110 +1,110 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * REST API LP, load all content want via REST. | |
| 4 | - * | |
| 5 | - * @since 4.2.5.7 | |
| 6 | - * @version 1.0.1 | |
| 7 | - */ | |
| 8 | - | |
| 9 | -class LP_REST_AJAX_Controller extends LP_Abstract_REST_Controller { | |
| 10 | - public function __construct() { | |
| 11 | - $this->namespace = 'lp/v1'; | |
| 12 | - $this->rest_base = 'load_content_via_ajax'; | |
| 13 | - | |
| 14 | - parent::__construct(); | |
| 15 | - } | |
| 16 | - | |
| 17 | - public function register_routes() { | |
| 18 | - $this->routes = array( | |
| 19 | - '/' => array( | |
| 20 | - array( | |
| 21 | - 'methods' => WP_REST_Server::ALLMETHODS, | |
| 22 | - 'callback' => array( $this, 'get_content' ), | |
| 23 | - 'permission_callback' => '__return_true', | |
| 24 | - ), | |
| 25 | - ), | |
| 26 | - ); | |
| 27 | - | |
| 28 | - parent::register_routes(); | |
| 29 | - } | |
| 30 | - | |
| 31 | - /** | |
| 32 | - * @param WP_REST_Request $request | |
| 33 | - * Has two params: callback and args. | |
| 34 | - * Data type of callback is [ 'class' => '', 'method' => '' ]. | |
| 35 | - * Data type of args is array. | |
| 36 | - * | |
| 37 | - * @return LP_REST_Response | |
| 38 | - * @since 4.2.5.7 | |
| 39 | - * @version 1.0.2 | |
| 40 | - */ | |
| 41 | - public function get_content( WP_REST_Request $request ): LP_REST_Response { | |
| 42 | - $response = new LP_REST_Response(); | |
| 43 | - | |
| 44 | - try { | |
| 45 | - /** | |
| 46 | - * Check verify nonce. | |
| 47 | - * | |
| 48 | - * Certificate, custom lpct-hmmbiz.com-certificate, lpct-assignment-file-jyoti-nagda, | |
| 49 | - * lpct-hivetutoring-schedule-enroll-users-to-courses using. | |
| 50 | - */ | |
| 51 | - if ( ! wp_verify_nonce( $request->get_header( 'X-WP-Nonce' ), 'wp_rest' ) ) { | |
| 52 | - throw new Exception( 'Error: invalid nonce!' ); | |
| 53 | - } | |
| 54 | - | |
| 55 | - $params = $request->get_params(); | |
| 56 | - | |
| 57 | - if ( empty( $params['callback'] ) || | |
| 58 | - ! isset( $params['args'] ) ) { | |
| 59 | - throw new Exception( 'Error: params invalid!' ); | |
| 60 | - } | |
| 61 | - | |
| 62 | - // @var array $args | |
| 63 | - $args = $params['args']; | |
| 64 | - $callBack = $params['callback']; | |
| 65 | - if ( $request->get_method() === 'GET' ) { | |
| 66 | - $args = LP_Helper::json_decode( $params['args'], true ); | |
| 67 | - $callBack = LP_Helper::json_decode( $params['callback'], true ); | |
| 68 | - } | |
| 69 | - | |
| 70 | - if ( empty( $callBack['class'] ) || | |
| 71 | - empty( $callBack['method'] ) ) { | |
| 72 | - throw new Exception( 'Error: callback invalid!' ); | |
| 73 | - } | |
| 74 | - | |
| 75 | - $class = $callBack['class']; | |
| 76 | - $method = $callBack['method']; | |
| 77 | - $data = null; | |
| 78 | - | |
| 79 | - // Security: check callback is registered. | |
| 80 | - $allow_callbacks = apply_filters( | |
| 81 | - 'lp/rest/ajax/allow_callback', | |
| 82 | - [] | |
| 83 | - ); | |
| 84 | - $callBackStr = $class . ':' . $method; | |
| 85 | - if ( ! in_array( $callBackStr, $allow_callbacks ) ) { | |
| 86 | - throw new Exception( 'Error: callback is not register!' ); | |
| 87 | - } | |
| 88 | - | |
| 89 | - // Check class and method is callable. | |
| 90 | - if ( is_callable( [ $class, $method ] ) ) { | |
| 91 | - $data = call_user_func( [ $class, $method ], $args ); | |
| 92 | - } else { | |
| 93 | - throw new Exception( 'Error: callback is not callable!' ); | |
| 94 | - } | |
| 95 | - | |
| 96 | - if ( ! $data instanceof stdClass && ! isset( $data->content ) ) { | |
| 97 | - throw new Exception( 'Error: data content invalid!' ); | |
| 98 | - } | |
| 99 | - | |
| 100 | - $response->status = 'success'; | |
| 101 | - $response->message = 'Success!'; | |
| 102 | - $response->data = $data; | |
| 103 | - } catch ( Throwable $e ) { | |
| 104 | - $response->status = 'error'; | |
| 105 | - $response->message = $e->getMessage(); | |
| 106 | - } | |
| 107 | - | |
| 108 | - return $response; | |
| 109 | - } | |
| 110 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * REST API LP, load all content want via REST. | |
| 4 | + * | |
| 5 | + * @since 4.2.5.7 | |
| 6 | + * @version 1.0.1 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +class LP_REST_AJAX_Controller extends LP_Abstract_REST_Controller { | |
| 10 | + public function __construct() { | |
| 11 | + $this->namespace = 'lp/v1'; | |
| 12 | + $this->rest_base = 'load_content_via_ajax'; | |
| 13 | + | |
| 14 | + parent::__construct(); | |
| 15 | + } | |
| 16 | + | |
| 17 | + public function register_routes() { | |
| 18 | + $this->routes = array( | |
| 19 | + '/' => array( | |
| 20 | + array( | |
| 21 | + 'methods' => WP_REST_Server::ALLMETHODS, | |
| 22 | + 'callback' => array( $this, 'get_content' ), | |
| 23 | + 'permission_callback' => '__return_true', | |
| 24 | + ), | |
| 25 | + ), | |
| 26 | + ); | |
| 27 | + | |
| 28 | + parent::register_routes(); | |
| 29 | + } | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * @param WP_REST_Request $request | |
| 33 | + * Has two params: callback and args. | |
| 34 | + * Data type of callback is [ 'class' => '', 'method' => '' ]. | |
| 35 | + * Data type of args is array. | |
| 36 | + * | |
| 37 | + * @return LP_REST_Response | |
| 38 | + * @since 4.2.5.7 | |
| 39 | + * @version 1.0.2 | |
| 40 | + */ | |
| 41 | + public function get_content( WP_REST_Request $request ): LP_REST_Response { | |
| 42 | + $response = new LP_REST_Response(); | |
| 43 | + | |
| 44 | + try { | |
| 45 | + /** | |
| 46 | + * Check verify nonce. | |
| 47 | + * | |
| 48 | + * Certificate, custom lpct-hmmbiz.com-certificate, lpct-assignment-file-jyoti-nagda, | |
| 49 | + * lpct-hivetutoring-schedule-enroll-users-to-courses using. | |
| 50 | + */ | |
| 51 | + if ( ! wp_verify_nonce( $request->get_header( 'X-WP-Nonce' ), 'wp_rest' ) ) { | |
| 52 | + throw new Exception( 'Error: invalid nonce!' ); | |
| 53 | + } | |
| 54 | + | |
| 55 | + $params = $request->get_params(); | |
| 56 | + | |
| 57 | + if ( empty( $params['callback'] ) || | |
| 58 | + ! isset( $params['args'] ) ) { | |
| 59 | + throw new Exception( 'Error: params invalid!' ); | |
| 60 | + } | |
| 61 | + | |
| 62 | + // @var array $args | |
| 63 | + $args = $params['args']; | |
| 64 | + $callBack = $params['callback']; | |
| 65 | + if ( $request->get_method() === 'GET' ) { | |
| 66 | + $args = LP_Helper::json_decode( $params['args'], true ); | |
| 67 | + $callBack = LP_Helper::json_decode( $params['callback'], true ); | |
| 68 | + } | |
| 69 | + | |
| 70 | + if ( empty( $callBack['class'] ) || | |
| 71 | + empty( $callBack['method'] ) ) { | |
| 72 | + throw new Exception( 'Error: callback invalid!' ); | |
| 73 | + } | |
| 74 | + | |
| 75 | + $class = $callBack['class']; | |
| 76 | + $method = $callBack['method']; | |
| 77 | + $data = null; | |
| 78 | + | |
| 79 | + // Security: check callback is registered. | |
| 80 | + $allow_callbacks = apply_filters( | |
| 81 | + 'lp/rest/ajax/allow_callback', | |
| 82 | + [] | |
| 83 | + ); | |
| 84 | + $callBackStr = $class . ':' . $method; | |
| 85 | + if ( ! in_array( $callBackStr, $allow_callbacks ) ) { | |
| 86 | + throw new Exception( 'Error: callback is not register!' ); | |
| 87 | + } | |
| 88 | + | |
| 89 | + // Check class and method is callable. | |
| 90 | + if ( is_callable( [ $class, $method ] ) ) { | |
| 91 | + $data = call_user_func( [ $class, $method ], $args ); | |
| 92 | + } else { | |
| 93 | + throw new Exception( 'Error: callback is not callable!' ); | |
| 94 | + } | |
| 95 | + | |
| 96 | + if ( ! $data instanceof stdClass && ! isset( $data->content ) ) { | |
| 97 | + throw new Exception( 'Error: data content invalid!' ); | |
| 98 | + } | |
| 99 | + | |
| 100 | + $response->status = 'success'; | |
| 101 | + $response->message = 'Success!'; | |
| 102 | + $response->data = $data; | |
| 103 | + } catch ( Throwable $e ) { | |
| 104 | + $response->status = 'error'; | |
| 105 | + $response->message = $e->getMessage(); | |
| 106 | + } | |
| 107 | + | |
| 108 | + return $response; | |
| 109 | + } | |
| 110 | +} | |