PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.9.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.9.1
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 / Ajax / LoadContentViaAjax.php

LoadContentViaAjax.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.9.1, at inc/Ajax/LoadContentViaAjax.php

85 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * class AjaxBase
4 *
5 * @since 4.2.7.6
6 * @version 1.0.1
7 */
8
9 namespace LearnPress\Ajax;
10
11 use Exception;
12 use LP_Helper;
13 use LP_REST_Response;
14 use stdClass;
15 use Throwable;
16
17 class LoadContentViaAjax extends AbstractAjax {
18 public function load_content_via_ajax() {
19 $response = new LP_REST_Response();
20
21 try {
22 $params = wp_unslash( $_REQUEST['data'] ?? '' );
23 if ( empty( $params ) ) {
24 throw new Exception( 'Error: params invalid!' );
25 }
26
27 $params = LP_Helper::json_decode( $params, true );
28
29 if ( empty( $params['callback'] ) ||
30 ! isset( $params['args'] ) ) {
31 throw new Exception( 'Error: params invalid!' );
32 }
33
34 // @var array $args
35 $args = $params['args'];
36 $callBack = $params['callback'];
37
38 if ( empty( $callBack['class'] ) ||
39 empty( $callBack['method'] ) ) {
40 throw new Exception( 'Error: callback invalid!' );
41 }
42
43 $class = $callBack['class'];
44 $method = $callBack['method'];
45
46 // Security: check callback is registered.
47 $allow_callbacks = apply_filters(
48 'lp/rest/ajax/allow_callback',
49 [
50 'LP_Admin_Dashboard:order_statistic',
51 'LP_Admin_Dashboard:plugin_status_content',
52 ]
53 );
54 $callBackStr = $class . ':' . $method;
55 if ( ! in_array( $callBackStr, $allow_callbacks ) ) {
56 throw new Exception( 'Error: callback is not register!' );
57 }
58
59 // Check class and method is callable.
60 if ( is_callable( [ $class, $method ] ) ) {
61 $data = call_user_func( [ $class, $method ], $args );
62 } else {
63 throw new Exception( 'Error: callback is not callable!' );
64 }
65
66 if ( ! $data instanceof stdClass && ! isset( $data->content ) ) {
67 throw new Exception( 'Error: data content invalid!' );
68 }
69
70 $response->message = $data->message ?? '';
71 unset( $data->message );
72
73 $response->status = $data->status ?? 'success';
74 unset( $data->status );
75
76 $response->data = $data;
77 } catch ( Throwable $e ) {
78 $response->status = 'error';
79 $response->message = $e->getMessage();
80 }
81
82 wp_send_json( $response );
83 }
84 }
85