| 1 |
<?php |
| 2 |
|
| 3 |
namespace FloatingButton\Publisher; |
| 4 |
|
| 5 |
use Walker_Nav_Menu; |
| 6 |
|
| 7 |
defined( 'ABSPATH' ) || exit; |
| 8 |
|
| 9 |
class Navigation extends Walker_Nav_Menu { |
| 10 |
|
| 11 |
/** |
| 12 |
* [private stored in start_el and used in start_lvl to get item ID ] |
| 13 |
* @var [array] |
| 14 |
*/ |
| 15 |
private $curItem; |
| 16 |
|
| 17 |
public function start_el( &$output, $data_object, $depth = 0, $args = null, $current_object_id = 0 ) { |
| 18 |
// Restores the more descriptive, specific name for use within this method. |
| 19 |
$menu_item = $data_object; |
| 20 |
|
| 21 |
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { |
| 22 |
$t = ''; |
| 23 |
$n = ''; |
| 24 |
} else { |
| 25 |
$t = "\t"; |
| 26 |
$n = "\n"; |
| 27 |
} |
| 28 |
$indent = ( $depth ) ? str_repeat( $t, $depth ) : ''; |
| 29 |
|
| 30 |
$classes = empty( $menu_item->classes ) ? array() : (array) $menu_item->classes; |
| 31 |
$classes[] = 'menu-item-' . $menu_item->ID; |
| 32 |
|
| 33 |
/** |
| 34 |
* Filters the arguments for a single nav menu item. |
| 35 |
* |
| 36 |
* @param stdClass $args An object of wp_nav_menu() arguments. |
| 37 |
* @param WP_Post $menu_item Menu item data object. |
| 38 |
* @param int $depth Depth of menu item. Used for padding. |
| 39 |
* |
| 40 |
* @since 4.4.0 |
| 41 |
* |
| 42 |
*/ |
| 43 |
$args = apply_filters( 'nav_menu_item_args', $args, $menu_item, $depth ); |
| 44 |
|
| 45 |
/** |
| 46 |
* Filters the CSS classes applied to a menu item's list item element. |
| 47 |
* |
| 48 |
* @param string[] $classes Array of the CSS classes that are applied to the menu item's `<li>` element. |
| 49 |
* @param WP_Post $menu_item The current menu item object. |
| 50 |
* @param stdClass $args An object of wp_nav_menu() arguments. |
| 51 |
* @param int $depth Depth of menu item. Used for padding. |
| 52 |
* |
| 53 |
* @since 3.0.0 |
| 54 |
* @since 4.1.0 The `$depth` parameter was added. |
| 55 |
* |
| 56 |
*/ |
| 57 |
$class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $menu_item, $args, $depth ) ); |
| 58 |
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; |
| 59 |
|
| 60 |
/** |
| 61 |
* Filters the ID attribute applied to a menu item's list item element. |
| 62 |
* |
| 63 |
* @param string $menu_item_id The ID attribute applied to the menu item's `<li>` element. |
| 64 |
* @param WP_Post $menu_item The current menu item. |
| 65 |
* @param stdClass $args An object of wp_nav_menu() arguments. |
| 66 |
* @param int $depth Depth of menu item. Used for padding. |
| 67 |
* |
| 68 |
* @since 3.0.1 |
| 69 |
* @since 4.1.0 The `$depth` parameter was added. |
| 70 |
* |
| 71 |
*/ |
| 72 |
$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $menu_item->ID, $menu_item, $args, $depth ); |
| 73 |
$id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; |
| 74 |
|
| 75 |
$output .= $indent . '<li' . $id . $class_names . '>'; |
| 76 |
|
| 77 |
$atts = array(); |
| 78 |
$atts['title'] = ! empty( $menu_item->attr_title ) ? $menu_item->attr_title : ''; |
| 79 |
$atts['target'] = ! empty( $menu_item->target ) ? $menu_item->target : ''; |
| 80 |
if ( '_blank' === $menu_item->target && empty( $menu_item->xfn ) ) { |
| 81 |
$atts['rel'] = 'noopener'; |
| 82 |
} else { |
| 83 |
$atts['rel'] = $menu_item->xfn; |
| 84 |
} |
| 85 |
$atts['href'] = ! empty( $menu_item->url ) ? $menu_item->url : ''; |
| 86 |
$atts['aria-current'] = $menu_item->current ? 'page' : ''; |
| 87 |
|
| 88 |
/** |
| 89 |
* Filters the HTML attributes applied to a menu item's anchor element. |
| 90 |
* |
| 91 |
* @param array $atts { |
| 92 |
* The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored. |
| 93 |
* |
| 94 |
* @type string $title Title attribute. |
| 95 |
* @type string $target Target attribute. |
| 96 |
* @type string $rel The rel attribute. |
| 97 |
* @type string $href The href attribute. |
| 98 |
* @type string $aria-current The aria-current attribute. |
| 99 |
* } |
| 100 |
* |
| 101 |
* @param WP_Post $menu_item The current menu item object. |
| 102 |
* @param stdClass $args An object of wp_nav_menu() arguments. |
| 103 |
* @param int $depth Depth of menu item. Used for padding. |
| 104 |
* |
| 105 |
* @since 3.6.0 |
| 106 |
* @since 4.1.0 The `$depth` parameter was added. |
| 107 |
* |
| 108 |
*/ |
| 109 |
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth ); |
| 110 |
|
| 111 |
$attributes = ''; |
| 112 |
foreach ( $atts as $attr => $value ) { |
| 113 |
if ( is_scalar( $value ) && '' !== $value && false !== $value ) { |
| 114 |
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); |
| 115 |
$attributes .= ' ' . $attr . '="' . $value . '"'; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** This filter is documented in wp-includes/post-template.php */ |
| 120 |
$title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID ); |
| 121 |
|
| 122 |
|
| 123 |
$item_output = $args->before; |
| 124 |
|
| 125 |
|
| 126 |
if ( $args->walker->has_children ) { |
| 127 |
$item_output .= '<details>'; |
| 128 |
$item_output .= '<summary>'; |
| 129 |
$item_output .= $menu_item->title; |
| 130 |
$item_output .= '</summary>'; |
| 131 |
} else { |
| 132 |
$item_output .= '<a' . $attributes . '>'; |
| 133 |
$item_output .= $menu_item->title; |
| 134 |
$item_output .= '</a>'; |
| 135 |
} |
| 136 |
|
| 137 |
$item_output .= $args->after; |
| 138 |
|
| 139 |
/** |
| 140 |
* Filters a menu item's starting output. |
| 141 |
* |
| 142 |
* The menu item's starting output only includes `$args->before`, the opening `<a>`, |
| 143 |
* the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is |
| 144 |
* no filter for modifying the opening and closing `<li>` for a menu item. |
| 145 |
* |
| 146 |
* @param string $item_output The menu item's starting HTML output. |
| 147 |
* @param WP_Post $menu_item Menu item data object. |
| 148 |
* @param int $depth Depth of menu item. Used for padding. |
| 149 |
* @param stdClass $args An object of wp_nav_menu() arguments. |
| 150 |
* |
| 151 |
* @since 3.0.0 |
| 152 |
* |
| 153 |
*/ |
| 154 |
$output .= $item_output; |
| 155 |
} |
| 156 |
|
| 157 |
public function end_el( &$output, $data_object, $depth = 0, $args = null ) { |
| 158 |
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { |
| 159 |
$t = ''; |
| 160 |
$n = ''; |
| 161 |
} else { |
| 162 |
$t = "\t"; |
| 163 |
$n = "\n"; |
| 164 |
} |
| 165 |
if ( $args->walker->has_children ) { |
| 166 |
$output .= '</details>'; |
| 167 |
} |
| 168 |
$output .= "</li>{$n}"; |
| 169 |
} |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
|