From bb463b5265789293841ac4768d0ad91695aa0d77 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 22 Jan 2019 21:54:26 +0000 Subject: [PATCH] Formatting: Prevent `wp_targeted_link_rel()` adding an empty attribute. Bypass adding a `rel` attribute when the `wp_targeted_link_rel` filter returns an empty string or other falsy result. Props mcmwebsol, spartank, meatman89fs. Fixes #45352. git-svn-id: https://develop.svn.wordpress.org/trunk@44691 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/formatting.php | 5 +++++ tests/phpunit/tests/formatting/WPTargetedLinkRel.php | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 462ebaf1f4..dc4bf24648 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -3065,6 +3065,11 @@ function wp_targeted_link_rel_callback( $matches ) { */ $rel = apply_filters( 'wp_targeted_link_rel', 'noopener noreferrer', $link_html ); + // Avoid additional regex if the filter removes rel values. + if ( ! $rel ) { + return ""; + } + // Value with delimiters, spaces around are optional. $attr_regex = '|rel\s*=\s*?(\\\\{0,1}["\'])(.*?)\\1|i'; preg_match( $attr_regex, $link_html, $rel_match ); diff --git a/tests/phpunit/tests/formatting/WPTargetedLinkRel.php b/tests/phpunit/tests/formatting/WPTargetedLinkRel.php index 49c843ec2c..5693f02973 100644 --- a/tests/phpunit/tests/formatting/WPTargetedLinkRel.php +++ b/tests/phpunit/tests/formatting/WPTargetedLinkRel.php @@ -71,4 +71,16 @@ class Tests_Targeted_Link_Rel extends WP_UnitTestCase { $expected = '

Links: Change me Do not change me

'; $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); } + + /** + * Ensure empty rel attributes are not added. + * + * @ticket 45352. + */ + public function test_ignore_if_wp_targeted_link_rel_nulled() { + add_filter( 'wp_targeted_link_rel', '__return_empty_string' ); + $content = '

Links: Do not change me

'; + $expected = '

Links: Do not change me

'; + $this->assertEquals( $expected, wp_targeted_link_rel( $content ) ); + } }