← All changes
|
inc/rest-api/v1/frontend/class-lp-rest-widgets-controller.php
+71
-71
4.3.9
→
4.4.9
View file →
| @@ -1,71 +1,71 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * REST API LP Widget. | |
| 4 | - * | |
| 5 | - * @author Nhamdv <[email protected]> | |
| 6 | - */ | |
| 7 | - | |
| 8 | -class LP_REST_Widgets_Controller extends LP_Abstract_REST_Controller { | |
| 9 | - public function __construct() { | |
| 10 | - $this->namespace = 'lp/v1'; | |
| 11 | - $this->rest_base = 'widgets'; | |
| 12 | - | |
| 13 | - parent::__construct(); | |
| 14 | - } | |
| 15 | - | |
| 16 | - public function register_routes() { | |
| 17 | - $this->routes = array( | |
| 18 | - 'api' => array( | |
| 19 | - array( | |
| 20 | - 'methods' => WP_REST_Server::CREATABLE, | |
| 21 | - 'callback' => array( $this, 'get_content_widgets' ), | |
| 22 | - 'permission_callback' => '__return_true', | |
| 23 | - ), | |
| 24 | - ), | |
| 25 | - ); | |
| 26 | - | |
| 27 | - parent::register_routes(); | |
| 28 | - } | |
| 29 | - | |
| 30 | - public function get_content_widgets( WP_REST_Request $request ): LP_REST_Response { | |
| 31 | - global $wp_widget_factory; | |
| 32 | - | |
| 33 | - $response = new LP_REST_Response(); | |
| 34 | - $response->data = ''; | |
| 35 | - | |
| 36 | - try { | |
| 37 | - $params = $request->get_params(); | |
| 38 | - $widget_id = $params['widget'] ?? false; // LP_Widget. | |
| 39 | - $instance = $params['instance'] ?? false; | |
| 40 | - | |
| 41 | - if ( empty( $widget_id ) || empty( $instance ) ) { | |
| 42 | - throw new Exception( 'Error: No params!' ); | |
| 43 | - } | |
| 44 | - | |
| 45 | - $widget_object = $wp_widget_factory->get_widget_object( $widget_id ); | |
| 46 | - | |
| 47 | - if ( ! method_exists( $widget_object, 'lp_rest_api_content' ) ) { | |
| 48 | - throw new Exception( 'Error: No method lp_rest_api_content!' ); | |
| 49 | - } | |
| 50 | - | |
| 51 | - $instance = json_decode( $instance, true ); | |
| 52 | - | |
| 53 | - unset( $params['instance'] ); | |
| 54 | - unset( $params['hash'] ); | |
| 55 | - | |
| 56 | - $data = $widget_object->lp_rest_api_content( $instance, $params ); // LP_Widget->lp_rest_api_content. | |
| 57 | - | |
| 58 | - if ( is_wp_error( $data ) ) { | |
| 59 | - throw new Exception( $data->get_error_message() ); | |
| 60 | - } | |
| 61 | - | |
| 62 | - $response->status = 'success'; | |
| 63 | - $response->data = $data; | |
| 64 | - } catch ( Throwable $th ) { | |
| 65 | - $response->status = 'error'; | |
| 66 | - $response->message = $th->getMessage(); | |
| 67 | - } | |
| 68 | - | |
| 69 | - return $response; | |
| 70 | - } | |
| 71 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * REST API LP Widget. | |
| 4 | + * | |
| 5 | + * @author Nhamdv <[email protected]> | |
| 6 | + */ | |
| 7 | + | |
| 8 | +class LP_REST_Widgets_Controller extends LP_Abstract_REST_Controller { | |
| 9 | + public function __construct() { | |
| 10 | + $this->namespace = 'lp/v1'; | |
| 11 | + $this->rest_base = 'widgets'; | |
| 12 | + | |
| 13 | + parent::__construct(); | |
| 14 | + } | |
| 15 | + | |
| 16 | + public function register_routes() { | |
| 17 | + $this->routes = array( | |
| 18 | + 'api' => array( | |
| 19 | + array( | |
| 20 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 21 | + 'callback' => array( $this, 'get_content_widgets' ), | |
| 22 | + 'permission_callback' => '__return_true', | |
| 23 | + ), | |
| 24 | + ), | |
| 25 | + ); | |
| 26 | + | |
| 27 | + parent::register_routes(); | |
| 28 | + } | |
| 29 | + | |
| 30 | + public function get_content_widgets( WP_REST_Request $request ): LP_REST_Response { | |
| 31 | + global $wp_widget_factory; | |
| 32 | + | |
| 33 | + $response = new LP_REST_Response(); | |
| 34 | + $response->data = ''; | |
| 35 | + | |
| 36 | + try { | |
| 37 | + $params = $request->get_params(); | |
| 38 | + $widget_id = $params['widget'] ?? false; // LP_Widget. | |
| 39 | + $instance = $params['instance'] ?? false; | |
| 40 | + | |
| 41 | + if ( empty( $widget_id ) || empty( $instance ) ) { | |
| 42 | + throw new Exception( 'Error: No params!' ); | |
| 43 | + } | |
| 44 | + | |
| 45 | + $widget_object = $wp_widget_factory->get_widget_object( $widget_id ); | |
| 46 | + | |
| 47 | + if ( ! method_exists( $widget_object, 'lp_rest_api_content' ) ) { | |
| 48 | + throw new Exception( 'Error: No method lp_rest_api_content!' ); | |
| 49 | + } | |
| 50 | + | |
| 51 | + $instance = json_decode( $instance, true ); | |
| 52 | + | |
| 53 | + unset( $params['instance'] ); | |
| 54 | + unset( $params['hash'] ); | |
| 55 | + | |
| 56 | + $data = $widget_object->lp_rest_api_content( $instance, $params ); // LP_Widget->lp_rest_api_content. | |
| 57 | + | |
| 58 | + if ( is_wp_error( $data ) ) { | |
| 59 | + throw new Exception( $data->get_error_message() ); | |
| 60 | + } | |
| 61 | + | |
| 62 | + $response->status = 'success'; | |
| 63 | + $response->data = $data; | |
| 64 | + } catch ( Throwable $th ) { | |
| 65 | + $response->status = 'error'; | |
| 66 | + $response->message = $th->getMessage(); | |
| 67 | + } | |
| 68 | + | |
| 69 | + return $response; | |
| 70 | + } | |
| 71 | +} | |