PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.5
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.5
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.4.5, at inc/Ajax/LoadContentViaAjax.php

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