PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
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
← All changes | inc/class-lp-widget.php +155 -34 4.1.7.24.4.8 View file →
@@ -111,9 +111,9 @@
111 111
112 112 /**
113 113 * Output the html at the end of a widget.
114 114 *
115 - * @param array $args Arguments.
115 + * @param array $args Arguments.
116 116 */
117 117 public function widget_end( $args ) {
118 118 echo wp_kses_post( $args['after_widget'] );
119 119 }
@@ -122,15 +122,23 @@
122 122 * Output Widgets HTML.
123 123 *
124 124 * @param [type] $args
125 125 * @param [type] $instance
126 + *
126 127 * @return void
127 128 */
128 129 public function widget( $args, $instance ) {
129 130 wp_enqueue_script( 'lp-widgets' );
131 + if ( $this->id_base === 'learnpress_widget_course_filter' ) {
132 + wp_enqueue_script( 'lp-course-filter' );
133 + }
130 134
131 - do_action( 'before_show_lp_widget_content' );
135 + do_action( 'learn-press/widget/before', $this, $args, $instance );
132 136
137 + if ( empty( $instance['show_in_rest'] ) ) {
138 + $this->widget_in_rest = false;
139 + }
140 +
133 141 $data = array_merge(
134 142 $this->widget_data_attr,
135 143 array(
136 144 'widget' => $this->widget_id,
@@ -137,9 +145,9 @@
137 145 'instance' => wp_json_encode( $instance ),
138 146 )
139 147 );
140 148
141 - echo wp_kses_post( $this->lp_widget_content( $data, $args, $instance ) );
149 + echo $this->lp_widget_content( $data, $args, $instance );
142 150 }
143 151
144 152 /**
145 153 * Show widget content.
@@ -146,8 +154,9 @@
146 154 *
147 155 * @param array $data Data attribute HTML for Rest API js.
148 156 * @param [type] $args Default Widget Args
149 157 * @param [type] $instance Default Widget Instance
158 + *
150 159 * @return string HTML
151 160 */
152 161 public function lp_widget_content( $data, $args, $instance ) {
153 162 ob_start();
@@ -155,22 +164,24 @@
155 164 $this->widget_start( $args, $instance );
156 165
157 166 if ( ! is_admin() && $this->widget_in_rest ) {
158 167 ?>
159 - <div class="learnpress-widget-wrapper learnpress-widget-wrapper__restapi" data-widget="<?php echo htmlentities( wp_json_encode( $data ) ); ?>" >
160 - <?php echo lp_skeleton_animation_html( 5 ); ?>
168 + <div class="learnpress-widget-wrapper learnpress-widget-wrapper__restapi"
169 + data-widget="<?php echo htmlentities( wp_json_encode( $data ) ); ?>">
170 + <?php lp_skeleton_animation_html( 5 ); ?>
171 + <div class="lp-widget-loading-change"></div>
161 172 </div>
162 -
163 173 <?php
164 174 } else { // Use for Preview in Widget Editor since WordPress 5.8
165 175 $content = $this->lp_rest_api_content( $instance, array() );
166 176
167 - echo '<div class="learnpress-widget-wrapper">';
177 + echo '<div class="learnpress-widget-wrapper" data-widget="' . htmlentities( wp_json_encode( $data ) ) . '">';
178 + echo '<div class="lp-widget-loading-change"></div>';
168 179
169 180 if ( is_wp_error( $content ) ) {
170 - echo wp_kses_post( $content->get_error_message() );
181 + echo $content->get_error_message();
171 182 } else {
172 - echo wp_kses_post( $content );
183 + echo $content;
173 184 }
174 185
175 186 echo '</div>';
176 187 }
@@ -184,8 +195,9 @@
184 195 * Send content for API
185 196 *
186 197 * @param array $instance Widget Instance
187 198 * @param array $params RestAPI param need for content.
199 + *
188 200 * @return string || WP_Error
189 201 */
190 202 public function lp_rest_api_content( $instance, $params ) {
191 203 return 'No content for Rest API';
@@ -208,9 +220,9 @@
208 220 }
209 221
210 222 switch ( $setting['type'] ) {
211 223 case 'number':
212 - $instance[ $key ] = absint( $new_instance[ $key ] );
224 + $instance[ $key ] = absint( $new_instance[ $key ] ?? '' );
213 225
214 226 if ( isset( $setting['min'] ) && '' !== $setting['min'] ) {
215 227 $instance[ $key ] = max( $instance[ $key ], $setting['min'] );
216 228 }
@@ -224,10 +236,13 @@
224 236 break;
225 237 case 'checkbox':
226 238 $instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1;
227 239 break;
240 + case 'sortable-checkbox':
241 + $instance[ $key ] = empty( $new_instance[ $key ] ) ? [] : map_deep( $new_instance[ $key ], 'sanitize_text_field' );
242 + break;
228 243 default:
229 - $instance[ $key ] = isset( $new_instance[ $key ] ) ? sanitize_text_field( $new_instance[ $key ] ) : $setting['std'];
244 + $instance[ $key ] = isset( $new_instance[ $key ] ) ? sanitize_text_field( $new_instance[ $key ] ) : ( $setting['std'] ?? '' );
230 245 break;
231 246 }
232 247
233 248 /**
@@ -243,55 +258,84 @@
243 258 * Outputs the settings update form.
244 259 */
245 260 public function form( $instance ) {
246 261 if ( empty( $this->settings ) ) {
247 - echo '<p>' . esc_html_e( 'There is no options for this widget.', 'learnpress' ) . '</p>';
262 + echo '<p>' . esc_html__( 'There are no options for this widget.', 'learnpress' ) . '</p>';
263 +
248 264 return;
249 265 }
250 266
251 267 foreach ( $this->settings as $key => $setting ) {
268 + $class = $setting['class'] ?? '';
269 + $value = $instance[ $key ] ?? $setting['std'] ?? '';
252 270
253 - $class = isset( $setting['class'] ) ? $setting['class'] : '';
254 - $value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
255 -
256 271 switch ( $setting['type'] ) {
257 -
258 272 case 'text':
259 273 ?>
260 274 <p>
261 - <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 ?>
262 - <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 ); ?>" />
275 + <label
276 + for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label>
277 + <?php
278 + // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
279 + ?>
280 + <input class="widefat <?php echo esc_attr( $class ); ?>"
281 + id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"
282 + name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="text"
283 + value="<?php echo esc_attr( $value ); ?>"/>
263 284 </p>
264 285 <?php
265 286 break;
266 -
287 + case 'hidden':
288 + ?>
289 + <p>
290 + <input
291 + class="fields-sort"
292 + id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"
293 + name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="hidden"
294 + value="<?php echo esc_attr( $value ); ?>"/>
295 + </p>
296 + <?php
297 + break;
267 298 case 'number':
268 299 ?>
269 300 <p>
270 - <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label>
271 - <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 ); ?>" />
301 + <label
302 + for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label>
303 + <input class="widefat <?php echo esc_attr( $class ); ?>"
304 + id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"
305 + name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="number"
306 + step="<?php echo isset( $setting['step'] ) ? esc_attr( $setting['step'] ) : '1'; ?>"
307 + min="<?php echo isset( $setting['min'] ) ? esc_attr( $setting['min'] ) : ''; ?>"
308 + max="<?php echo isset( $setting['max'] ) ? esc_attr( $setting['max'] ) : ''; ?>"
309 + value="<?php echo esc_attr( $value ); ?>"/>
272 310 </p>
273 311 <?php
274 312 break;
275 -
276 313 case 'select':
277 314 ?>
278 315 <p>
279 - <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label>
280 - <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 ) ); ?>">
316 + <label
317 + for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label>
318 + <select class="widefat <?php echo esc_attr( $class ); ?>"
319 + id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"
320 + name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>">
281 321 <?php foreach ( $setting['options'] as $option_key => $option_value ) : ?>
282 - <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option>
322 + <option
323 + value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option>
283 324 <?php endforeach; ?>
284 325 </select>
285 326 </p>
286 327 <?php
287 328 break;
288 -
289 329 case 'textarea':
290 330 ?>
291 331 <p>
292 - <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label>
293 - <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>
332 + <label
333 + for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label>
334 + <textarea class="widefat <?php echo esc_attr( $class ); ?>"
335 + id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"
336 + name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" cols="20"
337 + rows="3"><?php echo esc_textarea( $value ); ?></textarea>
294 338 <?php if ( isset( $setting['desc'] ) ) : ?>
295 339 <small><?php echo esc_html( $setting['desc'] ); ?></small>
296 340 <?php endif; ?>
297 341 </p>
@@ -296,25 +340,102 @@
296 340 <?php endif; ?>
297 341 </p>
298 342 <?php
299 343 break;
300 -
301 344 case 'checkbox':
302 345 ?>
303 346 <p>
304 - <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 ); ?> />
305 - <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label>
347 + <input class="checkbox <?php echo esc_attr( $class ); ?>"
348 + id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"
349 + name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="checkbox"
350 + value="1" <?php checked( $value, 1 ); ?> />
351 + <label
352 + for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label>
306 353 </p>
307 354 <?php
308 355 break;
356 + case 'sortable-checkbox':
357 + $values_default = $setting['std'] ?? array();
358 + $order = $instance['fields_order'] ?? '';
359 + ?>
360 + <div class="sortable-wrapper">
361 + <label><?php echo wp_kses_post( $setting['label'] ); ?></label>
362 + <div>
363 + <?php
364 + $options = $setting['options'] ?? array();
365 + if ( ! empty( $order ) ) {
366 + $order = explode( ',', $order );
367 + $new_options = array();
368 + foreach ( $order as $order_val ) {
369 + if ( isset( $options[ $order_val ] ) ) {
370 + $new_options[ $order_val ] = $options[ $order_val ];
371 + }
372 + }
309 373
374 + $options = $new_options;
375 + }
376 + ?>
377 + <div class="sortable <?php echo esc_attr( $class ); ?>">
378 + <?php
379 + foreach ( $options as $option_name => $option ) {
380 + $checked_value = ! empty( $instance ) ? ( $instance[ $key ] ?? array() ) : $values_default;
381 + ?>
382 + <div class="sortable__item">
383 + <i class="dashicons dashicons-menu drag"></i>
384 + <input class="checkbox"
385 + id="<?php echo esc_attr( $this->get_field_id( $option['id'] ) ); ?>"
386 + name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>[]"
387 + type="checkbox"
388 + value="<?php echo esc_attr( $option_name ); ?>"
389 + <?php checked( in_array( $option_name, $checked_value ) ); ?>
390 + />
391 + <label
392 + for="<?php echo esc_attr( $this->get_field_id( $option['id'] ) ); ?>"><?php echo wp_kses_post( $option['label'] ); ?></label>
393 + </div>
394 + <?php
395 + }
396 + ?>
397 + </div>
398 + </div>
399 + </div>
400 + <script>
401 + jQuery(document).trigger('learnpress/widgets/select');
402 + </script>
403 + <?php
404 + break;
405 +
310 406 case 'autocomplete':
407 + $data_struct = [
408 + 'urlApi' => get_rest_url( null, 'lp/v1/admin/tools/search-course' ),
409 + 'dataType' => 'courses',
410 + 'keyGetValue' => [
411 + 'value' => 'ID',
412 + 'text' => '{{post_title}} (#{{ID}})',
413 + 'key_render' => [
414 + 'post_title' => 'post_title',
415 + 'ID' => 'ID',
416 + ],
417 + ],
418 + 'setting' => [
419 + 'placeholder' => esc_html__( 'Choose Course', 'learnpress' ),
420 + ],
421 + ];
311 422 ?>
312 423 <p>
313 - <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"><?php echo wp_kses_post( $setting['label'] ); ?></label>
314 - <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;">
424 + <label for="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>">
425 + <?php echo wp_kses_post( $setting['label'] ); ?>
426 + </label>
427 + <select class="lp-tom-select"
428 + id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>"
429 + name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>"
430 + data-rest-url="<?php echo get_rest_url(); ?>"
431 + data-post-type="<?php echo esc_attr( $setting['post_type'] ?? LP_COURSE_CPT ); ?>"
432 + data-saved = "<?php echo esc_attr( $value ?? '' ); ?>"
433 + data-struct = "<?php echo htmlentities2( json_encode( $data_struct ) ); ?>"
434 + style="width: 300px;">
315 435 <?php if ( ! empty( $value ) ) : ?>
316 - <option value="<?php echo esc_attr( $value ); ?>" selected="selected"><?php echo esc_html( get_the_title( $value ) ); ?></option>
436 + <option value="<?php echo esc_attr( $value ); ?>"
437 + selected="selected"><?php echo esc_html( get_the_title( $value ) ); ?></option>
317 438 <?php endif; ?>
318 439 <script>
319 440 jQuery(document).trigger('learnpress/widgets/select');
320 441 </script>