Skip to content

Commit 396f6fb

Browse files
committed
Menus: Improve performance by calling get_privacy_policy_url() once per Walker_Nav_Menu instance rather than for every nav menu item.
The `start_el()` method in `Walker_Nav_Menu` was calling `get_privacy_policy_url()` for every menu item when building menus. This resulted in redundant queries, particularly for menus with many items. This obtains the `get_privacy_policy_url()` value in the constructor for reuse in the `start_el()` method to improve performance. Redundant code to construct the privacy policy page is also refactored into the `set_up()` method during tests. Props arzola, swissspidy, westonruter, mukesh27. Fixes #62818. git-svn-id: https://develop.svn.wordpress.org/trunk@59674 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f2f13cb commit 396f6fb

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

src/wp-includes/class-walker-nav-menu.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ class Walker_Nav_Menu extends Walker {
3939
'id' => 'db_id',
4040
);
4141

42+
/**
43+
* The URL to the privacy policy page.
44+
*
45+
* @since 6.8.0
46+
* @var string
47+
*/
48+
private $privacy_policy_url;
49+
50+
/**
51+
* Constructor.
52+
*
53+
* @since 6.8.0
54+
*/
55+
public function __construct() {
56+
$this->privacy_policy_url = get_privacy_policy_url();
57+
}
58+
4259
/**
4360
* Starts the list before the elements are added.
4461
*
@@ -236,7 +253,7 @@ public function start_el( &$output, $data_object, $depth = 0, $args = null, $cur
236253
$atts['rel'] = ! empty( $menu_item->xfn ) ? $menu_item->xfn : '';
237254

238255
if ( ! empty( $menu_item->url ) ) {
239-
if ( get_privacy_policy_url() === $menu_item->url ) {
256+
if ( $this->privacy_policy_url === $menu_item->url ) {
240257
$atts['rel'] = empty( $atts['rel'] ) ? 'privacy-policy' : $atts['rel'] . ' privacy-policy';
241258
}
242259

tests/phpunit/tests/menu/walker-nav-menu.php

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class Tests_Menu_Walker_Nav_Menu extends WP_UnitTestCase {
1717
*/
1818
private $orig_wp_nav_menu_max_depth;
1919

20+
/**
21+
* The ID of the privacy policy page.
22+
*
23+
* @var int
24+
*/
25+
private $privacy_policy_id;
26+
2027
/**
2128
* Setup.
2229
*/
@@ -27,6 +34,19 @@ public function set_up() {
2734

2835
/** Walker_Nav_Menu class */
2936
require_once ABSPATH . 'wp-includes/class-walker-nav-menu.php';
37+
38+
$post_id = self::factory()->post->create(
39+
array(
40+
'post_type' => 'page',
41+
'post_title' => 'Test Privacy Policy',
42+
'post_status' => 'publish',
43+
)
44+
);
45+
46+
// Set the privacy policy page.
47+
update_option( 'wp_page_for_privacy_policy', $post_id );
48+
$this->privacy_policy_id = (int) get_option( 'wp_page_for_privacy_policy' );
49+
3050
$this->walker = new Walker_Nav_Menu();
3151

3252
$this->orig_wp_nav_menu_max_depth = $_wp_nav_menu_max_depth;
@@ -39,6 +59,7 @@ public function tear_down() {
3959
global $_wp_nav_menu_max_depth;
4060

4161
$_wp_nav_menu_max_depth = $this->orig_wp_nav_menu_max_depth;
62+
delete_option( 'wp_page_for_privacy_policy' );
4263
parent::tear_down();
4364
}
4465

@@ -136,23 +157,12 @@ public function data_start_el_with_empty_attributes() {
136157
* @param string $target Optional. The target value. Default empty string.
137158
*/
138159
public function test_walker_nav_menu_start_el_should_add_rel_privacy_policy_to_privacy_policy_url( $expected, $xfn = '', $target = '' ) {
139-
$post_id = self::factory()->post->create(
140-
array(
141-
'post_type' => 'page',
142-
'post_title' => 'Test Privacy Policy',
143-
'post_status' => 'publish',
144-
)
145-
);
146-
147-
// Set the privacy policy page.
148-
update_option( 'wp_page_for_privacy_policy', $post_id );
149-
$privacy_policy_id = (int) get_option( 'wp_page_for_privacy_policy' );
150160

151161
$output = '';
152162

153163
$item = array(
154-
'ID' => $privacy_policy_id,
155-
'object_id' => $privacy_policy_id,
164+
'ID' => $this->privacy_policy_id,
165+
'object_id' => $this->privacy_policy_id,
156166
'title' => 'Privacy Policy',
157167
'target' => $target,
158168
'xfn' => $xfn,
@@ -251,23 +261,12 @@ public function test_walker_nav_menu_start_el_should_not_add_rel_privacy_policy_
251261
* @covers Walker_Nav_Menu::start_el
252262
*/
253263
public function test_walker_nav_menu_start_el_should_not_add_rel_privacy_policy_when_no_url_is_passed() {
254-
$post_id = self::factory()->post->create(
255-
array(
256-
'post_type' => 'page',
257-
'post_title' => 'Test Privacy Policy',
258-
'post_status' => 'publish',
259-
)
260-
);
261-
262-
// Set the privacy policy page.
263-
update_option( 'wp_page_for_privacy_policy', $post_id );
264-
$privacy_policy_id = (int) get_option( 'wp_page_for_privacy_policy' );
265264

266265
$output = '';
267266

268267
$item = array(
269-
'ID' => $privacy_policy_id,
270-
'object_id' => $privacy_policy_id,
268+
'ID' => $this->privacy_policy_id,
269+
'object_id' => $this->privacy_policy_id,
271270
'title' => 'Privacy Policy',
272271
'target' => '',
273272
'xfn' => '',
@@ -296,22 +295,11 @@ public function test_walker_nav_menu_start_el_should_not_add_rel_privacy_policy_
296295
* @covers Walker_Nav_Menu::start_el
297296
*/
298297
public function test_walker_nav_menu_start_el_should_add_rel_privacy_policy_when_id_does_not_match_but_url_does() {
299-
$post_id = self::factory()->post->create(
300-
array(
301-
'post_type' => 'page',
302-
'post_title' => 'Test Privacy Policy',
303-
'post_status' => 'publish',
304-
)
305-
);
306-
307-
// Set the privacy policy page.
308-
update_option( 'wp_page_for_privacy_policy', $post_id );
309-
$privacy_policy_id = (int) get_option( 'wp_page_for_privacy_policy' );
310298

311299
$output = '';
312300

313301
// Ensure the ID does not match the privacy policy.
314-
$not_privacy_policy_id = $privacy_policy_id - 1;
302+
$not_privacy_policy_id = $this->privacy_policy_id - 1;
315303

316304
$item = array(
317305
'ID' => $not_privacy_policy_id,

0 commit comments

Comments
 (0)