Make WordPress Core

Changeset 54410


Ignore:
Timestamp:
10/07/2022 09:58:32 AM (4 years ago)
Author:
spacedmonkey
Message:

Query: Move call to update_menu_item_cache in WP_Query

Move call to update_menu_item_cache in WP_Query to after post meta caches for menu items are primed.

Props spacedmonkey, peterwilsoncc, mukesh27.
See #55620.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r54352 r54410  
    32693269                }
    32703270
    3271                 if ( ! empty( $this->posts ) && $q['update_menu_item_cache'] ) {
    3272                         update_menu_item_cache( $this->posts );
    3273                 }
    3274 
    32753271                if ( ! $q['suppress_filters'] ) {
    32763272                        /**
    … …  
    34673463                }
    34683464
     3465                if ( ! empty( $this->posts ) && $q['update_menu_item_cache'] ) {
     3466                        update_menu_item_cache( $this->posts );
     3467                }
     3468
    34693469                if ( $q['lazy_load_term_meta'] ) {
    34703470                        wp_queue_posts_for_term_meta_lazyload( $this->posts );
  • trunk/tests/phpunit/tests/post/nav-menu.php

    r54088 r54410  
    266266        }
    267267
     268
     269        /**
     270         * @ticket 55620
     271         * @covers ::update_menu_item_cache
     272         */
     273        public function test_wp_get_nav_menu_items_cache_primes_posts() {
     274                $post_ids     = self::factory()->post->create_many( 3 );
     275                $menu_nav_ids = array();
     276                foreach ( $post_ids as $post_id ) {
     277                        $menu_nav_ids[] = wp_update_nav_menu_item(
     278                                $this->menu_id,
     279                                0,
     280                                array(
     281                                        'menu-item-type'      => 'post_type',
     282                                        'menu-item-object'    => 'post',
     283                                        'menu-item-object-id' => $post_id,
     284                                        'menu-item-status'    => 'publish',
     285                                )
     286                        );
     287                }
     288
     289                // Delete post and post meta caches.
     290                wp_cache_delete_multiple( $menu_nav_ids, 'posts' );
     291                wp_cache_delete_multiple( $menu_nav_ids, 'post_meta' );
     292                wp_cache_delete_multiple( $post_ids, 'posts' );
     293                wp_cache_delete_multiple( $post_ids, 'post_meta' );
     294
     295                $action = new MockAction();
     296                add_filter( 'update_post_metadata_cache', array( $action, 'filter' ), 10, 2 );
     297
     298                $start_num_queries = get_num_queries();
     299                wp_get_nav_menu_items( $this->menu_id );
     300                $queries_made = get_num_queries() - $start_num_queries;
     301                $this->assertSame( 7, $queries_made, 'Only does 7 database queries when running wp_get_nav_menu_items.' );
     302
     303                $args = $action->get_args();
     304                $this->assertSameSets( $menu_nav_ids, $args[0][1], '_prime_post_caches() was not executed.' );
     305                $this->assertSameSets( $post_ids, $args[1][1], '_prime_post_caches() was not executed.' );
     306        }
     307
     308        /**
     309         * @ticket 55620
     310         * @covers ::update_menu_item_cache
     311         */
     312        public function test_wp_get_nav_menu_items_cache_primes_terms() {
     313                register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     314                $term_ids     = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
     315                $menu_nav_ids = array();
     316                foreach ( $term_ids as $term_id ) {
     317                        $menu_nav_ids[] = wp_update_nav_menu_item(
     318                                $this->menu_id,
     319                                0,
     320                                array(
     321                                        'menu-item-type'      => 'taxonomy',
     322                                        'menu-item-object'    => 'wptests_tax',
     323                                        'menu-item-object-id' => $term_id,
     324                                        'menu-item-status'    => 'publish',
     325                                )
     326                        );
     327                }
     328                // Delete post and post meta caches.
     329                wp_cache_delete_multiple( $menu_nav_ids, 'posts' );
     330                wp_cache_delete_multiple( $menu_nav_ids, 'post_meta' );
     331                // Delete term caches.
     332                wp_cache_delete_multiple( $term_ids, 'terms' );
     333                $action_terms = new MockAction();
     334                add_filter( 'update_term_metadata_cache', array( $action_terms, 'filter' ), 10, 2 );
     335
     336                $action_posts = new MockAction();
     337                add_filter( 'update_post_metadata_cache', array( $action_posts, 'filter' ), 10, 2 );
     338
     339                $start_num_queries = get_num_queries();
     340                wp_get_nav_menu_items( $this->menu_id );
     341                $queries_made = get_num_queries() - $start_num_queries;
     342                $this->assertSame( 7, $queries_made, 'Only does 7 database queries when running wp_get_nav_menu_items.' );
     343
     344                $args = $action_terms->get_args();
     345                $last = end( $args );
     346                $this->assertSameSets( $term_ids, $last[1], '_prime_term_caches() was not executed.' );
     347
     348                $args = $action_posts->get_args();
     349                $this->assertSameSets( $menu_nav_ids, $args[0][1], '_prime_post_caches() was not executed.' );
     350        }
     351
    268352        /**
    269353         * @ticket 13910
Note: See TracChangeset for help on using the changeset viewer.