| @@ -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,27 +122,32 @@ | ||
| 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 | |
| 133 | - $serialized_instance = serialize( $instance ); | |
| 137 | + if ( empty( $instance['show_in_rest'] ) ) { | |
| 138 | + $this->widget_in_rest = false; | |
| 139 | + } | |
| 134 | 140 | |
| 135 | 141 | $data = array_merge( |
| 136 | 142 | $this->widget_data_attr, |
| 137 | 143 | array( |
| 138 | 144 | 'widget' => $this->widget_id, |
| 139 | - 'instance' => base64_encode( $serialized_instance ), | |
| 140 | - 'hash' => wp_hash( $serialized_instance ), | |
| 145 | + 'instance' => wp_json_encode( $instance ), | |
| 141 | 146 | ) |
| 142 | 147 | ); |
| 143 | 148 | |
| 144 | - echo wp_kses_post( $this->lp_widget_content( $data, $args, $instance ) ); | |
| 149 | + echo $this->lp_widget_content( $data, $args, $instance ); | |
| 145 | 150 | } |
| 146 | 151 | |
| 147 | 152 | /** |
| 148 | 153 | * Show widget content. |
| @@ -149,8 +154,9 @@ | ||
| 149 | 154 | * |
| 150 | 155 | * @param array $data Data attribute HTML for Rest API js. |
| 151 | 156 | * @param [type] $args Default Widget Args |
| 152 | 157 | * @param [type] $instance Default Widget Instance |
| 158 | + * | |
| 153 | 159 | * @return string HTML |
| 154 | 160 | */ |
| 155 | 161 | public function lp_widget_content( $data, $args, $instance ) { |
| 156 | 162 | ob_start(); |
| @@ -158,22 +164,24 @@ | ||
| 158 | 164 | $this->widget_start( $args, $instance ); |
| 159 | 165 | |
| 160 | 166 | if ( ! is_admin() && $this->widget_in_rest ) { |
| 161 | 167 | ?> |
| 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 ); ?> | |
| 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> | |
| 164 | 172 | </div> |
| 165 | - | |
| 166 | 173 | <?php |
| 167 | 174 | } else { // Use for Preview in Widget Editor since WordPress 5.8 |
| 168 | 175 | $content = $this->lp_rest_api_content( $instance, array() ); |
| 169 | 176 | |
| 170 | - 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>'; | |
| 171 | 179 | |
| 172 | 180 | if ( is_wp_error( $content ) ) { |
| 173 | - echo wp_kses_post( $content->get_error_message() ); | |
| 181 | + echo $content->get_error_message(); | |
| 174 | 182 | } else { |
| 175 | - echo wp_kses_post( $content ); | |
| 183 | + echo $content; | |
| 176 | 184 | } |
| 177 | 185 | |
| 178 | 186 | echo '</div>'; |
| 179 | 187 | } |
| @@ -187,8 +195,9 @@ | ||
| 187 | 195 | * Send content for API |
| 188 | 196 | * |
| 189 | 197 | * @param array $instance Widget Instance |
| 190 | 198 | * @param array $params RestAPI param need for content. |
| 199 | + * | |
| 191 | 200 | * @return string || WP_Error |
| 192 | 201 | */ |
| 193 | 202 | public function lp_rest_api_content( $instance, $params ) { |
| 194 | 203 | return 'No content for Rest API'; |
| @@ -211,9 +220,9 @@ | ||
| 211 | 220 | } |
| 212 | 221 | |
| 213 | 222 | switch ( $setting['type'] ) { |
| 214 | 223 | case 'number': |
| 215 | - $instance[ $key ] = absint( $new_instance[ $key ] ); | |
| 224 | + $instance[ $key ] = absint( $new_instance[ $key ] ?? '' ); | |
| 216 | 225 | |
| 217 | 226 | if ( isset( $setting['min'] ) && '' !== $setting['min'] ) { |
| 218 | 227 | $instance[ $key ] = max( $instance[ $key ], $setting['min'] ); |
| 219 | 228 | } |
| @@ -227,10 +236,13 @@ | ||
| 227 | 236 | break; |
| 228 | 237 | case 'checkbox': |
| 229 | 238 | $instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1; |
| 230 | 239 | break; |
| 240 | + case 'sortable-checkbox': | |
| 241 | + $instance[ $key ] = empty( $new_instance[ $key ] ) ? [] : map_deep( $new_instance[ $key ], 'sanitize_text_field' ); | |
| 242 | + break; | |
| 231 | 243 | default: |
| 232 | - $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'] ?? '' ); | |
| 233 | 245 | break; |
| 234 | 246 | } |
| 235 | 247 | |
| 236 | 248 | /** |
| @@ -246,55 +258,84 @@ | ||
| 246 | 258 | * Outputs the settings update form. |
| 247 | 259 | */ |
| 248 | 260 | public function form( $instance ) { |
| 249 | 261 | if ( empty( $this->settings ) ) { |
| 250 | - 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 | + | |
| 251 | 264 | return; |
| 252 | 265 | } |
| 253 | 266 | |
| 254 | 267 | foreach ( $this->settings as $key => $setting ) { |
| 268 | + $class = $setting['class'] ?? ''; | |
| 269 | + $value = $instance[ $key ] ?? $setting['std'] ?? ''; | |
| 255 | 270 | |
| 256 | - $class = isset( $setting['class'] ) ? $setting['class'] : ''; | |
| 257 | - $value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std']; | |
| 258 | - | |
| 259 | 271 | switch ( $setting['type'] ) { |
| 260 | - | |
| 261 | 272 | case 'text': |
| 262 | 273 | ?> |
| 263 | 274 | <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 ); ?>" /> | |
| 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 ); ?>"/> | |
| 266 | 284 | </p> |
| 267 | 285 | <?php |
| 268 | 286 | break; |
| 269 | - | |
| 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; | |
| 270 | 298 | case 'number': |
| 271 | 299 | ?> |
| 272 | 300 | <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 ); ?>" /> | |
| 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 ); ?>"/> | |
| 275 | 310 | </p> |
| 276 | 311 | <?php |
| 277 | 312 | break; |
| 278 | - | |
| 279 | 313 | case 'select': |
| 280 | 314 | ?> |
| 281 | 315 | <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 ) ); ?>"> | |
| 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 ) ); ?>"> | |
| 284 | 321 | <?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> | |
| 322 | + <option | |
| 323 | + value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option> | |
| 286 | 324 | <?php endforeach; ?> |
| 287 | 325 | </select> |
| 288 | 326 | </p> |
| 289 | 327 | <?php |
| 290 | 328 | break; |
| 291 | - | |
| 292 | 329 | case 'textarea': |
| 293 | 330 | ?> |
| 294 | 331 | <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> | |
| 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> | |
| 297 | 338 | <?php if ( isset( $setting['desc'] ) ) : ?> |
| 298 | 339 | <small><?php echo esc_html( $setting['desc'] ); ?></small> |
| 299 | 340 | <?php endif; ?> |
| 300 | 341 | </p> |
| @@ -299,25 +340,102 @@ | ||
| 299 | 340 | <?php endif; ?> |
| 300 | 341 | </p> |
| 301 | 342 | <?php |
| 302 | 343 | break; |
| 303 | - | |
| 304 | 344 | case 'checkbox': |
| 305 | 345 | ?> |
| 306 | 346 | <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> | |
| 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> | |
| 309 | 353 | </p> |
| 310 | 354 | <?php |
| 311 | 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 | + } | |
| 312 | 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 | + | |
| 313 | 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 | + ]; | |
| 314 | 422 | ?> |
| 315 | 423 | <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;"> | |
| 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;"> | |
| 318 | 435 | <?php if ( ! empty( $value ) ) : ?> |
| 319 | - <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> | |
| 320 | 438 | <?php endif; ?> |
| 321 | 439 | <script> |
| 322 | 440 | jQuery(document).trigger('learnpress/widgets/select'); |
| 323 | 441 | </script> |