Make WordPress Core

Changeset 59674


Ignore:
Timestamp:
01/21/2025 09:24:59 PM (19 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.

Location:
trunk
Files:
2 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                        }
  • trunk/tests/phpunit/tests/menu/walker-nav-menu.php

    r59120 r59674  
    1919
    2020        /**
     21         * The ID of the privacy policy page.
     22         *
     23         * @var int
     24         */
     25        private $privacy_policy_id;
     26
     27        /**
    2128         * Setup.
    2229         */
     
    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
     
    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        }
     
    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,
     
    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'    => '',
     
    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(
Note: See TracChangeset for help on using the changeset viewer.