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