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.

Built from https://develop.svn.wordpress.org/trunk@59674


git-svn-id: http://core.svn.wordpress.org/trunk@59017 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Weston Ruter
2025-01-21 21:26:23 +00:00
parent 1cd758d2ee
commit 091d5e460e
2 changed files with 19 additions and 2 deletions

View File

@@ -39,6 +39,23 @@ class Walker_Nav_Menu extends Walker {
'id' => 'db_id',
);
/**
* The URL to the privacy policy page.
*
* @since 6.8.0
* @var string
*/
private $privacy_policy_url;
/**
* Constructor.
*
* @since 6.8.0
*/
public function __construct() {
$this->privacy_policy_url = get_privacy_policy_url();
}
/**
* Starts the list before the elements are added.
*
@@ -236,7 +253,7 @@ class Walker_Nav_Menu extends Walker {
$atts['rel'] = ! empty( $menu_item->xfn ) ? $menu_item->xfn : '';
if ( ! empty( $menu_item->url ) ) {
if ( get_privacy_policy_url() === $menu_item->url ) {
if ( $this->privacy_policy_url === $menu_item->url ) {
$atts['rel'] = empty( $atts['rel'] ) ? 'privacy-policy' : $atts['rel'] . ' privacy-policy';
}

View File

@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.8-alpha-59673';
$wp_version = '6.8-alpha-59674';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.