From 091d5e460e43e386408924f01e902adf4c49aeec Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 21 Jan 2025 21:26:23 +0000 Subject: [PATCH] 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 --- wp-includes/class-walker-nav-menu.php | 19 ++++++++++++++++++- wp-includes/version.php | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/wp-includes/class-walker-nav-menu.php b/wp-includes/class-walker-nav-menu.php index 2b06d54b44..3d9f442bb8 100644 --- a/wp-includes/class-walker-nav-menu.php +++ b/wp-includes/class-walker-nav-menu.php @@ -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'; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 8f9a4de7af..9c1cd69092 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.