<?php

/* -----------------------------------------------------------
	Walker to ReWrap li submenu parent with button instead of
 -----------------------------------------------------------*/

function wrap_parent_menu_item_buttons($output, $item, $depth, $args)
{
    // #### MENU HOMEGRADE HEADER
    if ($args->theme_location === "homegrade" && $args->menu_id === "menu-homegrade-navlist" && in_array('menu-item-has-children', $item->classes, true)) {
        $output = '<button type="button" class="menu-item__submenu-toggle" aria-expanded="false" aria-controls="sub-menu-' . $item->ID . '">' . $item->title . '</button>';
    }

    // #### MENU HOMEGRADE FOOTER
    if ($args->theme_location === "homegrade" && $args->menu_class === "menu-homegrade--footer" && in_array('menu-item-has-children', $item->classes, true)) {
        $output = '<h6 class="page-group-title">' . $item->title . '</h6>';
    }

    return $output;
}
add_filter('walker_nav_menu_start_el', 'wrap_parent_menu_item_buttons', 10, 4);




/* -----------------------------------------------------------
	Hide Page presse from homegrade top-menu but not footer
 -----------------------------------------------------------*/


function metiers_patrimoine_filter_menu_items($items, $args)
{

    // ID de l'élément de menu à masquer
    $isMenuHomegradeInFooter = $args->menu_class === 'menu-homegrade--footer';

    foreach ($items as $key => $item) {
        $isPagePresse = $item->object_id == "2784" || $item->object_id == "4973";

        if ($isPagePresse && !$isMenuHomegradeInFooter) {
            unset($items[$key]);
        }
    }

    return $items;
}
add_filter('wp_nav_menu_objects', 'metiers_patrimoine_filter_menu_items', 10, 2);



/* -----------------------------------------------------------
	PASS CUSTOM WPML NAV SWITCHER TO MENU
 -----------------------------------------------------------*/

add_filter('wp_nav_menu_items', 'add_custom_item_to_menu', 10, 2);
function add_custom_item_to_menu($items, $args)
{

    $isMenuHomegradeInFooter = $args->menu_class === 'menu-homegrade--footer';

    // restore_current_blog();



    if ($args->theme_location === 'homegrade' && !$isMenuHomegradeInFooter) {
        /* BOUTON METIERS DU PATRIMOINE */

        // Getting label before switching blog
        $current_lang = apply_filters('wpml_current_language', null);

        $button_label = $current_lang == 'nl' ?  'Vakmensen in het erfgoed' : 'Artisans du patrimoine';
        switch_to_blog(2);
        // $home_url_metiers_patrimoine = get_home_url();
        $home_url_metiers_patrimoine = get_the_permalink(43);
        restore_current_blog();

        $items .= '<li id="menu-item-artisan-du-patrimoine" class="menu-item menu-item-type-post_type menu-item-object-page menu-navlink ">';
        $items .= '<a href="' . $home_url_metiers_patrimoine . '">';
        $items .=  $button_label;
        $items .= '</a>';
        $items .= '</li>';


        /* WPML NAV SWITCHER */
        ob_start();
        restore_current_blog();
        // do_action('wpml_language_switcher', array('flags' => false, 'translated' => false, 'link_current' => true));
        do_action('wpml_add_language_selector');

        switch_to_blog(1);
        $wpmlnav = ob_get_clean();

        $items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page menu-navlink">';
        $items .= $wpmlnav;
        $items .= '</li>';
    }


    // switch_to_blog(1);

    return $items;
}
