diff --git src/wp-includes/class-walker-nav-menu.php src/wp-includes/class-walker-nav-menu.php
index f46f14c177..25f0042ca6 100644
|
|
class Walker_Nav_Menu extends Walker { |
172 | 172 | $atts = array(); |
173 | 173 | $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : ''; |
174 | 174 | $atts['target'] = ! empty( $item->target ) ? $item->target : ''; |
175 | | $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; |
| 175 | if ( '_blank' === $item->target && empty( $item->xfn ) ) { |
| 176 | $atts['rel'] = 'noopener noreferrer'; |
| 177 | } else { |
| 178 | $atts['rel'] = $item->xfn; |
| 179 | } |
176 | 180 | $atts['href'] = ! empty( $item->url ) ? $item->url : ''; |
177 | 181 | $atts['aria-current'] = $item->current ? 'page' : ''; |
178 | 182 | |
diff --git tests/phpunit/tests/menu/walker-nav-menu.php tests/phpunit/tests/menu/walker-nav-menu.php
new file mode 100644
index 0000000000..e60a9550d4
-
|
+
|
|
| 1 | <?php |
| 2 | /** |
| 3 | * @group navmenus |
| 4 | * @group walker |
| 5 | */ |
| 6 | class Tests_Walker_Nav_Menu extends WP_UnitTestCase { |
| 7 | |
| 8 | /** |
| 9 | * @var \Walker_Nav_Menu The instance of the walker. |
| 10 | */ |
| 11 | public $walker; |
| 12 | |
| 13 | /** |
| 14 | * Setup. |
| 15 | */ |
| 16 | public function setUp() { |
| 17 | global $_wp_nav_menu_max_depth; |
| 18 | |
| 19 | parent::setUp(); |
| 20 | |
| 21 | /** Walker_Nav_Menu_Edit class */ |
| 22 | require_once ABSPATH . 'wp-includes/class-walker-nav-menu.php'; |
| 23 | $this->walker = new Walker_Nav_Menu(); |
| 24 | |
| 25 | $this->_wp_nav_menu_max_depth = $_wp_nav_menu_max_depth; |
| 26 | parent::setUp(); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Tear down |
| 31 | */ |
| 32 | public function tearDown() { |
| 33 | global $_wp_nav_menu_max_depth; |
| 34 | |
| 35 | $_wp_nav_menu_max_depth = $this->_wp_nav_menu_max_depth; |
| 36 | parent::tearDown(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Tests when an items target it _blank, that rel="'noopener noreferrer" is added. |
| 41 | * |
| 42 | * @ticket #43290 |
| 43 | */ |
| 44 | public function test_noopener_no_referrer_for_target_blank() { |
| 45 | $expected = ''; |
| 46 | $post_id = $this->factory->post->create(); |
| 47 | $post_title = get_the_title( $post_id ); |
| 48 | |
| 49 | $item = array( |
| 50 | 'ID' => $post_id, |
| 51 | 'object_id' => $post_id, |
| 52 | 'title' => $post_title, |
| 53 | 'target' => '_blank', |
| 54 | 'xfn' => '', |
| 55 | 'current' => false, |
| 56 | ); |
| 57 | |
| 58 | $args = array( |
| 59 | 'before' => '', |
| 60 | 'after' => '', |
| 61 | 'link_before' => '', |
| 62 | 'link_after' => '', |
| 63 | ); |
| 64 | |
| 65 | $this->walker->start_el( $expected, (object) $item, 0, (object) $args ); |
| 66 | |
| 67 | $this->assertSame( "<li id=\"menu-item-{$post_id}\" class=\"menu-item-{$post_id}\"><a target=\"_blank\" rel=\"noopener noreferrer\">{$post_title}</a>", $expected ); |
| 68 | } |
| 69 | } |