PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.1
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.2.1, at inc/class-lp-widget.php

333 lines 9.4 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 $data = array_merge(
134 $this->widget_data_attr,
135 array(
136 'widget' => $this->widget_id,
137 'instance' => wp_json_encode( $instance ),
138 )
139 );
140
141 echo wp_kses_post( $this->lp_widget_content( $data, $args, $instance ) );
142 }
143
144 /**
145 * Show widget content.
146 *
147 * @param array $data Data attribute HTML for Rest API js.
148 * @param [type] $args Default Widget Args
149 * @param [type] $instance Default Widget Instance
150 * @return string HTML
151 */
152 public function lp_widget_content( $data, $args, $instance ) {
153 ob_start();
154
155 $this->widget_start( $args, $instance );
156
157 if ( ! is_admin() && $this->widget_in_rest ) {
158 ?>
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 ); ?>
161 </div>
162
163 <?php
164 } else { // Use for Preview in Widget Editor since WordPress 5.8
165 $content = $this->lp_rest_api_content( $instance, array() );
166
167 echo '<div class="learnpress-widget-wrapper">';
168
169 if ( is_wp_error( $content ) ) {
170 echo wp_kses_post( $content->get_error_message() );
171 } else {
172 echo wp_kses_post( $content );
173 }
174
175 echo '</div>';
176 }
177
178 $this->widget_end( $args );
179
180 return ob_get_clean();
181 }
182
183 /**
184 * Send content for API
185 *
186 * @param array $instance Widget Instance
187 * @param array $params RestAPI param need for content.
188 * @return string || WP_Error
189 */
190 public function lp_rest_api_content( $instance, $params ) {
191 return 'No content for Rest API';
192 }
193
194 /**
195 * Updates a particular instance of a widget.
196 */
197 public function update( $new_instance, $old_instance ) {
198
199 $instance = $old_instance;
200
201 if ( empty( $this->settings ) ) {
202 return $instance;
203 }
204
205 foreach ( $this->settings as $key => $setting ) {
206 if ( ! isset( $setting['type'] ) ) {
207 continue;
208 }
209
210 switch ( $setting['type'] ) {
211 case 'number':
212 $instance[ $key ] = absint( $new_instance[ $key ] );
213
214 if ( isset( $setting['min'] ) && '' !== $setting['min'] ) {
215 $instance[ $key ] = max( $instance[ $key ], $setting['min'] );
216 }
217
218 if ( isset( $setting['max'] ) && '' !== $setting['max'] ) {
219 $instance[ $key ] = min( $instance[ $key ], $setting['max'] );
220 }
221 break;
222 case 'textarea':
223 $instance[ $key ] = wp_kses( trim( wp_unslash( $new_instance[ $key ] ) ), wp_kses_allowed_html( 'post' ) );
224 break;
225 case 'checkbox':
226 $instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1;
227 break;
228 default:
229 $instance[ $key ] = isset( $new_instance[ $key ] ) ? sanitize_text_field( $new_instance[ $key ] ) : $setting['std'];
230 break;
231 }
232
233 /**
234 * Sanitize the value of a setting.
235 */
236 $instance[ $key ] = apply_filters( 'learnpress_widget_settings_sanitize_option', $instance[ $key ], $new_instance, $key, $setting );
237 }
238
239 return $instance;
240 }
241
242 /**
243 * Outputs the settings update form.
244 */
245 public function form( $instance ) {
246 if ( empty( $this->settings ) ) {
247 echo '<p>' . esc_html_e( 'There are no options for this widget.', 'learnpress' ) . '</p>';
248 return;
249 }
250
251 foreach ( $this->settings as $key => $setting ) {
252
253 $class = isset( $setting['class'] ) ? $setting['class'] : '';
254 $value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
255
256 switch ( $setting['type'] ) {
257
258 case 'text':
259 ?>
260 <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 ); ?>" />
263 </p>
264 <?php
265 break;
266
267 case 'number':
268 ?>
269 <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 ); ?>" />
272 </p>
273 <?php
274 break;
275
276 case 'select':
277 ?>
278 <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 ) ); ?>">
281 <?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>
283 <?php endforeach; ?>
284 </select>
285 </p>
286 <?php
287 break;
288
289 case 'textarea':
290 ?>
291 <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>
294 <?php if ( isset( $setting['desc'] ) ) : ?>
295 <small><?php echo esc_html( $setting['desc'] ); ?></small>
296 <?php endif; ?>
297 </p>
298 <?php
299 break;
300
301 case 'checkbox':
302 ?>
303 <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>
306 </p>
307 <?php
308 break;
309
310 case 'autocomplete':
311 ?>
312 <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;">
315 <?php if ( ! empty( $value ) ) : ?>
316 <option value="<?php echo esc_attr( $value ); ?>" selected="selected"><?php echo esc_html( get_the_title( $value ) ); ?></option>
317 <?php endif; ?>
318 <script>
319 jQuery(document).trigger('learnpress/widgets/select');
320 </script>
321 </select>
322 </p>
323 <?php
324 break;
325
326 default:
327 do_action( 'learnpress_widget_field_' . $setting['type'], $key, $value, $setting, $instance );
328 break;
329 }
330 }
331 }
332 }
333