<?php
defined('ABSPATH') || exit;

/**
 * REEID Page SEO — Hreflang (NON-WOO ONLY)
 *
 * Rules:
 * - Emits hreflang ONLY for real, published translations
 * - Uses ONE authoritative hub (true source)
 * - Emits EXACT SAME cluster on ALL pages
 * - NO license gating
 * - NO retroactive repair
 * - NO guessing
 * - STRICT language prefix mode
 * - FORCE percent-encoded path output (UTF-8 safe)
 */

add_action('wp_head', function () {

    // -------------------------------
    // Hard guards
    // -------------------------------
    if ( is_admin() ) {
        return;
    }

    if ( function_exists('is_product') && is_product() ) {
        return;
    }

    if ( function_exists('is_woocommerce') && is_woocommerce() ) {
        return;
    }

   
    if ( ! get_option('reeid_seo_override_hreflang') ) {
        return;
    }

    if ( ! is_singular() ) {
        return;
    }

    $post_id = (int) get_queried_object_id();
    if ( $post_id <= 0 ) {
        return;
    }

    // -------------------------------
    // Resolve TRUE hreflang hub ONCE
    // -------------------------------
    $source_id = (int) get_post_meta($post_id, '_reeid_translation_source', true);
    if ( $source_id <= 0 ) {
        $source_id = $post_id;
    }

    // -------------------------------
    // Load authoritative translation map
    // -------------------------------
    $map = get_post_meta($source_id, '_reeid_translation_map', true);
    if ( ! is_array($map) || empty($map) ) {
        return;
    }

 // -------------------------------
// Build cluster (FORCED PREFIX SAFE)
// -------------------------------
$printed      = [];
$default_lang = get_option( 'reeid_translation_source_lang' );

foreach ( $map as $lang => $pid ) {

    $pid = (int) $pid;

    if ( $pid <= 0 ) {
        continue;
    }

    if ( get_post_status( $pid ) !== 'publish' ) {
        continue;
    }

    $lang_code = get_post_meta( $pid, '_reeid_translation_lang', true );
    if ( ! $lang_code ) {
        $lang_code = $default_lang;
    }

    $lang_code = strtolower( trim( (string) $lang_code ) );

    $post = get_post( $pid );
    if ( ! $post ) {
        continue;
    }

    // Use raw post_name to avoid filter conflicts
    $url = get_permalink( $pid );
$url = esc_url_raw( $url );
$url = preg_replace_callback(
    '/[^A-Za-z0-9\-._~:\/?#\[\]@!$&\'()*+,;=%]/u',
    function($m) { return rawurlencode($m[0]); },
    $url
);


    $printed[ $lang_code ] = $url;
}

if ( empty( $printed ) ) {
    return;
}


    // -------------------------------
    // Emit EXACT SAME SET everywhere
    // -------------------------------
    foreach ( $printed as $lang => $url ) {
        echo "\n" . '<link rel="alternate" hreflang="' . esc_attr($lang) . '" href="' . esc_url($url) . '" />';
    }

    // x-default = default source language URL
    if ( $default_lang && isset($printed[$default_lang]) ) {
        echo "\n" . '<link rel="alternate" hreflang="x-default" href="' . esc_url($printed[$default_lang]) . "\" />\n";
    }

}, 1);
