Skip to content
wiki.fftac.org

Sitehealth - Source Excerpt 01

Back to Sitehealth

Summary

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

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

<?php

namespace UAIXLocaleRouter\Diagnostics;

use UAIXLocaleRouter\Admin\AdminPages;
use UAIXLocaleRouter\Plugin;

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

final class SiteHealth {
	/**
	 * Add Site Health debug information for the plugin.
	 *
	 * @param array $info Existing debug information.
	 * @return array
	 */
	public static function filter_debug_information( $info ) {
		$status                 = Plugin::system_status();
		$localized_diagnostics  = Plugin::localized_home_diagnostics( $status );
		$htaccess_status        = isset( $status['htaccess'] ) && is_array( $status['htaccess'] ) ? $status['htaccess'] : Plugin::htaccess_diagnostics();

		$info['uaixlr'] = array(
			'label'  => __( 'UAIX Locale Router', 'uaix-locale-router' ),
			'fields' => array(
				'plugin_version' => array(
					'label' => __( 'Plugin version', 'uaix-locale-router' ),
					'value' => $status['plugin_version'],
				),
				'install_version' => array(
					'label' => __( 'Installed version', 'uaix-locale-router' ),
					'value' => $status['install_version'] ? $status['install_version'] : __( 'Not recorded', 'uaix-locale-router' ),
				),
				'runtime_booted' => array(
					'label' => __( 'Runtime booted', 'uaix-locale-router' ),
					'value' => $status['runtime_booted'] ? __( 'Yes', 'uaix-locale-router' ) : __( 'No', 'uaix-locale-router' ),
				),
				'default_locale' => array(
					'label' => __( 'Default locale', 'uaix-locale-router' ),
					'value' => $status['default_locale'],
				),
				'default_url_tag' => array(
					'label' => __( 'Default URL tag', 'uaix-locale-router' ),
					'value' => $status['default_url_tag'],
				),
				'enabled_locales_count' => array(
					'label' => __( 'Enabled locales', 'uaix-locale-router' ),
					'value' => (string) $status['enabled_locales_count'],
				),
				'home_url' => array(
					'label' => __( 'Home URL', 'uaix-locale-router' ),
					'value' => $status['home_url'],
				),
				'request_scheme' => array(
					'label' => __( 'Detected request scheme', 'uaix-locale-router' ),
					'value' => $status['request_scheme'] ? $status['request_scheme'] : __( 'Unknown', 'uaix-locale-router' ),
				),
				'effective_front_scheme' => array(
					'label' => __( 'Effective front-end scheme', 'uaix-locale-router' ),
					'value' => $status['effective_front_scheme'],
				),
				'pretty_permalinks_enabled' => array(
					'label' => __( 'Pretty permalinks enabled', 'uaix-locale-router' ),
					'value' => $status['pretty_permalinks_enabled'] ? __( 'Yes', 'uaix-locale-router' ) : __( 'No', 'uaix-locale-router' ),
				),
				'localized_home_url' => array(
					'label' => __( 'Localized home URL', 'uaix-locale-router' ),
					'value' => $status['localized_home_url'],
				),
				'localized_home_check' => array(
					'label' => __( 'Localized home check', 'uaix-locale-router' ),
					'value' => ucfirst( (string) $localized_diagnostics['severity'] ),
				),
				'localized_home_response' => array(
					'label' => __( 'Localized home response', 'uaix-locale-router' ),
					'value' => $localized_diagnostics['status_code'] > 0 ? (string) $localized_diagnostics['status_code'] : ( $localized_diagnostics['response_error'] ? $localized_diagnostics['response_error'] : __( 'No response recorded', 'uaix-locale-router' ) ),
				),
				'localized_home_rewrite_handoff' => array(
					'label' => __( 'Localized URL rewrite handoff', 'uaix-locale-router' ),
					'value' => ! empty( $localized_diagnostics['likely_server_rewrite_issue'] ) ? __( 'Likely blocked before WordPress', 'uaix-locale-router' ) : __( 'No rewrite gap detected', 'uaix-locale-router' ),
				),
				'htaccess_server' => array(
					'label' => __( '.htaccess server environment', 'uaix-locale-router' ),
					'value' => $htaccess_status['server_label'],
				),
				'htaccess_path' => array(
					'label' => __( '.htaccess path', 'uaix-locale-router' ),
					'value' => $htaccess_status['path'],
				),
				'htaccess_block_present' => array(
					'label' => __( 'UAIX .htaccess block', 'uaix-locale-router' ),
					'value' => $htaccess_status['marker_present'] ? __( 'Present', 'uaix-locale-router' ) : __( 'Missing', 'uaix-locale-router' ),
				),
				'wordpress_htaccess_block' => array(
					'label' => __( 'WordPress .htaccess block', 'uaix-locale-router' ),
					'value' => ! empty( $htaccess_status['wordpress_marker_present'] )
						? ( ! empty( $htaccess_status['wordpress_marker_current'] ) ? __( 'Current', 'uaix-locale-router' ) : __( 'Needs refresh', 'uaix-locale-router' ) )
						: __( 'Not managed here', 'uaix-locale-router' ),
				),
				'htaccess_auto_repair' => array(
					'label' => __( '.htaccess auto repair', 'uaix-locale-router' ),
					'value' => $htaccess_status['auto_repair_available'] ? __( 'Available', 'uaix-locale-router' ) : ( $htaccess_status['manual_reason'] ? $htaccess_status['manual_reason'] : __( 'Not available', 'uaix-locale-router' ) ),
				),
				'dictionary_dir' => array(
					'label' => __( 'Dictionary directory', 'uaix-locale-router' ),
					'value' => $status['dictionary_dir'],
				),
				'dictionary_dir_writable' => array(
					'label' => __( 'Dictionary directory writable', 'uaix-locale-router' ),
					'value' => $status['dictionary_dir_writable'] ? __( 'Yes', 'uaix-locale-router' ) : __( 'No', 'uaix-locale-router' ),
				),
				'validation_table_exists' => array(
					'label' => __( 'Validation table ready', 'uaix-locale-router' ),
					'value' => $status['validation_table_exists'] ? __( 'Yes', 'uaix-locale-router' ) : __( 'No', 'uaix-locale-router' ),
				),
				'debug_log_path' => array(
					'label' => __( 'Debug log path', 'uaix-locale-router' ),
					'value' => $status['debug_log_path'],
				),
			),
		);

		return $info;
	}

