PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / rest-api / v1 / frontend / class-lp-rest-widgets-controller.php

class-lp-rest-widgets-controller.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7.3, at inc/rest-api/v1/frontend/class-lp-rest-widgets-controller.php

72 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API LP Widget.
4 *
5 * @author Nhamdv <daonham95@gmail.com>
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 ) {
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 rest_ensure_response( $response );
70 }
71 }
72