Localizedhomediagnostics - Source Excerpt 01
Back to Localizedhomediagnostics
Summary
This source excerpt preserves a bounded section of Antichrist.net/wp-content/plugins/uaix-locale-router/src/Diagnostics/LocalizedHomeDiagnostics.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/LocalizedHomeDiagnostics.php
<?php
namespace UAIXLocaleRouter\Diagnostics;
use UAIXLocaleRouter\LocaleRepository;
use UAIXLocaleRouter\Plugin;
use UAIXLocaleRouter\Router;
use UAIXLocaleRouter\Support\Path;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
final class LocalizedHomeDiagnostics {
/**
* Refresh the site-root locale rewrite block during install or upgrade when safe.
*
* @return void
*/
public static function maybe_repair_htaccess_on_install() {
$diagnostics = Plugin::htaccess_diagnostics();
if ( empty( $diagnostics['supports_htaccess'] ) || empty( $diagnostics['auto_repair_available'] ) ) {
return;
}
if ( empty( $diagnostics['repair_needed'] ) ) {
return;
}
Plugin::repair_htaccess_block();
}
/**
* Repair locale-prefixed handoff during a reachable bare request when possible.
*
* This lets a normal request to "/" recover from a missing Apache/LiteSpeed
* marker block before the user lands on a server-level 404 at "/en-us/".
*
* @param string $request_path Current request path.
* @return void
*/
public static function maybe_self_heal_localized_handoff( $request_path = '/' ) {
$settings = Plugin::settings();
if ( empty( $settings['enabled'] ) ) {
return;
}
$request_path = Path::normalize_path( $request_path );
if ( Router::is_excluded_path( $request_path ) ) {
return;
}
$segments = Path::segments( $request_path );
if ( ! empty( $segments ) ) {
$possible_locale = LocaleRepository::normalize_url_tag( $segments[0] );
if ( LocaleRepository::is_enabled_url_tag( $possible_locale ) ) {
return;
}
}
if ( get_transient( 'uaixlr_runtime_handoff_repair_lock' ) ) {
return;
}
set_transient( 'uaixlr_runtime_handoff_repair_lock', time(), 5 * MINUTE_IN_SECONDS );
$status = self::localized_home_status_snapshot();
$htaccess_status = isset( $status['htaccess'] ) && is_array( $status['htaccess'] ) ? $status['htaccess'] : array();
if ( empty( $htaccess_status['supports_htaccess'] ) || empty( $htaccess_status['auto_repair_available'] ) ) {
return;
}
$diagnostics = self::localized_home_diagnostics( $status, true );
if ( empty( $diagnostics['likely_server_rewrite_issue'] ) ) {
return;
}
Plugin::repair_htaccess_block();
}
/**
* Return the minimal status payload needed for localized-home diagnostics.
*
* @return array
*/
private static function localized_home_status_snapshot() {
$home_url = (string) get_option( 'home' );
$home_scheme = (string) wp_parse_url( $home_url, PHP_URL_SCHEME );
$request_scheme = Path::detect_request_scheme();
$default_url_tag = LocaleRepository::get_default_url_tag();
$permalink_structure = (string) get_option( 'permalink_structure', '' );
return array(
'localized_home_url' => Path::build_front_url( Path::localize_path( '/', $default_url_tag ) ),
'home_url' => $home_url,
'home_scheme' => $home_scheme,
'request_scheme' => $request_scheme,
'default_url_tag' => $default_url_tag,
'pretty_permalinks_enabled' => '' !== $permalink_structure,
'home_scheme_mismatch' => '' !== $request_scheme && '' !== $home_scheme && strtolower( $request_scheme ) !== strtolower( $home_scheme ),
'htaccess' => Plugin::htaccess_diagnostics(),
);
}
/**
* Return a localized-home loopback diagnostic payload.
*
* @param array $status Plugin status payload.
* @param bool $force_refresh Whether to bypass the transient cache.
* @return array
*/
public static function localized_home_diagnostics( array $status = array(), $force_refresh = false ) {
if ( empty( $status ) ) {
$status = Plugin::system_status();
}
$signature = md5(
implode(
'|',
array(
isset( $status['localized_home_url'] ) ? (string) $status['localized_home_url'] : '',
isset( $status['home_url'] ) ? (string) $status['home_url'] : '',
isset( $status['default_url_tag'] ) ? (string) $status['default_url_tag'] : '',
! empty( $status['pretty_permalinks_enabled'] ) ? '1' : '0',
isset( $status['request_scheme'] ) ? (string) $status['request_scheme'] : '',
isset( $status['home_scheme'] ) ? (string) $status['home_scheme'] : '',
)
)
);
if ( ! $force_refresh ) {
$cached = get_transient( 'uaixlr_localized_home_check' );
if ( is_array( $cached ) && isset( $cached['signature'] ) && $signature === $cached['signature'] ) {
return $cached;
}
}
$issues = array();
$severity = 'good';
$localized = isset( $status['localized_home_url'] ) ? (string) $status['localized_home_url'] : '';
$label = __( 'UAIX Locale Router localized homepage is reachable.', 'uaix-locale-router' );
$status_code = 0;
$response_error = '';
$likely_server_rewrite_issue = false;
$htaccess_status = isset( $status['htaccess'] ) && is_array( $status['htaccess'] ) ? $status['htaccess'] : Plugin::htaccess_diagnostics();
if ( empty( $status['pretty_permalinks_enabled'] ) ) {
$severity = 'recommended';
$issues[] = __( 'Pretty permalinks are disabled, so locale-prefixed routes cannot behave like standard WordPress paths.', 'uaix-locale-router' );
}
if ( ! empty( $status['home_scheme_mismatch'] ) && 'critical' !== $severity ) {
$severity = 'recommended';
$issues[] = sprintf(
/* translators: 1: detected request scheme, 2: configured home scheme */
__( 'The current request scheme is %1$s but the configured home URL scheme is %2$s. Redirects may downgrade protocol until the site URL settings are corrected.', 'uaix-locale-router' ),
$status['request_scheme'],
$status['home_scheme']
);
}
$response = wp_remote_get(
$localized,
array(
'timeout' => 10,
'redirection' => 0,
)
);
if ( is_wp_error( $response ) ) {
if ( 'critical' !== $severity ) {
$severity = 'recommended';
}
$response_error = $response->get_error_message();
$issues[] = sprintf(
/* translators: %s: HTTP error message */
__( 'Loopback request could not confirm the localized homepage: %s', 'uaix-locale-router' ),
$response_error
);
} else {
$status_code = (int) wp_remote_retrieve_response_code( $response );
if ( $status_code < 200 || $status_code >= 400 ) {
$severity = 'critical';
$issues[] = sprintf(
/* translators: %d: HTTP status code */
__( 'The localized homepage returned HTTP %d.', 'uaix-locale-router' ),
$status_code
);
if ( 404 === $status_code ) {
$likely_server_rewrite_issue = true;
$issues[] = __( 'The most likely cause is that the web server is returning a 404 for the locale-prefixed pretty URL before WordPress can route it.', 'uaix-locale-router' );
$issues[] = __( 'On Apache and LiteSpeed, make sure the site-root .htaccess file beside wp-admin, wp-content, and index.php contains both a current WordPress rewrite block and the UAIX Locale Router block so locale-prefixed requests are forwarded to WordPress.', 'uaix-locale-router' );
if ( ! empty( $htaccess_status['wordpress_marker_conflict'] ) ) {
$issues[] = __( 'The existing # BEGIN WordPress block does not match the current site root and may be rewriting requests to the wrong index target, such as /en-us/index.php.', 'uaix-locale-router' );
}
}
}
}
if ( ! empty( $issues ) ) {
$label = __( 'UAIX Locale Router localized homepage check needs attention.', 'uaix-locale-router' );
}
$summary = __( 'The localized homepage URL responded successfully.', 'uaix-locale-router' );
if ( $likely_server_rewrite_issue ) {
$summary = __( 'Locale-prefixed pretty URLs are not reaching WordPress yet. Fix the site-root rewrite handoff first.', 'uaix-locale-router' );
} elseif ( ! empty( $response_error ) ) {
$summary = __( 'The plugin could not complete its localized-home loopback request from inside WordPress.', 'uaix-locale-router' );
} elseif ( 'good' !== $severity ) {
$summary = __( 'The localized homepage URL is reachable, but the routing environment still needs cleanup.', 'uaix-locale-router' );
}
$result = array(
'signature' => $signature,
'label' => $label,
'severity' => $severity,
'summary' => $summary,
'localized_home_url' => $localized,
'status_code' => $status_code,
'response_error' => $response_error,
'issues' => $issues,
'likely_server_rewrite_issue' => $likely_server_rewrite_issue,