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
This commit is contained in:
Weston Ruter 2017-07-18 22:10:41 +00:00
parent 9ddb744f33
commit c941da4468
4 changed files with 36 additions and 12 deletions

View File

@ -170,8 +170,6 @@ add_filter( 'widget_text_content', 'wptexturize' );
add_filter( 'widget_text_content', 'convert_smilies', 20 ); add_filter( 'widget_text_content', 'convert_smilies', 20 );
add_filter( 'widget_text_content', 'wpautop' ); add_filter( 'widget_text_content', 'wpautop' );
add_filter( 'widget_custom_html_content', 'balanceTags' );
add_filter( 'date_i18n', 'wp_maybe_decline_date' ); add_filter( 'date_i18n', 'wp_maybe_decline_date' );
// RSS filters // RSS filters

View File

@ -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 */ /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); $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. * Filters the content of the Custom HTML widget.

View File

@ -213,10 +213,11 @@ class WP_Widget_Text extends WP_Widget {
* *
* @since 2.3.0 * @since 2.3.0
* @since 4.4.0 Added the `$this` parameter. * @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 string $text The widget content.
* @param array $instance Array of settings for the current widget. * @param array $instance Array of settings for the current widget.
* @param WP_Widget_Text $this Current Text widget instance. * @param WP_Widget_Text|WP_Widget_Custom_HTML $this Current Text widget instance.
*/ */
$text = apply_filters( 'widget_text', $text, $instance, $this ); $text = apply_filters( 'widget_text', $text, $instance, $this );

View File

@ -20,6 +20,13 @@ class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase {
*/ */
protected $widget_custom_html_content_args; protected $widget_custom_html_content_args;
/**
* Args passed to the widget_text filter.
*
* @var array
*/
protected $widget_text_args;
/** /**
* Test constructor. * Test constructor.
* *
@ -52,22 +59,26 @@ class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase {
'content' => $content, 'content' => $content,
); );
$this->assertEquals( 10, has_filter( 'widget_custom_html_content', 'balanceTags' ) );
update_option( 'use_balanceTags', 0 ); update_option( 'use_balanceTags', 0 );
add_filter( 'widget_custom_html_content', array( $this, 'filter_widget_custom_html_content' ), 5, 3 ); 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(); ob_start();
$this->widget_custom_html_content_args = null; $this->widget_custom_html_content_args = null;
$this->widget_text_args = null;
$widget->widget( $args, $instance ); $widget->widget( $args, $instance );
$output = ob_get_clean(); $output = ob_get_clean();
$this->assertNotEmpty( $this->widget_custom_html_content_args ); $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( '<p>', $output ); $this->assertNotContains( '<p>', $output );
$this->assertNotContains( '<br>', $output ); $this->assertNotContains( '<br>', $output );
$this->assertNotContains( '</u>', $output ); $this->assertNotContains( '</u>', $output );
$this->assertEquals( $instance, $this->widget_text_args[1] );
$this->assertEquals( $instance, $this->widget_custom_html_content_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] ); $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_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 ); update_option( 'use_balanceTags', 1 );
ob_start(); 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 string $widget_content The widget content.
* @param array $instance Array of settings for the current widget. * @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. * @return string Widget content.
*/ */
function filter_widget_custom_html_content( $widget_content, $instance, $widget ) { 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]'; $widget_content .= '[filter:widget_custom_html_content]';
return $widget_content; return $widget_content;
} }