Settingspage - Source Excerpt 03
Summary
This source excerpt preserves a bounded section of Antichrist.net/wp-content/plugins/uaix-locale-router/src/Admin/SettingsPage.php so readers can inspect the evidence without opening the full source file.
**Source path:** Antichrist.net/wp-content/plugins/uaix-locale-router/src/Admin/SettingsPage.php
return $settings;
}
/**
* Handle adding a locale to the supported list.
*
* @return void
*/
public static function handle_add_locale() {
self::assert_permissions( 'uaixlr_add_locale' );
$tag = isset( $_POST['new_locale_tag'] ) ? LocaleValidator::canonicalize_tag( wp_unslash( $_POST['new_locale_tag'] ) ) : '';
if ( '' === $tag || ! LocaleValidator::is_valid_locale_tag( $tag ) ) {
Plugin::set_flash_notice( 'Please enter a valid locale tag such as en-US.', 'error' );
wp_safe_redirect( AdminPages::page_url( 'uaixlr-locales' ) );
exit;
}
if ( ! empty( LocaleRepository::get_supported_locale( $tag ) ) ) {
Plugin::set_flash_notice( sprintf( '%s is already in the supported locale list.', $tag ), 'info' );
wp_safe_redirect( AdminPages::page_url( 'uaixlr-locales' ) );
exit;
}
$locales = LocaleRepository::get_supported_locales();
$locales[] = LocaleRepository::build_record_for_tag(
$tag,
array(
'enabled' => false,
'isDefault' => false,
'fallback' => Plugin::settings()['default_locale'],
)
);
LocaleRepository::save_supported_locales( $locales );
Plugin::clear_localized_home_diagnostics();
Plugin::set_flash_notice( sprintf( '%s added to the locale catalog.', $tag ) );
wp_safe_redirect( AdminPages::page_url( 'uaixlr-locales' ) );
exit;
}
/**
* Handle supported locale updates.
*
* @return void
*/
public static function handle_update_locales() {
self::assert_permissions( 'uaixlr_update_locales' );
$posted_locales = isset( $_POST['supported_locales'] ) && is_array( $_POST['supported_locales'] ) ? wp_unslash( $_POST['supported_locales'] ) : array();
$default_locale = isset( $_POST['default_locale'] ) ? LocaleValidator::canonicalize_tag( wp_unslash( $_POST['default_locale'] ) ) : '';
$updated = array();
$existing = array();
foreach ( LocaleRepository::get_supported_locales() as $record ) {
$existing[ $record['tag'] ] = $record;
}
foreach ( $posted_locales as $tag => $record ) {
if ( ! is_array( $record ) || ! empty( $record['remove'] ) ) {
continue;
}
$tag = LocaleValidator::canonicalize_tag( $tag );
if ( '' === $tag ) {
continue;
}
$preserved = isset( $existing[ $tag ] ) ? $existing[ $tag ] : array();
$overrides = array(
'enabled' => ! empty( $record['enabled'] ),
'isDefault' => $tag === $default_locale,
'fallback' => isset( $record['fallback'] ) ? LocaleValidator::canonicalize_tag( $record['fallback'] ) : Plugin::settings()['default_locale'],
);
foreach ( array( 'language', 'nativeName', 'region', 'script', 'isRtl' ) as $field ) {
if ( array_key_exists( $field, $preserved ) ) {
$overrides[ $field ] = $preserved[ $field ];
}
}
$updated[] = LocaleRepository::build_record_for_tag(
$tag,
$overrides
);
}
if ( empty( $updated ) ) {
Plugin::set_flash_notice( 'At least one locale must remain configured.', 'error' );
wp_safe_redirect( AdminPages::page_url( 'uaixlr-locales' ) );
exit;
}
$has_enabled = false;
foreach ( $updated as &$record ) {
if ( ! empty( $record['enabled'] ) ) {
$has_enabled = true;
}
}
unset( $record );
if ( ! $has_enabled ) {
$updated[0]['enabled'] = true;
}
$default_exists = false;
foreach ( $updated as &$record ) {
$record['isDefault'] = $record['tag'] === $default_locale;
if ( $record['isDefault'] ) {
$record['enabled'] = true;
$default_exists = true;
}
}
unset( $record );
if ( ! $default_exists ) {
$updated[0]['isDefault'] = true;
$updated[0]['enabled'] = true;
$default_locale = $updated[0]['tag'];
}
LocaleRepository::save_supported_locales( $updated );
$settings = Plugin::settings();
$settings['default_locale'] = $default_locale;
Plugin::update_settings( $settings );
Plugin::clear_localized_home_diagnostics();
Plugin::set_flash_notice( 'Supported locales updated.' );
wp_safe_redirect( AdminPages::page_url( 'uaixlr-locales' ) );
exit;
}
/**
* Capability and nonce guard.
*
* @param string $action Nonce action.
* @return void
*/
private static function assert_permissions( $action ) {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to manage locale settings.', 'uaix-locale-router' ) );
}
check_admin_referer( $action );
}
}