<?php
/**
 * Add the active language code as a data attribute on the body element.
 *
 * @package WPR_Translate
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Resolve the current language code for body attributes.
 *
 * @return string
 */
function wpr_translate_get_body_language_code() {
    // Start with an empty language code.
    $language_code = '';

    if ( is_admin() && function_exists( 'wpr_translate_return_current_language_admin' ) ) {
        // Use the admin-selected language when editing content in wp-admin.
        $language_code = sanitize_key( wpr_translate_return_current_language_admin() );
    }

    if ( '' === $language_code && ! is_admin() && function_exists( 'wpestate_get_current_language' ) ) {
        // Use the front-end helper to keep consistency with theme expectations.
        $frontend_language = sanitize_key( wpestate_get_current_language() );

        if ( '' !== $frontend_language && function_exists( 'wpestate_translation_get_active_languages' ) ) {
            $languages = wpestate_translation_get_active_languages();

            if ( isset( $languages[ $frontend_language ]['code'] ) ) {
                $frontend_language = sanitize_key( $languages[ $frontend_language ]['code'] );
            }
        }

        $language_code = $frontend_language;
    }

    if ( '' === $language_code && function_exists( 'wpestate_translation_get_active_languages' ) && function_exists( 'wpr_translation_get_default_language_code' ) ) {
        // Fall back to the default language code when no explicit selection exists.
        $languages     = wpestate_translation_get_active_languages();
        $language_code = wpr_translation_get_default_language_code( array_values( $languages ) );
        $language_code = sanitize_key( $language_code );
    }

    // Return the normalized language code.
    return $language_code;
}

/**
 * Print a script that stamps the body with the active language code.
 *
 * @return void
 */
function wpr_translate_print_body_language_attribute() {
    static $printed = false;

    if ( $printed ) {
        // Avoid printing the script multiple times.
        return;
    }

    $language_code = wpr_translate_get_body_language_code();

    if ( '' === $language_code ) {
        // No language code means no attribute to set.
        return;
    }

    // Output a small inline script to set the data attribute for automated tests.
    printf(
        '<script>(function(){var body=document.body;var lang=%s;if(!body){document.addEventListener("DOMContentLoaded",function(){document.body&&document.body.setAttribute("data-wpr-language",lang);});return;}body.setAttribute("data-wpr-language",lang);})();</script>',
        wp_json_encode( $language_code )
    );

    // Mark the attribute as printed for this request.
    $printed = true;
}

add_action( 'wp_body_open', 'wpr_translate_print_body_language_attribute', 5 );
add_action( 'wp_footer', 'wpr_translate_print_body_language_attribute', 1 );
add_action( 'admin_footer', 'wpr_translate_print_body_language_attribute', 1 );
