Changeset 631788
- Timestamp:
- 11/29/2012 02:31:49 PM (13 years ago)
- File:
-
- 1 edited
-
codepress-menu/tags/2.2/codepress-menu.php (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
codepress-menu/tags/2.2/codepress-menu.php
r610406 r631788 3 3 Plugin Name: Codepress Menu 4 4 Plugin URI: http://wordpress.org/extend/plugins/codepress-menu/ 5 Version: 2. 1.25 Version: 2.2 6 6 Description: A more flexible take on displaying a WordPress menu in your theme 7 7 Author: David Mosterd … … 24 24 */ 25 25 26 require_once 'codepress-menu-item.php'; 27 28 class Codepress_Walker_Nav_Menu extends Walker_Nav_Menu 29 { 26 class Codepress_Walker_Nav_Menu extends Walker_Nav_Menu { 27 30 28 /** 31 29 * Indents any object as long as it has a unique id and that of its parent. … … 33 31 * @since 2.1 34 32 */ 35 protected function indent( array $items, $parentId = 0 ) 36 { 37 extract($this->db_fields); 33 protected function indent( array $items, $parent_id = 0 ) { 34 extract( $this->db_fields ); 38 35 39 36 $indent = array(); 40 37 41 foreach( $items as $v) {42 if ( $v->$parent == $parentId) {43 $ v->setChildren( $this->indent($items, $v->$id));38 foreach( $items as $item ) { 39 if ( $item->$parent == $parent_id ) { 40 $item->children = $this->indent( $items, $item->$id ); 44 41 45 $indent[] = $ v;42 $indent[] = $item; 46 43 } 47 44 } … … 55 52 * @since 2.0 56 53 */ 57 protected function level( array $items, $level, $reached = 1, $currentBranch = false)58 { 59 foreach ( $items as $item ) {54 protected function level( $items, $level, $reached = 1, $currentBranch = false) { 55 56 foreach ( $items as $item ) { 60 57 61 58 // check for current or ancestor 62 if ( $item-> isCurrent() || $item->isAncestor()|| $currentBranch ) {59 if ( $item->current || $item->current_item_ancestor || $currentBranch ) { 63 60 64 61 // catch top level 65 62 if ( $level == 1 && $reached == 1 ) { 66 return array( $item);63 return array( $item ); 67 64 } 68 65 69 66 // catch all other levels 70 if ( ++$reached == $level && $item->hasChildren() ) {71 return $item-> getChildren();67 if ( $reached + 1 == $level && ! empty( $item->children ) ) { 68 return $item->children; 72 69 } 73 70 74 if ( $item->hasChildren() ) {75 return $this->level( $item->getChildren(), $level, $reached, true);71 if ( ! empty( $item->children ) ) { 72 return $this->level( $item->children, $level, $reached + 1, true ); 76 73 } 77 74 } … … 86 83 * @since 1.0 87 84 */ 88 protected function depth(array $items, $depth, $level = 1) 89 { 90 foreach($items as $item) { 91 // unset the children 92 if ($depth == $level) { 93 $item->unsetChildren(); 94 } 95 96 // if still filled, go a level deeper 97 if ( $item->hasChildren() ) { 98 $item->setChildren( $this->depth($item->getChildren(), $depth, $level + 1) ); 99 } 85 protected function depth( $items, $depth, $level = 1 ) { 86 87 foreach( $items as $k => $item ) { 88 // unset the children, or go a level deeper 89 if ( $depth == $level ) 90 unset( $items[$k]->children ); 91 elseif ( ! empty( $item->children ) ) 92 $items[$k]->children = $this->depth( $item->children, $depth, $level + 1 ); 100 93 } 101 94 102 return $items;95 return $items; 103 96 } 104 97 … … 108 101 * @since 2.1 109 102 */ 110 protected function markHeadAndTail(array $items) 111 { 103 protected function mark_first_and_last( array $items ) { 112 104 $i = 0; 113 105 114 foreach( $items as $item) {106 foreach( $items as $item ) { 115 107 if ( $i == 0 ) { 116 $item-> addClass('first');108 $item->classes[] = 'first'; 117 109 } 118 110 119 111 $i++; 120 112 121 if ( $i == count( $items) ) {122 $item-> addClass('last');123 } 124 125 if ( $item->hasChildren() ) {126 $item-> setChildren( $this->markHeadAndTail($item->children));113 if ( $i == count( $items ) ) { 114 $item->classes[] = 'last'; 115 } 116 117 if ( ! empty( $item->children ) ) { 118 $item->children = $this->mark_first_and_last( $item->children ); 127 119 } 128 120 } … … 130 122 return $items; 131 123 } 124 125 /** 126 * Sanitize classes by removing some of them and renaming others. 127 * 128 * @since 2.1.1 129 */ 130 protected function sanitize_classes( $items, $args ) { 131 132 foreach( $items as $k => $item ) { 133 134 // always allow custom assigned classes 135 $filtered = (array) get_post_meta( $item->ID, '_menu_item_classes', true ); 136 137 $allowed = array(); 138 139 if ( $args->classes == 'minimal' || $args->classes == 'simple' ) { 140 $allowed = array( 141 'current-menu-item' => 'current', 142 'current-menu-parent' => 'current-parent', 143 'current-menu-ancestor' => 'current-ancestor', 144 ); 145 } 146 147 if ( $args->classes == 'sanitize' ) { 148 $allowed = array( 149 'current-menu-item', 150 'current-menu-parent', 151 'current-menu-ancestor', 152 ); 153 } 154 155 if ( $args->classes == 'simple' || $args->classes == 'sanitize' ) { 156 $allowed = array_merge( $allowed, array( 'first', 'last' ) ); 157 } 158 159 $allowed = apply_filters( 'codepress_menu_filter_classes', $allowed, $item, $args ); 160 161 // check for class that is allowed, but needs transformation 162 foreach ( $item->classes as $class ) { 163 if ( array_key_exists( $class, $allowed ) ) { 164 $filtered[] = $allowed[$class]; 165 } elseif ( in_array( $class, $allowed ) ) { 166 $filtered[] = $class; 167 } 168 } 169 170 $items[$k]->classes = array_filter( $filtered );; 171 } 172 173 return $items; 174 } 132 175 133 176 /** … … 136 179 * @since 2.1 137 180 */ 138 public function walk($items, $depth, $args) 139 { 140 // check if wp_nav_menu_objects fired 141 if ( !( current($items) instanceof Codepress_Menu_Item ) ) { 142 return ''; 143 } 144 145 // a ordered list of filters to apply 146 $filters = array('indent', 'level', 'depth', 'markHeadAndTail'); 147 148 foreach($filters as $filter) { 149 // check for filter arguments 150 switch($filter) { 151 case 'sanitize': 152 if ( $args->sanitize ) { 153 $items = $this->sanitize($items); 154 } 155 break; 156 case 'level': 157 if ( $args->level > 0) { 158 $items = $this->level($items, $args->level); 159 } 160 break; 161 case 'depth': 162 $items = $this->depth($items, $args->depth); 163 break; 164 default: 165 $items = $this->$filter($items); 166 } 167 } 168 169 return $this->getMenu($items, $args); 181 public function walk( $items, $depth, $args ) { 182 183 $items = $this->indent( $items ); 184 185 if ( $args->level ) 186 $items = $this->level( $items, $args->level ); 187 188 $items = $this->depth( $items, $depth ); 189 190 if ( $args->classes ) { 191 $items = $this->mark_first_and_last( $items ); 192 $items = $this->sanitize_classes( $items, $args ); 193 } 194 195 $items = apply_filters( 'codepress_menu_filter', $items ); 196 197 return $this->get_menu( $items, $args ); 170 198 } 171 199 … … 176 204 * @since 1.0 177 205 */ 178 public function getMenu($items, $args, $level = 1) 179 { 206 public function get_menu( $items, $args, $level = 1 ) { 180 207 $output = ''; 181 208 182 209 if ( $level > 1) { 183 $this->start_lvl( $output, 0, $args);184 } 185 186 foreach( $items as $item) {187 $this->start_el( $output, $item, 0, $args);188 189 if ( $item->hasChildren() ) {190 $output .= $this->get Menu($item->getChildren(), $args, $level + 1);191 } 192 193 $this->end_el( $output, $item, 0, $args);210 $this->start_lvl( $output, 0, $args ); 211 } 212 213 foreach( $items as $item ) { 214 $this->start_el( $output, $item, 0, $args ); 215 216 if ( ! empty( $item->children ) ) { 217 $output .= $this->get_menu( $item->children, $args, $level + 1); 218 } 219 220 $this->end_el( $output, $item, 0, $args ); 194 221 } 195 222 196 223 if ( $level > 1 ) { 197 $this->end_lvl( $output, 0, $args);224 $this->end_lvl( $output, 0, $args ); 198 225 } 199 226 … … 203 230 204 231 /** 205 * Sanitize classes232 * Init Codepress Menu. 206 233 * 207 * @return array 234 * @uses apply_filters() To ignore certain theme_locations. 235 * @since 1.0 208 236 */ 209 function codepress_menu_item_sanitize_classes($classes, $item, $args) 210 { 211 if ( $args->classes ) { 212 $classes = $item->filterClasses($classes, $args->classes); 213 } 214 215 return $classes; 216 } 217 add_filter('nav_menu_css_class', 'codepress_menu_item_sanitize_classes', 10, 3); 218 219 /** 220 * Sanitize id 221 * 222 * @return string 223 */ 224 function codepress_menu_item_sanitize_id($id, $item, $args) 225 { 226 if ( $args->classes ) { 227 $id = false; 228 } 229 230 return $id; 231 } 232 add_filter('nav_menu_item_id', 'codepress_menu_item_sanitize_id', 10, 3); 233 234 /** 235 * Set codepress menu defaults 236 * 237 */ 238 function codepress_menu_args($args) 239 { 240 $default = array( 237 function codepress_menu_init( $args ) { 238 $ignore = apply_filters( 'codepress_ignore_theme_locations', array() ); 239 240 if ( ! empty( $ignore ) && in_array( $args['theme_location'], $ignore ) ) 241 return $args; 242 243 $defaults = array( 241 244 'level' => 0, 242 245 'classes' => false, 246 'walker' => new Codepress_Walker_Nav_Menu, 243 247 ); 244 248 245 return array_merge( $default, $args ); 246 } 247 add_filter('wp_nav_menu_args', 'codepress_menu_args', 10, 1); 248 249 /** 250 * Set codepress menu walker 251 * 252 */ 253 function codepress_menu_set_walker($args) 254 { 255 $args['walker'] = new Codepress_Walker_Nav_Menu(); 249 foreach( $defaults as $option => $default ) { 250 if ( empty( $args[$option] ) ) 251 $args[$option] = $default; 252 } 256 253 257 254 return $args; 258 255 } 259 add_filter('wp_nav_menu_args', 'codepress_menu_set_walker', 5, 1); 260 261 /** 262 * Extend current menu item 263 * 264 * @param array $items 265 * @return \Codepress_Menu_Item 266 */ 267 function codepress_menu_items($items) 268 { 269 $codepress_items = array(); 270 271 foreach($items as $item) { 272 $codepress_items[] = new Codepress_Menu_Item($item); 273 } 274 275 return $codepress_items; 276 } 277 add_filter('wp_nav_menu_objects', 'codepress_menu_items', 10, 1); 278 279 /** 280 * Extend the minimal and sanitize class set with 'first' and 'last' to get :first-child and :last-child support in IE7+ 281 * Will drop once IE9+ is the standard 282 * 283 * @param array $allowed 284 * @param string $mode 285 * @return array 286 */ 287 function codepress_menu_filter_classes($allowed, $mode) 288 { 289 if ( $mode == 'simple' || $mode == 'sanitize' ) { 290 $allowed[] = 'first'; 291 $allowed[] = 'last'; 292 } 293 294 return $allowed; 295 } 296 add_filter( 'codepress_menu_filter_classes', 'codepress_menu_filter_classes', 10, 2 ); 297 298 /** 299 * Checks if the filters have not reduced the itemset to zero. If so, act as wp_nav_menu and return false. 300 * 301 * @todo searching for li tag might break on a custom walker 302 * @param string $nav_menu 303 * @param array $args 304 * @return boolean|string 305 */ 306 function codepress_menu_return_false_on_empty_list_after_filters($nav_menu, $args) 307 { 308 if ( strpos($nav_menu, "</li>") === false ) { 309 return false; 310 } 311 312 return $nav_menu; 313 } 314 add_filter( 'wp_nav_menu', 'codepress_menu_return_false_on_empty_list_after_filters', 10, 2 ); 256 add_filter( 'wp_nav_menu_args', 'codepress_menu_init' );
Note: See TracChangeset
for help on using the changeset viewer.