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

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

122 lines 3.8 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 stdClass;
21 use Throwable;
22
23 class TemplateAJAX {
24 /**
25 * When AJAX load done will innerHTML to class lp-target self.
26 * To use this, you need add hook 'lp/rest/ajax/allow_callback' to register for security reasons.
27 *
28 * @param array $args [ method_request' => 'GET/POST',... ],
29 * method_request: default is POST, use to set method type call to server.
30 * param html_loading_before_show_content: html loading before content call API to render.
31 * param html_loading_after_content_loaded: html loading when content rendered, and change.
32 * 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.
33 *
34 * @param array $callback [ 'class', 'method' ] method use to render content html
35 *
36 * @return string
37 * @since 4.2.5.7
38 * @version 1.0.1
39 */
40 public static function load_content_via_ajax( array $args = [], array $callback = [] ): string {
41 $html_wrapper = [
42 '<div class="lp-load-ajax-element">' => '</div>',
43 ];
44 $html_content = '';
45
46 try {
47 if ( ! isset( $callback['class'] ) || ! isset( $callback['method'] ) ) {
48 throw new Exception( 'Missing args callback class || method' );
49 }
50
51 if ( isset( $args['html_no_load_ajax_first'] ) ) {
52 $html_wrapper = [];
53 }
54
55 $target_id = uniqid( 'lp-target-' );
56 $data = [
57 'args' => $args,
58 'callback' => $callback,
59 'id' => $target_id,
60 ];
61
62 // Remove argument html no need send via AJAX.
63 if ( ! empty( $args ) ) {
64 $keys_unset = [
65 'html_no_load_ajax_first',
66 'html_loading_before_show_content',
67 'html_loading_after_content_loaded',
68 ];
69
70 foreach ( $keys_unset as $key ) {
71 if ( isset( $data['args'][ $key ] ) ) {
72 unset( $data['args'][ $key ] );
73 }
74 }
75 }
76
77 // If empty args, set to stdClass to JS read like an object, not array.
78 if ( empty( $data['args'] ) ) {
79 $data['args'] = new stdClass();
80 }
81
82 $data_send = esc_attr( htmlentities2( json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ) );
83 ob_start();
84 lp_skeleton_animation_html( 10 );
85 $el_loading_before_show_content_default = ob_get_clean();
86
87 $el_loading_before_show_content = sprintf(
88 '<div class="loading-first">%s</div>',
89 $args['html_loading_before_show_content'] ?? $el_loading_before_show_content_default
90 );
91 if ( isset( $args['html_no_load_ajax_first'] ) ) {
92 $el_loading_before_show_content = '';
93 }
94
95 $el_loading_after_content_loaded = sprintf(
96 '<div class="loading-after">%s</div>',
97 $args['html_loading_after_content_loaded'] ?? '<div class="lp-loading-change lp-hidden"></div>'
98 );
99
100 $html_el_target = sprintf(
101 '<div class="lp-target" data-id="%s" data-send="%s">%s</div>',
102 $target_id,
103 $data_send,
104 $args['html_no_load_ajax_first'] ?? ''
105 );
106 $html_content = sprintf(
107 '%1s%2s%3s',
108 $el_loading_before_show_content,
109 $html_el_target,
110 $el_loading_after_content_loaded
111 );
112 } catch ( Throwable $e ) {
113 error_log( __METHOD__ . ' ' . $e->getMessage() );
114 }
115
116 return Template::instance()->nest_elements( $html_wrapper, $html_content );
117 }
118
119 public function create_lp_target_with_data_for_load_ajax() {
120 }
121 }
122