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