Widgets: Ensure return value of `widget_text` filter is not dropped but passed into `widget_text_content` filter for rendering.

Amends [40631].
Props vijustin, swissspidy, westonruter.
See #35243.
Fixes #40772.



git-svn-id: https://develop.svn.wordpress.org/trunk@40673 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter 2017-05-15 22:35:41 +00:00
parent 88fd7b8652
commit 74128e0e3f
2 changed files with 19 additions and 12 deletions

View File

@ -67,7 +67,7 @@ class WP_Widget_Text extends WP_Widget {
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
$widget_text = ! empty( $instance['text'] ) ? $instance['text'] : '';
$text = ! empty( $instance['text'] ) ? $instance['text'] : '';
/**
* Filters the content of the Text widget.
@ -75,11 +75,11 @@ class WP_Widget_Text extends WP_Widget {
* @since 2.3.0
* @since 4.4.0 Added the `$this` parameter.
*
* @param string $widget_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 $this Current Text widget instance.
*/
$text = apply_filters( 'widget_text', $widget_text, $instance, $this );
$text = apply_filters( 'widget_text', $text, $instance, $this );
if ( isset( $instance['filter'] ) ) {
if ( 'content' === $instance['filter'] ) {
@ -91,11 +91,11 @@ class WP_Widget_Text extends WP_Widget {
*
* @since 4.8.0
*
* @param string $widget_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 $this Current Text widget instance.
*/
$text = apply_filters( 'widget_text_content', $widget_text, $instance, $this );
$text = apply_filters( 'widget_text_content', $text, $instance, $this );
} elseif ( $instance['filter'] ) {
$text = wpautop( $text ); // Back-compat for instances prior to 4.8.

View File

@ -74,8 +74,8 @@ class Test_WP_Widget_Text extends WP_UnitTestCase {
'filter' => false,
);
add_filter( 'widget_text_content', array( $this, 'filter_widget_text_content' ), 10, 3 );
add_filter( 'widget_text', array( $this, 'filter_widget_text' ), 10, 3 );
add_filter( 'widget_text_content', array( $this, 'filter_widget_text_content' ), 5, 3 );
add_filter( 'widget_text', array( $this, 'filter_widget_text' ), 5, 3 );
// Test with filter=false.
ob_start();
@ -85,6 +85,8 @@ class Test_WP_Widget_Text extends WP_UnitTestCase {
$this->assertNotContains( '<br />', $output );
$this->assertEmpty( $this->widget_text_content_args );
$this->assertNotEmpty( $this->widget_text_args );
$this->assertContains( '[filter:widget_text]', $output );
$this->assertNotContains( '[filter:widget_text_content]', $output );
// Test with filter=true.
$instance['filter'] = true;
@ -98,6 +100,8 @@ class Test_WP_Widget_Text extends WP_UnitTestCase {
$this->assertEquals( $instance, $this->widget_text_args[1] );
$this->assertEquals( $widget, $this->widget_text_args[2] );
$this->assertEmpty( $this->widget_text_content_args );
$this->assertContains( '[filter:widget_text]', $output );
$this->assertNotContains( '[filter:widget_text_content]', $output );
// Test with filter=content, the upgraded widget.
$instance['filter'] = 'content';
@ -111,9 +115,10 @@ class Test_WP_Widget_Text extends WP_UnitTestCase {
$this->assertEquals( $instance, $this->widget_text_args[1] );
$this->assertEquals( $widget, $this->widget_text_args[2] );
$this->assertCount( 3, $this->widget_text_content_args );
$this->assertEquals( wpautop( $instance['text'] ), $this->widget_text_content_args[0] );
$this->assertEquals( $instance['text'] . '[filter:widget_text]', $this->widget_text_content_args[0] );
$this->assertEquals( $instance, $this->widget_text_content_args[1] );
$this->assertEquals( $widget, $this->widget_text_content_args[2] );
$this->assertContains( wpautop( $instance['text'] . '[filter:widget_text][filter:widget_text_content]' ), $output );
}
/**
@ -127,6 +132,7 @@ class Test_WP_Widget_Text extends WP_UnitTestCase {
function filter_widget_text( $widget_text, $instance, $widget ) {
$this->widget_text_args = func_get_args();
$widget_text .= '[filter:widget_text]';
return $widget_text;
}
@ -141,6 +147,7 @@ class Test_WP_Widget_Text extends WP_UnitTestCase {
function filter_widget_text_content( $widget_text, $instance, $widget ) {
$this->widget_text_content_args = func_get_args();
$widget_text .= '[filter:widget_text_content]';
return $widget_text;
}