WordPress.org

Make WordPress Core

Changeset 14560


Ignore:
Timestamp:
05/11/10 15:55:17 (5 years ago)
Author:
ryan
Message:

Don't prime menu item cache if a persistent cache is being used. Prime the menu item cache only on the first call to wp_get_nav_menu_items(). Avoids unneeded queries when wp_get_nav_menu_items() is called repeatedly. see #12734

File:
1 edited

Legend:

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

    r14557 r14560  
    406406 */ 
    407407function wp_get_nav_menu_items( $menu, $args = array() ) { 
     408    global $_wp_using_ext_object_cache; 
     409 
    408410    $menu = wp_get_nav_menu_object( $menu ); 
    409411 
    410412    if ( ! $menu ) 
    411413        return false; 
     414 
     415    static $fetched = array(); 
    412416 
    413417    $items = get_objects_in_term( $menu->term_id, 'nav_menu' ); 
     
    430434 
    431435    // Get all posts and terms at once to prime the caches 
    432     $posts = array(); 
    433     $terms = array(); 
    434     foreach ( $items as $item ) { 
    435         $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true ); 
    436         $object = get_post_meta( $item->ID, '_menu_item_object', true ); 
    437         $type = get_post_meta( $item->ID, '_menu_item_type', true ); 
    438  
    439         if ( 'post_type' == $type ) 
    440             $posts[$object][] = $object_id; 
    441         elseif ( 'taxonomy' == $type) 
    442             $terms[$object][] = $object_id; 
    443     } 
    444  
    445     if ( !empty($posts) ) { 
    446         foreach ( array_keys($posts) as $post_type ) { 
    447             get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) ); 
    448         } 
    449     } 
    450     unset($posts); 
    451  
    452     if ( !empty($terms) ) { 
    453         foreach ( array_keys($terms) as $taxonomy ) { 
    454             get_terms($taxonomy, array('include' => $terms[$taxonomy]) ); 
    455         } 
    456     } 
    457     unset($terms); 
     436    if ( empty($fetched[$menu->term_id]) || $_wp_using_ext_object_cache ) { 
     437        $fetched[$menu->term_id] = true; 
     438        $posts = array(); 
     439        $terms = array(); 
     440        foreach ( $items as $item ) { 
     441            $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true ); 
     442            $object = get_post_meta( $item->ID, '_menu_item_object', true ); 
     443            $type = get_post_meta( $item->ID, '_menu_item_type', true ); 
     444     
     445            if ( 'post_type' == $type ) 
     446                $posts[$object][] = $object_id; 
     447            elseif ( 'taxonomy' == $type) 
     448                $terms[$object][] = $object_id; 
     449        } 
     450     
     451        if ( !empty($posts) ) { 
     452            foreach ( array_keys($posts) as $post_type ) { 
     453                get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) ); 
     454            } 
     455        } 
     456        unset($posts); 
     457     
     458        if ( !empty($terms) ) { 
     459            foreach ( array_keys($terms) as $taxonomy ) { 
     460                get_terms($taxonomy, array('include' => $terms[$taxonomy]) ); 
     461            } 
     462        } 
     463        unset($terms); 
     464    } 
    458465 
    459466    $items = array_map( 'wp_setup_nav_menu_item', $items ); 
Note: See TracChangeset for help on using the changeset viewer.