Created
September 9, 2011 17:30
-
-
Save tott/1206819 to your computer and use it in GitHub Desktop.
cache nav menus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Wrapper function around wp_nav_menu() that will cache the wp_nav_menu for all tag/category | |
* pages used in the nav menus | |
*/ | |
function cached_nav_menu( $args = array(), $prime_cache = true ) { | |
global $wp_query; | |
$queried_object_id = empty( $wp_query->queried_object_id ) ? 0 : (int) $wp_query->queried_object_id; | |
$nav_menu_key = md5( serialize( $args ) . '-' . $queried_object_id ); | |
$my_args = wp_parse_args( $args ); | |
$my_args = apply_filters( 'wp_nav_menu_args', $my_args ); | |
$my_args = (object) $my_args; | |
if ( ( isset( $my_args->echo ) && true === $my_args->echo ) || !isset( $my_args->echo ) ) { | |
$echo = true; | |
} else { | |
$echo = false; | |
} | |
$skip_cache = false; | |
$use_cache = ( true === $prime_cache ) ? false : true; | |
if ( is_singular() ) { | |
$skip_cache = true; | |
} else if ( !in_array( $queried_object_id, get_nav_menu_cache_objects( $use_cache ) ) ) { | |
$skip_cache = true; | |
} | |
if ( true === $skip_cache || true === $prime_cache || false === ( $nav_menu = get_transient( $nav_menu_key ) ) ) { | |
if ( false === $echo ) { | |
$nav_menu = wp_nav_menu( $args ); | |
} else { | |
ob_start(); | |
wp_nav_menu( $args ); | |
$nav_menu = ob_get_clean(); | |
if ( $args['menu_class'] == 'hot-topics' ) | |
dbgx_trace_var( $nav_menu ); | |
} | |
if ( false === $skip_cache ) | |
set_transient( $nav_menu_key, $nav_menu ); | |
} | |
if ( true === $echo ) | |
echo $nav_menu; | |
else | |
return $nav_menu; | |
} | |
add_action( 'wp_update_nav_menu', 'update_nav_menu_objects' ); | |
function update_nav_menu_objects( $menu_id = null, $menu_data = null ) { | |
cached_nav_menu( array( 'echo' => false ), $prime_cache = true ); | |
} | |
function get_nav_menu_cache_objects( $use_cache = true ) { | |
$object_ids = get_transient( 'nav_menu_cache_object_ids' ); | |
if ( true === $use_cache && !empty( $object_ids ) ) { | |
return $object_ids; | |
} | |
$object_ids = $objects = array(); | |
$menus = wp_get_nav_menus(); | |
foreach ( $menus as $menu_maybe ) { | |
if ( $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id ) ) { | |
foreach( $menu_items as $menu_item ) { | |
if ( preg_match( "#.*/category/([^/]+)/?$#", $menu_item->url, $match ) ) | |
$objects['category'][] = $match[1]; | |
if ( preg_match( "#.*/tag/([^/]+)/?$#", $menu_item->url, $match ) ) | |
$objects['post_tag'][] = $match[1]; | |
} | |
} | |
} | |
if ( !empty( $objects ) ) { | |
foreach( $objects as $taxonomy => $term_names ) { | |
foreach( $term_names as $term_name ) { | |
$term = get_term_by( 'slug', $term_name, $taxonomy ); | |
if ( $term ) | |
$object_ids[] = $term->term_id; | |
} | |
} | |
} | |
$object_ids[] = 0; // that's for the homepage | |
set_transient( 'nav_menu_cache_object_ids', $object_ids ); | |
return $object_ids; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment