Changeset 788799
- Timestamp:
- 10/16/2013 02:16:53 PM (12 years ago)
- Location:
- voce-cached-nav/trunk
- Files:
-
- 3 edited
-
. (modified) (1 prop)
-
readme.txt (modified) (3 diffs)
-
voce-cached-nav.php (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
voce-cached-nav/trunk
-
Property
svn:ignore
set to
wpsvn-deploy
README.md
test
tests
.git
.gitignore
-
Property
svn:ignore
set to
-
voce-cached-nav/trunk/readme.txt
r631827 r788799 1 === Plugin Name===2 Contributors: markparolisi, voceplatforms 3 Tags: nav menus1 === Voce Cached Nav === 2 Contributors: markparolisi, voceplatforms, nattyait 3 Tags: nav, menus, cache, caching, performance 4 4 Requires at least: 3.3 5 Tested up to: 3. 46 Stable tag: 1. 05 Tested up to: 3.6 6 Stable tag: 1.1 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 12 12 == Description == 13 13 14 Replace your template calls to `wp_nav_menu` with ` wp_cached_nav_menu` to retreive cached copies of menu objects.14 Replace your template calls to `wp_nav_menu` with `voce_cached_nav_menu` to retreive cached copies of menu objects. 15 15 16 16 == Installation == 17 17 18 18 1. Upload `voce-cached-nav` to the `/wp-content/plugins/` directory 19 1. Activate the plugin through the 'Plugins' menu in WordPress20 1. Replace calls to `wp_nav_menu` with `wp_cached_nav_menu` in your templates19 2. Activate the plugin through the 'Plugins' menu in WordPress 20 3. Replace calls to `wp_nav_menu` with `voce_cached_nav_menu` in your templates 21 21 22 22 == Frequently Asked Questions == 23 23 24 = Can I pass the same arguments to wp_cached_nav_menu? =24 = Can I pass the same arguments to voce_cached_nav_menu? = 25 25 26 26 Yes. The caching class is essentially just a refactor of that large core function with caching at all possible levels. … … 30 30 == Changelog == 31 31 32 = 1.1 = 33 * 3.6 compatibility 34 32 35 = 1.0 = 33 36 * Initial release. -
voce-cached-nav/trunk/voce-cached-nav.php
r631827 r788799 4 4 Plugin URI: http://voceconnect.com 5 5 Description: Serve cached WordPress Navigation Objects. 6 Version: 1. 06 Version: 1.1 7 7 Author: Mark Parolisi 8 8 License: GPL2 … … 20 20 const ITEMSPREFIX = 'wp_nav_items-'; 21 21 const MENUIDS = 'wp_nav_menus'; 22 22 23 23 /** 24 24 * Set the action hooks to update the cache … … 35 35 /** 36 36 * @method action_wp_update_nav_menu 37 * @param Integer $menu_id 37 * @param Integer $menu_id 38 38 */ 39 39 public static function action_wp_update_nav_menu( $menu_id ) { … … 43 43 /** 44 44 * @method action_wp_delete_nav_menu 45 * @param Integer $menu_id 45 * @param Integer $menu_id 46 46 */ 47 47 public static function action_wp_delete_nav_menu( $menu_id ) { … … 54 54 * @method action_save_post 55 55 */ 56 public static function action_save_post( ) {56 public static function action_save_post() { 57 57 // Passing 0 will ensure that all caches are deleted. 58 self::get_nav_menus(); 58 59 self::delete_menu_objects_cache( 0 ); 60 } 61 62 public static function get_nav_menus() { 63 $menus = get_transient( self::MENUIDS ); 64 if ( !is_array( $menus ) ) { 65 $menus = wp_get_nav_menus(); 66 foreach ( $menus as $menu ) { 67 self::update_menu_ids_cache( $menu->term_id ); 68 } 69 } 70 return $menus; 59 71 } 60 72 … … 62 74 * @method delete_menu_objects_cache 63 75 * @param Integer $menu_id 64 * @return type 76 * 77 * @return type 65 78 */ 66 79 public static function delete_menu_objects_cache( $menu_id ) { 67 80 //if given an existing menu_id delete just that menu 68 if ( term_exists( $menu_id, 'nav_menu' ) ) {81 if ( term_exists( (int) $menu_id, 'nav_menu' ) ) { 69 82 return delete_transient( self::ITEMSPREFIX . $menu_id ); 70 83 } else { //delete all cached menus recursively 71 84 $all_cached_menus = get_transient( self::MENUIDS ); 72 85 if ( is_array( $all_cached_menus ) ) { 73 foreach ( $all_cached_menus as $menu_id) {86 foreach ( $all_cached_menus as $menu_id ) { 74 87 self::delete_menu_objects_cache( $menu_id ); 75 88 } … … 80 93 /** 81 94 * @method update_menu_ids_cache 82 * @param Integer $menu_id 95 * @param Integer $menu_id 83 96 */ 84 97 public static function update_menu_ids_cache( $menu_id ) { … … 87 100 if ( is_array( $cache ) ) { 88 101 // If the menu ID is not already in cache and is a valid menu 89 if ( !in_array( $menu_id, $cache ) && term_exists( $menu_id, 'nav_menu' ) ) {102 if ( !in_array( $menu_id, $cache ) && term_exists( (int) $menu_id, 'nav_menu' ) ) { 90 103 $cache = array_merge( $cache, array( $menu_id ) ); 91 104 } 92 foreach ( $cache as $key => $cached_id) {105 foreach ( $cache as $key => $cached_id ) { 93 106 // Remove the menu ID if it's invalid 94 if ( !term_exists( $cached_id, 'nav_menu' ) ) {95 unset( $cache[ $key] );107 if ( !term_exists( (int) $cached_id, 'nav_menu' ) ) { 108 unset( $cache[ $key ] ); 96 109 } 97 110 } … … 99 112 // If this is executing for the first time 100 113 } else { 101 if ( term_exists( $menu_id, 'nav_menu' ) ) {114 if ( term_exists( (int) $menu_id, 'nav_menu' ) ) { 102 115 $data = array( $menu_id ); 103 116 } 104 117 } 105 118 set_transient( self::MENUIDS, $data ); 106 }107 108 /**109 * @method parse_args110 * @param Array $args111 * @return Object Filtered args112 */113 public static function parse_args( $args ) {114 $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',115 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',116 'depth' => 0, 'walker' => '', 'theme_location' => '' );117 118 $args = wp_parse_args( $args, $defaults );119 $args = apply_filters( 'wp_nav_menu_args', $args );120 return (object) $args;121 119 } 122 120 … … 124 122 * @method get_nav_menu_object 125 123 * @param Object $args 126 * @return type 124 * 125 * @return type 127 126 */ 128 127 public static function get_nav_menu_object( $args ) { 129 if ( empty( $args->menu ) ) { 130 $locations = get_nav_menu_locations(); 131 if ( empty( $locations ) ) { 132 return false; 133 } 134 $menu_lookup = $locations[$args->theme_location]; 135 } else { 136 $menu_lookup = $args->menu; 137 } 138 if ( $cache = get_transient( self::MENUPREFIX . $menu_lookup ) ) { 139 $menu = $cache; 140 } else { 141 $menu = wp_get_nav_menu_object( $menu_lookup ); 128 $menu_lookup = $args->menu; 129 $menu = get_transient( self::MENUPREFIX . $menu_lookup ); 130 if ( empty( $menu ) ) { 131 $menu = wp_get_nav_menu_object( $args->menu ); 142 132 set_transient( self::MENUPREFIX . $menu_lookup, $menu ); 133 } 134 135 // Get the nav menu based on the theme_location 136 if ( !$menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) ) { 137 $menu_lookup = $locations[ $args->theme_location ]; 138 $menu = get_transient( self::MENUPREFIX . $menu_lookup ); 139 if ( empty( $menu ) ) { 140 $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] ); 141 set_transient( self::MENUPREFIX . $menu_lookup, $menu ); 142 } 143 } 144 145 // get the first menu that has items if we still can't find a menu 146 if ( !$menu && !$args->theme_location ) { 147 $menus = self::get_nav_menus(); 148 foreach ( $menus as $menu_maybe ) { 149 if ( $menu_items = self::get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ) ) { 150 $menu = $menu_maybe; 151 break; 152 } 153 } 143 154 } 144 155 … … 149 160 * @method get_nav_menu_items 150 161 * @param Integer $term_id 151 * @return type 152 */ 153 public static function get_nav_menu_items( $term_id ) { 162 * 163 * @return type 164 */ 165 public static function get_nav_menu_items( $term_id, $args ) { 154 166 if ( $cache = get_transient( self::ITEMSPREFIX . $term_id ) ) { 155 167 $items = $cache; 156 168 } else { 157 $items = wp_get_nav_menu_items( $term_id );169 $items = wp_get_nav_menu_items( $term_id, $args ); 158 170 set_transient( self::ITEMSPREFIX . $term_id, $items ); 159 171 } … … 164 176 * @method menu 165 177 * @staticvar array $menu_id_slugs 166 * @param {Array} $args 167 * @return boolean 168 */ 169 public static function menu( $args = array( ) ) { 170 171 $args = self::parse_args( $args ); 172 178 * 179 * @param {Array} $args 180 * 181 * @return boolean 182 */ 183 public static function menu( $args = array() ) { 184 static $menu_id_slugs = array(); 185 186 $defaults = array( 187 'menu' => '', 188 'container' => 'div', 189 'container_class' => '', 190 'container_id' => '', 191 'menu_class' => 'menu', 192 'menu_id' => '', 193 'echo' => true, 194 'fallback_cb' => 'wp_page_menu', 195 'before' => '', 196 'after' => '', 197 'link_before' => '', 198 'link_after' => '', 199 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>', 200 'depth' => 0, 201 'walker' => '', 202 'theme_location' => '' 203 ); 204 205 $args = wp_parse_args( $args, $defaults ); 206 $args = apply_filters( 'wp_nav_menu_args', $args ); 207 $args = (object) $args; 208 209 // Get the nav menu based on the requested menu 210 // move get menu part to self::get_nav_menu_object function 211 // to manage cache 173 212 $menu = self::get_nav_menu_object( $args ); 174 213 175 214 // If the menu exists, get its items. 176 if ( $menu && !is_wp_error( $menu ) && !isset( $menu_items ) && property_exists( $menu, 'term_id' ) ) { 177 $menu_items = self::get_nav_menu_items( $menu->term_id ); 178 } 179 180 // If no menu was found or if the menu has no items and no location was requested, call the fallback_cb if it exists 181 if ( (!$menu || is_wp_error( $menu ) || ( isset( $menu_items ) 182 && empty( $menu_items ) && !$args->theme_location ) ) 183 && $args->fallback_cb && is_callable( $args->fallback_cb ) ) { 184 return call_user_func( $args->fallback_cb, (array) $args ); 185 } 186 187 // If no fallback function was specified and the menu doesn't exists, bail. 188 if ( !$menu || is_wp_error( $menu ) ) { 189 return false; 190 } 191 192 static $menu_id_slugs = array( ); 215 if ( $menu && !is_wp_error( $menu ) && !isset( $menu_items ) ) //replace wp_get_nav_menu_items with self::get_nav_menu_items to manage cache 216 { 217 $menu_items = self::get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) ); 218 } 219 220 /* 221 * If no menu was found: 222 * - Fall back (if one was specified), or bail. 223 * 224 * If no menu items were found: 225 * - Fall back, but only if no theme location was specified. 226 * - Otherwise, bail. 227 */ 228 if ( ( !$menu || is_wp_error( $menu ) || ( isset( $menu_items ) && empty( $menu_items ) && !$args->theme_location ) ) && $args->fallback_cb && is_callable( $args->fallback_cb ) ) return call_user_func( $args->fallback_cb, (array) $args ); 229 230 if ( !$menu || is_wp_error( $menu ) ) return false; 193 231 194 232 $nav_menu = $items = ''; 195 233 196 // Set up the $menu_item variables197 _wp_menu_item_classes_by_context( $menu_items );198 199 $sorted_menu_items = array( );200 foreach ((array) $menu_items as $key => $menu_item) {201 $sorted_menu_items[$menu_item->menu_order] = $menu_item;202 }203 unset( $menu_items );204 205 $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );206 207 234 $show_container = false; 208 209 235 if ( $args->container ) { 210 236 $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) ); … … 216 242 } 217 243 } 244 245 // Set up the $menu_item variables 246 _wp_menu_item_classes_by_context( $menu_items ); 247 248 $sorted_menu_items = array(); 249 foreach ( (array) $menu_items as $key => $menu_item ) $sorted_menu_items[ $menu_item->menu_order ] = $menu_item; 250 251 unset( $menu_items ); 252 253 $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args ); 254 218 255 $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args ); 219 256 unset( $sorted_menu_items ); … … 225 262 $wrap_id = 'menu-' . $menu->slug; 226 263 while ( in_array( $wrap_id, $menu_id_slugs ) ) { 227 if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) ) { 228 $wrap_id = preg_replace( '#-(\d+)$#', '-' . ++$matches[1], $wrap_id ); 229 } else { 264 if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) ) $wrap_id = preg_replace( '#-(\d+)$#', '-' . ++$matches[ 1 ], $wrap_id ); else 230 265 $wrap_id = $wrap_id . '-1'; 231 } 232 } 233 } 234 $menu_id_slugs[] = $wrap_id; 266 } 267 } 268 $menu_id_slugs[ ] = $wrap_id; 235 269 236 270 $wrap_class = $args->menu_class ? $args->menu_class : ''; … … 240 274 $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args ); 241 275 276 // Don't print any markup if there are no items at this point. 277 if ( empty( $items ) ) return false; 278 242 279 $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items ); 243 280 unset( $items ); 244 281 245 if ( $show_container ) { 246 $nav_menu .= '</' . $args->container . '>'; 247 } 282 if ( $show_container ) $nav_menu .= '</' . $args->container . '>'; 248 283 249 284 $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args ); 250 285 251 if ( $args->echo ) { 252 echo $nav_menu; 253 } else { 286 if ( $args->echo ) echo $nav_menu; else 254 287 return $nav_menu; 255 }256 288 } 257 289 … … 260 292 Voce_Cached_Nav::init(); 261 293 262 /** 263 * Just a template tag 264 * @method wp_cached_nav_menu 265 * @param Array $args 266 */ 267 function wp_cached_nav_menu( $args ) { 268 Voce_Cached_Nav::menu( $args ); 294 if ( !function_exists( 'wp_cached_nav_menu' ) ) { 295 function wp_cached_nav_menu( $args ) { 296 voce_cached_nav_menu( $args ); 297 } 269 298 } 270 299 300 function voce_cached_nav_menu( $args ) { 301 return Voce_Cached_Nav::menu( $args ); 302 303 } 304 271 305 }
Note: See TracChangeset
for help on using the changeset viewer.