Skip to content
wiki.fftac.org

Settingspage - Source Excerpt 02

Back to Settingspage

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

<input type="search" id="uaixlr_locale_search" name="locale_search" value="<?php echo esc_attr( $search ); ?>" class="regular-text" />
							<?php submit_button( 'Search', 'secondary', 'submit', false ); ?>
						</p>
					</form>

					<?php if ( ! empty( $catalog_results ) ) : ?>
						<table class="widefat striped">
							<thead>
								<tr>
									<th>Tag</th>
									<th>Native Name</th>
									<th>Language</th>
								</tr>
							</thead>
							<tbody>
								<?php foreach ( $catalog_results as $record ) : ?>
									<tr>
										<td><code><?php echo esc_html( $record['tag'] ); ?></code></td>
										<td><?php echo esc_html( $record['nativeName'] ); ?></td>
										<td><?php echo esc_html( $record['language'] ); ?></td>
									</tr>
								<?php endforeach; ?>
							</tbody>
						</table>
					<?php endif; ?>
				</div>

				<div class="uaixlr-card uaixlr-card-wide">
					<h2>Supported Locales</h2>
					<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
						<?php wp_nonce_field( 'uaixlr_update_locales' ); ?>
						<input type="hidden" name="action" value="uaixlr_update_locales" />

						<table class="widefat striped">
							<thead>
								<tr>
									<th>Enabled</th>
									<th>Default</th>
									<th>Tag</th>
									<th>Native Name</th>
									<th>Fallback</th>
									<th>Catalog</th>
									<th>Remove</th>
								</tr>
							</thead>
							<tbody>
								<?php foreach ( $supported_locales as $locale ) : ?>
									<tr>
										<td>
											<input type="checkbox" name="supported_locales[<?php echo esc_attr( $locale['tag'] ); ?>][enabled]" value="1" <?php checked( ! empty( $locale['enabled'] ) ); ?> />
											<input type="hidden" name="supported_locales[<?php echo esc_attr( $locale['tag'] ); ?>][tag]" value="<?php echo esc_attr( $locale['tag'] ); ?>" />
										</td>
										<td><input type="radio" name="default_locale" value="<?php echo esc_attr( $locale['tag'] ); ?>" <?php checked( $default_locale, $locale['tag'] ); ?> /></td>
										<td><code><?php echo esc_html( $locale['tag'] ); ?></code></td>
										<td><?php echo esc_html( $locale['nativeName'] ); ?></td>
										<td>
											<select name="supported_locales[<?php echo esc_attr( $locale['tag'] ); ?>][fallback]">
												<?php foreach ( $supported_locales as $fallback_locale ) : ?>
													<option value="<?php echo esc_attr( $fallback_locale['tag'] ); ?>" <?php selected( $locale['fallback'], $fallback_locale['tag'] ); ?>>
														<?php echo esc_html( $fallback_locale['tag'] ); ?>
													</option>
												<?php endforeach; ?>
											</select>
										</td>
										<td><?php echo wp_kses_post( ! empty( $locale['catalogHit'] ) ? Html::badge( 'Known', 'success' ) : Html::badge( 'Custom', 'warning' ) ); ?></td>
										<td><input type="checkbox" name="supported_locales[<?php echo esc_attr( $locale['tag'] ); ?>][remove]" value="1" /></td>
									</tr>
								<?php endforeach; ?>
							</tbody>
						</table>

						<?php submit_button( 'Save Locale Configuration' ); ?>
					</form>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Handle general settings save.
	 *
	 * @return void
	 */
	public static function handle_save_settings() {
		self::assert_permissions( 'uaixlr_save_settings' );

		$posted   = isset( $_POST['settings'] ) && is_array( $_POST['settings'] ) ? wp_unslash( $_POST['settings'] ) : array();
		$settings = self::sanitize_settings_payload( $posted, Plugin::settings() );

		$supported_locales = LocaleRepository::get_supported_locales();

		foreach ( $supported_locales as &$locale ) {
			$locale['isDefault'] = $locale['tag'] === $settings['default_locale'];

			if ( $locale['isDefault'] ) {
				$locale['enabled'] = true;
			}
		}
		unset( $locale );

		LocaleRepository::save_supported_locales( $supported_locales );

		Plugin::update_settings( $settings );
		Plugin::set_flash_notice( 'UAIX Locale Router settings saved.' );

		wp_safe_redirect( AdminPages::page_url( 'uaixlr-settings' ) );
		exit;
	}

	/**
	 * Sanitize general settings from an admin form payload.
	 *
	 * @param array $posted Posted settings.
	 * @param array $settings Existing settings.
	 * @return array
	 */
	public static function sanitize_settings_payload( array $posted, array $settings ) {
		$settings['enabled']                 = ! empty( $posted['enabled'] );
		$settings['default_locale']          = isset( $posted['default_locale'] ) ? LocaleValidator::canonicalize_tag( $posted['default_locale'] ) : LocaleRepository::get_default_locale();
		$settings['redirect_bare_urls']      = ! empty( $posted['redirect_bare_urls'] );
		$settings['redirect_status_code']    = isset( $posted['redirect_status_code'] ) && 302 === absint( $posted['redirect_status_code'] ) ? 302 : 301;
		$settings['trailing_slash_strategy'] = isset( $posted['trailing_slash_strategy'] ) ? sanitize_key( $posted['trailing_slash_strategy'] ) : 'always';
		$settings['rest_localization_mode']  = isset( $posted['rest_localization_mode'] ) ? sanitize_key( $posted['rest_localization_mode'] ) : 'unlocalized';
		$settings['content_link_rewriting']  = ! empty( $posted['content_link_rewriting'] );
		$settings['enqueue_front_assets']    = ! empty( $posted['enqueue_front_assets'] );
		$settings['debug_banner_enabled']    = ! empty( $posted['debug_banner_enabled'] );
		$settings['cleanup_legacy_ns12']     = ! empty( $posted['cleanup_legacy_ns12'] );
		$settings['exclusion_patterns']      = isset( $posted['exclusion_patterns'] ) ? sanitize_textarea_field( $posted['exclusion_patterns'] ) : '';

		if ( ! in_array( $settings['trailing_slash_strategy'], array( 'always', 'never', 'preserve' ), true ) ) {
			$settings['trailing_slash_strategy'] = 'always';
		}

		if ( ! in_array( $settings['rest_localization_mode'], array( 'unlocalized', 'prefixed' ), true ) ) {
			$settings['rest_localization_mode'] = 'unlocalized';
		}

		$default_record = LocaleRepository::get_supported_locale( $settings['default_locale'] );

		if ( empty( $default_record ) || empty( $default_record['enabled'] ) ) {
			$settings['default_locale'] = LocaleRepository::get_default_locale();
		}