From cf60c451b09e0d51f835e61e4052dd365c4ad69f Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Wed, 2 Jun 2021 01:17:37 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@51058 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-embed.php | 2 ++ src/wp-includes/default-filters.php | 3 ++ .../widgets/class-wp-widget-block.php | 29 ++++++++++--------- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/class-wp-embed.php b/src/wp-includes/class-wp-embed.php index aff25412c3..4b189f9334 100644 --- a/src/wp-includes/class-wp-embed.php +++ b/src/wp-includes/class-wp-embed.php @@ -31,6 +31,7 @@ class WP_Embed { // Hack to get the [embed] shortcode to run before wpautop(). add_filter( 'the_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(). add_shortcode( 'embed', '__return_false' ); @@ -38,6 +39,7 @@ class WP_Embed { // Attempts to embed all URLs in a post. add_filter( 'the_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. add_action( 'edit_form_advanced', array( $this, 'maybe_run_ajax_cache' ) ); diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index d2a89ed4d1..8663196248 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -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', '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' ); // RSS filters. diff --git a/src/wp-includes/widgets/class-wp-widget-block.php b/src/wp-includes/widgets/class-wp-widget-block.php index 4c5abb38a4..b191bfaf31 100644 --- a/src/wp-includes/widgets/class-wp-widget-block.php +++ b/src/wp-includes/widgets/class-wp-widget-block.php @@ -66,20 +66,21 @@ class WP_Widget_Block extends WP_Widget { $args['before_widget'] ); - // Handle embeds for block widgets. - // - // When this feature is added to core it may need to be implemented - // differently. WP_Widget_Text is a good reference, that applies a - // filter for its content, which WP_Embed uses in its constructor. - // See https://core.trac.wordpress.org/ticket/51566. - global $wp_embed; - $content = $wp_embed->run_shortcode( $instance['content'] ); - $content = $wp_embed->autoembed( $content ); - - $content = do_blocks( $content ); - $content = do_shortcode( $content ); - - echo $content; + /** + * Filters the content of the Block widget before output. + * + * @since 5.8.0 + * + * @param string $content The widget content. + * @param array $instance Array of settings for the current widget. + * @param WP_Widget_Text $widget Current Block widget instance. + */ + echo apply_filters( + 'widget_block_content', + $instance['content'], + $instance, + $this + ); echo $args['after_widget']; }