PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
4.4.9 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 All 140 releases
learnpress / inc / TemplateHooks / TemplateAJAX.php

TemplateAJAX.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.9, at inc/TemplateHooks/TemplateAJAX.php

124 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Template load via AJAX.
4 * Use for load any content via AJAX.
5 *
6 * Logic:
7 * 1. Create html has class .lp-load-ajax-element attach setting, args want to handle on Template.
8 * 2. Create html element target to attach content via AJAX.
9 * 3. (loadAJAX.js) JS detect html element target and send data to server.
10 * 4. (LP_REST_AJAX_Controller) Server receive data, call callback Class::Method and render content.
11 *
12 * @since 4.2.5.7
13 * @version 1.0.1
14 */
15
16 namespace LearnPress\TemplateHooks;
17
18 use Exception;
19 use LearnPress\Helpers\Template;
20 use LP_Debug;
21 use stdClass;
22 use Throwable;
23
24 class TemplateAJAX {
25 /**
26 * When AJAX load done will innerHTML to class lp-target self.
27 * To use this, you need add hook 'lp/rest/ajax/allow_callback' to register for security reasons.
28 *
29 * @param array $args [ method_request' => 'GET/POST',... ],
30 * method_request: default is POST, use to set method type call to server.
31 * param html_loading_before_show_content: html loading before content call API to render.
32 * param html_loading_after_content_loaded: html loading when content rendered, and change.
33 * param html_no_load_ajax_first: if not empty, remove el_loading_before_show_content, will not load ajax first, only send data to handle AJAX via event.
34 *
35 * @param array $callback [ 'class', 'method' ] method use to render content html
36 *
37 * @return string
38 * @since 4.2.5.7
39 * @version 1.0.1
40 */
41 public static function load_content_via_ajax( array $args = [], array $callback = [] ): string {
42 $html_wrapper = [
43 '<div class="lp-load-ajax-element">' => '</div>',
44 ];
45 $html_content = '';
46
47 try {
48 if ( ! isset( $callback['class'] ) || ! isset( $callback['method'] ) ) {
49 throw new Exception( 'Missing args callback class || method' );
50 }
51
52 if ( isset( $args['html_no_load_ajax_first'] ) ) {
53 $html_wrapper = [];
54 }
55
56 $target_id = uniqid( 'lp-target-' );
57 $data = [
58 'args' => $args,
59 'callback' => $callback,
60 'id' => $target_id,
61 'method_request' => $args['method_request'] ?? 'POST',
62 ];
63
64 // Remove argument html no need send via AJAX.
65 if ( ! empty( $args ) ) {
66 $keys_unset = [
67 'html_no_load_ajax_first',
68 'html_loading_before_show_content',
69 'html_loading_after_content_loaded',
70 ];
71
72 foreach ( $keys_unset as $key ) {
73 if ( isset( $data['args'][ $key ] ) ) {
74 unset( $data['args'][ $key ] );
75 }
76 }
77 }
78
79 // If empty args, set to stdClass to JS read like an object, not array.
80 if ( empty( $data['args'] ) ) {
81 $data['args'] = new stdClass();
82 }
83
84 $data_send = esc_attr( htmlentities2( json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ) );
85 ob_start();
86 lp_skeleton_animation_html( 10 );
87 $el_loading_before_show_content_default = ob_get_clean();
88
89 $el_loading_before_show_content = sprintf(
90 '<div class="loading-first">%s</div>',
91 $args['html_loading_before_show_content'] ?? $el_loading_before_show_content_default
92 );
93 if ( isset( $args['html_no_load_ajax_first'] ) ) {
94 $el_loading_before_show_content = '';
95 }
96
97 $el_loading_after_content_loaded = sprintf(
98 '<div class="loading-after">%s</div>',
99 $args['html_loading_after_content_loaded'] ?? '<div class="lp-loading-change lp-hidden"></div>'
100 );
101
102 $html_el_target = sprintf(
103 '<div class="lp-target" data-id="%s" data-send="%s">%s</div>',
104 $target_id,
105 $data_send,
106 $args['html_no_load_ajax_first'] ?? ''
107 );
108 $html_content = sprintf(
109 '%1s%2s%3s',
110 $el_loading_before_show_content,
111 $html_el_target,
112 $el_loading_after_content_loaded
113 );
114 } catch ( Throwable $e ) {
115 LP_Debug::error_log( $e );
116 }
117
118 return Template::instance()->nest_elements( $html_wrapper, $html_content );
119 }
120
121 public function create_lp_target_with_data_for_load_ajax() {
122 }
123 }
124