wp/wp-includes/widgets/class-wp-widget-text.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    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' ) ) {
   340 		<?php
   342 		<?php
   341 		echo $args['after_widget'];
   343 		echo $args['after_widget'];
   342 	}
   344 	}
   343 
   345 
   344 	/**
   346 	/**
   345 	 * Inject max-width and remove height for videos too constrained to fit inside sidebars on frontend.
   347 	 * Injects max-width and removes height for videos too constrained to fit inside sidebars on frontend.
   346 	 *
   348 	 *
   347 	 * @since 4.9.0
   349 	 * @since 4.9.0
   348 	 *
   350 	 *
   349 	 * @see WP_Widget_Media_Video::inject_video_max_width_style()
   351 	 * @see WP_Widget_Media_Video::inject_video_max_width_style()
   350 	 *
   352 	 *
   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 &#8220;Custom HTML&#8221; widget. How about trying that widget instead?' ); ?></p>
   500 				$widget_info_message = __( 'This widget may contain code that may work better in the &#8220;Custom HTML&#8221; 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 &#8220;Custom HTML&#8221; 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 &#8220;Custom HTML&#8221; 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>
   509 			<?php
   520 			<?php
   510 		endif;
   521 		endif;
   511 	}
   522 	}
   512 
   523 
   513 	/**
   524 	/**
   514 	 * Render form template scripts.
   525 	 * Renders form template scripts.
   515 	 *
   526 	 *
   516 	 * @since 4.8.0
   527 	 * @since 4.8.0
   517 	 * @since 4.9.0 The method is now static.
   528 	 * @since 4.9.0 The method is now static.
   518 	 */
   529 	 */
   519 	public static function render_control_template_scripts() {
   530 	public static function render_control_template_scripts() {
   559 					</div>
   570 					</div>
   560 				</div>
   571 				</div>
   561 			<?php endif; ?>
   572 			<?php endif; ?>
   562 
   573 
   563 			<p>
   574 			<p>
   564 				<label for="{{ elementIdPrefix }}text" class="screen-reader-text"><?php esc_html_e( 'Content:' ); ?></label>
   575 				<label for="{{ elementIdPrefix }}text" class="screen-reader-text"><?php /* translators: Hidden accessibility text. */ esc_html_e( 'Content:' ); ?></label>
   565 				<textarea id="{{ elementIdPrefix }}text" class="widefat text wp-editor-area" style="height: 200px" rows="16" cols="20"></textarea>
   576 				<textarea id="{{ elementIdPrefix }}text" class="widefat text wp-editor-area" style="height: 200px" rows="16" cols="20"></textarea>
   566 			</p>
   577 			</p>
   567 		</script>
   578 		</script>
   568 		<?php
   579 		<?php
   569 	}
   580 	}