Make WordPress Core


Ignore:
Timestamp:
06/15/2022 10:18:02 AM (3 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/tests/phpunit/tests/post/nav-menu.php

    r52389 r53504  
    204204
    205205    /**
     206     * @ticket 55620
     207     */
     208    public function test_update_menu_item_cache_primed_post() {
     209        $post_id = self::factory()->post->create();
     210        wp_update_nav_menu_item(
     211            $this->menu_id,
     212            0,
     213            array(
     214                'menu-item-type'      => 'post_type',
     215                'menu-item-object'    => 'post',
     216                'menu-item-object-id' => $post_id,
     217                'menu-item-status'    => 'publish',
     218            )
     219        );
     220
     221        $posts_query  = new WP_Query();
     222        $query_result = $posts_query->query( array( 'post_type' => 'nav_menu_item' ) );
     223
     224        wp_cache_delete( $post_id, 'posts' );
     225        $action = new MockAction();
     226        add_filter( 'update_post_metadata_cache', array( $action, 'filter' ), 10, 2 );
     227
     228        update_menu_item_cache( $query_result );
     229
     230        $args = $action->get_args();
     231        $last = end( $args );
     232        $this->assertEqualSets( array( $post_id ), $last[1], '_prime_post_caches was not executed.' );
     233    }
     234
     235    /**
     236     * @ticket 55620
     237     */
     238    public function test_update_menu_item_cache_primed_terms() {
     239        register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) );
     240        $term_id = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     241        wp_update_nav_menu_item(
     242            $this->menu_id,
     243            0,
     244            array(
     245                'menu-item-type'      => 'taxonomy',
     246                'menu-item-object'    => 'wptests_tax',
     247                'menu-item-object-id' => $term_id,
     248                'menu-item-status'    => 'publish',
     249            )
     250        );
     251
     252        $posts_query  = new WP_Query();
     253        $query_result = $posts_query->query( array( 'post_type' => 'nav_menu_item' ) );
     254
     255        wp_cache_delete( $term_id, 'terms' );
     256        $action = new MockAction();
     257        add_filter( 'update_term_metadata_cache', array( $action, 'filter' ), 10, 2 );
     258
     259        update_menu_item_cache( $query_result );
     260
     261        $args = $action->get_args();
     262        $last = end( $args );
     263        $this->assertEqualSets( array( $term_id ), $last[1], '_prime_term_caches was not executed.' );
     264    }
     265
     266    /**
    206267     * @ticket 13910
    207268     */
Note: See TracChangeset for help on using the changeset viewer.