Make WordPress Core

Ticket #40907: apply-widget_text-filter-in-custom-html-widget.2.diff

File apply-widget_text-filter-in-custom-html-widget.2.diff, 5.3 KB (added by westonruter, 7 years ago)
  • src/wp-includes/default-filters.php

    diff --git src/wp-includes/default-filters.php src/wp-includes/default-filters.php
    index ada9c53c3f..8f0237028d 100644
    add_filter( 'widget_text_content', 'wptexturize' ); 
    170170add_filter( 'widget_text_content', 'convert_smilies',  20 );
    171171add_filter( 'widget_text_content', 'wpautop'              );
    172172
    173 add_filter( 'widget_custom_html_content', 'balanceTags' );
    174 
    175173add_filter( 'date_i18n', 'wp_maybe_decline_date' );
    176174
    177175// RSS filters
  • src/wp-includes/widgets/class-wp-widget-custom-html.php

    diff --git src/wp-includes/widgets/class-wp-widget-custom-html.php src/wp-includes/widgets/class-wp-widget-custom-html.php
    index 2db50f4d22..5de618285f 100644
    class WP_Widget_Custom_HTML extends WP_Widget { 
    6161                /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
    6262                $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
    6363
    64                 $content = $instance['content'];
     64                /** This filter is documented in wp-includes/widgets/class-wp-widget-text.php */
     65                $content = apply_filters( 'widget_text', $instance['content'], $instance, $this );
    6566
    6667                /**
    6768                 * Filters the content of the Custom HTML widget.
  • src/wp-includes/widgets/class-wp-widget-text.php

    diff --git src/wp-includes/widgets/class-wp-widget-text.php src/wp-includes/widgets/class-wp-widget-text.php
    index 69b7b91aea..2297d2bee2 100644
    class WP_Widget_Text extends WP_Widget { 
    213213                 *
    214214                 * @since 2.3.0
    215215                 * @since 4.4.0 Added the `$this` parameter.
     216                 * @since 4.8.1 The `$this` param may now be a `WP_Widget_Custom_HTML` object in addition to a `WP_Widget_Text` object.
    216217                 *
    217                  * @param string         $text     The widget content.
    218                  * @param array          $instance Array of settings for the current widget.
    219                  * @param WP_Widget_Text $this     Current Text widget instance.
     218                 * @param string                               $text     The widget content.
     219                 * @param array                                $instance Array of settings for the current widget.
     220                 * @param WP_Widget_Text|WP_Widget_Custom_HTML $this     Current Text widget instance.
    220221                 */
    221222                $text = apply_filters( 'widget_text', $text, $instance, $this );
    222223
  • tests/phpunit/tests/widgets/custom-html-widget.php

    diff --git tests/phpunit/tests/widgets/custom-html-widget.php tests/phpunit/tests/widgets/custom-html-widget.php
    index 3305d8ad35..edbdae3c50 100644
    class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase { 
    2121        protected $widget_custom_html_content_args;
    2222
    2323        /**
     24         * Args passed to the widget_text filter.
     25         *
     26         * @var array
     27         */
     28        protected $widget_text_args;
     29
     30        /**
    2431         * Test constructor.
    2532         *
    2633         * @covers WP_Widget_Custom_HTML::__constructor
    class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase { 
    5259                        'content' => $content,
    5360                );
    5461
    55                 $this->assertEquals( 10, has_filter( 'widget_custom_html_content', 'balanceTags' ) );
    56 
    5762                update_option( 'use_balanceTags', 0 );
    5863                add_filter( 'widget_custom_html_content', array( $this, 'filter_widget_custom_html_content' ), 5, 3 );
     64                add_filter( 'widget_text', array( $this, 'filter_widget_text' ), 10, 3 );
    5965                ob_start();
    6066                $this->widget_custom_html_content_args = null;
    6167                $widget->widget( $args, $instance );
    6268                $output = ob_get_clean();
    6369                $this->assertNotEmpty( $this->widget_custom_html_content_args );
    64                 $this->assertContains( '[filter:widget_custom_html_content]', $output );
     70                $this->assertContains( '[filter:widget_text][filter:widget_custom_html_content]', $output );
    6571                $this->assertNotContains( '<p>', $output );
    6672                $this->assertNotContains( '<br>', $output );
    6773                $this->assertNotContains( '</u>', $output );
     74                $this->assertEquals( $instance, $this->widget_text_args[1] );
    6875                $this->assertEquals( $instance, $this->widget_custom_html_content_args[1] );
     76                $this->assertSame( $widget, $this->widget_text_args[2] );
    6977                $this->assertSame( $widget, $this->widget_custom_html_content_args[2] );
    7078                remove_filter( 'widget_custom_html_content', array( $this, 'filter_widget_custom_html_content' ), 5, 3 );
     79                remove_filter( 'widget_text', array( $this, 'filter_widget_text' ), 10 );
    7180
    7281                update_option( 'use_balanceTags', 1 );
    7382                ob_start();
    class Test_WP_Widget_Custom_HTML extends WP_UnitTestCase { 
    7786        }
    7887
    7988        /**
    80          * Filters the content of the Custom HTML widget.
     89         * Filters the content of the Custom HTML widget using the legacy widget_text filter.
     90         *
     91         * @param string                $text     The widget content.
     92         * @param array                 $instance Array of settings for the current widget.
     93         * @param WP_Widget_Custom_HTML $widget   Current widget instance.
     94         * @return string Widget content.
     95         */
     96        function filter_widget_text( $text, $instance, $widget ) {
     97                $this->widget_text_args = func_get_args();
     98
     99                $text .= '[filter:widget_text]';
     100                return $text;
     101        }
     102
     103        /**
     104         * Filters the content of the Custom HTML widget using the dedicated widget_custom_html_content filter.
    81105         *
    82106         * @param string                $widget_content The widget content.
    83107         * @param array                 $instance       Array of settings for the current widget.