Skip to content
wiki.fftac.org

Settings

**Site relevance:** Antichrist.net

**Memory type:** plugin source memory

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

**Size:** 3.1 KB

Summary

namespace UAIXLocaleRouter;

Source Preview

This source file is short enough to preview directly on its source-memory page.

<?php

namespace UAIXLocaleRouter;

use UAIXLocaleRouter\Support\Json;


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

/**
 * Centralized settings access with per-request caching.
 */
final class Settings {
	/**
	 * Cached merged settings.
	 *
	 * @var array|null
	 */
	private static $settings = null;

	/**
	 * Default settings payload.
	 *
	 * @return array
	 */
	public static function defaults() {
		return array(
			'enabled'                 => true,
			'default_locale'          => 'en-US',
			'url_casing_strategy'     => 'lowercase',
			'trailing_slash_strategy' => 'always',
			'redirect_bare_urls'      => true,
			'redirect_status_code'    => 301,
			'rest_localization_mode'  => 'unlocalized',
			'content_link_rewriting'  => true,
			'seo_metadata_enabled'    => true,
			'exclusion_patterns'      => '',
			'enqueue_front_assets'    => true,
			'debug_banner_enabled'    => false,
			'cleanup_legacy_ns12'     => false,
		);
	}

	/**
	 * Ensure required options exist.
	 *
	 * @return void
	 */
	public static function ensure_defaults() {
		if ( false === get_option( 'uaixlr_settings', false ) ) {
			add_option( 'uaixlr_settings', self::defaults() );
		}

		if ( false === get_option( 'uaixlr_supported_locales', false ) ) {
			$defaults = Json::decode_file( Plugin::default_supported_locales_path(), array() );

			if ( ! is_array( $defaults ) || empty( $defaults ) ) {
				$defaults = array(
					array(
						'tag'        => 'en-US',
						'urlTag'     => 'en-us',
						'language'   => 'English',
						'nativeName' => 'English (United States)',
						'region'     => 'US',
						'script'     => 'Latn',
						'isRtl'      => false,
						'enabled'    => true,
						'isDefault'  => true,
						'fallback'   => 'en-US',
					),
				);
			}

			add_option( 'uaixlr_supported_locales', $defaults );
		}

		if ( false === get_option( 'uaixlr_default_locale', false ) ) {
			add_option( 'uaixlr_default_locale', 'en-US' );
		}

		if ( false === get_option( 'uaixlr_dictionary_manifest', false ) ) {
			add_option( 'uaixlr_dictionary_manifest', array() );
		}
	}

	/**
	 * Read merged plugin settings.
	 *
	 * @return array
	 */
	public static function get() {
		if ( null !== self::$settings ) {
			return self::$settings;
		}

		$settings = get_option( 'uaixlr_settings', array() );

		if ( ! is_array( $settings ) ) {
			$settings = array();
		}

		self::$settings = wp_parse_args( $settings, self::defaults() );

		return self::$settings;
	}

	/**
	 * Persist settings and invalidate derived caches.
	 *
	 * @param array $settings Sanitized settings.
	 * @return void
	 */
	public static function update( array $settings ) {
		self::$settings = wp_parse_args( $settings, self::defaults() );

		update_option( 'uaixlr_settings', self::$settings );
		update_option( 'uaixlr_default_locale', self::$settings['default_locale'] );

		Plugin::clear_localized_home_diagnostics();
		Plugin::clear_link_review_diagnostics();
	}

	/**
	 * Reset the request-local settings cache.
	 *
	 * @return void
	 */
	public static function reset_cache() {
		self::$settings = null;
	}
}