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.

git-svn-id: https://develop.svn.wordpress.org/trunk@54410 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris 2022-10-07 09:58:32 +00:00
parent f3c0f743b3
commit fd60449f92
2 changed files with 88 additions and 4 deletions

View File

@ -3268,10 +3268,6 @@ class WP_Query {
wp_cache_set( $cache_key, $cache_value, 'posts' );
}
if ( ! empty( $this->posts ) && $q['update_menu_item_cache'] ) {
update_menu_item_cache( $this->posts );
}
if ( ! $q['suppress_filters'] ) {
/**
* Filters the raw post results array, prior to status checks.
@ -3466,6 +3462,10 @@ class WP_Query {
$this->posts = array();
}
if ( ! empty( $this->posts ) && $q['update_menu_item_cache'] ) {
update_menu_item_cache( $this->posts );
}
if ( $q['lazy_load_term_meta'] ) {
wp_queue_posts_for_term_meta_lazyload( $this->posts );
}

View File

@ -265,6 +265,90 @@ class Tests_Post_Nav_Menu extends WP_UnitTestCase {
$this->assertSameSets( array( $term_id ), $last[1], '_prime_term_caches() was not executed.' );
}
/**
* @ticket 55620
* @covers ::update_menu_item_cache
*/
public function test_wp_get_nav_menu_items_cache_primes_posts() {
$post_ids = self::factory()->post->create_many( 3 );
$menu_nav_ids = array();
foreach ( $post_ids as $post_id ) {
$menu_nav_ids[] = wp_update_nav_menu_item(
$this->menu_id,
0,
array(
'menu-item-type' => 'post_type',
'menu-item-object' => 'post',
'menu-item-object-id' => $post_id,
'menu-item-status' => 'publish',
)
);
}
// Delete post and post meta caches.
wp_cache_delete_multiple( $menu_nav_ids, 'posts' );
wp_cache_delete_multiple( $menu_nav_ids, 'post_meta' );
wp_cache_delete_multiple( $post_ids, 'posts' );
wp_cache_delete_multiple( $post_ids, 'post_meta' );
$action = new MockAction();
add_filter( 'update_post_metadata_cache', array( $action, 'filter' ), 10, 2 );
$start_num_queries = get_num_queries();
wp_get_nav_menu_items( $this->menu_id );
$queries_made = get_num_queries() - $start_num_queries;
$this->assertSame( 7, $queries_made, 'Only does 7 database queries when running wp_get_nav_menu_items.' );
$args = $action->get_args();
$this->assertSameSets( $menu_nav_ids, $args[0][1], '_prime_post_caches() was not executed.' );
$this->assertSameSets( $post_ids, $args[1][1], '_prime_post_caches() was not executed.' );
}
/**
* @ticket 55620
* @covers ::update_menu_item_cache
*/
public function test_wp_get_nav_menu_items_cache_primes_terms() {
register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
$term_ids = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
$menu_nav_ids = array();
foreach ( $term_ids as $term_id ) {
$menu_nav_ids[] = wp_update_nav_menu_item(
$this->menu_id,
0,
array(
'menu-item-type' => 'taxonomy',
'menu-item-object' => 'wptests_tax',
'menu-item-object-id' => $term_id,
'menu-item-status' => 'publish',
)
);
}
// Delete post and post meta caches.
wp_cache_delete_multiple( $menu_nav_ids, 'posts' );
wp_cache_delete_multiple( $menu_nav_ids, 'post_meta' );
// Delete term caches.
wp_cache_delete_multiple( $term_ids, 'terms' );
$action_terms = new MockAction();
add_filter( 'update_term_metadata_cache', array( $action_terms, 'filter' ), 10, 2 );
$action_posts = new MockAction();
add_filter( 'update_post_metadata_cache', array( $action_posts, 'filter' ), 10, 2 );
$start_num_queries = get_num_queries();
wp_get_nav_menu_items( $this->menu_id );
$queries_made = get_num_queries() - $start_num_queries;
$this->assertSame( 7, $queries_made, 'Only does 7 database queries when running wp_get_nav_menu_items.' );
$args = $action_terms->get_args();
$last = end( $args );
$this->assertSameSets( $term_ids, $last[1], '_prime_term_caches() was not executed.' );
$args = $action_posts->get_args();
$this->assertSameSets( $menu_nav_ids, $args[0][1], '_prime_post_caches() was not executed.' );
}
/**
* @ticket 13910
*/