Skip to content
wiki.fftac.org

Localerepository - Source Excerpt 01

Back to Localerepository

Summary

This source excerpt preserves a bounded section of Antichrist.net/wp-content/plugins/uaix-locale-router/src/LocaleRepository.php so readers can inspect the evidence without opening the full source file.

**Source path:** Antichrist.net/wp-content/plugins/uaix-locale-router/src/LocaleRepository.php

<?php

namespace UAIXLocaleRouter;

use UAIXLocaleRouter\Support\Json;


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

final class LocaleRepository {
	/**
	 * Cached locale catalog.
	 *
	 * @var array|null
	 */
	private static $catalog = null;

	/**
	 * Cached supported locales for this request.
	 *
	 * @var array|null
	 */
	private static $supported_locales = null;

	/**
	 * Return supported locales.
	 *
	 * @return array
	 */
	public static function get_supported_locales() {
		if ( null !== self::$supported_locales ) {
			return self::$supported_locales;
		}

		$supported = get_option( 'uaixlr_supported_locales', array() );

		if ( ! is_array( $supported ) || empty( $supported ) ) {
			$supported = Json::decode_file( Plugin::default_supported_locales_path(), array() );
		}

		$supported = self::merge_missing_default_locales( is_array( $supported ) ? $supported : array() );

		$normalized = array();

		foreach ( $supported as $record ) {
			if ( ! is_array( $record ) ) {
				continue;
			}

			$normalized[] = self::hydrate_display_metadata( LocaleValidator::normalize_supported_record( $record ) );
		}

		self::$supported_locales = $normalized;

		return self::$supported_locales;
	}

	/**
	 * Merge bundled default locales that are missing from saved settings.
	 *
	 * @param array $supported Locale records.
	 * @return array
	 */
	private static function merge_missing_default_locales( array $supported ) {
		$defaults = Json::decode_file( Plugin::default_supported_locales_path(), array() );

		if ( ! is_array( $defaults ) || empty( $defaults ) ) {
			return $supported;
		}

		$known_tags = array();

		foreach ( $supported as $record ) {
			if ( ! is_array( $record ) ) {
				continue;
			}

			$tag = isset( $record['tag'] ) ? LocaleValidator::canonicalize_tag( $record['tag'] ) : '';

			if ( '' !== $tag ) {
				$known_tags[ $tag ] = true;
			}
		}

		foreach ( $defaults as $record ) {
			if ( ! is_array( $record ) ) {
				continue;
			}

			$tag = isset( $record['tag'] ) ? LocaleValidator::canonicalize_tag( $record['tag'] ) : '';

			if ( '' === $tag || isset( $known_tags[ $tag ] ) ) {
				continue;
			}

			$supported[]        = $record;
			$known_tags[ $tag ] = true;
		}

		return $supported;
	}

	/**
	 * Persist supported locales.
	 *
	 * @param array $locales Locale records.
	 * @return void
	 */
	public static function save_supported_locales( array $locales ) {
		$normalized = array();

		foreach ( $locales as $record ) {
			if ( ! is_array( $record ) ) {
				continue;
			}

			$tag = isset( $record['tag'] ) ? LocaleValidator::canonicalize_tag( $record['tag'] ) : '';

			if ( '' === $tag ) {
				continue;
			}

			$normalized[ $tag ] = LocaleValidator::normalize_supported_record( $record );
		}

		update_option( 'uaixlr_supported_locales', array_values( $normalized ) );
		self::$supported_locales = null;
	}

	/**
	 * Enabled locales only.
	 *
	 * @return array
	 */
	public static function get_enabled_locales() {
		return array_values(
			array_filter(
				self::get_supported_locales(),
				static function ( $record ) {
					return ! empty( $record['enabled'] );
				}
			)
		);
	}

	/**
	 * Default locale tag.
	 *
	 * @return string
	 */
	public static function get_default_locale() {
		$settings = Plugin::settings();
		$locale   = isset( $settings['default_locale'] ) ? LocaleValidator::canonicalize_tag( $settings['default_locale'] ) : '';

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

		foreach ( self::get_supported_locales() as $record ) {
			if ( ! empty( $record['isDefault'] ) ) {
				return $record['tag'];
			}
		}

		return 'en-US';
	}

	/**
	 * Default locale URL tag.
	 *
	 * @return string
	 */
	public static function get_default_url_tag() {
		return self::locale_to_url_tag( self::get_default_locale() );
	}

	/**
	 * Resolve the best locale URL tag for a bare request.
	 *
	 * Falls back to the admin-configured default locale when no
	 * enabled locale matches the browser language preferences.
	 *
	 * @param string $accept_language Accept-Language header value.
	 * @return string
	 */
	public static function get_redirect_url_tag_for_request( $accept_language = '' ) {
		$enabled_locales = self::get_enabled_locales();
		$default_url_tag = self::get_default_url_tag();

		if ( empty( $enabled_locales ) ) {
			return $default_url_tag;
		}

		foreach ( self::parse_accept_language_header( $accept_language ) as $preferred_tag ) {
			$match = self::match_enabled_locale_by_preference( $preferred_tag, $enabled_locales );

			if ( ! empty( $match['urlTag'] ) ) {
				return $match['urlTag'];
			}
		}

		return $default_url_tag;
	}

	/**
	 * Convert a locale tag to a URL tag.
	 *
	 * @param string $locale Locale tag.
	 * @return string
	 */
	public static function locale_to_url_tag( $locale ) {
		return strtolower( str_replace( '_', '-', LocaleValidator::canonicalize_tag( $locale ) ) );
	}

	/**
	 * Normalize a URL tag.
	 *
	 * @param string $url_tag URL tag.
	 * @return string
	 */
	public static function normalize_url_tag( $url_tag ) {
		return strtolower( trim( (string) $url_tag ) );
	}

	/**
	 * Return the compact self-name used in public locale switchers.
	 *
	 * @param array $locale Locale record.
	 * @return string
	 */
	public static function switcher_label( array $locale ) {
		$tag      = isset( $locale['tag'] ) ? LocaleValidator::canonicalize_tag( $locale['tag'] ) : '';
		$metadata = self::default_display_metadata( $tag );
		$label    = isset( $metadata['nativeName'] ) ? (string) $metadata['nativeName'] : '';

		if ( '' === trim( $label ) && isset( $locale['nativeName'] ) ) {
			$label = (string) $locale['nativeName'];
		}

		$label = self::simplify_locale_label( $label );

		if ( '' === $label ) {
			$label = '' !== $tag ? $tag : ( isset( $locale['urlTag'] ) ? (string) $locale['urlTag'] : '' );
		}

		return $label;
	}