Make WordPress Core


Ignore:
Timestamp:
01/21/2025 09:24:59 PM (5 months ago)
Author:
westonruter
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-walker-nav-menu.php

    r59179 r59674  
    4141
    4242    /**
     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
     59    /**
    4360     * Starts the list before the elements are added.
    4461     *
     
    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            }
Note: See TracChangeset for help on using the changeset viewer.