	/**
	 * Register Site Health tests for the plugin.
	 *
	 * @param array $tests Existing Site Health tests.
	 * @return array
	 */
	public static function filter_site_status_tests( $tests ) {
		$tests['direct']['uaixlr_runtime'] = array(
			'label' => __( 'UAIX Locale Router runtime', 'uaix-locale-router' ),
			'test'  => array( __CLASS__, 'site_health_test_runtime' ),
		);

		$tests['direct']['uaixlr_localized_home'] = array(
			'label' => __( 'UAIX Locale Router localized homepage', 'uaix-locale-router' ),
			'test'  => array( __CLASS__, 'site_health_test_localized_home' ),
		);

		return $tests;
	}

	/**
	 * Run the direct Site Health test for plugin readiness.
	 *
	 * @return array
	 */
	public static function site_health_test_runtime() {
		$status   = Plugin::system_status();
		$issues   = array();
		$severity = 'good';
		$label    = __( 'UAIX Locale Router is ready for localized routing.', 'uaix-locale-router' );

		if ( ! $status['runtime_booted'] ) {
			$severity = 'critical';
			$issues[] = __( 'The runtime did not finish booting.', 'uaix-locale-router' );
		}

		if ( ! $status['dictionary_dir_writable'] ) {
			$severity = 'critical';
			$issues[] = __( 'The dictionary storage directory is not writable.', 'uaix-locale-router' );
		}

		if ( ! $status['validation_table_exists'] && 'critical' !== $severity ) {
			$severity = 'recommended';
			$issues[] = __( 'Validation history storage is not ready yet.', 'uaix-locale-router' );
		}

		if ( $status['enabled_locales_count'] < 1 && 'critical' !== $severity ) {
			$severity = 'recommended';
			$issues[] = __( 'At least one locale should remain enabled.', 'uaix-locale-router' );
		}

		if ( ! empty( $issues ) ) {
			$label = __( 'UAIX Locale Router needs attention.', 'uaix-locale-router' );
		}

		$description = '<p>' . esc_html__( 'This test checks whether locale routing, storage, and diagnostics are ready.', 'uaix-locale-router' ) . '</p>';

		if ( ! empty( $issues ) ) {
			$description .= '<ul>';

			foreach ( $issues as $issue ) {
				$description .= '<li>' . esc_html( $issue ) . '</li>';
			}

			$description .= '</ul>';
		}

		$actions = sprintf(
			'<p><a class="button button-primary" href="%s">%s</a></p>',
			esc_url( AdminPages::page_url( 'uaixlr-status' ) ),
			esc_html__( 'Open UAIX Locale Router Status', 'uaix-locale-router' )
		);

		return array(
			'label'       => $label,
			'status'      => $severity,
			'badge'       => array(
				'label' => __( 'UAIX Locale Router', 'uaix-locale-router' ),
				'color' => 'blue',
			),
			'description' => $description,
			'actions'     => $actions,
			'test'        => 'uaixlr_runtime',
		);
	}

	/**
	 * Run a localized-home loopback Site Health test.
	 *
	 * @return array
	 */
	public static function site_health_test_localized_home() {
		$status      = Plugin::system_status();
		$diagnostics = Plugin::localized_home_diagnostics( $status, true );
		$htaccess    = isset( $diagnostics['htaccess_status'] ) && is_array( $diagnostics['htaccess_status'] ) ? $diagnostics['htaccess_status'] : Plugin::htaccess_diagnostics();