Changeset 53504
- Timestamp:
- 06/15/2022 10:18:02 AM (2 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-query.php
r53483 r53504 757 757 * @type bool $update_post_meta_cache Whether to update the post meta cache. Default true. 758 758 * @type bool $update_post_term_cache Whether to update the post term cache. Default true. 759 * @type bool $update_menu_item_cache Whether to update the menu item cache. Default false. 759 760 * @type bool $lazy_load_term_meta Whether to lazy-load term meta. Setting to false will 760 761 * disable cache priming for term meta, so that each … … 1872 1873 } 1873 1874 1875 if ( ! isset( $q['update_menu_item_cache'] ) ) { 1876 $q['update_menu_item_cache'] = false; 1877 } 1878 1874 1879 if ( ! isset( $q['lazy_load_term_meta'] ) ) { 1875 1880 $q['lazy_load_term_meta'] = $q['update_post_term_cache']; … … 3146 3151 /** @var WP_Post[] */ 3147 3152 $this->posts = array_map( 'get_post', $this->posts ); 3153 } 3154 3155 if ( ! empty( $this->posts ) && $q['update_menu_item_cache'] ) { 3156 update_menu_item_cache( $this->posts ); 3148 3157 } 3149 3158 -
trunk/src/wp-includes/nav-menu.php
r53455 r53504 692 692 } 693 693 694 static $fetched = array();695 696 694 if ( ! taxonomy_exists( 'nav_menu' ) ) { 697 695 return false; … … 699 697 700 698 $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( 709 708 array( 710 709 'taxonomy' => 'nav_menu', … … 721 720 } 722 721 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 750 722 $items = array_map( 'wp_setup_nav_menu_item', $items ); 751 723 … … 779 751 */ 780 752 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 */ 762 function 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 } 781 787 } 782 788 -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php
r53455 r53504 999 999 } 1000 1000 1001 $query_args['update_menu_item_cache'] = true; 1002 1001 1003 return $query_args; 1002 1004 } -
trunk/tests/phpunit/tests/post/nav-menu.php
r52389 r53504 204 204 205 205 /** 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 /** 206 267 * @ticket 13910 207 268 */
Note: See TracChangeset
for help on using the changeset viewer.