<?php
/**
 * Locale adjustments for front-end date formatting.
 *
 * @package WPR_Translate
 */

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

/**
 * Ensure the active language locale is used for front-end date formatting.
 *
 * @param string $locale Current locale.
 *
 * @return string
 */
function wpr_translate_filter_frontend_locale( $locale ) {
    // Only adjust locale on the front end.
    if ( is_admin() || wp_doing_ajax() ) {
        return $locale;
    }

    if ( ! function_exists( 'wpr_translate_get_active_locale' ) ) {
        return $locale;
    }

    // Prefer the active language locale when available.
    $active_locale = wpr_translate_get_active_locale();

    if ( '' === $active_locale ) {
        return $locale;
    }

    return $active_locale;
}

// Apply the locale filter early so get_the_date() uses the active language locale.
add_filter( 'locale', 'wpr_translate_filter_frontend_locale', 5 );

/**
 * Re-format front-end post dates using the active language locale.
 *
 * @param string|int $the_date Formatted date string or timestamp for U/G formats.
 * @param string     $format   PHP date format.
 * @param WP_Post    $post     Post object.
 *
 * @return string|int
 */
function wpr_translate_filter_get_the_date( $the_date, $format, $post ) {
    // Only adjust front-end output.
    if ( is_admin() || wp_doing_ajax() ) {
        return $the_date;
    }

    if ( ! function_exists( 'wpr_translate_get_active_locale' ) ) {
        return $the_date;
    }

    if ( ! function_exists( 'switch_to_locale' ) || ! function_exists( 'restore_previous_locale' ) ) {
        return $the_date;
    }

    $format = (string) $format;

    if ( 'U' === $format || 'G' === $format ) {
        // Do not alter timestamp-only formats.
        return $the_date;
    }

    $active_locale = wpr_translate_get_active_locale();

    if ( '' === $active_locale ) {
        return $the_date;
    }

    $post = get_post( $post );

    if ( ! $post ) {
        return $the_date;
    }

    // Match core behavior by formatting the post time in local time.
    $timestamp = get_post_time( 'U', false, $post, true );

    if ( ! $timestamp ) {
        return $the_date;
    }

    // Temporarily switch locale so date_i18n uses the active language.
    $switched = switch_to_locale( $active_locale );

    if ( ! $switched && function_exists( 'load_default_textdomain' ) && class_exists( 'WP_Locale' ) ) {
        // Ensure the default textdomain and locale object match the active language.
        load_default_textdomain( $active_locale );
        $GLOBALS['wp_locale'] = new WP_Locale();
    }

    $use_format = '' !== $format ? $format : get_option( 'date_format' );
    $translated = date_i18n( $use_format, $timestamp );

    if ( $switched ) {
        // Restore the previous locale after formatting.
        restore_previous_locale();
    }

    if ( '' === $translated ) {
        return $the_date;
    }

    return $translated;
}

// Re-translate post dates after core formatting is applied.
add_filter( 'get_the_date', 'wpr_translate_filter_get_the_date', 20, 3 );

/**
 * Re-format front-end modified dates using the active language locale.
 *
 * @param string|int $the_date Formatted date string or timestamp for U/G formats.
 * @param string     $format   PHP date format.
 * @param WP_Post    $post     Post object.
 *
 * @return string|int
 */
function wpr_translate_filter_get_the_modified_date( $the_date, $format, $post ) {
    // Only adjust front-end output.
    if ( is_admin() || wp_doing_ajax() ) {
        return $the_date;
    }

    if ( ! function_exists( 'wpr_translate_get_active_locale' ) ) {
        return $the_date;
    }

    if ( ! function_exists( 'switch_to_locale' ) || ! function_exists( 'restore_previous_locale' ) ) {
        return $the_date;
    }

    $format = (string) $format;

    if ( 'U' === $format || 'G' === $format ) {
        // Do not alter timestamp-only formats.
        return $the_date;
    }

    $active_locale = wpr_translate_get_active_locale();

    if ( '' === $active_locale ) {
        return $the_date;
    }

    $post = get_post( $post );

    if ( ! $post ) {
        return $the_date;
    }

    // Match core behavior by formatting the post modified time in local time.
    $timestamp = get_post_modified_time( 'U', false, $post, true );

    if ( ! $timestamp ) {
        return $the_date;
    }

    // Temporarily switch locale so date_i18n uses the active language.
    $switched = switch_to_locale( $active_locale );

    if ( ! $switched && function_exists( 'load_default_textdomain' ) && class_exists( 'WP_Locale' ) ) {
        // Ensure the default textdomain and locale object match the active language.
        load_default_textdomain( $active_locale );
        $GLOBALS['wp_locale'] = new WP_Locale();
    }

    $use_format = '' !== $format ? $format : get_option( 'date_format' );
    $translated = date_i18n( $use_format, $timestamp );

    if ( $switched ) {
        // Restore the previous locale after formatting.
        restore_previous_locale();
    }

    if ( '' === $translated ) {
        return $the_date;
    }

    return $translated;
}

// Re-translate post modified dates after core formatting is applied.
add_filter( 'get_the_modified_date', 'wpr_translate_filter_get_the_modified_date', 20, 3 );

/**
 * Re-format front-end post times using the active language locale.
 *
 * @param string|int $the_time Formatted time string or timestamp for U/G formats.
 * @param string     $format   PHP time format.
 * @param WP_Post    $post     Post object.
 *
 * @return string|int
 */
function wpr_translate_filter_get_the_time( $the_time, $format, $post ) {
    // Only adjust front-end output.
    if ( is_admin() || wp_doing_ajax() ) {
        return $the_time;
    }

    if ( ! function_exists( 'wpr_translate_get_active_locale' ) ) {
        return $the_time;
    }

    if ( ! function_exists( 'switch_to_locale' ) || ! function_exists( 'restore_previous_locale' ) ) {
        return $the_time;
    }

    $format = (string) $format;

    if ( 'U' === $format || 'G' === $format ) {
        // Do not alter timestamp-only formats.
        return $the_time;
    }

    $active_locale = wpr_translate_get_active_locale();

    if ( '' === $active_locale ) {
        return $the_time;
    }

    $post = get_post( $post );

    if ( ! $post ) {
        return $the_time;
    }

    // Match core behavior by formatting the post time in local time.
    $timestamp = get_post_time( 'U', false, $post, true );

    if ( ! $timestamp ) {
        return $the_time;
    }

    // Temporarily switch locale so date_i18n uses the active language.
    $switched = switch_to_locale( $active_locale );

    if ( ! $switched && function_exists( 'load_default_textdomain' ) && class_exists( 'WP_Locale' ) ) {
        // Ensure the default textdomain and locale object match the active language.
        load_default_textdomain( $active_locale );
        $GLOBALS['wp_locale'] = new WP_Locale();
    }

    $use_format = '' !== $format ? $format : get_option( 'time_format' );
    $translated = date_i18n( $use_format, $timestamp );

    if ( $switched ) {
        // Restore the previous locale after formatting.
        restore_previous_locale();
    }

    if ( '' === $translated ) {
        return $the_time;
    }

    return $translated;
}

// Re-translate post times after core formatting is applied.
add_filter( 'get_the_time', 'wpr_translate_filter_get_the_time', 20, 3 );
