PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7
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 / class-lp-widget.php

class-lp-widget.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7, at inc/class-lp-widget.php

336 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Widget class
4 *
5 * @package Learnpress/Abstracts
6 * @author ThimPress <nhamdv>
7 */
8
9 defined( 'ABSPATH' ) || exit;
10
11 /**
12 * LP_Widget
13 *
14 * @version 4.0.0
15 * @extends WP_Widget
16 */
17 class LP_Widget extends WP_Widget {
18
19 /**
20 * CSS class.
21 *
22 * @var string
23 */
24 public $widget_cssclass;
25
26 /**
27 * Widget description.
28 *
29 * @var string
30 */
31 public $widget_description;
32
33 /**
34 * Widget ID.
35 *
36 * @var string
37 */
38 public $widget_id;
39
40 /**
41 * Widget name.
42 *
43 * @var string
44 */
45 public $widget_name;
46
47 /**
48 * Enable rest_api for LearnPress.
49 *
50 * @var string
51 */
52 public $widget_in_rest = true;
53
54 /**
55 * New param add to rest api need for data.
56 *
57 * @var array
58 */
59 public $widget_data_attr = array();
60
61 /**
62 * Settings.
63 *
64 * @var array
65 */
66 public $settings;
67
68 /**
69 * Constructor.
70 */
71 public function __construct() {
72 $widget_ops = array(
73 'classname' => $this->widget_cssclass,
74 'description' => $this->widget_description,
75 'customize_selective_refresh' => true,
76 );
77
78 parent::__construct( $this->widget_id, $this->widget_name, $widget_ops );
79 }
80
81 /**
82 * Get this widgets title.
83 */
84 protected function get_instance_title( $instance ) {
85 if ( isset( $instance['title'] ) ) {
86 return $instance['title'];
87 }
88
89 if ( isset( $this->settings, $this->settings['title'], $this->settings['title']['std'] ) ) {
90 return $this->settings['title']['std'];
91 }
92
93 return '';
94 }
95
96 /**
97 * Output the html at the start of a widget.
98 *
99 * @param array $args Arguments.
100 * @param array $instance Instance.
101 */
102 public function widget_start( $args, $instance ) {
103 echo wp_kses_post( $args['before_widget'] );
104
105 $title = apply_filters( 'widget_title', $this->get_instance_title( $instance ), $instance, $this->id_base );
106
107 if ( $title ) {
108 echo wp_kses_post( $args['before_title'] . $title . $args['after_title'] );
109 }
110 }
111
112 /**
113 * Output the html at the end of a widget.
114 *
115 * @param array $args Arguments.
116 */
117 public function widget_end( $args ) {
118 echo wp_kses_post( $args['after_widget'] );
119 }
120
121 /**
122 * Output Widgets HTML.
123 *
124 * @param [type] $args
125 * @param [type] $instance
126 * @return void
127 */
128 public function widget( $args, $instance ) {
129 wp_enqueue_script( 'lp-widgets' );
130
131 do_action( 'before_show_lp_widget_content' );
132
133 $serialized_instance = serialize( $instance );
134
135 $data = array_merge(
136 $this->widget_data_attr,
137 array(
138 'widget' => $this->widget_id,
139 'instance' => base64_encode( $serialized_instance ),
140 'hash' => wp_hash( $serialized_instance ),
141 )
142 );
143
144 echo wp_kses_post( $this->lp_widget_content( $data, $args, $instance ) );
145 }
146
147 /**
148 * Show widget content.
149 *
150 * @param array $data Data attribute HTML for Rest API js.
151 * @param [type] $args Default Widget Args
152 * @param [type] $instance Default Widget Instance
153 * @return string HTML
154 */
155 public function lp_widget_content( $data, $args, $instance ) {
156 ob_start();
157
158 $this->widget_start( $args, $instance );
159
160 if ( ! is_admin() && $this->widget_in_rest ) {
161 ?>
162 <div class="learnpress-widget-wrapper learnpress-widget-wrapper__restapi" data-widget="<?php echo htmlentities( wp_json_encode( $data ) ); ?>" >
163 <?php echo lp_skeleton_animation_html( 5 ); ?>
164 </div>
165
166 <?php
167 } else { // Use for Preview in Widget Editor since WordPress 5.8
168 $content = $this->lp_rest_api_content( $instance, array() );
169
170 echo '<div class="learnpress-widget-wrapper">';
171
172 if ( is_wp_error( $content ) ) {
173 echo wp_kses_post( $content->get_error_message() );
174 } else {
175 echo wp_kses_post( $content );
176 }
177
178 echo '</div>';
179 }
180
181 $this->widget_end( $args );
182
183 return ob_get_clean();
184 }
185
186 /**
187 * Send content for API
188 *
189 * @param array $instance Widget Instance
190 * @param array $params RestAPI param need for content.
191 * @return string || WP_Error
192 */
193 public function lp_rest_api_content( $instance, $params ) {
194 return 'No content for Rest API';
195 }
196
197 /**
198 * Updates a particular instance of a widget.
199 */
200 public function update( $new_instance, $old_instance ) {
201
202 $instance = $old_instance;
203
204 if ( empty( $this->settings ) ) {
205 return $instance;
206 }
207
208 foreach ( $this->settings as $key => $setting ) {
209 if ( ! isset( $setting['type'] ) ) {
210 continue;
211 }
212
213 switch ( $setting['type'] ) {
214 case 'number':
215 $instance[ $key ] = absint( $new_instance[ $key ] );
216
217 if ( isset( $setting['min'] ) && '' !== $setting['min'] ) {
218 $instance[ $key ] = max( $instance[ $key ], $setting['min'] );
219 }
220
221 if ( isset( $setting['max'] ) && '' !== $setting['max'] ) {
222 $instance[ $key ] = min( $instance[ $key ], $setting['max'] );
223 }
224 break;
225 case 'textarea':
226 $instance[ $key ] = wp_kses( trim( wp_unslash( $new_instance[ $key ] ) ), wp_kses_allowed_html( 'post' ) );
227 break;
228 case 'checkbox':
229 $instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1;
230 break;
231 default:
232 $instance[ $key ] = isset( $new_instance[ $key ] ) ? sanitize_text_field( $new_instance[ $key ] ) : $setting['std'];
233 break;
234 }
235
236 /**
237 * Sanitize the value of a setting.
238 */
239 $instance[ $key ] = apply_filters( 'learnpress_widget_settings_sanitize_option', $instance[ $key ], $new_instance, $key, $setting );
240 }
241
242 return $instance;
243 }
244
245 /**
246 * Outputs the settings update form.
247 */
248 public function form( $instance ) {
249 if ( empty( $this->settings ) ) {
250 echo '<p>' . esc_html_e( 'There is no options for this widget.', 'learnpress' ) . '</p>';
251 return;
252 }
253
254 foreach ( $this->settings as $key => $setting ) {
255
256 $class = isset( $setting['class'] ) ? $setting['class'] : '';
257 $value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
258
259 switch ( $setting['type'] ) {
260
261 case 'text':
262 ?>
263 <p>
264 <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label><?php // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped ?>
265 <input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="text" value="<?php echo esc_attr( $value ); ?>" />
266 </p>
267 <?php
268 break;
269
270 case 'number':
271 ?>
272 <p>
273 <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label>
274 <input class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="number" step="<?php echo isset( $setting['step'] ) ? esc_attr( $setting['step'] ) : '1'; ?>" min="<?php echo isset( $setting['min'] ) ? esc_attr( $setting['min'] ) : ''; ?>" max="<?php echo isset( $setting['max'] ) ? esc_attr( $setting['max'] ) : ''; ?>" value="<?php echo esc_attr( $value ); ?>" />
275 </p>
276 <?php
277 break;
278
279 case 'select':
280 ?>
281 <p>
282 <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label>
283 <select class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>">
284 <?php foreach ( $setting['options'] as $option_key => $option_value ) : ?>
285 <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option>
286 <?php endforeach; ?>
287 </select>
288 </p>
289 <?php
290 break;
291
292 case 'textarea':
293 ?>
294 <p>
295 <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label>
296 <textarea class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" cols="20" rows="3"><?php echo esc_textarea( $value ); ?></textarea>
297 <?php if ( isset( $setting['desc'] ) ) : ?>
298 <small><?php echo esc_html( $setting['desc'] ); ?></small>
299 <?php endif; ?>
300 </p>
301 <?php
302 break;
303
304 case 'checkbox':
305 ?>
306 <p>
307 <input class="checkbox <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="checkbox" value="1" <?php checked( $value, 1 ); ?> />
308 <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label>
309 </p>
310 <?php
311 break;
312
313 case 'autocomplete':
314 ?>
315 <p>
316 <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label>
317 <select class="widefat lp-widget_select_course" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" data-rest-url="<?php echo get_rest_url(); ?>" data-post-type="<?php echo esc_attr( $setting['post_type'] ?? LP_COURSE_CPT ); ?>" style="width: 300px;">
318 <?php if ( ! empty( $value ) ) : ?>
319 <option value="<?php echo esc_attr( $value ); ?>" selected="selected"><?php echo esc_html( get_the_title( $value ) ); ?></option>
320 <?php endif; ?>
321 <script>
322 jQuery(document).trigger('learnpress/widgets/select');
323 </script>
324 </select>
325 </p>
326 <?php
327 break;
328
329 default:
330 do_action( 'learnpress_widget_field_' . $setting['type'], $key, $value, $setting, $instance );
331 break;
332 }
333 }
334 }
335 }
336