Plugin Directory

Changeset 788799


Ignore:
Timestamp:
10/16/2013 02:16:53 PM (12 years ago)
Author:
markparolisi
Message:

3.6 updates

Location:
voce-cached-nav/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • voce-cached-nav/trunk

    • Property svn:ignore set to
      wpsvn-deploy
      README.md
      test
      tests
      .git
      .gitignore
  • voce-cached-nav/trunk/readme.txt

    r631827 r788799  
    1 === Plugin Name ===
    2 Contributors: markparolisi, voceplatforms
    3 Tags: nav menus
     1=== Voce Cached Nav ===
     2Contributors: markparolisi, voceplatforms, nattyait
     3Tags: nav, menus, cache, caching, performance
    44Requires at least: 3.3
    5 Tested up to: 3.4
    6 Stable tag: 1.0
     5Tested up to: 3.6
     6Stable tag: 1.1
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1212== Description ==
    1313
    14 Replace your template calls to `wp_nav_menu` with `wp_cached_nav_menu` to retreive cached copies of menu objects.
     14Replace your template calls to `wp_nav_menu` with `voce_cached_nav_menu` to retreive cached copies of menu objects.
    1515
    1616== Installation ==
    1717
    18181. Upload `voce-cached-nav` to the `/wp-content/plugins/` directory
    19 1. Activate the plugin through the 'Plugins' menu in WordPress
    20 1. Replace calls to `wp_nav_menu` with `wp_cached_nav_menu` in your templates
     192. Activate the plugin through the 'Plugins' menu in WordPress
     203. Replace calls to `wp_nav_menu` with `voce_cached_nav_menu` in your templates
    2121
    2222== Frequently Asked Questions ==
    2323
    24 = Can I pass the same arguments to wp_cached_nav_menu? =
     24= Can I pass the same arguments to voce_cached_nav_menu? =
    2525
    2626Yes. The caching class is essentially just a refactor of that large core function with caching at all possible levels.
     
    3030== Changelog ==
    3131
     32= 1.1 =
     33* 3.6 compatibility
     34
    3235= 1.0 =
    3336* Initial release.
  • voce-cached-nav/trunk/voce-cached-nav.php

    r631827 r788799  
    44  Plugin URI: http://voceconnect.com
    55  Description: Serve cached WordPress Navigation Objects.
    6   Version: 1.0
     6  Version: 1.1
    77  Author: Mark Parolisi
    88  License: GPL2
     
    2020        const ITEMSPREFIX = 'wp_nav_items-';
    2121        const MENUIDS = 'wp_nav_menus';
    22        
     22
    2323        /**
    2424         * Set the action hooks to update the cache
     
    3535        /**
    3636         * @method action_wp_update_nav_menu
    37          * @param Integer $menu_id 
     37         * @param Integer $menu_id
    3838         */
    3939        public static function action_wp_update_nav_menu( $menu_id ) {
     
    4343        /**
    4444         * @method action_wp_delete_nav_menu
    45          * @param Integer $menu_id 
     45         * @param Integer $menu_id
    4646         */
    4747        public static function action_wp_delete_nav_menu( $menu_id ) {
     
    5454         * @method action_save_post
    5555         */
    56         public static function action_save_post( ) {
     56        public static function action_save_post() {
    5757            // Passing 0 will ensure that all caches are deleted.
     58            self::get_nav_menus();
    5859            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;
    5971        }
    6072
     
    6274         * @method delete_menu_objects_cache
    6375         * @param Integer $menu_id
    64          * @return type
     76         *
     77         * @return type
    6578         */
    6679        public static function delete_menu_objects_cache( $menu_id ) {
    6780            //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' ) ) {
    6982                return delete_transient( self::ITEMSPREFIX . $menu_id );
    7083            } else { //delete all cached menus recursively
    7184                $all_cached_menus = get_transient( self::MENUIDS );
    7285                if ( is_array( $all_cached_menus ) ) {
    73                     foreach ($all_cached_menus as $menu_id) {
     86                    foreach ( $all_cached_menus as $menu_id ) {
    7487                        self::delete_menu_objects_cache( $menu_id );
    7588                    }
     
    8093        /**
    8194         * @method update_menu_ids_cache
    82          * @param Integer $menu_id 
     95         * @param Integer $menu_id
    8396         */
    8497        public static function update_menu_ids_cache( $menu_id ) {
     
    87100            if ( is_array( $cache ) ) {
    88101                // 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' ) ) {
    90103                    $cache = array_merge( $cache, array( $menu_id ) );
    91104                }
    92                 foreach ($cache as $key => $cached_id) {
     105                foreach ( $cache as $key => $cached_id ) {
    93106                    // 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 ] );
    96109                    }
    97110                }
     
    99112                // If this is executing for the first time
    100113            } else {
    101                 if ( term_exists( $menu_id, 'nav_menu' ) ) {
     114                if ( term_exists( (int) $menu_id, 'nav_menu' ) ) {
    102115                    $data = array( $menu_id );
    103116                }
    104117            }
    105118            set_transient( self::MENUIDS, $data );
    106         }
    107 
    108         /**
    109          * @method parse_args
    110          * @param Array $args
    111          * @return Object Filtered args
    112          */
    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;
    121119        }
    122120
     
    124122         * @method get_nav_menu_object
    125123         * @param Object $args
    126          * @return type
     124         *
     125         * @return type
    127126         */
    128127        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 );
    142132                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                }
    143154            }
    144155
     
    149160         * @method get_nav_menu_items
    150161         * @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 ) {
    154166            if ( $cache = get_transient( self::ITEMSPREFIX . $term_id ) ) {
    155167                $items = $cache;
    156168            } else {
    157                 $items = wp_get_nav_menu_items( $term_id );
     169                $items = wp_get_nav_menu_items( $term_id, $args );
    158170                set_transient( self::ITEMSPREFIX . $term_id, $items );
    159171            }
     
    164176         * @method menu
    165177         * @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
    173212            $menu = self::get_nav_menu_object( $args );
    174213
    175214            // 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;
    193231
    194232            $nav_menu = $items = '';
    195233
    196             // Set up the $menu_item variables
    197             _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 
    207234            $show_container = false;
    208 
    209235            if ( $args->container ) {
    210236                $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
     
    216242                }
    217243            }
     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
    218255            $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
    219256            unset( $sorted_menu_items );
     
    225262                $wrap_id = 'menu-' . $menu->slug;
    226263                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
    230265                        $wrap_id = $wrap_id . '-1';
    231                     }
    232                 }
    233             }
    234             $menu_id_slugs[] = $wrap_id;
     266                }
     267            }
     268            $menu_id_slugs[ ] = $wrap_id;
    235269
    236270            $wrap_class = $args->menu_class ? $args->menu_class : '';
     
    240274            $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );
    241275
     276            // Don't print any markup if there are no items at this point.
     277            if ( empty( $items ) ) return false;
     278
    242279            $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
    243280            unset( $items );
    244281
    245             if ( $show_container ) {
    246                 $nav_menu .= '</' . $args->container . '>';
    247             }
     282            if ( $show_container ) $nav_menu .= '</' . $args->container . '>';
    248283
    249284            $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );
    250285
    251             if ( $args->echo ) {
    252                 echo $nav_menu;
    253             } else {
     286            if ( $args->echo ) echo $nav_menu; else
    254287                return $nav_menu;
    255             }
    256288        }
    257289
     
    260292    Voce_Cached_Nav::init();
    261293
    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        }
    269298    }
    270299
     300    function voce_cached_nav_menu( $args ) {
     301        return Voce_Cached_Nav::menu( $args );
     302
     303    }
     304
    271305}
Note: See TracChangeset for help on using the changeset viewer.