<?php
/**
 * Diagnostic script for Polylang configuration.
 */

// Load WordPress
require_once dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/wp-load.php';

if ( ! function_exists( 'pll_languages_list' ) ) {
    die( "Error: Polylang is not active or API functions are not available.\n" );
}

echo "--- POLYLANG LANGUAGES ---\n";
$languages = pll_languages_list( array( 'fields' => 'locale' ) );
$languages_slugs = pll_languages_list( array( 'fields' => 'slug' ) );
print_r( $languages );
print_r( $languages_slugs );

echo "--- NAV MENU LOCATIONS & ASSIGNMENTS ---\n";
$locations = get_registered_nav_menus();
$nav_menu_locations = get_nav_menu_locations();
foreach ( $locations as $location => $description ) {
    $assigned_menu_id = isset( $nav_menu_locations[ $location ] ) ? $nav_menu_locations[ $location ] : 0;
    echo "Location: {$location} ({$description}) | Assigned Menu ID: {$assigned_menu_id}\n";
}

echo "--- EXISTING MENUS & ITEMS ---\n";
$menus = wp_get_nav_menus();
foreach ( $menus as $menu ) {
    echo "Menu ID: {$menu->term_id} | Name: {$menu->name} | Slug: {$menu->slug}\n";
    $menu_items = wp_get_nav_menu_items( $menu->term_id );
    if ( $menu_items ) {
        foreach ( $menu_items as $item ) {
            echo "  - Item ID: {$item->ID} | Title: {$item->title} | Object ID (Post/Page/etc): {$item->object_id} | Type: {$item->type} | Parent: {$item->menu_item_parent}\n";
        }
    } else {
        echo "  (No items)\n";
    }
}

echo "--- POLYLANG OPTIONS ---\n";
$pll_options = get_option( 'polylang' );
print_r( $pll_options );

echo "--- EXISTING PAGES ---\n";
$pages = get_pages();
foreach ( $pages as $page ) {
    $lang = pll_get_post_language( $page->ID );
    echo "Page ID: {$page->ID} | Title: {$page->post_title} | Slug: {$page->post_name} | Language: {$lang}\n";
}
