REST API: Prime caches for linked objects in menu item REST API controller.

Add a new parameter to `WP_Query` called `update_menu_item_cache` that when set to true, primes the caches for linked terms and posts for menu item post objects. This change moves logic 
found in `wp_get_nav_menu_items` into a new function called `update_menu_item_cache`.  Update the menu item REST API controller, to pass the `update_menu_item_cache` parameter to the 
arguments used for the  `WP_Query` run to get menu items. 

Props furi3r,  TimothyBlynJacobs, spacedmonkey, peterwilsoncc, mitogh.
Fixes #55620.

 --This line, and those below, will be ignored--

M    src/wp-includes/class-wp-query.php
M    src/wp-includes/nav-menu.php
M    src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php
M    tests/phpunit/tests/post/nav-menu.php


git-svn-id: https://develop.svn.wordpress.org/trunk@53504 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris
2022-06-15 10:18:02 +00:00
parent f3441e37ed
commit aef77be627
4 changed files with 115 additions and 37 deletions

View File

@@ -202,6 +202,67 @@ class Tests_Post_Nav_Menu extends WP_UnitTestCase {
$this->assertEquals( $t, $menu_items[0]->object_id );
}
/**
* @ticket 55620
*/
public function test_update_menu_item_cache_primed_post() {
$post_id = self::factory()->post->create();
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',
)
);
$posts_query = new WP_Query();
$query_result = $posts_query->query( array( 'post_type' => 'nav_menu_item' ) );
wp_cache_delete( $post_id, 'posts' );
$action = new MockAction();
add_filter( 'update_post_metadata_cache', array( $action, 'filter' ), 10, 2 );
update_menu_item_cache( $query_result );
$args = $action->get_args();
$last = end( $args );
$this->assertEqualSets( array( $post_id ), $last[1], '_prime_post_caches was not executed.' );
}
/**
* @ticket 55620
*/
public function test_update_menu_item_cache_primed_terms() {
register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
$term_id = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
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',
)
);
$posts_query = new WP_Query();
$query_result = $posts_query->query( array( 'post_type' => 'nav_menu_item' ) );
wp_cache_delete( $term_id, 'terms' );
$action = new MockAction();
add_filter( 'update_term_metadata_cache', array( $action, 'filter' ), 10, 2 );
update_menu_item_cache( $query_result );
$args = $action->get_args();
$last = end( $args );
$this->assertEqualSets( array( $term_id ), $last[1], '_prime_term_caches was not executed.' );
}
/**
* @ticket 13910
*/