<?php

function spiko_page_menu_args($args) {
    if (!isset($args['show_home']))
        $args['show_home'] = true;
    return $args;
}

add_filter('wp_page_menu_args', 'spiko_page_menu_args');

function spiko_fallback_page_menu($args = array()) {
    $defaults = array('sort_column' => 'menu_order, post_title', 'menu_class' => 'menu', 'echo' => true, 'link_before' => '', 'link_after' => '');
    $args = wp_parse_args($args, $defaults);
    $args = apply_filters('wp_page_menu_args', $args);
    $menu = '';
    $list_args = $args;
    // Show Home in the menu
    if (!empty($args['show_home'])) {
        if (true === $args['show_home'] || '1' === $args['show_home'] || 1 === $args['show_home'])
            $text = esc_html__('Home', 'spiko' );
        else
            $text = $args['show_home'];
        $class = '';
        if (is_front_page() && !is_paged()) {
            $class = 'class="nav-item menu-item active"';
        } else {
            $class = 'class="nav-item menu-item"';
        }
        $menu .= '<li ' . $class . '><a class="nav-link 1" href="' . esc_url(home_url('/')) . '" title="' . esc_attr($text) . '">' . $args['link_before'] . $text . $args['link_after'] . '</a></li>';
        // If the front page is a page, add it to the exclude list
        if (get_option('show_on_front') == 'page') {
            if (!empty($list_args['exclude'])) {
                $list_args['exclude'] .= ',';
            } else {
                $list_args['exclude'] = '';
            }
            $list_args['exclude'] .= get_option('page_on_front');
        }
    }
    $list_args['echo'] = false;
    $list_args['title_li'] = '';
    $list_args['walker'] = new Spiko_Walker_Page_Menu;
    $menu .= str_replace(array("\r", "\n", "\t"), '', wp_list_pages($list_args));
    if ($menu)
        $menu = '<ul class="' . esc_attr($args['menu_class']) . '">' . $menu . '</ul>';

    $menu = $menu . "\n";
    $menu = apply_filters('wp_page_menu', $menu, $args);
    if ($args['echo'])
      echo $menu;
    else
        return $menu;
}

class Spiko_Walker_Page_Menu extends Walker_Page {

    function start_lvl(&$output, $depth = 0, $args = array()) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<ul class='dropdown-menu default'>\n";
    }

    function start_el(&$output, $page, $depth = 0, $args = array(), $current_page = 0) {
        if ($depth)
            $indent = str_repeat("\t", $depth);
        else
            $indent = '';

        if ($depth === 0) {
            $child_class = 'nav-link';
        } else if ($depth > 0) {
            $child_class = 'dropdown-item';
        } else {
            $child_class = '';
        }
        extract($args, EXTR_SKIP);
        $css_class = array('menu-item page_item dropdown', 'page-item-' . $page->ID);
        if (!empty($current_page)) {
            $_current_page = get_post($current_page);
            if (in_array($page->ID, $_current_page->ancestors))
                $css_class[] = 'current_page_ancestor';
            if ($page->ID == $current_page)
                $css_class[] = 'nav-item active';
            elseif ($_current_page && $page->ID == $_current_page->post_parent)
                $css_class[] = 'current_page_parent';
        } elseif ($page->ID == get_option('page_for_posts')) {
            $css_class[] = 'current_page_parent';
        }
        $css_class = implode(' ', apply_filters('page_css_class', $css_class, $page, $depth, $args, $current_page));
        $output .= $indent . '<li class="nav-item ' . $css_class . '"><a class="' . $child_class . '" href="' . get_permalink($page->ID) . '"><span class="menu-text">' . $link_before . apply_filters('the_title', $page->post_title, $page->ID) . $link_after . '</span></a>';
        if (!empty($show_date)) {
            if ('modified' == $show_date)
                $time = $page->post_modified;
            else
                $time = $page->post_date;
            $output .= " " . mysql2date($date_format, $time);
        }
    }

}