| Conditions | 39 |
| Paths | > 20000 |
| Total Lines | 243 |
| Code Lines | 104 |
| Lines | 7 |
| Ratio | 2.88 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 95 | public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { |
||
| 96 | View Code Duplication | if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { |
|
| 97 | $t = ''; |
||
| 98 | $n = ''; |
||
| 99 | } else { |
||
| 100 | $t = "\t"; |
||
| 101 | $n = "\n"; |
||
| 102 | } |
||
| 103 | $indent = ( $depth ) ? str_repeat( $t, $depth ) : ''; |
||
| 104 | |||
| 105 | $value = ''; |
||
| 106 | $class_names = $value; |
||
| 107 | $classes = empty( $item->classes ) ? array() : (array) $item->classes; |
||
| 108 | |||
| 109 | // Initialize some holder variables to store specially handled item |
||
| 110 | // wrappers and icons. |
||
| 111 | $extra_link_classes = array(); |
||
| 112 | $icon_classes = array(); |
||
| 113 | $icon_class_string = ''; |
||
| 114 | |||
| 115 | // Loop and begin handling any special linkmod or icon classes. |
||
| 116 | foreach ( $classes as $key => $class ) { |
||
| 117 | /** |
||
| 118 | * Find any custom link mods or icons, store in their holder |
||
| 119 | * arrays and remove them from the classes array. |
||
| 120 | * |
||
| 121 | * Supported linkmods: .disabled, .dropdown-header, .dropdown-divider |
||
| 122 | * Supported iconsets: Font Awesome 4/5, Glypicons |
||
| 123 | */ |
||
| 124 | if ( preg_match( '/disabled/', $class ) ) { |
||
| 125 | // Test for .disabled. |
||
| 126 | $extra_link_classes[] = $class; |
||
| 127 | unset( $classes[ $key ] ); |
||
| 128 | } elseif ( preg_match( '/dropdown-header|dropdown-divider/', $class ) && $depth > 0 ) { |
||
| 129 | // Test for .dropdown-header or .dropdown-divider and a |
||
| 130 | // depth greater than 0 - IE inside a dropdown. |
||
| 131 | $extra_link_classes[] = $class; |
||
| 132 | unset( $classes[ $key ] ); |
||
| 133 | } elseif ( preg_match( '/fa-(\S*)?|fas(\s?)|fa(\s?)/', $class ) ) { |
||
| 134 | // Font Awesome. |
||
| 135 | $icon_classes[] = $class; |
||
| 136 | unset( $classes[ $key ] ); |
||
| 137 | } elseif ( preg_match( '/glyphicons-(\S*)?|glyphicons(\s?)/', $class ) ) { |
||
| 138 | // Glyphicons. |
||
| 139 | $icon_classes[] = $class; |
||
| 140 | unset( $classes[ $key ] ); |
||
| 141 | } |
||
| 142 | } // End foreach(). |
||
| 143 | |||
| 144 | // Join any icon classes plucked from $classes into a string. |
||
| 145 | $icon_class_string = join( ' ', $icon_classes ); |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Filters the arguments for a single nav menu item. |
||
| 149 | * |
||
| 150 | * WP 4.4.0 |
||
| 151 | * |
||
| 152 | * @param stdClass $args An object of wp_nav_menu() arguments. |
||
| 153 | * @param WP_Post $item Menu item data object. |
||
| 154 | * @param int $depth Depth of menu item. Used for padding. |
||
| 155 | */ |
||
| 156 | $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth ); |
||
| 157 | |||
| 158 | $classes[] = 'menu-item-' . $item->ID; |
||
| 159 | $classes[] = 'nav-item'; |
||
| 160 | |||
| 161 | // Add .dropdown or .active classes where they are needed. |
||
| 162 | if ( $args->has_children ) { |
||
| 163 | $classes[] = 'dropdown'; |
||
| 164 | } |
||
| 165 | if ( in_array( 'current-menu-item', $classes, true ) || in_array( 'current-menu-parent', $classes, true ) ) { |
||
| 166 | $classes[] = 'active'; |
||
| 167 | } |
||
| 168 | |||
| 169 | // Allow filtering the classes. |
||
| 170 | $classes = apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ); |
||
| 171 | |||
| 172 | // Form a string of classes in format: class="class_names". |
||
| 173 | $class_names = join( ' ', $classes ); |
||
| 174 | $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Filters the ID applied to a menu item's list item element. |
||
| 178 | * |
||
| 179 | * @since WP 3.0.1 |
||
| 180 | * @since WP 4.1.0 The `$depth` parameter was added. |
||
| 181 | * |
||
| 182 | * @param string $menu_id The ID that is applied to the menu item's `<li>` element. |
||
| 183 | * @param WP_Post $item The current menu item. |
||
| 184 | * @param stdClass $args An object of wp_nav_menu() arguments. |
||
| 185 | * @param int $depth Depth of menu item. Used for padding. |
||
| 186 | */ |
||
| 187 | $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth ); |
||
| 188 | $id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; |
||
| 189 | |||
| 190 | $output .= $indent . '<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement"' . $id . $value . $class_names . '>'; |
||
| 191 | |||
| 192 | // initialize array for holding the $atts for the link item. |
||
| 193 | $atts = array(); |
||
| 194 | |||
| 195 | // Set title from item to the $atts array - if title is empty then |
||
| 196 | // default to item title. |
||
| 197 | if ( empty( $item->attr_title ) ) { |
||
| 198 | $atts['title'] = ! empty( $item->title ) ? strip_tags( $item->title ) : ''; |
||
| 199 | } else { |
||
| 200 | $atts['title'] = $item->attr_title; |
||
| 201 | } |
||
| 202 | |||
| 203 | $atts['target'] = ! empty( $item->target ) ? $item->target : ''; |
||
| 204 | $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; |
||
| 205 | // If item has_children add atts to <a>. |
||
| 206 | if ( $args->has_children && 0 === $depth && $args->depth > 1 ) { |
||
| 207 | $atts['href'] = '#'; |
||
| 208 | $atts['data-toggle'] = 'dropdown'; |
||
| 209 | $atts['aria-haspopup'] = 'true'; |
||
| 210 | $atts['aria-expanded'] = 'false'; |
||
| 211 | $atts['class'] = 'dropdown-toggle nav-link'; |
||
| 212 | $atts['id'] = 'menu-item-dropdown-' . $item->ID; |
||
| 213 | } else { |
||
| 214 | $atts['href'] = ! empty( $item->url ) ? $item->url : ''; |
||
| 215 | // Items in dropdowns use .dropdown-item instead of .nav-link. |
||
| 216 | if ( $depth > 0 ) { |
||
| 217 | $atts['class'] = 'dropdown-item'; |
||
| 218 | } else { |
||
| 219 | $atts['class'] = 'nav-link'; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | // Set this as an indetifier flag to ease identifying special item |
||
| 224 | // types later in the processing. Default will be '' or 'link'. |
||
| 225 | $type_flag = ''; |
||
| 226 | // Loop through the array of extra link classes plucked from the |
||
| 227 | // parent <li>s classes array. |
||
| 228 | if ( ! empty( $extra_link_classes ) ) { |
||
| 229 | foreach ( $extra_link_classes as $link_class ) { |
||
| 230 | if ( ! empty( $link_class ) ) { |
||
| 231 | // update $atts with the extra classname. |
||
| 232 | $atts['class'] .= ' ' . esc_attr( $link_class ); |
||
| 233 | |||
| 234 | // check for special class types we need additional handling for. |
||
| 235 | if ( 'disabled' === $link_class ) { |
||
| 236 | // Convert link to '#' and unset open targets. |
||
| 237 | $atts['href'] = '#'; |
||
| 238 | unset( $atts['target'] ); |
||
| 239 | } elseif ( 'dropdown-header' === $link_class ) { |
||
| 240 | // Store a type flag and unset href and target. |
||
| 241 | $type_flag = 'dropdown-header'; |
||
| 242 | unset( $atts['href'] ); |
||
| 243 | unset( $atts['target'] ); |
||
| 244 | } elseif ( 'dropdown-divider' === $link_class ) { |
||
| 245 | // Store a type flag and unset href and target. |
||
| 246 | $type_flag = 'dropdown-divider'; |
||
| 247 | unset( $atts['href'] ); |
||
| 248 | unset( $atts['target'] ); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | // Allow filtering of the $atts array before using it. |
||
| 255 | $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args ); |
||
| 256 | |||
| 257 | // Build a string of html containing all the atts for the item. |
||
| 258 | $attributes = ''; |
||
| 259 | foreach ( $atts as $attr => $value ) { |
||
| 260 | if ( ! empty( $value ) ) { |
||
| 261 | $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); |
||
| 262 | $attributes .= ' ' . $attr . '="' . $value . '"'; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * START appending the internal item contents to the output. |
||
| 268 | */ |
||
| 269 | $item_output = $args->before; |
||
| 270 | |||
| 271 | /** |
||
| 272 | * This is the start of the internal nav item. Depending on what |
||
| 273 | * kind of link mod we have we need different wrapper elements. |
||
| 274 | */ |
||
| 275 | if ( 'dropdown-header' === $type_flag ) { |
||
| 276 | // For a header use a span with the .h6 class instead of a real |
||
| 277 | // header tag so that it doesn't confuse screen readers. |
||
| 278 | $item_output .= '<span class="dropdown-header h6"' . $attributes . '>'; |
||
| 279 | } elseif ( 'dropdown-divider' === $type_flag ) { |
||
| 280 | // this is a divider. |
||
| 281 | $item_output .= '<div class="dropdown-divider"' . $attributes . '>'; |
||
| 282 | } else { |
||
| 283 | // With no link mod type set this must be a standard <a> tag. |
||
| 284 | $item_output .= '<a' . $attributes . '>'; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Initiate empty icon var, then if we have a string containing any |
||
| 289 | * icon classes form the icon markup with an <i> element. This is |
||
| 290 | * output inside of the item before the $title (the link text). |
||
| 291 | */ |
||
| 292 | $icon_html = ''; |
||
| 293 | if ( ! empty( $icon_class_string ) ) { |
||
| 294 | // append an <i> with the icon classes to what is output before links. |
||
| 295 | $icon_html = '<i class="' . esc_attr( $icon_class_string ) . '" aria-hidden="true"></i> '; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** This filter is documented in wp-includes/post-template.php */ |
||
| 299 | $title = apply_filters( 'the_title', $item->title, $item->ID ); |
||
| 300 | /** |
||
| 301 | * Filters a menu item's title. |
||
| 302 | * |
||
| 303 | * @since 4.4.0 |
||
| 304 | * |
||
| 305 | * @param string $title The menu item's title. |
||
| 306 | * @param WP_Post $item The current menu item. |
||
| 307 | * @param stdClass $args An object of wp_nav_menu() arguments. |
||
| 308 | * @param int $depth Depth of menu item. Used for padding. |
||
| 309 | */ |
||
| 310 | $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth ); |
||
| 311 | |||
| 312 | // Put the item contents into $output. |
||
| 313 | $item_output .= $args->link_before . $icon_html . $title . $args->link_after; |
||
| 314 | |||
| 315 | /** |
||
| 316 | * This is the end of the internal nav item. We need to close the |
||
| 317 | * correct element depending on the type of link or link mod. |
||
| 318 | */ |
||
| 319 | if ( 'dropdown-header' === $type_flag ) { |
||
| 320 | // this is a header. |
||
| 321 | $item_output .= '</span>'; |
||
| 322 | } elseif ( 'dropdown-divider' === $type_flag ) { |
||
| 323 | // this is a divider. |
||
| 324 | $item_output .= '</div>'; |
||
| 325 | } else { |
||
| 326 | // it's most likely a link at this point. |
||
| 327 | $item_output .= '</a>'; |
||
| 328 | } |
||
| 329 | |||
| 330 | $item_output .= $args->after; |
||
| 331 | /** |
||
| 332 | * END appending the internal item contents to the output. |
||
| 333 | */ |
||
| 334 | |||
| 335 | $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); |
||
| 336 | |||
| 337 | } |
||
| 338 | |||
| 425 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.