From 08228902186d6ad0b83310a8c6f565fad2e31916 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 18 Jul 2017 04:16:16 +0000 Subject: [PATCH] Widgets: Discount shortcodes and leading/trailing line breaks for triggering Text widget legacy mode. Since plugin-added shortcode handling is just-in-time deferred to run after `wpautop`, there is no need to retain the presence of shortcodes to trigger legacy mode. Also updates Text widget legacy mode notice informing of Custom HTML widget. Amends [41050]. Props westonruter, melchoyce. Fixes #40951 for trunk. git-svn-id: https://develop.svn.wordpress.org/trunk@41070 602fd350-edb4-49c9-b593-d223f7449a82 --- .../widgets/class-wp-widget-text.php | 13 ++----------- tests/phpunit/tests/widgets/text-widget.php | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/wp-includes/widgets/class-wp-widget-text.php b/src/wp-includes/widgets/class-wp-widget-text.php index ad4667bb0f..69b7b91aea 100644 --- a/src/wp-includes/widgets/class-wp-widget-text.php +++ b/src/wp-includes/widgets/class-wp-widget-text.php @@ -95,7 +95,7 @@ class WP_Widget_Text extends WP_Widget { } $wpautop = ! empty( $instance['filter'] ); - $has_line_breaks = ( false !== strpos( $instance['text'], "\n" ) ); + $has_line_breaks = ( false !== strpos( trim( $instance['text'] ), "\n" ) ); // If auto-paragraphs are not enabled and there are line breaks, then ensure legacy mode. if ( ! $wpautop && $has_line_breaks ) { @@ -107,15 +107,6 @@ class WP_Widget_Text extends WP_Widget { return true; } - /* - * If a shortcode is present (with support added by a plugin), assume legacy mode - * since shortcodes would apply at the widget_text filter and thus be applied - * before wpautop runs at the widget_text_content filter. - */ - if ( preg_match( '/' . get_shortcode_regex() . '/', $instance['text'] ) ) { - return true; - } - // In the rare case that DOMDocument is not available we cannot reliably sniff content and so we assume legacy. if ( ! class_exists( 'DOMDocument' ) ) { // @codeCoverageIgnoreStart @@ -348,7 +339,7 @@ class WP_Widget_Text extends WP_Widget {

-

+

diff --git a/tests/phpunit/tests/widgets/text-widget.php b/tests/phpunit/tests/widgets/text-widget.php index af3e2755f5..764b4eaeac 100644 --- a/tests/phpunit/tests/widgets/text-widget.php +++ b/tests/phpunit/tests/widgets/text-widget.php @@ -265,6 +265,18 @@ class Test_WP_Widget_Text extends WP_UnitTestCase { ) ); $this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when text is empty.' ); + $instance = array_merge( $base_instance, array( + 'text' => "\nOne line", + 'filter' => false, + ) ); + $this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when there is leading whitespace.' ); + + $instance = array_merge( $base_instance, array( + 'text' => "\nOne line\n\n", + 'filter' => false, + ) ); + $this->assertFalse( $widget->is_legacy_instance( $instance ), 'Not legacy when there is trailing whitespace.' ); + $instance = array_merge( $base_instance, array( 'text' => "One\nTwo", 'filter' => false, @@ -295,12 +307,6 @@ class Test_WP_Widget_Text extends WP_UnitTestCase { ) ); $this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy when HTML comment is present.' ); - $instance = array_merge( $base_instance, array( - 'text' => 'Here is a [gallery]', - 'filter' => true, - ) ); - $this->assertTrue( $widget->is_legacy_instance( $instance ), 'Legacy mode when a shortcode is present.' ); - // Check text examples that will not migrate to TinyMCE. $legacy_text_examples = array( '',