|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WPEssential\Theme; |
| 4 | + |
| 5 | +if ( ! \defined( 'ABSPATH' ) ) |
| 6 | +{ |
| 7 | + exit; // Exit if accessed directly. |
| 8 | +} |
| 9 | + |
| 10 | +final class MenuWalker extends \Walker_Nav_Menu |
| 11 | +{ |
| 12 | + |
| 13 | + /** |
| 14 | + * Starts the element output. |
| 15 | + * |
| 16 | + * @param string $output Used to append additional content (passed by reference). |
| 17 | + * @param \WP_Post $data_object Menu item data object. |
| 18 | + * @param int $depth Depth of menu item. Used for padding. |
| 19 | + * @param \stdClass $args An object of wp_nav_menu() arguments. |
| 20 | + * @param int $current_object_id Optional. ID of the current menu item. Default 0. |
| 21 | + * |
| 22 | + * @since 3.0.0 |
| 23 | + * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added. |
| 24 | + * @since 5.9.0 Renamed `$item` to `$data_object` and `$id` to `$current_object_id` |
| 25 | + * to match parent class for PHP 8 named parameter support. |
| 26 | + * @since 6.7.0 Removed redundant title attributes. |
| 27 | + * |
| 28 | + * @see Walker::start_el() |
| 29 | + * |
| 30 | + */ |
| 31 | + public function start_el ( &$output, $data_object, $depth = 0, $args = null, $current_object_id = 0 ) |
| 32 | + { |
| 33 | + // Restores the more descriptive, specific name for use within this method. |
| 34 | + $menu_item = $data_object; |
| 35 | + |
| 36 | + if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) |
| 37 | + { |
| 38 | + $t = ''; |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + $t = "\t"; |
| 43 | + } |
| 44 | + $indent = ( $depth ) ? str_repeat( $t, $depth ) : ''; |
| 45 | + |
| 46 | + $classes = empty( $menu_item->classes ) ? [] : (array) $menu_item->classes; |
| 47 | + $classes[] = 'menu-item-' . $menu_item->ID; |
| 48 | + |
| 49 | + /** |
| 50 | + * Filters the arguments for a single nav menu item. |
| 51 | + * |
| 52 | + * @param stdClass $args An object of wp_nav_menu() arguments. |
| 53 | + * @param \WP_Post $menu_item Menu item data object. |
| 54 | + * @param int $depth Depth of menu item. Used for padding. |
| 55 | + * |
| 56 | + * @since 4.4.0 |
| 57 | + * |
| 58 | + */ |
| 59 | + $args = apply_filters( 'nav_menu_item_args', $args, $menu_item, $depth ); |
| 60 | + |
| 61 | + /** |
| 62 | + * Filters the CSS classes applied to a menu item's list item element. |
| 63 | + * |
| 64 | + * @param string[] $classes Array of the CSS classes that are applied to the menu item's `<li>` element. |
| 65 | + * @param \WP_Post $menu_item The current menu item object. |
| 66 | + * @param stdClass $args An object of wp_nav_menu() arguments. |
| 67 | + * @param int $depth Depth of menu item. Used for padding. |
| 68 | + * |
| 69 | + * @since 3.0.0 |
| 70 | + * @since 4.1.0 The `$depth` parameter was added. |
| 71 | + * |
| 72 | + */ |
| 73 | + $class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $menu_item, $args, $depth ) ); |
| 74 | + |
| 75 | + /** |
| 76 | + * Filters the ID attribute applied to a menu item's list item element. |
| 77 | + * |
| 78 | + * @param string $menu_item_id The ID attribute applied to the menu item's `<li>` element. |
| 79 | + * @param \WP_Post $menu_item The current menu item. |
| 80 | + * @param stdClass $args An object of wp_nav_menu() arguments. |
| 81 | + * @param int $depth Depth of menu item. Used for padding. |
| 82 | + * |
| 83 | + * @since 3.0.1 |
| 84 | + * @since 4.1.0 The `$depth` parameter was added. |
| 85 | + * |
| 86 | + */ |
| 87 | + $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $menu_item->ID, $menu_item, $args, $depth ); |
| 88 | + |
| 89 | + $li_atts = []; |
| 90 | + $li_atts[ 'id' ] = ! empty( $id ) ? $id : ''; |
| 91 | + $li_atts[ 'class' ] = ! empty( $class_names ) ? $class_names : ''; |
| 92 | + $li_atts[ 'class' ] .= ' wpe-fs-px-14 wpe-fw-medium wpe-position-rel'; |
| 93 | + |
| 94 | + /** |
| 95 | + * Filters the HTML attributes applied to a menu's list item element. |
| 96 | + * |
| 97 | + * @param array $li_atts { |
| 98 | + * The HTML attributes applied to the menu item's `<li>` element, empty strings are ignored. |
| 99 | + * |
| 100 | + * @type string $class HTML CSS class attribute. |
| 101 | + * @type string $id HTML id attribute. |
| 102 | + * } |
| 103 | + * |
| 104 | + * @param \WP_Post $menu_item The current menu item object. |
| 105 | + * @param stdClass $args An object of wp_nav_menu() arguments. |
| 106 | + * @param int $depth Depth of menu item. Used for padding. |
| 107 | + * |
| 108 | + * @since 6.3.0 |
| 109 | + * |
| 110 | + */ |
| 111 | + $li_atts = apply_filters( 'nav_menu_item_attributes', $li_atts, $menu_item, $args, $depth ); |
| 112 | + $li_attributes = $this->build_atts( $li_atts ); |
| 113 | + |
| 114 | + $output .= $indent . '<li' . $li_attributes . '>'; |
| 115 | + |
| 116 | + /** This filter is documented in wp-includes/post-template.php */ |
| 117 | + $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID ); |
| 118 | + |
| 119 | + // Save filtered value before filtering again. |
| 120 | + $the_title_filtered = $title; |
| 121 | + |
| 122 | + /** |
| 123 | + * Filters a menu item's title. |
| 124 | + * |
| 125 | + * @param string $title The menu item's title. |
| 126 | + * @param WP_Post $menu_item The current menu item object. |
| 127 | + * @param stdClass $args An object of wp_nav_menu() arguments. |
| 128 | + * @param int $depth Depth of menu item. Used for padding. |
| 129 | + * |
| 130 | + * @since 4.4.0 |
| 131 | + * |
| 132 | + */ |
| 133 | + $title = apply_filters( 'nav_menu_item_title', $title, $menu_item, $args, $depth ); |
| 134 | + |
| 135 | + $atts = []; |
| 136 | + $atts[ 'target' ] = ! empty( $menu_item->target ) ? $menu_item->target : ''; |
| 137 | + $atts[ 'rel' ] = ! empty( $menu_item->xfn ) ? $menu_item->xfn : ''; |
| 138 | + |
| 139 | + if ( ! empty( $menu_item->url ) ) |
| 140 | + { |
| 141 | + if ( get_privacy_policy_url() === $menu_item->url ) |
| 142 | + { |
| 143 | + $atts[ 'rel' ] = empty( $atts[ 'rel' ] ) ? 'privacy-policy' : $atts[ 'rel' ] . ' privacy-policy'; |
| 144 | + } |
| 145 | + |
| 146 | + $atts[ 'href' ] = $menu_item->url; |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + $atts[ 'href' ] = ''; |
| 151 | + } |
| 152 | + |
| 153 | + $atts[ 'aria-current' ] = $menu_item->current ? 'page' : ''; |
| 154 | + |
| 155 | + // Add title attribute only if it does not match the link text (before or after filtering). |
| 156 | + if ( ! empty( $menu_item->attr_title ) |
| 157 | + && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $menu_item->title ) ) |
| 158 | + && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $the_title_filtered ) ) |
| 159 | + && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $title ) ) |
| 160 | + ) |
| 161 | + { |
| 162 | + $atts[ 'title' ] = $menu_item->attr_title; |
| 163 | + } |
| 164 | + else |
| 165 | + { |
| 166 | + $atts[ 'title' ] = ''; |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Filters the HTML attributes applied to a menu item's anchor element. |
| 171 | + * |
| 172 | + * @param array $atts { |
| 173 | + * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored. |
| 174 | + * |
| 175 | + * @type string $title Title attribute. |
| 176 | + * @type string $target Target attribute. |
| 177 | + * @type string $rel The rel attribute. |
| 178 | + * @type string $href The href attribute. |
| 179 | + * @type string $aria -current The aria-current attribute. |
| 180 | + * } |
| 181 | + * |
| 182 | + * @param WP_Post $menu_item The current menu item object. |
| 183 | + * @param stdClass $args An object of wp_nav_menu() arguments. |
| 184 | + * @param int $depth Depth of menu item. Used for padding. |
| 185 | + * |
| 186 | + * @since 3.6.0 |
| 187 | + * @since 4.1.0 The `$depth` parameter was added. |
| 188 | + * |
| 189 | + */ |
| 190 | + $atts = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth ); |
| 191 | + $attributes = $this->build_atts( $atts ); |
| 192 | + |
| 193 | + $item_output = $args->before; |
| 194 | + $item_output .= '<a' . $attributes . '>'; |
| 195 | + $item_output .= $args->link_before . $title . $args->link_after; |
| 196 | + $item_output .= '</a>'; |
| 197 | + $item_output .= $args->after; |
| 198 | + |
| 199 | + /** |
| 200 | + * Filters a menu item's starting output. |
| 201 | + * |
| 202 | + * The menu item's starting output only includes `$args->before`, the opening `<a>`, |
| 203 | + * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is |
| 204 | + * no filter for modifying the opening and closing `<li>` for a menu item. |
| 205 | + * |
| 206 | + * @param string $item_output The menu item's starting HTML output. |
| 207 | + * @param WP_Post $menu_item Menu item data object. |
| 208 | + * @param int $depth Depth of menu item. Used for padding. |
| 209 | + * @param stdClass $args An object of wp_nav_menu() arguments. |
| 210 | + * |
| 211 | + * @since 3.0.0 |
| 212 | + * |
| 213 | + */ |
| 214 | + $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $menu_item, $depth, $args ); |
| 215 | + } |
| 216 | +} |
0 commit comments