From c941da4468e147df68b631fa12ac88820cf5e859 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 18 Jul 2017 22:10:41 +0000 Subject: [PATCH] Widgets: Replace adding `balanceTags` on `widget_custom_html_content` filter in favor of just applying `widget_text` filters in the Custom HTML widget. Ensures that users who copy HTML from the Text widget in legacy mode over to the Custom HTML widget will continue to get all of the same filters applied, including tag balancing and shortcodes, if a plugin added support. Plugins still have the `widget_text_content` and `widget_custom_html_content` filters they can use to target the specific widget types. Amends [40893]. See #40951. Fixes #40907 for trunk. git-svn-id: https://develop.svn.wordpress.org/trunk@41086 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/default-filters.php | 2 -- .../widgets/class-wp-widget-custom-html.php | 3 +- .../widgets/class-wp-widget-text.php | 7 ++-- .../tests/widgets/custom-html-widget.php | 36 +++++++++++++++---- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index ada9c53c3f..8f0237028d 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -170,8 +170,6 @@ add_filter( 'widget_text_content', 'wptexturize' ); add_filter( 'widget_text_content', 'convert_smilies', 20 ); add_filter( 'widget_text_content', 'wpautop' ); -add_filter( 'widget_custom_html_content', 'balanceTags' ); - add_filter( 'date_i18n', 'wp_maybe_decline_date' ); // RSS filters diff --git a/src/wp-includes/widgets/class-wp-widget-custom-html.php b/src/wp-includes/widgets/class-wp-widget-custom-html.php index 2db50f4d22..5de618285f 100644 --- a/src/wp-includes/widgets/class-wp-widget-custom-html.php +++ b/src/wp-includes/widgets/class-wp-widget-custom-html.php @@ -61,7 +61,8 @@ class WP_Widget_Custom_HTML extends WP_Widget { /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); - $content = $instance['content']; + /** This filter is documented in wp-includes/widgets/class-wp-widget-text.php */ + $content = apply_filters( 'widget_text', $instance['content'], $instance, $this ); /** * Filters the content of the Custom HTML widget. diff --git a/src/wp-includes/widgets/class-wp-widget-text.php b/src/wp-includes/widgets/class-wp-widget-text.php index 69b7b91aea..2297d2bee2 100644 --- a/src/wp-includes/widgets/class-wp-widget-text.php +++ b/src/wp-includes/widgets/class-wp-widget-text.php @@ -213,10 +213,11 @@ class WP_Widget_Text extends WP_Widget { * * @since 2.3.0 * @since 4.4.0 Added the `$this` parameter. + * @since 4.8.1 The `$this` param may now be a `WP_Widget_Custom_HTML` object in addition to a `WP_Widget_Text` object. * - * @param string $text The widget content. - * @param array $instance Array of settings for the current widget. - * @param WP_Widget_Text $this Current Text widget instance. + * @param string $text The widget content. + * @param array $instance Array of settings for the current widget. + * @param WP_Widget_Text|WP_Widget_Custom_HTML $this Current Text widget instance. */ $text = apply_filters( 'widget_text', $text, $instance, $this ); diff --git a/tests/phpunit/tests/widgets/custom-html-widget.php b/tests/phpunit/tests/widgets/custom-html-widget.php index 3305d8ad35..c7c2e91bf8 100644 --- a/tests/phpunit/tests/widgets/custom-html-widget.php +++ b/tests/phpunit/tests/widgets/custom-html-widget.php @@ -20,6 +20,13 @@ class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase { */ protected $widget_custom_html_content_args; + /** + * Args passed to the widget_text filter. + * + * @var array + */ + protected $widget_text_args; + /** * Test constructor. * @@ -52,22 +59,26 @@ class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase { 'content' => $content, ); - $this->assertEquals( 10, has_filter( 'widget_custom_html_content', 'balanceTags' ) ); - update_option( 'use_balanceTags', 0 ); add_filter( 'widget_custom_html_content', array( $this, 'filter_widget_custom_html_content' ), 5, 3 ); + add_filter( 'widget_text', array( $this, 'filter_widget_text' ), 10, 3 ); ob_start(); $this->widget_custom_html_content_args = null; + $this->widget_text_args = null; $widget->widget( $args, $instance ); $output = ob_get_clean(); $this->assertNotEmpty( $this->widget_custom_html_content_args ); - $this->assertContains( '[filter:widget_custom_html_content]', $output ); + $this->assertNotEmpty( $this->widget_text_args ); + $this->assertContains( '[filter:widget_text][filter:widget_custom_html_content]', $output ); $this->assertNotContains( '

', $output ); $this->assertNotContains( '
', $output ); $this->assertNotContains( '', $output ); + $this->assertEquals( $instance, $this->widget_text_args[1] ); $this->assertEquals( $instance, $this->widget_custom_html_content_args[1] ); + $this->assertSame( $widget, $this->widget_text_args[2] ); $this->assertSame( $widget, $this->widget_custom_html_content_args[2] ); remove_filter( 'widget_custom_html_content', array( $this, 'filter_widget_custom_html_content' ), 5, 3 ); + remove_filter( 'widget_text', array( $this, 'filter_widget_text' ), 10 ); update_option( 'use_balanceTags', 1 ); ob_start(); @@ -77,7 +88,21 @@ class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase { } /** - * Filters the content of the Custom HTML widget. + * Filters the content of the Custom HTML widget using the legacy widget_text filter. + * + * @param string $text The widget content. + * @param array $instance Array of settings for the current widget. + * @param WP_Widget_Custom_HTML $widget Current widget instance. + * @return string Widget content. + */ + function filter_widget_text( $text, $instance, $widget ) { + $this->widget_text_args = array( $text, $instance, $widget ); + $text .= '[filter:widget_text]'; + return $text; + } + + /** + * Filters the content of the Custom HTML widget using the dedicated widget_custom_html_content filter. * * @param string $widget_content The widget content. * @param array $instance Array of settings for the current widget. @@ -85,8 +110,7 @@ class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase { * @return string Widget content. */ function filter_widget_custom_html_content( $widget_content, $instance, $widget ) { - $this->widget_custom_html_content_args = func_get_args(); - + $this->widget_custom_html_content_args = array( $widget_content, $instance, $widget ); $widget_content .= '[filter:widget_custom_html_content]'; return $widget_content; }