Make WordPress Core


Ignore:
Timestamp:
06/15/2022 10:18:02 AM (4 years ago)
Author:
spacedmonkey
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/nav-menu.php

    r53455 r53504  
    692692        }
    693693
    694         static $fetched = array();
    695 
    696694        if ( ! taxonomy_exists( 'nav_menu' ) ) {
    697695                return false;
    … …  
    699697
    700698        $defaults = array(
    701                 'order'       => 'ASC',
    702                 'orderby'     => 'menu_order',
    703                 'post_type'   => 'nav_menu_item',
    704                 'post_status' => 'publish',
    705                 'output'      => ARRAY_A,
    706                 'output_key'  => 'menu_order',
    707                 'nopaging'    => true,
    708                 'tax_query'   => array(
     699                'order'                  => 'ASC',
     700                'orderby'                => 'menu_order',
     701                'post_type'              => 'nav_menu_item',
     702                'post_status'            => 'publish',
     703                'output'                 => ARRAY_A,
     704                'output_key'             => 'menu_order',
     705                'nopaging'               => true,
     706                'update_menu_item_cache' => true,
     707                'tax_query'              => array(
    709708                        array(
    710709                                'taxonomy' => 'nav_menu',
    … …  
    721720        }
    722721
    723         // Prime posts and terms caches.
    724         if ( empty( $fetched[ $menu->term_id ] ) ) {
    725                 $fetched[ $menu->term_id ] = true;
    726                 $post_ids                  = array();
    727                 $term_ids                  = array();
    728                 foreach ( $items as $item ) {
    729                         $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
    730                         $type      = get_post_meta( $item->ID, '_menu_item_type', true );
    731 
    732                         if ( 'post_type' === $type ) {
    733                                 $post_ids[] = (int) $object_id;
    734                         } elseif ( 'taxonomy' === $type ) {
    735                                 $term_ids[] = (int) $object_id;
    736                         }
    737                 }
    738 
    739                 if ( ! empty( $post_ids ) ) {
    740                         _prime_post_caches( $post_ids, false );
    741                 }
    742                 unset( $post_ids );
    743 
    744                 if ( ! empty( $term_ids ) ) {
    745                         _prime_term_caches( $term_ids );
    746                 }
    747                 unset( $term_ids );
    748         }
    749 
    750722        $items = array_map( 'wp_setup_nav_menu_item', $items );
    751723
    … …  
    779751         */
    780752        return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );
     753}
     754
     755/**
     756 * Prime all linked objects to menu items.
     757 *
     758 * @since 6.1.0
     759 *
     760 * @param WP_Post[] $menu_items Array post objects of menu items.
     761 */
     762function update_menu_item_cache( $menu_items ) {
     763        $post_ids = array();
     764        $term_ids = array();
     765
     766        foreach ( $menu_items as $menu_item ) {
     767                if ( 'nav_menu_item' !== $menu_item->post_type ) {
     768                        continue;
     769                }
     770                $object_id = get_post_meta( $menu_item->ID, '_menu_item_object_id', true );
     771                $type      = get_post_meta( $menu_item->ID, '_menu_item_type', true );
     772
     773                if ( 'post_type' === $type ) {
     774                        $post_ids[] = (int) $object_id;
     775                } elseif ( 'taxonomy' === $type ) {
     776                        $term_ids[] = (int) $object_id;
     777                }
     778        }
     779
     780        if ( ! empty( $post_ids ) ) {
     781                _prime_post_caches( $post_ids, false );
     782        }
     783
     784        if ( ! empty( $term_ids ) ) {
     785                _prime_term_caches( $term_ids );
     786        }
    781787}
    782788
Note: See TracChangeset for help on using the changeset viewer.