Widget block: Add `widget_block_content` filter

Adds a new 'widget_block_content' filter to the widget block and hooks
`run_shortcode`, `autoembed`, `do_blocks`, and `do_shortcode` into it by
default. This is simlar to `widget_text_content.`

Fixes #51566.
Props talldanwp.

Built from https://develop.svn.wordpress.org/trunk@51058


git-svn-id: http://core.svn.wordpress.org/trunk@50667 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
noisysocks 2021-06-02 01:18:57 +00:00
parent 918610fb88
commit 38cea1e659
4 changed files with 21 additions and 15 deletions

View File

@ -31,6 +31,7 @@ class WP_Embed {
// Hack to get the [embed] shortcode to run before wpautop(). // Hack to get the [embed] shortcode to run before wpautop().
add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 ); add_filter( 'the_content', array( $this, 'run_shortcode' ), 8 );
add_filter( 'widget_text_content', array( $this, 'run_shortcode' ), 8 ); add_filter( 'widget_text_content', array( $this, 'run_shortcode' ), 8 );
add_filter( 'widget_block_content', array( $this, 'run_shortcode' ), 8 );
// Shortcode placeholder for strip_shortcodes(). // Shortcode placeholder for strip_shortcodes().
add_shortcode( 'embed', '__return_false' ); add_shortcode( 'embed', '__return_false' );
@ -38,6 +39,7 @@ class WP_Embed {
// Attempts to embed all URLs in a post. // Attempts to embed all URLs in a post.
add_filter( 'the_content', array( $this, 'autoembed' ), 8 ); add_filter( 'the_content', array( $this, 'autoembed' ), 8 );
add_filter( 'widget_text_content', array( $this, 'autoembed' ), 8 ); add_filter( 'widget_text_content', array( $this, 'autoembed' ), 8 );
add_filter( 'widget_block_content', array( $this, 'autoembed' ), 8 );
// After a post is saved, cache oEmbed items via Ajax. // After a post is saved, cache oEmbed items via Ajax.
add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) ); add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) );

View File

@ -214,6 +214,9 @@ add_filter( 'widget_text_content', 'wp_filter_content_tags' );
add_filter( 'widget_text_content', 'wp_replace_insecure_home_url' ); add_filter( 'widget_text_content', 'wp_replace_insecure_home_url' );
add_filter( 'widget_text_content', 'do_shortcode', 11 ); // Runs after wpautop(); note that $post global will be null when shortcodes run. add_filter( 'widget_text_content', 'do_shortcode', 11 ); // Runs after wpautop(); note that $post global will be null when shortcodes run.
add_filter( 'widget_block_content', 'do_blocks', 9 );
add_filter( 'widget_block_content', 'do_shortcode', 11 );
add_filter( 'wp_get_custom_css', 'wp_replace_insecure_home_url' ); add_filter( 'wp_get_custom_css', 'wp_replace_insecure_home_url' );
// RSS filters. // RSS filters.

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.8-alpha-51057'; $wp_version = '5.8-alpha-51058';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

View File

@ -66,20 +66,21 @@ class WP_Widget_Block extends WP_Widget {
$args['before_widget'] $args['before_widget']
); );
// Handle embeds for block widgets. /**
// * Filters the content of the Block widget before output.
// When this feature is added to core it may need to be implemented *
// differently. WP_Widget_Text is a good reference, that applies a * @since 5.8.0
// filter for its content, which WP_Embed uses in its constructor. *
// See https://core.trac.wordpress.org/ticket/51566. * @param string $content The widget content.
global $wp_embed; * @param array $instance Array of settings for the current widget.
$content = $wp_embed->run_shortcode( $instance['content'] ); * @param WP_Widget_Text $widget Current Block widget instance.
$content = $wp_embed->autoembed( $content ); */
echo apply_filters(
$content = do_blocks( $content ); 'widget_block_content',
$content = do_shortcode( $content ); $instance['content'],
$instance,
echo $content; $this
);
echo $args['after_widget']; echo $args['after_widget'];
} }