wp/wp-includes/widgets/class-wp-widget-block.php
changeset 18 be944660c56a
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
       
     1 <?php
       
     2 /**
       
     3  * Widget API: WP_Widget_Block class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Widgets
       
     7  * @since 5.8.0
       
     8  */
       
     9 
       
    10 /**
       
    11  * Core class used to implement a Block widget.
       
    12  *
       
    13  * @since 5.8.0
       
    14  *
       
    15  * @see WP_Widget
       
    16  */
       
    17 class WP_Widget_Block extends WP_Widget {
       
    18 
       
    19 	/**
       
    20 	 * Default instance.
       
    21 	 *
       
    22 	 * @since 5.8.0
       
    23 	 * @var array
       
    24 	 */
       
    25 	protected $default_instance = array(
       
    26 		'content' => '',
       
    27 	);
       
    28 
       
    29 	/**
       
    30 	 * Sets up a new Block widget instance.
       
    31 	 *
       
    32 	 * @since 5.8.0
       
    33 	 */
       
    34 	public function __construct() {
       
    35 		$widget_ops  = array(
       
    36 			'classname'                   => 'widget_block',
       
    37 			'description'                 => __( 'A widget containing a block.' ),
       
    38 			'customize_selective_refresh' => true,
       
    39 			'show_instance_in_rest'       => true,
       
    40 		);
       
    41 		$control_ops = array(
       
    42 			'width'  => 400,
       
    43 			'height' => 350,
       
    44 		);
       
    45 		parent::__construct( 'block', __( 'Block' ), $widget_ops, $control_ops );
       
    46 
       
    47 		add_filter( 'is_wide_widget_in_customizer', array( $this, 'set_is_wide_widget_in_customizer' ), 10, 2 );
       
    48 	}
       
    49 
       
    50 	/**
       
    51 	 * Outputs the content for the current Block widget instance.
       
    52 	 *
       
    53 	 * @since 5.8.0
       
    54 	 *
       
    55 	 * @param array $args     Display arguments including 'before_title', 'after_title',
       
    56 	 *                        'before_widget', and 'after_widget'.
       
    57 	 * @param array $instance Settings for the current Block widget instance.
       
    58 	 */
       
    59 	public function widget( $args, $instance ) {
       
    60 		$instance = wp_parse_args( $instance, $this->default_instance );
       
    61 
       
    62 		echo str_replace(
       
    63 			'widget_block',
       
    64 			$this->get_dynamic_classname( $instance['content'] ),
       
    65 			$args['before_widget']
       
    66 		);
       
    67 
       
    68 		/**
       
    69 		 * Filters the content of the Block widget before output.
       
    70 		 *
       
    71 		 * @since 5.8.0
       
    72 		 *
       
    73 		 * @param string          $content  The widget content.
       
    74 		 * @param array           $instance Array of settings for the current widget.
       
    75 		 * @param WP_Widget_Block $widget   Current Block widget instance.
       
    76 		 */
       
    77 		echo apply_filters(
       
    78 			'widget_block_content',
       
    79 			$instance['content'],
       
    80 			$instance,
       
    81 			$this
       
    82 		);
       
    83 
       
    84 		echo $args['after_widget'];
       
    85 	}
       
    86 
       
    87 	/**
       
    88 	 * Calculates the classname to use in the block widget's container HTML.
       
    89 	 *
       
    90 	 * Usually this is set to `$this->widget_options['classname']` by
       
    91 	 * dynamic_sidebar(). In this case, however, we want to set the classname
       
    92 	 * dynamically depending on the block contained by this block widget.
       
    93 	 *
       
    94 	 * If a block widget contains a block that has an equivalent legacy widget,
       
    95 	 * we display that legacy widget's class name. This helps with theme
       
    96 	 * backwards compatibility.
       
    97 	 *
       
    98 	 * @since 5.8.0
       
    99 	 *
       
   100 	 * @param string $content The HTML content of the current block widget.
       
   101 	 * @return string The classname to use in the block widget's container HTML.
       
   102 	 */
       
   103 	private function get_dynamic_classname( $content ) {
       
   104 		$blocks = parse_blocks( $content );
       
   105 
       
   106 		$block_name = isset( $blocks[0] ) ? $blocks[0]['blockName'] : null;
       
   107 
       
   108 		switch ( $block_name ) {
       
   109 			case 'core/paragraph':
       
   110 				$classname = 'widget_block widget_text';
       
   111 				break;
       
   112 			case 'core/calendar':
       
   113 				$classname = 'widget_block widget_calendar';
       
   114 				break;
       
   115 			case 'core/search':
       
   116 				$classname = 'widget_block widget_search';
       
   117 				break;
       
   118 			case 'core/html':
       
   119 				$classname = 'widget_block widget_custom_html';
       
   120 				break;
       
   121 			case 'core/archives':
       
   122 				$classname = 'widget_block widget_archive';
       
   123 				break;
       
   124 			case 'core/latest-posts':
       
   125 				$classname = 'widget_block widget_recent_entries';
       
   126 				break;
       
   127 			case 'core/latest-comments':
       
   128 				$classname = 'widget_block widget_recent_comments';
       
   129 				break;
       
   130 			case 'core/tag-cloud':
       
   131 				$classname = 'widget_block widget_tag_cloud';
       
   132 				break;
       
   133 			case 'core/categories':
       
   134 				$classname = 'widget_block widget_categories';
       
   135 				break;
       
   136 			case 'core/audio':
       
   137 				$classname = 'widget_block widget_media_audio';
       
   138 				break;
       
   139 			case 'core/video':
       
   140 				$classname = 'widget_block widget_media_video';
       
   141 				break;
       
   142 			case 'core/image':
       
   143 				$classname = 'widget_block widget_media_image';
       
   144 				break;
       
   145 			case 'core/gallery':
       
   146 				$classname = 'widget_block widget_media_gallery';
       
   147 				break;
       
   148 			case 'core/rss':
       
   149 				$classname = 'widget_block widget_rss';
       
   150 				break;
       
   151 			default:
       
   152 				$classname = 'widget_block';
       
   153 		}
       
   154 
       
   155 		/**
       
   156 		 * The classname used in the block widget's container HTML.
       
   157 		 *
       
   158 		 * This can be set according to the name of the block contained by the block widget.
       
   159 		 *
       
   160 		 * @since 5.8.0
       
   161 		 *
       
   162 		 * @param string $classname  The classname to be used in the block widget's container HTML,
       
   163 		 *                           e.g. 'widget_block widget_text'.
       
   164 		 * @param string $block_name The name of the block contained by the block widget,
       
   165 		 *                           e.g. 'core/paragraph'.
       
   166 		 */
       
   167 		return apply_filters( 'widget_block_dynamic_classname', $classname, $block_name );
       
   168 	}
       
   169 
       
   170 	/**
       
   171 	 * Handles updating settings for the current Block widget instance.
       
   172 	 *
       
   173 	 * @since 5.8.0
       
   174 
       
   175 	 * @param array $new_instance New settings for this instance as input by the user via
       
   176 	 *                            WP_Widget::form().
       
   177 	 * @param array $old_instance Old settings for this instance.
       
   178 	 * @return array Settings to save or bool false to cancel saving.
       
   179 	 */
       
   180 	public function update( $new_instance, $old_instance ) {
       
   181 		$instance = array_merge( $this->default_instance, $old_instance );
       
   182 
       
   183 		if ( current_user_can( 'unfiltered_html' ) ) {
       
   184 			$instance['content'] = $new_instance['content'];
       
   185 		} else {
       
   186 			$instance['content'] = wp_kses_post( $new_instance['content'] );
       
   187 		}
       
   188 
       
   189 		return $instance;
       
   190 	}
       
   191 
       
   192 	/**
       
   193 	 * Outputs the Block widget settings form.
       
   194 	 *
       
   195 	 * @since 5.8.0
       
   196 	 *
       
   197 	 * @see WP_Widget_Custom_HTML::render_control_template_scripts()
       
   198 	 *
       
   199 	 * @param array $instance Current instance.
       
   200 	 */
       
   201 	public function form( $instance ) {
       
   202 		$instance = wp_parse_args( (array) $instance, $this->default_instance );
       
   203 		?>
       
   204 		<p>
       
   205 			<label for="<?php echo $this->get_field_id( 'content' ); ?>"><?php echo __( 'Block HTML:' ); ?></label>
       
   206 			<textarea id="<?php echo $this->get_field_id( 'content' ); ?>" name="<?php echo $this->get_field_name( 'content' ); ?>" rows="6" cols="50" class="widefat code"><?php echo esc_textarea( $instance['content'] ); ?></textarea>
       
   207 		</p>
       
   208 		<?php
       
   209 	}
       
   210 
       
   211 	/**
       
   212 	 * Makes sure no block widget is considered to be wide.
       
   213 	 *
       
   214 	 * @since 5.8.0
       
   215 	 *
       
   216 	 * @param bool   $is_wide   Whether the widget is considered wide.
       
   217 	 * @param string $widget_id Widget ID.
       
   218 	 * @return bool Updated `is_wide` value.
       
   219 	 */
       
   220 	public function set_is_wide_widget_in_customizer( $is_wide, $widget_id ) {
       
   221 		if ( strpos( $widget_id, 'block-' ) === 0 ) {
       
   222 			return false;
       
   223 		}
       
   224 
       
   225 		return $is_wide;
       
   226 	}
       
   227 }