<?php

if (! defined('ABSPATH')) {
    exit;
} function puffergo_resolve_menu_id($location, $menu_name)
{
    $locations = get_nav_menu_locations();

    if (! is_array($locations)) {
        $locations = array();
    }

if (! empty($locations[ $location ])) {
        return (int) $locations[ $location ];
    } $menu_obj = wp_get_nav_menu_object($menu_name);

    if ($menu_obj && isset($menu_obj->term_id)) {
        $menu_id = (int) $menu_obj->term_id;
        $locations[ $location ] = $menu_id;
        set_theme_mod('nav_menu_locations', $locations);

        return $menu_id;
    }

return 0;
} function puffergo_get_menu_tree($location, $menu_name)
{
    $menu_id = puffergo_resolve_menu_id($location, $menu_name);

    if ($menu_id <= 0) {
        return array();
    } $menu_items = wp_get_nav_menu_items($menu_id);

    if (! is_array($menu_items) || empty($menu_items)) {
        return array();
    } $by_parent = array();

    foreach ($menu_items as $item) {
        $pid = (int) $item->menu_item_parent;

        if (! isset($by_parent[ $pid ])) {
            $by_parent[ $pid ] = array();
        } $by_parent[ $pid ][] = $item;
    }

return puffergo_build_menu_tree($by_parent, 0, PufferGo_Polylang::current_language());
} function puffergo_build_menu_tree(array $by_parent, $parent_id, $lang = '')
{
    $items = isset($by_parent[ $parent_id ]) ? $by_parent[ $parent_id ] : array();

    if (empty($items)) {
        return array();
    } usort($items, function ($a, $b) { return ((int) $a->menu_order) - ((int) $b->menu_order); });
    $out = array();

    foreach ($items as $item) {
        $obj = new stdClass();
        $obj->id = isset($item->ID) ? (int) $item->ID : 0;
        $obj->url = '' !== $lang ? PufferGo_Polylang::localize_url((string) $item->url, $lang) : (string) $item->url;
        $obj->title = $item->title;
        $obj->object_id = isset($item->object_id) ? (int) $item->object_id : 0;
        $obj->object = isset($item->object) ? $item->object : '';
        $obj->icon = puffergo_get_menu_item_icon_slug($obj->id);
        $obj->description = isset($item->description) ? $item->description : '';
        $obj->children = puffergo_build_menu_tree($by_parent, (int) $item->ID, $lang);
        $out[] = $obj;
    }

return $out;
} function puffergo_ensure_primary_menu()
{
    $menu_id = puffergo_resolve_menu_id('puffergo_primary', 'PufferGo Header');

    if ($menu_id > 0) {
        return $menu_id;
    } $created = wp_create_nav_menu('PufferGo Header');

    if (is_wp_error($created)) {
        return 0;
    } $menu_id = (int) $created;
    $locations = get_nav_menu_locations();

    if (! is_array($locations)) {
        $locations = array();
    } $locations['puffergo_primary'] = $menu_id;
    set_theme_mod('nav_menu_locations', $locations);

    return $menu_id;
} function puffergo_ensure_footer_menu()
{
    $menu_id = puffergo_resolve_menu_id('puffergo_footer', 'PufferGo Footer');

    if ($menu_id > 0) {
        return $menu_id;
    } $created = wp_create_nav_menu('PufferGo Footer');

    if (is_wp_error($created)) {
        return 0;
    } $menu_id = (int) $created;
    $locations = get_nav_menu_locations();

    if (! is_array($locations)) {
        $locations = array();
    } $locations['puffergo_footer'] = $menu_id;
    set_theme_mod('nav_menu_locations', $locations);

    return $menu_id;
} function puffergo_add_custom_link_to_menu($menu_id, $title, $url)
{
    $items = wp_get_nav_menu_items($menu_id);

    if (is_array($items)) {
        foreach ($items as $item) {
            if ($item->url === $url) {
                return true;
            }
        }
    } $result = wp_update_nav_menu_item($menu_id, 0, array('menu-item-title' => $title, 'menu-item-url' => $url, 'menu-item-type' => 'custom', 'menu-item-status' => 'publish', ));

    return ! is_wp_error($result);
} function puffergo_add_page_to_menu($menu_id, $object_id, $title = '')
{
    $items = wp_get_nav_menu_items($menu_id);

    if (is_array($items)) {
        foreach ($items as $item) {
            if ((int) $item->object_id === (int) $object_id) {
                return true;
            }
        }
    }

if ('' === $title) {
        $title = get_the_title($object_id);
    } $result = wp_update_nav_menu_item($menu_id, 0, array('menu-item-title' => $title, 'menu-item-object' => 'page', 'menu-item-object-id' => $object_id, 'menu-item-type' => 'post_type', 'menu-item-status' => 'publish', ));

    return ! is_wp_error($result);
} function puffergo_sync_primary_menu(array $items)
{
    $menu_id = puffergo_ensure_primary_menu();

    if ($menu_id <= 0) {
        return false;
    } $snapshot = wp_get_nav_menu_items($menu_id) ?: array();
    puffergo_sync_menu_level($menu_id, $items, 0, $snapshot);

    return true;
} function puffergo_rebuild_footer_menu(array $sections)
{
    $menu_id = puffergo_ensure_footer_menu();

    if ($menu_id <= 0) {
        return false;
    } $existing = wp_get_nav_menu_items($menu_id);

    if (is_array($existing)) {
        foreach ($existing as $item) {
            if (get_post_meta($item->ID, '_puffergo_managed', true)) {
                wp_delete_post($item->ID, true);
            }
        }
    }

foreach ($sections as $section) {
        $title = isset($section['title']) ? sanitize_text_field((string) $section['title']) : '';
        $links = isset($section['links']) && is_array($section['links']) ? $section['links'] : array();

        if ($title === '' || empty($links)) {
            continue;
        } $parent_id = wp_update_nav_menu_item($menu_id, 0, array('menu-item-title' => $title, 'menu-item-url' => '#', 'menu-item-type' => 'custom', 'menu-item-status' => 'publish', ));

        if (is_wp_error($parent_id)) {
            continue;
        } update_post_meta($parent_id, '_puffergo_managed', true);

        foreach ($links as $link) {
            $name = isset($link['name']) ? sanitize_text_field((string) $link['name']) : '';
            $url = puffergo_normalize_link_url(isset($link['url']) ? (string) $link['url'] : '');

            if ($name === '' || $url === '') {
                continue;
            } $child_id = wp_update_nav_menu_item($menu_id, 0, array('menu-item-title' => $name, 'menu-item-url' => $url, 'menu-item-type' => 'custom', 'menu-item-status' => 'publish', 'menu-item-parent-id' => (int) $parent_id, ));

            if (! is_wp_error($child_id)) {
                update_post_meta($child_id, '_puffergo_managed', true);
            }
        }
    }

return true;
} function puffergo_sync_menu_level($menu_id, array $items, $parent_id, array $snapshot)
{
    $managed_by_id = array();

    foreach ($snapshot as $item) {
        if ((int) $item->menu_item_parent !== (int) $parent_id) {
            continue;
        }

if (! get_post_meta($item->ID, '_puffergo_managed', true)) {
            continue;
        } $managed_by_id[ $item->ID ] = $item;
    } $matched_ids = array();
    $order = 1;

    foreach ($items as $spec) {
        $label = isset($spec['label']) ? sanitize_text_field((string) $spec['label']) : '';
        $url = isset($spec['url']) ? (string) $spec['url'] : '';
        $children = isset($spec['children']) && is_array($spec['children']) ? $spec['children'] : array();

        if ($label === '' || $url === '') {
            continue;
        } $path = puffergo_url_to_path($url);
        $found = null;

        foreach ($managed_by_id as $candidate) {
            if (puffergo_url_to_path($candidate->url) === $path) {
                $found = $candidate;
                break;
            }
        }

if ($found) {
            if ($found->title !== $label || (int) $found->menu_order !== $order) {
                wp_update_nav_menu_item($menu_id, $found->ID, array('menu-item-title' => $label, 'menu-item-url' => $url, 'menu-item-status' => 'publish', 'menu-item-parent-id' => (int) $parent_id, 'menu-item-position' => $order, ));
            } $item_id = $found->ID;
            $matched_ids[ $found->ID ] = true;
        } else {
            $item_id = wp_update_nav_menu_item($menu_id, 0, array('menu-item-title' => $label, 'menu-item-url' => $url, 'menu-item-type' => 'custom', 'menu-item-status' => 'publish', 'menu-item-parent-id' => (int) $parent_id, 'menu-item-position' => $order, ));

            if (is_wp_error($item_id)) {
                continue;
            } update_post_meta($item_id, '_puffergo_managed', true);
        } $order++;

        if (! empty($children)) {
            puffergo_sync_menu_level($menu_id, $children, $item_id, $snapshot);
        }
    }

foreach ($managed_by_id as $id => $item) {
        if (! isset($matched_ids[ $id ])) {
            puffergo_delete_menu_item_tree($id, $snapshot);
        }
    }
} function puffergo_delete_menu_item_tree($item_id, array $snapshot)
{
    foreach ($snapshot as $child) {
        if ((int) $child->menu_item_parent === (int) $item_id) {
            puffergo_delete_menu_item_tree($child->ID, $snapshot);
        }
    } wp_delete_post($item_id, true);
} function puffergo_url_to_path($url)
{
    if ($url === '#') {
        return '#';
    } $path = (string) wp_parse_url($url, PHP_URL_PATH);

    return '/' . trim($path, '/');
} function puffergo_normalize_link_url($url)
{
    $url = trim((string) $url);

    if ($url === '' || $url === '#') {
        return $url;
    }

if (preg_match('/^[a-zA-Z][a-zA-Z0-9+\-.]*:/', $url) === 1) {
        return $url;
    }

if (false !== filter_var($url, FILTER_VALIDATE_EMAIL)) {
        $email = sanitize_email($url);

        return $email !== '' ? 'mailto:' . $email : '';
    }

if (preg_match('/^[0-9+\s().\\-]{5,}$/', $url) === 1) {
        $phone = preg_replace('/[^0-9+]/', '', $url);

        return $phone !== '' ? 'tel:' . $phone : '';
    }

return home_url('/') . ltrim($url, '/');
}
