| Conditions | 27 |
| Paths | > 20000 |
| Total Lines | 201 |
| Code Lines | 76 |
| Lines | 7 |
| Ratio | 3.48 % |
| 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 |
||
| 96 | public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { |
||
| 97 | View Code Duplication | if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { |
|
| 98 | $t = ''; |
||
| 99 | $n = ''; |
||
| 100 | } else { |
||
| 101 | $t = "\t"; |
||
| 102 | $n = "\n"; |
||
| 103 | } |
||
| 104 | $indent = ( $depth ) ? str_repeat( $t, $depth ) : ''; |
||
| 105 | |||
| 106 | $classes = empty( $item->classes ) ? array() : (array) $item->classes; |
||
| 107 | |||
| 108 | // Initialize some holder variables to store specially handled item |
||
| 109 | // wrappers and icons. |
||
| 110 | $linkmod_classes = array(); |
||
| 111 | $icon_classes = array(); |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get an updated $classes array without linkmod or icon classes. |
||
| 115 | * |
||
| 116 | * NOTE: linkmod and icon class arrays are passed by reference and |
||
| 117 | * are maybe modified before being used later in this function. |
||
| 118 | */ |
||
| 119 | $classes = self::seporate_linkmods_and_icons_from_classes( $classes, $linkmod_classes, $icon_classes, $depth ); |
||
| 120 | |||
| 121 | // Join any icon classes plucked from $classes into a string. |
||
| 122 | $icon_class_string = join( ' ', $icon_classes ); |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Filters the arguments for a single nav menu item. |
||
| 126 | * |
||
| 127 | * WP 4.4.0 |
||
| 128 | * |
||
| 129 | * @param stdClass $args An object of wp_nav_menu() arguments. |
||
| 130 | * @param WP_Post $item Menu item data object. |
||
| 131 | * @param int $depth Depth of menu item. Used for padding. |
||
| 132 | */ |
||
| 133 | $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth ); |
||
| 134 | |||
| 135 | // Add .dropdown or .active classes where they are needed. |
||
| 136 | if ( $args->has_children ) { |
||
| 137 | $classes[] = 'dropdown'; |
||
| 138 | } |
||
| 139 | if ( in_array( 'current-menu-item', $classes, true ) || in_array( 'current-menu-parent', $classes, true ) ) { |
||
| 140 | $classes[] = 'active'; |
||
| 141 | } |
||
| 142 | |||
| 143 | // Add some additional default classes to the item. |
||
| 144 | $classes[] = 'menu-item-' . $item->ID; |
||
| 145 | $classes[] = 'nav-item'; |
||
| 146 | |||
| 147 | // Allow filtering the classes. |
||
| 148 | $classes = apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ); |
||
| 149 | |||
| 150 | // Form a string of classes in format: class="class_names". |
||
| 151 | $class_names = join( ' ', $classes ); |
||
| 152 | $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Filters the ID applied to a menu item's list item element. |
||
| 156 | * |
||
| 157 | * @since WP 3.0.1 |
||
| 158 | * @since WP 4.1.0 The `$depth` parameter was added. |
||
| 159 | * |
||
| 160 | * @param string $menu_id The ID that is applied to the menu item's `<li>` element. |
||
| 161 | * @param WP_Post $item The current menu item. |
||
| 162 | * @param stdClass $args An object of wp_nav_menu() arguments. |
||
| 163 | * @param int $depth Depth of menu item. Used for padding. |
||
| 164 | */ |
||
| 165 | $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth ); |
||
| 166 | $id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; |
||
| 167 | |||
| 168 | $output .= $indent . '<li itemscope="itemscope" itemtype="https://www.schema.org/SiteNavigationElement"' . $id . $class_names . '>'; |
||
| 169 | |||
| 170 | // initialize array for holding the $atts for the link item. |
||
| 171 | $atts = array(); |
||
| 172 | |||
| 173 | // Set title from item to the $atts array - if title is empty then |
||
| 174 | // default to item title. |
||
| 175 | if ( empty( $item->attr_title ) ) { |
||
| 176 | $atts['title'] = ! empty( $item->title ) ? strip_tags( $item->title ) : ''; |
||
| 177 | } else { |
||
| 178 | $atts['title'] = $item->attr_title; |
||
| 179 | } |
||
| 180 | |||
| 181 | $atts['target'] = ! empty( $item->target ) ? $item->target : ''; |
||
| 182 | $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : ''; |
||
| 183 | // If item has_children add atts to <a>. |
||
| 184 | if ( $args->has_children && 0 === $depth && $args->depth > 1 ) { |
||
| 185 | $atts['href'] = '#'; |
||
| 186 | $atts['data-toggle'] = 'dropdown'; |
||
| 187 | $atts['aria-haspopup'] = 'true'; |
||
| 188 | $atts['aria-expanded'] = 'false'; |
||
| 189 | $atts['class'] = 'dropdown-toggle nav-link'; |
||
| 190 | $atts['id'] = 'menu-item-dropdown-' . $item->ID; |
||
| 191 | } else { |
||
| 192 | $atts['href'] = ! empty( $item->url ) ? $item->url : ''; |
||
| 193 | // Items in dropdowns use .dropdown-item instead of .nav-link. |
||
| 194 | if ( $depth > 0 ) { |
||
| 195 | $atts['class'] = 'dropdown-item'; |
||
| 196 | } else { |
||
| 197 | $atts['class'] = 'nav-link'; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | // update atts of this item based on any custom linkmod classes. |
||
| 202 | $atts = self::update_atts_for_linkmod_type( $atts, $linkmod_classes ); |
||
| 203 | // Allow filtering of the $atts array before using it. |
||
| 204 | $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args ); |
||
| 205 | |||
| 206 | // Build a string of html containing all the atts for the item. |
||
| 207 | $attributes = ''; |
||
| 208 | foreach ( $atts as $attr => $value ) { |
||
| 209 | if ( ! empty( $value ) ) { |
||
| 210 | $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); |
||
| 211 | $attributes .= ' ' . $attr . '="' . $value . '"'; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Set a typeflag to easily test if this is a linkmod or not. |
||
| 217 | */ |
||
| 218 | $linkmod_type = self::get_linkmod_type( $linkmod_classes ); |
||
| 219 | |||
| 220 | /** |
||
| 221 | * START appending the internal item contents to the output. |
||
| 222 | */ |
||
| 223 | $item_output = $args->before; |
||
| 224 | |||
| 225 | /** |
||
| 226 | * This is the start of the internal nav item. Depending on what |
||
| 227 | * kind of linkmod we have we may need different wrapper elements. |
||
| 228 | */ |
||
| 229 | if ( '' !== $linkmod_type ) { |
||
| 230 | // is linkmod, output the required element opener. |
||
| 231 | $item_output .= self::linkmod_element_open( $linkmod_type, $attributes ); |
||
| 232 | } else { |
||
| 233 | // With no link mod type set this must be a standard <a> tag. |
||
| 234 | $item_output .= '<a' . $attributes . '>'; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Initiate empty icon var, then if we have a string containing any |
||
| 239 | * icon classes form the icon markup with an <i> element. This is |
||
| 240 | * output inside of the item before the $title (the link text). |
||
| 241 | */ |
||
| 242 | $icon_html = ''; |
||
| 243 | if ( ! empty( $icon_class_string ) ) { |
||
| 244 | // append an <i> with the icon classes to what is output before links. |
||
| 245 | $icon_html = '<i class="' . esc_attr( $icon_class_string ) . '" aria-hidden="true"></i> '; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** This filter is documented in wp-includes/post-template.php */ |
||
| 249 | $title = apply_filters( 'the_title', $item->title, $item->ID ); |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Filters a menu item's title. |
||
| 253 | * |
||
| 254 | * @since WP 4.4.0 |
||
| 255 | * |
||
| 256 | * @param string $title The menu item's title. |
||
| 257 | * @param WP_Post $item The current menu item. |
||
| 258 | * @param stdClass $args An object of wp_nav_menu() arguments. |
||
| 259 | * @param int $depth Depth of menu item. Used for padding. |
||
| 260 | */ |
||
| 261 | $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth ); |
||
| 262 | |||
| 263 | /** |
||
| 264 | * If the .sr-only class was set apply to the nav items text only. |
||
| 265 | */ |
||
| 266 | if ( in_array( 'sr-only', $linkmod_classes, true ) ) { |
||
| 267 | $title = self::wrap_for_screen_reader( $title ); |
||
| 268 | $keys_to_unset = array_keys( $linkmod_classes, 'sr-only' ); |
||
| 269 | foreach ( $keys_to_unset as $k ) { |
||
| 270 | unset( $linkmod_classes[ $k ] ); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | // Put the item contents into $output. |
||
| 275 | $item_output .= $args->link_before . $icon_html . $title . $args->link_after; |
||
| 276 | |||
| 277 | /** |
||
| 278 | * This is the end of the internal nav item. We need to close the |
||
| 279 | * correct element depending on the type of link or link mod. |
||
| 280 | */ |
||
| 281 | if ( '' !== $linkmod_type ) { |
||
| 282 | // is linkmod, output the required element opener. |
||
| 283 | $item_output .= self::linkmod_element_close( $linkmod_type, $attributes ); |
||
| 284 | } else { |
||
| 285 | // With no link mod type set this must be a standard <a> tag. |
||
| 286 | $item_output .= '</a>'; |
||
| 287 | } |
||
| 288 | |||
| 289 | $item_output .= $args->after; |
||
| 290 | /** |
||
| 291 | * END appending the internal item contents to the output. |
||
| 292 | */ |
||
| 293 | |||
| 294 | $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); |
||
| 295 | |||
| 296 | } |
||
| 297 | |||
| 554 |
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.