wp/wp-includes/widgets/class-wp-widget-text.php
changeset 7 cf61fcea0001
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
       
     1 <?php
       
     2 /**
       
     3  * Widget API: WP_Widget_Text class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Widgets
       
     7  * @since 4.4.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Core class used to implement a Text widget.
       
    12  *
       
    13  * @since 2.8.0
       
    14  *
       
    15  * @see WP_Widget
       
    16  */
       
    17 class WP_Widget_Text extends WP_Widget {
       
    18 
       
    19 	/**
       
    20 	 * Whether or not the widget has been registered yet.
       
    21 	 *
       
    22 	 * @since 4.8.1
       
    23 	 * @var bool
       
    24 	 */
       
    25 	protected $registered = false;
       
    26 
       
    27 	/**
       
    28 	 * Sets up a new Text widget instance.
       
    29 	 *
       
    30 	 * @since 2.8.0
       
    31 	 */
       
    32 	public function __construct() {
       
    33 		$widget_ops = array(
       
    34 			'classname' => 'widget_text',
       
    35 			'description' => __( 'Arbitrary text.' ),
       
    36 			'customize_selective_refresh' => true,
       
    37 		);
       
    38 		$control_ops = array(
       
    39 			'width' => 400,
       
    40 			'height' => 350,
       
    41 		);
       
    42 		parent::__construct( 'text', __( 'Text' ), $widget_ops, $control_ops );
       
    43 	}
       
    44 
       
    45 	/**
       
    46 	 * Add hooks for enqueueing assets when registering all widget instances of this widget class.
       
    47 	 *
       
    48 	 * @param integer $number Optional. The unique order number of this widget instance
       
    49 	 *                        compared to other instances of the same class. Default -1.
       
    50 	 */
       
    51 	public function _register_one( $number = -1 ) {
       
    52 		parent::_register_one( $number );
       
    53 		if ( $this->registered ) {
       
    54 			return;
       
    55 		}
       
    56 		$this->registered = true;
       
    57 
       
    58 		wp_add_inline_script( 'text-widgets', sprintf( 'wp.textWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
       
    59 
       
    60 		if ( $this->is_preview() ) {
       
    61 			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) );
       
    62 		}
       
    63 
       
    64 		// Note that the widgets component in the customizer will also do the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts().
       
    65 		add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) );
       
    66 
       
    67 		// Note that the widgets component in the customizer will also do the 'admin_footer-widgets.php' action in WP_Customize_Widgets::print_footer_scripts().
       
    68 		add_action( 'admin_footer-widgets.php', array( 'WP_Widget_Text', 'render_control_template_scripts' ) );
       
    69 	}
       
    70 
       
    71 	/**
       
    72 	 * Determines whether a given instance is legacy and should bypass using TinyMCE.
       
    73 	 *
       
    74 	 * @since 4.8.1
       
    75 	 *
       
    76 	 * @param array $instance {
       
    77 	 *     Instance data.
       
    78 	 *
       
    79 	 *     @type string      $text   Content.
       
    80 	 *     @type bool|string $filter Whether autop or content filters should apply.
       
    81 	 *     @type bool        $legacy Whether widget is in legacy mode.
       
    82 	 * }
       
    83 	 * @return bool Whether Text widget instance contains legacy data.
       
    84 	 */
       
    85 	public function is_legacy_instance( $instance ) {
       
    86 
       
    87 		// Legacy mode when not in visual mode.
       
    88 		if ( isset( $instance['visual'] ) ) {
       
    89 			return ! $instance['visual'];
       
    90 		}
       
    91 
       
    92 		// Or, the widget has been added/updated in 4.8.0 then filter prop is 'content' and it is no longer legacy.
       
    93 		if ( isset( $instance['filter'] ) && 'content' === $instance['filter'] ) {
       
    94 			return false;
       
    95 		}
       
    96 
       
    97 		// If the text is empty, then nothing is preventing migration to TinyMCE.
       
    98 		if ( empty( $instance['text'] ) ) {
       
    99 			return false;
       
   100 		}
       
   101 
       
   102 		$wpautop = ! empty( $instance['filter'] );
       
   103 		$has_line_breaks = ( false !== strpos( trim( $instance['text'] ), "\n" ) );
       
   104 
       
   105 		// If auto-paragraphs are not enabled and there are line breaks, then ensure legacy mode.
       
   106 		if ( ! $wpautop && $has_line_breaks ) {
       
   107 			return true;
       
   108 		}
       
   109 
       
   110 		// If an HTML comment is present, assume legacy mode.
       
   111 		if ( false !== strpos( $instance['text'], '<!--' ) ) {
       
   112 			return true;
       
   113 		}
       
   114 
       
   115 		// In the rare case that DOMDocument is not available we cannot reliably sniff content and so we assume legacy.
       
   116 		if ( ! class_exists( 'DOMDocument' ) ) {
       
   117 			// @codeCoverageIgnoreStart
       
   118 			return true;
       
   119 			// @codeCoverageIgnoreEnd
       
   120 		}
       
   121 
       
   122 		$doc = new DOMDocument();
       
   123 		@$doc->loadHTML( sprintf(
       
   124 			'<!DOCTYPE html><html><head><meta charset="%s"></head><body>%s</body></html>',
       
   125 			esc_attr( get_bloginfo( 'charset' ) ),
       
   126 			$instance['text']
       
   127 		) );
       
   128 		$body = $doc->getElementsByTagName( 'body' )->item( 0 );
       
   129 
       
   130 		// See $allowedposttags.
       
   131 		$safe_elements_attributes = array(
       
   132 			'strong' => array(),
       
   133 			'em' => array(),
       
   134 			'b' => array(),
       
   135 			'i' => array(),
       
   136 			'u' => array(),
       
   137 			's' => array(),
       
   138 			'ul' => array(),
       
   139 			'ol' => array(),
       
   140 			'li' => array(),
       
   141 			'hr' => array(),
       
   142 			'abbr' => array(),
       
   143 			'acronym' => array(),
       
   144 			'code' => array(),
       
   145 			'dfn' => array(),
       
   146 			'a' => array(
       
   147 				'href' => true,
       
   148 			),
       
   149 			'img' => array(
       
   150 				'src' => true,
       
   151 				'alt' => true,
       
   152 			),
       
   153 		);
       
   154 		$safe_empty_elements = array( 'img', 'hr', 'iframe' );
       
   155 
       
   156 		foreach ( $body->getElementsByTagName( '*' ) as $element ) {
       
   157 			/** @var DOMElement $element */
       
   158 			$tag_name = strtolower( $element->nodeName );
       
   159 
       
   160 			// If the element is not safe, then the instance is legacy.
       
   161 			if ( ! isset( $safe_elements_attributes[ $tag_name ] ) ) {
       
   162 				return true;
       
   163 			}
       
   164 
       
   165 			// If the element is not safely empty and it has empty contents, then legacy mode.
       
   166 			if ( ! in_array( $tag_name, $safe_empty_elements, true ) && '' === trim( $element->textContent ) ) {
       
   167 				return true;
       
   168 			}
       
   169 
       
   170 			// If an attribute is not recognized as safe, then the instance is legacy.
       
   171 			foreach ( $element->attributes as $attribute ) {
       
   172 				/** @var DOMAttr $attribute */
       
   173 				$attribute_name = strtolower( $attribute->nodeName );
       
   174 
       
   175 				if ( ! isset( $safe_elements_attributes[ $tag_name ][ $attribute_name ] ) ) {
       
   176 					return true;
       
   177 				}
       
   178 			}
       
   179 		}
       
   180 
       
   181 		// Otherwise, the text contains no elements/attributes that TinyMCE could drop, and therefore the widget does not need legacy mode.
       
   182 		return false;
       
   183 	}
       
   184 
       
   185 	/**
       
   186 	 * Filter gallery shortcode attributes.
       
   187 	 *
       
   188 	 * Prevents all of a site's attachments from being shown in a gallery displayed on a
       
   189 	 * non-singular template where a $post context is not available.
       
   190 	 *
       
   191 	 * @since 4.9.0
       
   192 	 *
       
   193 	 * @param array $attrs Attributes.
       
   194 	 * @return array Attributes.
       
   195 	 */
       
   196 	public function _filter_gallery_shortcode_attrs( $attrs ) {
       
   197 		if ( ! is_singular() && empty( $attrs['id'] ) && empty( $attrs['include'] ) ) {
       
   198 			$attrs['id'] = -1;
       
   199 		}
       
   200 		return $attrs;
       
   201 	}
       
   202 
       
   203 	/**
       
   204 	 * Outputs the content for the current Text widget instance.
       
   205 	 *
       
   206 	 * @since 2.8.0
       
   207 	 *
       
   208 	 * @global WP_Post $post
       
   209 	 *
       
   210 	 * @param array $args     Display arguments including 'before_title', 'after_title',
       
   211 	 *                        'before_widget', and 'after_widget'.
       
   212 	 * @param array $instance Settings for the current Text widget instance.
       
   213 	 */
       
   214 	public function widget( $args, $instance ) {
       
   215 		global $post;
       
   216 
       
   217 		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
       
   218 
       
   219 		/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
       
   220 		$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
       
   221 
       
   222 		$text = ! empty( $instance['text'] ) ? $instance['text'] : '';
       
   223 		$is_visual_text_widget = ( ! empty( $instance['visual'] ) && ! empty( $instance['filter'] ) );
       
   224 
       
   225 		// In 4.8.0 only, visual Text widgets get filter=content, without visual prop; upgrade instance props just-in-time.
       
   226 		if ( ! $is_visual_text_widget ) {
       
   227 			$is_visual_text_widget = ( isset( $instance['filter'] ) && 'content' === $instance['filter'] );
       
   228 		}
       
   229 		if ( $is_visual_text_widget ) {
       
   230 			$instance['filter'] = true;
       
   231 			$instance['visual'] = true;
       
   232 		}
       
   233 
       
   234 		/*
       
   235 		 * Suspend legacy plugin-supplied do_shortcode() for 'widget_text' filter for the visual Text widget to prevent
       
   236 		 * shortcodes being processed twice. Now do_shortcode() is added to the 'widget_text_content' filter in core itself
       
   237 		 * and it applies after wpautop() to prevent corrupting HTML output added by the shortcode. When do_shortcode() is
       
   238 		 * added to 'widget_text_content' then do_shortcode() will be manually called when in legacy mode as well.
       
   239 		 */
       
   240 		$widget_text_do_shortcode_priority = has_filter( 'widget_text', 'do_shortcode' );
       
   241 		$should_suspend_legacy_shortcode_support = ( $is_visual_text_widget && false !== $widget_text_do_shortcode_priority );
       
   242 		if ( $should_suspend_legacy_shortcode_support ) {
       
   243 			remove_filter( 'widget_text', 'do_shortcode', $widget_text_do_shortcode_priority );
       
   244 		}
       
   245 
       
   246 		// Override global $post so filters (and shortcodes) apply in a consistent context.
       
   247 		$original_post = $post;
       
   248 		if ( is_singular() ) {
       
   249 			// Make sure post is always the queried object on singular queries (not from another sub-query that failed to clean up the global $post).
       
   250 			$post = get_queried_object();
       
   251 		} else {
       
   252 			// Nullify the $post global during widget rendering to prevent shortcodes from running with the unexpected context on archive queries.
       
   253 			$post = null;
       
   254 		}
       
   255 
       
   256 		// Prevent dumping out all attachments from the media library.
       
   257 		add_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
       
   258 
       
   259 		/**
       
   260 		 * Filters the content of the Text widget.
       
   261 		 *
       
   262 		 * @since 2.3.0
       
   263 		 * @since 4.4.0 Added the `$this` parameter.
       
   264 		 * @since 4.8.1 The `$this` param may now be a `WP_Widget_Custom_HTML` object in addition to a `WP_Widget_Text` object.
       
   265 		 *
       
   266 		 * @param string                               $text     The widget content.
       
   267 		 * @param array                                $instance Array of settings for the current widget.
       
   268 		 * @param WP_Widget_Text|WP_Widget_Custom_HTML $this     Current Text widget instance.
       
   269 		 */
       
   270 		$text = apply_filters( 'widget_text', $text, $instance, $this );
       
   271 
       
   272 		if ( $is_visual_text_widget ) {
       
   273 
       
   274 			/**
       
   275 			 * Filters the content of the Text widget to apply changes expected from the visual (TinyMCE) editor.
       
   276 			 *
       
   277 			 * By default a subset of the_content filters are applied, including wpautop and wptexturize.
       
   278 			 *
       
   279 			 * @since 4.8.0
       
   280 			 *
       
   281 			 * @param string         $text     The widget content.
       
   282 			 * @param array          $instance Array of settings for the current widget.
       
   283 			 * @param WP_Widget_Text $this     Current Text widget instance.
       
   284 			 */
       
   285 			$text = apply_filters( 'widget_text_content', $text, $instance, $this );
       
   286 		} else {
       
   287 			// Now in legacy mode, add paragraphs and line breaks when checkbox is checked.
       
   288 			if ( ! empty( $instance['filter'] ) ) {
       
   289 				$text = wpautop( $text );
       
   290 			}
       
   291 
       
   292 			/*
       
   293 			 * Manually do shortcodes on the content when the core-added filter is present. It is added by default
       
   294 			 * in core by adding do_shortcode() to the 'widget_text_content' filter to apply after wpautop().
       
   295 			 * Since the legacy Text widget runs wpautop() after 'widget_text' filters are applied, the widget in
       
   296 			 * legacy mode here manually applies do_shortcode() on the content unless the default
       
   297 			 * core filter for 'widget_text_content' has been removed, or if do_shortcode() has already
       
   298 			 * been applied via a plugin adding do_shortcode() to 'widget_text' filters.
       
   299 			 */
       
   300 			if ( has_filter( 'widget_text_content', 'do_shortcode' ) && ! $widget_text_do_shortcode_priority ) {
       
   301 				if ( ! empty( $instance['filter'] ) ) {
       
   302 					$text = shortcode_unautop( $text );
       
   303 				}
       
   304 				$text = do_shortcode( $text );
       
   305 			}
       
   306 		}
       
   307 
       
   308 		// Restore post global.
       
   309 		$post = $original_post;
       
   310 		remove_filter( 'shortcode_atts_gallery', array( $this, '_filter_gallery_shortcode_attrs' ) );
       
   311 
       
   312 		// Undo suspension of legacy plugin-supplied shortcode handling.
       
   313 		if ( $should_suspend_legacy_shortcode_support ) {
       
   314 			add_filter( 'widget_text', 'do_shortcode', $widget_text_do_shortcode_priority );
       
   315 		}
       
   316 
       
   317 		echo $args['before_widget'];
       
   318 		if ( ! empty( $title ) ) {
       
   319 			echo $args['before_title'] . $title . $args['after_title'];
       
   320 		}
       
   321 
       
   322 		$text = preg_replace_callback( '#<(video|iframe|object|embed)\s[^>]*>#i', array( $this, 'inject_video_max_width_style' ), $text );
       
   323 
       
   324 		?>
       
   325 			<div class="textwidget"><?php echo $text; ?></div>
       
   326 		<?php
       
   327 		echo $args['after_widget'];
       
   328 	}
       
   329 
       
   330 	/**
       
   331 	 * Inject max-width and remove height for videos too constrained to fit inside sidebars on frontend.
       
   332 	 *
       
   333 	 * @since 4.9.0
       
   334 	 *
       
   335 	 * @see WP_Widget_Media_Video::inject_video_max_width_style()
       
   336 	 * @param array $matches Pattern matches from preg_replace_callback.
       
   337 	 * @return string HTML Output.
       
   338 	 */
       
   339 	public function inject_video_max_width_style( $matches ) {
       
   340 		$html = $matches[0];
       
   341 		$html = preg_replace( '/\sheight="\d+"/', '', $html );
       
   342 		$html = preg_replace( '/\swidth="\d+"/', '', $html );
       
   343 		$html = preg_replace( '/(?<=width:)\s*\d+px(?=;?)/', '100%', $html );
       
   344 		return $html;
       
   345 	}
       
   346 
       
   347 	/**
       
   348 	 * Handles updating settings for the current Text widget instance.
       
   349 	 *
       
   350 	 * @since 2.8.0
       
   351 	 *
       
   352 	 * @param array $new_instance New settings for this instance as input by the user via
       
   353 	 *                            WP_Widget::form().
       
   354 	 * @param array $old_instance Old settings for this instance.
       
   355 	 * @return array Settings to save or bool false to cancel saving.
       
   356 	 */
       
   357 	public function update( $new_instance, $old_instance ) {
       
   358 		$new_instance = wp_parse_args( $new_instance, array(
       
   359 			'title' => '',
       
   360 			'text' => '',
       
   361 			'filter' => false, // For back-compat.
       
   362 			'visual' => null, // Must be explicitly defined.
       
   363 		) );
       
   364 
       
   365 		$instance = $old_instance;
       
   366 
       
   367 		$instance['title'] = sanitize_text_field( $new_instance['title'] );
       
   368 		if ( current_user_can( 'unfiltered_html' ) ) {
       
   369 			$instance['text'] = $new_instance['text'];
       
   370 		} else {
       
   371 			$instance['text'] = wp_kses_post( $new_instance['text'] );
       
   372 		}
       
   373 
       
   374 		$instance['filter'] = ! empty( $new_instance['filter'] );
       
   375 
       
   376 		// Upgrade 4.8.0 format.
       
   377 		if ( isset( $old_instance['filter'] ) && 'content' === $old_instance['filter'] ) {
       
   378 			$instance['visual'] = true;
       
   379 		}
       
   380 		if ( 'content' === $new_instance['filter'] ) {
       
   381 			$instance['visual'] = true;
       
   382 		}
       
   383 
       
   384 		if ( isset( $new_instance['visual'] ) ) {
       
   385 			$instance['visual'] = ! empty( $new_instance['visual'] );
       
   386 		}
       
   387 
       
   388 		// Filter is always true in visual mode.
       
   389 		if ( ! empty( $instance['visual'] ) ) {
       
   390 			$instance['filter'] = true;
       
   391 		}
       
   392 
       
   393 		return $instance;
       
   394 	}
       
   395 
       
   396 	/**
       
   397 	 * Enqueue preview scripts.
       
   398 	 *
       
   399 	 * These scripts normally are enqueued just-in-time when a playlist shortcode is used.
       
   400 	 * However, in the customizer, a playlist shortcode may be used in a text widget and
       
   401 	 * dynamically added via selective refresh, so it is important to unconditionally enqueue them.
       
   402 	 *
       
   403 	 * @since 4.9.3
       
   404 	 */
       
   405 	public function enqueue_preview_scripts() {
       
   406 		require_once dirname( dirname( __FILE__ ) ) . '/media.php';
       
   407 
       
   408 		wp_playlist_scripts( 'audio' );
       
   409 		wp_playlist_scripts( 'video' );
       
   410 	}
       
   411 
       
   412 	/**
       
   413 	 * Loads the required scripts and styles for the widget control.
       
   414 	 *
       
   415 	 * @since 4.8.0
       
   416 	 */
       
   417 	public function enqueue_admin_scripts() {
       
   418 		wp_enqueue_editor();
       
   419 		wp_enqueue_media();
       
   420 		wp_enqueue_script( 'text-widgets' );
       
   421 		wp_add_inline_script( 'text-widgets', 'wp.textWidgets.init();', 'after' );
       
   422 	}
       
   423 
       
   424 	/**
       
   425 	 * Outputs the Text widget settings form.
       
   426 	 *
       
   427 	 * @since 2.8.0
       
   428 	 * @since 4.8.0 Form only contains hidden inputs which are synced with JS template.
       
   429 	 * @since 4.8.1 Restored original form to be displayed when in legacy mode.
       
   430 	 * @see WP_Widget_Visual_Text::render_control_template_scripts()
       
   431 	 * @see _WP_Editors::editor()
       
   432 	 *
       
   433 	 * @param array $instance Current settings.
       
   434 	 * @return void
       
   435 	 */
       
   436 	public function form( $instance ) {
       
   437 		$instance = wp_parse_args(
       
   438 			(array) $instance,
       
   439 			array(
       
   440 				'title' => '',
       
   441 				'text' => '',
       
   442 			)
       
   443 		);
       
   444 		?>
       
   445 		<?php if ( ! $this->is_legacy_instance( $instance ) ) : ?>
       
   446 			<?php
       
   447 
       
   448 			if ( user_can_richedit() ) {
       
   449 				add_filter( 'the_editor_content', 'format_for_editor', 10, 2 );
       
   450 				$default_editor = 'tinymce';
       
   451 			} else {
       
   452 				$default_editor = 'html';
       
   453 			}
       
   454 
       
   455 			/** This filter is documented in wp-includes/class-wp-editor.php */
       
   456 			$text = apply_filters( 'the_editor_content', $instance['text'], $default_editor );
       
   457 
       
   458 			// Reset filter addition.
       
   459 			if ( user_can_richedit() ) {
       
   460 				remove_filter( 'the_editor_content', 'format_for_editor' );
       
   461 			}
       
   462 
       
   463 			// Prevent premature closing of textarea in case format_for_editor() didn't apply or the_editor_content filter did a wrong thing.
       
   464 			$escaped_text = preg_replace( '#</textarea#i', '&lt;/textarea', $text );
       
   465 
       
   466 			?>
       
   467 			<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" class="title sync-input" type="hidden" value="<?php echo esc_attr( $instance['title'] ); ?>">
       
   468 			<textarea id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" class="text sync-input" hidden><?php echo $escaped_text; ?></textarea>
       
   469 			<input id="<?php echo $this->get_field_id( 'filter' ); ?>" name="<?php echo $this->get_field_name( 'filter' ); ?>" class="filter sync-input" type="hidden" value="on">
       
   470 			<input id="<?php echo $this->get_field_id( 'visual' ); ?>" name="<?php echo $this->get_field_name( 'visual' ); ?>" class="visual sync-input" type="hidden" value="on">
       
   471 		<?php else : ?>
       
   472 			<input id="<?php echo $this->get_field_id( 'visual' ); ?>" name="<?php echo $this->get_field_name( 'visual' ); ?>" class="visual" type="hidden" value="">
       
   473 			<p>
       
   474 				<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
       
   475 				<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'] ); ?>"/>
       
   476 			</p>
       
   477 			<div class="notice inline notice-info notice-alt">
       
   478 				<?php if ( ! isset( $instance['visual'] ) ) : ?>
       
   479 					<p><?php _e( 'This widget may contain code that may work better in the &#8220;Custom HTML&#8221; widget. How about trying that widget instead?' ); ?></p>
       
   480 				<?php else : ?>
       
   481 					<p><?php _e( 'This widget may have contained code that may work better in the &#8220;Custom HTML&#8221; widget. If you haven&#8217;t yet, how about trying that widget instead?' ); ?></p>
       
   482 				<?php endif; ?>
       
   483 			</div>
       
   484 			<p>
       
   485 				<label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Content:' ); ?></label>
       
   486 				<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>
       
   487 			</p>
       
   488 			<p>
       
   489 				<input id="<?php echo $this->get_field_id( 'filter' ); ?>" name="<?php echo $this->get_field_name( 'filter' ); ?>" type="checkbox"<?php checked( ! empty( $instance['filter'] ) ); ?> />&nbsp;<label for="<?php echo $this->get_field_id( 'filter' ); ?>"><?php _e( 'Automatically add paragraphs' ); ?></label>
       
   490 			</p>
       
   491 		<?php
       
   492 		endif;
       
   493 	}
       
   494 
       
   495 	/**
       
   496 	 * Render form template scripts.
       
   497 	 *
       
   498 	 * @since 4.8.0
       
   499 	 * @since 4.9.0 The method is now static.
       
   500 	 */
       
   501 	public static function render_control_template_scripts() {
       
   502 		$dismissed_pointers = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
       
   503 		?>
       
   504 		<script type="text/html" id="tmpl-widget-text-control-fields">
       
   505 			<# var elementIdPrefix = 'el' + String( Math.random() ).replace( /\D/g, '' ) + '_' #>
       
   506 			<p>
       
   507 				<label for="{{ elementIdPrefix }}title"><?php esc_html_e( 'Title:' ); ?></label>
       
   508 				<input id="{{ elementIdPrefix }}title" type="text" class="widefat title">
       
   509 			</p>
       
   510 
       
   511 			<?php if ( ! in_array( 'text_widget_custom_html', $dismissed_pointers, true ) ) : ?>
       
   512 				<div hidden class="wp-pointer custom-html-widget-pointer wp-pointer-top">
       
   513 					<div class="wp-pointer-content">
       
   514 						<h3><?php _e( 'New Custom HTML Widget' ); ?></h3>
       
   515 						<?php if ( is_customize_preview() ) : ?>
       
   516 							<p><?php _e( 'Did you know there is a &#8220;Custom HTML&#8221; widget now? You can find it by pressing the &#8220;<a class="add-widget" href="#">Add a Widget</a>&#8221; button and searching for &#8220;HTML&#8221;. Check it out to add some custom code to your site!' ); ?></p>
       
   517 						<?php else : ?>
       
   518 							<p><?php _e( 'Did you know there is a &#8220;Custom HTML&#8221; widget now? You can find it by scanning the list of available widgets on this screen. Check it out to add some custom code to your site!' ); ?></p>
       
   519 						<?php endif; ?>
       
   520 						<div class="wp-pointer-buttons">
       
   521 							<a class="close" href="#"><?php _e( 'Dismiss' ); ?></a>
       
   522 						</div>
       
   523 					</div>
       
   524 					<div class="wp-pointer-arrow">
       
   525 						<div class="wp-pointer-arrow-inner"></div>
       
   526 					</div>
       
   527 				</div>
       
   528 			<?php endif; ?>
       
   529 
       
   530 			<?php if ( ! in_array( 'text_widget_paste_html', $dismissed_pointers, true ) ) : ?>
       
   531 				<div hidden class="wp-pointer paste-html-pointer wp-pointer-top">
       
   532 					<div class="wp-pointer-content">
       
   533 						<h3><?php _e( 'Did you just paste HTML?' ); ?></h3>
       
   534 						<p><?php _e( 'Hey there, looks like you just pasted HTML into the &#8220;Visual&#8221; tab of the Text widget. You may want to paste your code into the &#8220;Text&#8221; tab instead. Alternately, try out the new &#8220;Custom HTML&#8221; widget!' ); ?></p>
       
   535 						<div class="wp-pointer-buttons">
       
   536 							<a class="close" href="#"><?php _e( 'Dismiss' ); ?></a>
       
   537 						</div>
       
   538 					</div>
       
   539 					<div class="wp-pointer-arrow">
       
   540 						<div class="wp-pointer-arrow-inner"></div>
       
   541 					</div>
       
   542 				</div>
       
   543 			<?php endif; ?>
       
   544 
       
   545 			<p>
       
   546 				<label for="{{ elementIdPrefix }}text" class="screen-reader-text"><?php esc_html_e( 'Content:' ); ?></label>
       
   547 				<textarea id="{{ elementIdPrefix }}text" class="widefat text wp-editor-area" style="height: 200px" rows="16" cols="20"></textarea>
       
   548 			</p>
       
   549 		</script>
       
   550 		<?php
       
   551 	}
       
   552 }