From 81b29a2865b11c98f784e4e0ab93a53ffc07bef0 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Mon, 8 Nov 2021 23:05:50 +0000 Subject: [PATCH] General: Add "noopener" to `wp_list_bookmarks()` output. In the bookmarks walker `_walk_bookmarks()`, add a `'noopener'` to the bookmark's `rel` attribute when there's `target` attribute. Adds a new test class for `wp_list_bookmarks()` and tests for this change. Follow-up to [3880], [10712]. Props birgire, costdev, hellofromTonya, mukesh27 , sergeybiryukov, tw2113. Fixes #53839. git-svn-id: https://develop.svn.wordpress.org/trunk@52061 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/bookmark-template.php | 19 +++- .../tests/bookmark/wpListBookmarks.php | 103 ++++++++++++++++++ 2 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 tests/phpunit/tests/bookmark/wpListBookmarks.php diff --git a/src/wp-includes/bookmark-template.php b/src/wp-includes/bookmark-template.php index 4a156a76cf..cd07204ad9 100644 --- a/src/wp-includes/bookmark-template.php +++ b/src/wp-includes/bookmark-template.php @@ -102,13 +102,24 @@ function _walk_bookmarks( $bookmarks, $args = '' ) { $title = ' title="' . $title . '"'; } $rel = $bookmark->link_rel; + + $target = $bookmark->link_target; + if ( '' !== $target ) { + if ( is_string( $rel ) && '' !== $rel ) { + if ( ! str_contains( $rel, 'noopener' ) ) { + $rel = trim( $rel ) . ' noopener'; + } + } else { + $rel = 'noopener'; + } + + $target = ' target="' . $target . '"'; + } + if ( '' !== $rel ) { $rel = ' rel="' . esc_attr( $rel ) . '"'; } - $target = $bookmark->link_target; - if ( '' !== $target ) { - $target = ' target="' . $target . '"'; - } + $output .= ''; $output .= $parsed_args['link_before']; diff --git a/tests/phpunit/tests/bookmark/wpListBookmarks.php b/tests/phpunit/tests/bookmark/wpListBookmarks.php new file mode 100644 index 0000000000..02305e9565 --- /dev/null +++ b/tests/phpunit/tests/bookmark/wpListBookmarks.php @@ -0,0 +1,103 @@ +bookmark->create( $args ); + $this->assertStringContainsString( 'noopener', wp_list_bookmarks( 'echo=0' ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_wp_list_bookmarks_adds_noopener() { + return array( + 'target as "_blank"' => array( + 'args' => array( + 'link_name' => 'With _blank', + 'link_url' => 'https://www.wordpress.org', + 'link_target' => '_blank', + ), + ), + 'target as "_blank" and a link relationship' => array( + 'args' => array( + 'link_name' => 'With _blank and a link relationship', + 'link_url' => 'https://www.wordpress.org', + 'link_target' => '_blank', + 'rel' => 'me', + ), + ), + 'target as "_top"' => array( + 'args' => array( + 'link_name' => 'With _top', + 'link_url' => 'https://www.wordpress.org', + 'link_target' => '_top', + ), + ), + 'target as "_top" and a link relationship' => array( + 'args' => array( + 'link_name' => 'With _top and a link relationship', + 'link_url' => 'https://www.wordpress.org', + 'link_target' => '_top', + 'rel' => 'me', + ), + ), + ); + } + + /** + * Test that wp_list_bookmarks does not add "noopener" to the "rel" attribute. + * + * @dataProvider data_wp_list_bookmarks_does_not_add_noopener + * + * @ticket 53839 + * + * @param array $args The arguments to create the bookmark. + */ + public function test_wp_list_bookmarks_does_not_add_noopener( $args ) { + $bookmark = self::factory()->bookmark->create( $args ); + $this->assertStringNotContainsString( 'noopener', wp_list_bookmarks( 'echo=0' ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_wp_list_bookmarks_does_not_add_noopener() { + return array( + 'target as "_none"' => array( + 'args' => array( + 'link_name' => 'With _blank', + 'link_url' => 'https://www.wordpress.org', + 'link_target' => '_none', + ), + ), + 'target as "_none" and a link relationship' => array( + 'args' => array( + 'link_name' => 'With _blank and a link relationship', + 'link_url' => 'https://www.wordpress.org', + 'link_target' => '_none', + 'rel' => 'me', + ), + ), + ); + } +}