Skip to content
wiki.fftac.org

Dictionaryrepository - Source Excerpt 03

Back to Dictionaryrepository

Summary

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

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

return implode( ':', $hashes );
	}

	/**
	 * List stored dictionary files.
	 *
	 * @return array
	 */
	public static function stored_dictionary_files() {
		$files = glob( trailingslashit( Plugin::dictionaries_dir() ) . '*.json' );

		return is_array( $files ) ? $files : array();
	}

	/**
	 * Clear cached dictionaries.
	 *
	 * @param string $locale_tag Locale tag.
	 * @return void
	 */
	public static function clear_cache( $locale_tag ) {
		$locale_tag = LocaleValidator::canonicalize_tag( $locale_tag );
		$manifest   = self::dictionary_manifest();
		$file_hash  = self::dictionary_cache_hash(
			self::dictionary_candidate_paths( $locale_tag ),
			isset( $manifest[ $locale_tag ]['fileHash'] ) ? (string) $manifest[ $locale_tag ]['fileHash'] : 'missing'
		);
		$cache_key  = 'uaixlr_dict_' . md5( $locale_tag . ':' . $file_hash );

		wp_cache_delete( $cache_key, 'uaixlr' );
		unset( self::$dictionary_cache[ $cache_key ] );
	}

	/**
	 * Build the cache hash for one dictionary file path.
	 *
	 * @param string $file_path Dictionary file path.
	 * @param string $default Fallback hash when the file is missing.
	 * @return string
	 */
	private static function dictionary_file_hash( $file_path, $default = 'missing' ) {
		if ( $file_path && file_exists( $file_path ) ) {
			$mtime     = filemtime( $file_path );
			$file_size = filesize( $file_path );
			$stat_key  = $file_path . ':' . ( false === $mtime ? '0' : (string) $mtime ) . ':' . ( false === $file_size ? '0' : (string) $file_size );

			if ( isset( self::$file_hash_cache[ $stat_key ] ) ) {
				return self::$file_hash_cache[ $stat_key ];
			}

			$file_hash = hash_file( 'sha256', $file_path );

			if ( is_string( $file_hash ) && '' !== $file_hash ) {
				self::$file_hash_cache[ $stat_key ] = $file_hash;

				return $file_hash;
			}
		}

		return (string) $default;
	}

	/**
	 * Resolve fallback locale for a dictionary.
	 *
	 * @param string $locale_tag Locale tag.
	 * @param array  $dictionary Dictionary payload.
	 * @return string
	 */
	private static function resolve_fallback_locale( $locale_tag, array $dictionary ) {
		if ( ! empty( $dictionary['meta']['fallback'] ) ) {
			return LocaleValidator::canonicalize_tag( $dictionary['meta']['fallback'] );
		}

		$record = LocaleRepository::get_supported_locale( $locale_tag );

		if ( ! empty( $record['fallback'] ) ) {
			return LocaleValidator::canonicalize_tag( $record['fallback'] );
		}

		return LocaleRepository::get_default_locale();
	}
}