summaryrefslogtreecommitdiffhomepage
path: root/plugins/class-wp-picocss-navwalker.php
blob: 1cf617d9b88375bbcbc5b2fe703fc222bd2e543d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
class Pico_Details_Nav_Walker extends Walker_Nav_Menu {

    // Only called for submenus
    public function start_lvl( &$output, $depth = 0, $args = array() ) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<ul class=\"dropdown-menu\">\n";
    }

    public function end_lvl( &$output, $depth = 0, $args = array() ) {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul>\n";
    }

    public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        $has_children = in_array('menu-item-has-children', $item->classes);

        // Top-level item with children -> use <details>
        if ($depth === 0 && $has_children) {
            $output .= '<details class="dropdown">';
            $output .= '<summary role="button" class="outline contrast" data-theme="dark">' . esc_html($item->title) . '</summary>';
        } 
        // Top-level item WITHOUT children -> <li> with data-theme="dark"
        elseif ($depth === 0 && !$has_children) {
            $atts = [];
            $atts['href'] = !empty($item->url) ? esc_url($item->url) : '#';
            $attributes = '';
            foreach ($atts as $attr => $value) {
                $attributes .= " $attr=\"$value\"";
            }

            $output .= '<li data-theme="dark"><a' . $attributes . ' class="contrast">' . esc_html($item->title) . '</a>';
        }
        // Submenu items -> normal <li> without data-theme
        else {
            $atts = [];
            $atts['href'] = !empty($item->url) ? esc_url($item->url) : '#';
            $attributes = '';
            foreach ($atts as $attr => $value) {
                $attributes .= " $attr=\"$value\"";
            }

            $output .= '<li><a' . $attributes . ' class="contrast">' . esc_html($item->title) . '</a>';
        }
    }


    public function end_el( &$output, $item, $depth = 0, $args = array() ) {
        $has_children = in_array('menu-item-has-children', $item->classes);

        if ($depth === 0 && $has_children) {
            $output .= '</details>';
        } else {
            $output .= '</li>';
        }
    }
}