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 Built from https://develop.svn.wordpress.org/trunk@53504 git-svn-id: http://core.svn.wordpress.org/trunk@53093 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
73a2d87eae
commit
991acc48d4
|
@ -756,6 +756,7 @@ class WP_Query {
|
|||
* @type string $title Post title.
|
||||
* @type bool $update_post_meta_cache Whether to update the post meta cache. Default true.
|
||||
* @type bool $update_post_term_cache Whether to update the post term cache. Default true.
|
||||
* @type bool $update_menu_item_cache Whether to update the menu item cache. Default false.
|
||||
* @type bool $lazy_load_term_meta Whether to lazy-load term meta. Setting to false will
|
||||
* disable cache priming for term meta, so that each
|
||||
* get_term_meta() call will hit the database.
|
||||
|
@ -1871,6 +1872,10 @@ class WP_Query {
|
|||
$q['update_post_term_cache'] = true;
|
||||
}
|
||||
|
||||
if ( ! isset( $q['update_menu_item_cache'] ) ) {
|
||||
$q['update_menu_item_cache'] = false;
|
||||
}
|
||||
|
||||
if ( ! isset( $q['lazy_load_term_meta'] ) ) {
|
||||
$q['lazy_load_term_meta'] = $q['update_post_term_cache'];
|
||||
}
|
||||
|
@ -3147,6 +3152,10 @@ class WP_Query {
|
|||
$this->posts = array_map( 'get_post', $this->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.
|
||||
|
|
|
@ -691,21 +691,20 @@ function wp_get_nav_menu_items( $menu, $args = array() ) {
|
|||
return false;
|
||||
}
|
||||
|
||||
static $fetched = array();
|
||||
|
||||
if ( ! taxonomy_exists( 'nav_menu' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$defaults = array(
|
||||
'order' => 'ASC',
|
||||
'orderby' => 'menu_order',
|
||||
'post_type' => 'nav_menu_item',
|
||||
'post_status' => 'publish',
|
||||
'output' => ARRAY_A,
|
||||
'output_key' => 'menu_order',
|
||||
'nopaging' => true,
|
||||
'tax_query' => array(
|
||||
'order' => 'ASC',
|
||||
'orderby' => 'menu_order',
|
||||
'post_type' => 'nav_menu_item',
|
||||
'post_status' => 'publish',
|
||||
'output' => ARRAY_A,
|
||||
'output_key' => 'menu_order',
|
||||
'nopaging' => true,
|
||||
'update_menu_item_cache' => true,
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => 'nav_menu',
|
||||
'field' => 'term_taxonomy_id',
|
||||
|
@ -720,33 +719,6 @@ function wp_get_nav_menu_items( $menu, $args = array() ) {
|
|||
$items = array();
|
||||
}
|
||||
|
||||
// Prime posts and terms caches.
|
||||
if ( empty( $fetched[ $menu->term_id ] ) ) {
|
||||
$fetched[ $menu->term_id ] = true;
|
||||
$post_ids = array();
|
||||
$term_ids = array();
|
||||
foreach ( $items as $item ) {
|
||||
$object_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
|
||||
$type = get_post_meta( $item->ID, '_menu_item_type', true );
|
||||
|
||||
if ( 'post_type' === $type ) {
|
||||
$post_ids[] = (int) $object_id;
|
||||
} elseif ( 'taxonomy' === $type ) {
|
||||
$term_ids[] = (int) $object_id;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $post_ids ) ) {
|
||||
_prime_post_caches( $post_ids, false );
|
||||
}
|
||||
unset( $post_ids );
|
||||
|
||||
if ( ! empty( $term_ids ) ) {
|
||||
_prime_term_caches( $term_ids );
|
||||
}
|
||||
unset( $term_ids );
|
||||
}
|
||||
|
||||
$items = array_map( 'wp_setup_nav_menu_item', $items );
|
||||
|
||||
if ( ! is_admin() ) { // Remove invalid items only on front end.
|
||||
|
@ -780,6 +752,40 @@ function wp_get_nav_menu_items( $menu, $args = array() ) {
|
|||
return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prime all linked objects to menu items.
|
||||
*
|
||||
* @since 6.1.0
|
||||
*
|
||||
* @param WP_Post[] $menu_items Array post objects of menu items.
|
||||
*/
|
||||
function update_menu_item_cache( $menu_items ) {
|
||||
$post_ids = array();
|
||||
$term_ids = array();
|
||||
|
||||
foreach ( $menu_items as $menu_item ) {
|
||||
if ( 'nav_menu_item' !== $menu_item->post_type ) {
|
||||
continue;
|
||||
}
|
||||
$object_id = get_post_meta( $menu_item->ID, '_menu_item_object_id', true );
|
||||
$type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
|
||||
|
||||
if ( 'post_type' === $type ) {
|
||||
$post_ids[] = (int) $object_id;
|
||||
} elseif ( 'taxonomy' === $type ) {
|
||||
$term_ids[] = (int) $object_id;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $post_ids ) ) {
|
||||
_prime_post_caches( $post_ids, false );
|
||||
}
|
||||
|
||||
if ( ! empty( $term_ids ) ) {
|
||||
_prime_term_caches( $term_ids );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decorates a menu item object with the shared navigation menu item properties.
|
||||
*
|
||||
|
|
|
@ -998,6 +998,8 @@ class WP_REST_Menu_Items_Controller extends WP_REST_Posts_Controller {
|
|||
}
|
||||
}
|
||||
|
||||
$query_args['update_menu_item_cache'] = true;
|
||||
|
||||
return $query_args;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.1-alpha-53503';
|
||||
$wp_version = '6.1-alpha-53504';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue