2021-05-25 04:27:57 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Widget API: WP_Widget_Block class
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Widgets
|
|
|
|
* @since 5.8.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core class used to implement a Block widget.
|
|
|
|
*
|
|
|
|
* @since 5.8.0
|
|
|
|
*
|
|
|
|
* @see WP_Widget
|
|
|
|
*/
|
|
|
|
class WP_Widget_Block extends WP_Widget {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default instance.
|
|
|
|
*
|
|
|
|
* @since 5.8.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $default_instance = array(
|
|
|
|
'content' => '',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up a new Block widget instance.
|
|
|
|
*
|
|
|
|
* @since 5.8.0
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$widget_ops = array(
|
|
|
|
'classname' => 'widget_block',
|
|
|
|
'description' => __( 'A widget containing a block.' ),
|
|
|
|
'customize_selective_refresh' => true,
|
|
|
|
'show_instance_in_rest' => true,
|
|
|
|
);
|
|
|
|
$control_ops = array(
|
|
|
|
'width' => 400,
|
|
|
|
'height' => 350,
|
|
|
|
);
|
|
|
|
parent::__construct( 'block', __( 'Block' ), $widget_ops, $control_ops );
|
2021-06-28 04:35:57 -04:00
|
|
|
|
2021-05-25 04:27:57 -04:00
|
|
|
add_filter( 'is_wide_widget_in_customizer', array( $this, 'set_is_wide_widget_in_customizer' ), 10, 2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputs the content for the current Block widget instance.
|
|
|
|
*
|
|
|
|
* @since 5.8.0
|
|
|
|
*
|
2021-06-02 06:57:57 -04:00
|
|
|
* @param array $args Display arguments including 'before_title', 'after_title',
|
2021-05-25 04:27:57 -04:00
|
|
|
* 'before_widget', and 'after_widget'.
|
|
|
|
* @param array $instance Settings for the current Block widget instance.
|
|
|
|
*/
|
|
|
|
public function widget( $args, $instance ) {
|
|
|
|
$instance = wp_parse_args( $instance, $this->default_instance );
|
|
|
|
|
|
|
|
echo str_replace(
|
|
|
|
'widget_block',
|
|
|
|
$this->get_dynamic_classname( $instance['content'] ),
|
|
|
|
$args['before_widget']
|
|
|
|
);
|
|
|
|
|
2021-06-01 21:18:57 -04:00
|
|
|
/**
|
|
|
|
* Filters the content of the Block widget before output.
|
|
|
|
*
|
|
|
|
* @since 5.8.0
|
|
|
|
*
|
2021-06-02 06:57:57 -04:00
|
|
|
* @param string $content The widget content.
|
|
|
|
* @param array $instance Array of settings for the current widget.
|
|
|
|
* @param WP_Widget_Block $widget Current Block widget instance.
|
2021-06-01 21:18:57 -04:00
|
|
|
*/
|
|
|
|
echo apply_filters(
|
|
|
|
'widget_block_content',
|
|
|
|
$instance['content'],
|
|
|
|
$instance,
|
|
|
|
$this
|
|
|
|
);
|
2021-05-25 04:27:57 -04:00
|
|
|
|
|
|
|
echo $args['after_widget'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the classname to use in the block widget's container HTML.
|
|
|
|
*
|
2021-06-28 04:35:57 -04:00
|
|
|
* Usually this is set to `$this->widget_options['classname']` by
|
2021-05-25 04:27:57 -04:00
|
|
|
* dynamic_sidebar(). In this case, however, we want to set the classname
|
|
|
|
* dynamically depending on the block contained by this block widget.
|
|
|
|
*
|
|
|
|
* If a block widget contains a block that has an equivalent legacy widget,
|
|
|
|
* we display that legacy widget's class name. This helps with theme
|
|
|
|
* backwards compatibility.
|
|
|
|
*
|
|
|
|
* @since 5.8.0
|
|
|
|
*
|
2021-06-23 15:05:57 -04:00
|
|
|
* @param string $content The HTML content of the current block widget.
|
2021-05-25 04:27:57 -04:00
|
|
|
* @return string The classname to use in the block widget's container HTML.
|
|
|
|
*/
|
|
|
|
private function get_dynamic_classname( $content ) {
|
|
|
|
$blocks = parse_blocks( $content );
|
|
|
|
|
|
|
|
$block_name = isset( $blocks[0] ) ? $blocks[0]['blockName'] : null;
|
|
|
|
|
|
|
|
switch ( $block_name ) {
|
|
|
|
case 'core/paragraph':
|
|
|
|
$classname = 'widget_block widget_text';
|
|
|
|
break;
|
|
|
|
case 'core/calendar':
|
|
|
|
$classname = 'widget_block widget_calendar';
|
|
|
|
break;
|
|
|
|
case 'core/search':
|
|
|
|
$classname = 'widget_block widget_search';
|
|
|
|
break;
|
|
|
|
case 'core/html':
|
|
|
|
$classname = 'widget_block widget_custom_html';
|
|
|
|
break;
|
|
|
|
case 'core/archives':
|
|
|
|
$classname = 'widget_block widget_archive';
|
|
|
|
break;
|
|
|
|
case 'core/latest-posts':
|
|
|
|
$classname = 'widget_block widget_recent_entries';
|
|
|
|
break;
|
|
|
|
case 'core/latest-comments':
|
|
|
|
$classname = 'widget_block widget_recent_comments';
|
|
|
|
break;
|
|
|
|
case 'core/tag-cloud':
|
|
|
|
$classname = 'widget_block widget_tag_cloud';
|
|
|
|
break;
|
|
|
|
case 'core/categories':
|
|
|
|
$classname = 'widget_block widget_categories';
|
|
|
|
break;
|
|
|
|
case 'core/audio':
|
|
|
|
$classname = 'widget_block widget_media_audio';
|
|
|
|
break;
|
|
|
|
case 'core/video':
|
|
|
|
$classname = 'widget_block widget_media_video';
|
|
|
|
break;
|
|
|
|
case 'core/image':
|
|
|
|
$classname = 'widget_block widget_media_image';
|
|
|
|
break;
|
|
|
|
case 'core/gallery':
|
|
|
|
$classname = 'widget_block widget_media_gallery';
|
|
|
|
break;
|
|
|
|
case 'core/rss':
|
|
|
|
$classname = 'widget_block widget_rss';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$classname = 'widget_block';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The classname used in the block widget's container HTML.
|
|
|
|
*
|
2021-06-28 04:35:57 -04:00
|
|
|
* This can be set according to the name of the block contained by the block widget.
|
2021-05-25 04:27:57 -04:00
|
|
|
*
|
|
|
|
* @since 5.8.0
|
|
|
|
*
|
2021-06-28 04:35:57 -04:00
|
|
|
* @param string $classname The classname to be used in the block widget's container HTML,
|
|
|
|
* e.g. 'widget_block widget_text'.
|
|
|
|
* @param string $block_name The name of the block contained by the block widget,
|
|
|
|
* e.g. 'core/paragraph'.
|
2021-05-25 04:27:57 -04:00
|
|
|
*/
|
|
|
|
return apply_filters( 'widget_block_dynamic_classname', $classname, $block_name );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles updating settings for the current Block widget instance.
|
|
|
|
*
|
|
|
|
* @since 5.8.0
|
|
|
|
|
|
|
|
* @param array $new_instance New settings for this instance as input by the user via
|
|
|
|
* WP_Widget::form().
|
|
|
|
* @param array $old_instance Old settings for this instance.
|
|
|
|
* @return array Settings to save or bool false to cancel saving.
|
|
|
|
*/
|
|
|
|
public function update( $new_instance, $old_instance ) {
|
2021-07-13 01:59:01 -04:00
|
|
|
$instance = array_merge( $this->default_instance, $old_instance );
|
|
|
|
|
|
|
|
if ( current_user_can( 'unfiltered_html' ) ) {
|
|
|
|
$instance['content'] = $new_instance['content'];
|
|
|
|
} else {
|
|
|
|
$instance['content'] = wp_kses_post( $new_instance['content'] );
|
|
|
|
}
|
2021-05-25 04:27:57 -04:00
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputs the Block widget settings form.
|
|
|
|
*
|
|
|
|
* @since 5.8.0
|
|
|
|
*
|
|
|
|
* @see WP_Widget_Custom_HTML::render_control_template_scripts()
|
2021-06-23 15:05:57 -04:00
|
|
|
*
|
|
|
|
* @param array $instance Current instance.
|
2021-05-25 04:27:57 -04:00
|
|
|
*/
|
|
|
|
public function form( $instance ) {
|
|
|
|
$instance = wp_parse_args( (array) $instance, $this->default_instance );
|
|
|
|
?>
|
|
|
|
<p>
|
2021-09-15 07:49:01 -04:00
|
|
|
<label for="<?php echo $this->get_field_id( 'content' ); ?>">
|
|
|
|
<?php
|
|
|
|
/* translators: HTML code of the block, not an option that blocks HTML. */
|
|
|
|
_e( 'Block HTML:' );
|
|
|
|
?>
|
|
|
|
</label>
|
2021-05-25 04:27:57 -04:00
|
|
|
<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>
|
|
|
|
</p>
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-28 04:35:57 -04:00
|
|
|
* Makes sure no block widget is considered to be wide.
|
2021-05-25 04:27:57 -04:00
|
|
|
*
|
|
|
|
* @since 5.8.0
|
|
|
|
*
|
2021-06-28 04:35:57 -04:00
|
|
|
* @param bool $is_wide Whether the widget is considered wide.
|
|
|
|
* @param string $widget_id Widget ID.
|
|
|
|
* @return bool Updated `is_wide` value.
|
2021-05-25 04:27:57 -04:00
|
|
|
*/
|
|
|
|
public function set_is_wide_widget_in_customizer( $is_wide, $widget_id ) {
|
Code Modernization: Replace usage of `strpos()` with `str_starts_with()`.
`str_starts_with()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) begins with the given substring (needle).
WordPress core includes a polyfill for `str_starts_with()` on PHP < 8.0 as of WordPress 5.9.
This commit replaces `0 === strpos( ... )` with `str_starts_with()` in core files, making the code more readable and consistent, as well as improving performance.
While `strpos()` is slightly faster than the polyfill on PHP < 8.0, `str_starts_with()` is noticeably faster on PHP 8.0+, as it is optimized to avoid unnecessarily searching along the whole haystack if it does not find the needle.
Follow-up to [52039], [52040], [52326].
Props spacedmonkey, costdev, sabernhardt, mukesh27, desrosj, jorbin, TobiasBg, ayeshrajans, lgadzhev, SergeyBiryukov.
Fixes #58012.
Built from https://develop.svn.wordpress.org/trunk@55703
git-svn-id: http://core.svn.wordpress.org/trunk@55215 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-05-02 11:45:22 -04:00
|
|
|
if ( str_starts_with( $widget_id, 'block-' ) ) {
|
2021-05-25 04:27:57 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $is_wide;
|
|
|
|
}
|
|
|
|
}
|