42 ); |
42 ); |
43 parent::__construct( 'text', __( 'Text' ), $widget_ops, $control_ops ); |
43 parent::__construct( 'text', __( 'Text' ), $widget_ops, $control_ops ); |
44 } |
44 } |
45 |
45 |
46 /** |
46 /** |
47 * Add hooks for enqueueing assets when registering all widget instances of this widget class. |
47 * Adds hooks for enqueueing assets when registering all widget instances of this widget class. |
48 * |
48 * |
49 * @param int $number Optional. The unique order number of this widget instance |
49 * @param int $number Optional. The unique order number of this widget instance |
50 * compared to other instances of the same class. Default -1. |
50 * compared to other instances of the same class. Default -1. |
51 */ |
51 */ |
52 public function _register_one( $number = -1 ) { |
52 public function _register_one( $number = -1 ) { |
54 if ( $this->registered ) { |
54 if ( $this->registered ) { |
55 return; |
55 return; |
56 } |
56 } |
57 $this->registered = true; |
57 $this->registered = true; |
58 |
58 |
59 wp_add_inline_script( 'text-widgets', sprintf( 'wp.textWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) ); |
|
60 |
|
61 if ( $this->is_preview() ) { |
59 if ( $this->is_preview() ) { |
62 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) ); |
60 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) ); |
63 } |
61 } |
64 |
62 |
65 // Note that the widgets component in the customizer will also do |
63 /* |
66 // the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts(). |
64 * Note that the widgets component in the customizer will also do |
|
65 * the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts(). |
|
66 */ |
67 add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) ); |
67 add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) ); |
68 |
68 |
69 // Note that the widgets component in the customizer will also do |
69 /* |
70 // the 'admin_footer-widgets.php' action in WP_Customize_Widgets::print_footer_scripts(). |
70 * Note that the widgets component in the customizer will also do |
|
71 * the 'admin_footer-widgets.php' action in WP_Customize_Widgets::print_footer_scripts(). |
|
72 */ |
71 add_action( 'admin_footer-widgets.php', array( 'WP_Widget_Text', 'render_control_template_scripts' ) ); |
73 add_action( 'admin_footer-widgets.php', array( 'WP_Widget_Text', 'render_control_template_scripts' ) ); |
72 } |
74 } |
73 |
75 |
74 /** |
76 /** |
75 * Determines whether a given instance is legacy and should bypass using TinyMCE. |
77 * Determines whether a given instance is legacy and should bypass using TinyMCE. |
101 if ( empty( $instance['text'] ) ) { |
103 if ( empty( $instance['text'] ) ) { |
102 return false; |
104 return false; |
103 } |
105 } |
104 |
106 |
105 $wpautop = ! empty( $instance['filter'] ); |
107 $wpautop = ! empty( $instance['filter'] ); |
106 $has_line_breaks = ( false !== strpos( trim( $instance['text'] ), "\n" ) ); |
108 $has_line_breaks = ( str_contains( trim( $instance['text'] ), "\n" ) ); |
107 |
109 |
108 // If auto-paragraphs are not enabled and there are line breaks, then ensure legacy mode. |
110 // If auto-paragraphs are not enabled and there are line breaks, then ensure legacy mode. |
109 if ( ! $wpautop && $has_line_breaks ) { |
111 if ( ! $wpautop && $has_line_breaks ) { |
110 return true; |
112 return true; |
111 } |
113 } |
112 |
114 |
113 // If an HTML comment is present, assume legacy mode. |
115 // If an HTML comment is present, assume legacy mode. |
114 if ( false !== strpos( $instance['text'], '<!--' ) ) { |
116 if ( str_contains( $instance['text'], '<!--' ) ) { |
115 return true; |
117 return true; |
116 } |
118 } |
117 |
119 |
118 // In the rare case that DOMDocument is not available we cannot reliably sniff content and so we assume legacy. |
120 // In the rare case that DOMDocument is not available we cannot reliably sniff content and so we assume legacy. |
119 if ( ! class_exists( 'DOMDocument' ) ) { |
121 if ( ! class_exists( 'DOMDocument' ) ) { |
410 |
412 |
411 return $instance; |
413 return $instance; |
412 } |
414 } |
413 |
415 |
414 /** |
416 /** |
415 * Enqueue preview scripts. |
417 * Enqueues preview scripts. |
416 * |
418 * |
417 * These scripts normally are enqueued just-in-time when a playlist shortcode is used. |
419 * These scripts normally are enqueued just-in-time when a playlist shortcode is used. |
418 * However, in the customizer, a playlist shortcode may be used in a text widget and |
420 * However, in the customizer, a playlist shortcode may be used in a text widget and |
419 * dynamically added via selective refresh, so it is important to unconditionally enqueue them. |
421 * dynamically added via selective refresh, so it is important to unconditionally enqueue them. |
420 * |
422 * |
434 */ |
436 */ |
435 public function enqueue_admin_scripts() { |
437 public function enqueue_admin_scripts() { |
436 wp_enqueue_editor(); |
438 wp_enqueue_editor(); |
437 wp_enqueue_media(); |
439 wp_enqueue_media(); |
438 wp_enqueue_script( 'text-widgets' ); |
440 wp_enqueue_script( 'text-widgets' ); |
|
441 wp_add_inline_script( 'text-widgets', sprintf( 'wp.textWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) ); |
439 wp_add_inline_script( 'text-widgets', 'wp.textWidgets.init();', 'after' ); |
442 wp_add_inline_script( 'text-widgets', 'wp.textWidgets.init();', 'after' ); |
440 } |
443 } |
441 |
444 |
442 /** |
445 /** |
443 * Outputs the Text widget settings form. |
446 * Outputs the Text widget settings form. |
490 <input id="<?php echo $this->get_field_id( 'visual' ); ?>" name="<?php echo $this->get_field_name( 'visual' ); ?>" class="visual" type="hidden" value=""> |
493 <input id="<?php echo $this->get_field_id( 'visual' ); ?>" name="<?php echo $this->get_field_name( 'visual' ); ?>" class="visual" type="hidden" value=""> |
491 <p> |
494 <p> |
492 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> |
495 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> |
493 <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> |
496 <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> |
494 </p> |
497 </p> |
495 <div class="notice inline notice-info notice-alt"> |
498 <?php |
496 <?php if ( ! isset( $instance['visual'] ) ) : ?> |
499 if ( ! isset( $instance['visual'] ) ) { |
497 <p><?php _e( 'This widget may contain code that may work better in the “Custom HTML” widget. How about trying that widget instead?' ); ?></p> |
500 $widget_info_message = __( 'This widget may contain code that may work better in the “Custom HTML” widget. How about trying that widget instead?' ); |
498 <?php else : ?> |
501 } else { |
499 <p><?php _e( 'This widget may have contained code that may work better in the “Custom HTML” widget. If you have not yet, how about trying that widget instead?' ); ?></p> |
502 $widget_info_message = __( 'This widget may have contained code that may work better in the “Custom HTML” widget. If you have not yet, how about trying that widget instead?' ); |
500 <?php endif; ?> |
503 } |
501 </div> |
504 |
|
505 wp_admin_notice( |
|
506 $widget_info_message, |
|
507 array( |
|
508 'type' => 'info', |
|
509 'additional_classes' => array( 'notice-alt', 'inline' ), |
|
510 ) |
|
511 ); |
|
512 ?> |
502 <p> |
513 <p> |
503 <label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Content:' ); ?></label> |
514 <label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Content:' ); ?></label> |
504 <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>"><?php echo esc_textarea( $instance['text'] ); ?></textarea> |
515 <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>"><?php echo esc_textarea( $instance['text'] ); ?></textarea> |
505 </p> |
516 </p> |
506 <p> |
517 <p> |