| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* Localhost-only debug diagnostics helpers. |
| 8 |
* |
| 9 |
* abj404_is_local_debug_host() decides whether the current request comes from a |
| 10 |
* recognized local development host. abj404_get_simulated_db_latency_ms() reads |
| 11 |
* the optional simulated-latency knob (clamped, localhost-only) used to exercise |
| 12 |
* slow-DB code paths. abj404_show_diagnostic_latency_notice() is an intentional |
| 13 |
* no-op kept so the admin_notices wiring (if any) has a stable callback; the |
| 14 |
* real status lives on the plugin's Tools > Diagnostics card. |
| 15 |
*/ |
| 16 |
// allow-no-test-found: boot-time global debug helpers (localhost-only) wired in 404-solution.php; no same-named unit file. abj404_get_simulated_db_latency_ms is exercised in DatabaseQueryDiagnosticsTest, and the helpers are loaded through LoaderLazyLoadTest. |
| 17 |
|
| 18 |
if (!function_exists('abj404_get_simulated_db_latency_ms')) { |
| 19 |
/** @return bool */ |
| 20 |
function abj404_is_local_debug_host() { |
| 21 |
$serverName = array_key_exists('SERVER_NAME', $_SERVER) ? $_SERVER['SERVER_NAME'] : (array_key_exists('HTTP_HOST', $_SERVER) ? $_SERVER['HTTP_HOST'] : ''); |
| 22 |
$serverName = strtolower(trim((string)$serverName)); |
| 23 |
if ($serverName === '') { |
| 24 |
return false; |
| 25 |
} |
| 26 |
|
| 27 |
$normalizedHost = $serverName; |
| 28 |
if (strpos($normalizedHost, '[') === 0) { |
| 29 |
$endBracket = strpos($normalizedHost, ']'); |
| 30 |
if ($endBracket !== false) { |
| 31 |
$normalizedHost = substr($normalizedHost, 1, $endBracket - 1); |
| 32 |
} |
| 33 |
} else { |
| 34 |
$colonCount = substr_count($normalizedHost, ':'); |
| 35 |
if ($colonCount === 1 && preg_match('/:\d+$/', $normalizedHost)) { |
| 36 |
$normalizedHost = preg_replace('/:\d+$/', '', $normalizedHost); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
$normalizedHost = rtrim((string)$normalizedHost, '.'); |
| 41 |
return in_array($normalizedHost, array('127.0.0.1', '::1', 'localhost'), true); |
| 42 |
} |
| 43 |
|
| 44 |
/** @return int */ |
| 45 |
function abj404_get_simulated_db_latency_ms() { |
| 46 |
if (!abj404_is_local_debug_host()) { |
| 47 |
return 0; |
| 48 |
} |
| 49 |
if (defined('ABJ404_SIMULATED_DB_LATENCY_MS')) { |
| 50 |
return max(0, min(5000, absint(ABJ404_SIMULATED_DB_LATENCY_MS))); |
| 51 |
} |
| 52 |
$value = get_option('abj404_simulated_db_latency_ms', 0); |
| 53 |
return max(0, min(5000, absint(is_scalar($value) ? $value : 0))); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if (!function_exists('abj404_show_diagnostic_latency_notice')) { |
| 58 |
/** |
| 59 |
* admin_notices-style callback for the simulated-DB-latency status. |
| 60 |
* |
| 61 |
* This callback occupies an output-producing (impure) slot: historically it |
| 62 |
* echoed a floating "Simulated DB latency is ON/OFF" notice. That intrusive |
| 63 |
* notice was removed by design (see docs/ui-aesthetic/UI_AESTHETIC.md: default |
| 64 |
* to no new admin surface, prefer quiet status); the live status now lives on |
| 65 |
* the plugin's Tools > Diagnostics card (ToolsDiagnostics). The guarded checks |
| 66 |
* below are retained so this callback only ever considers acting in the same |
| 67 |
* context it used to (admin, plugin-admin capability, plugin page, local debug |
| 68 |
* host) -- but in every case it now renders nothing. Regression tests in |
| 69 |
* LoaderLazyLoadTest assert the empty output so the floating notice cannot be |
| 70 |
* silently reintroduced. |
| 71 |
* |
| 72 |
* Marked impure because it is a notice-output callback by role even though it |
| 73 |
* currently emits nothing. |
| 74 |
* |
| 75 |
* @phpstan-impure |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
function abj404_show_diagnostic_latency_notice() { |
| 79 |
if (!is_admin() || !abj404_current_user_is_plugin_admin()) { |
| 80 |
return; |
| 81 |
} |
| 82 |
$page = ABJ_404_Solution_RequestInputNormalizer::readText($_GET, array('name' => 'page')); |
| 83 |
if ($page !== ABJ404_PP) { |
| 84 |
return; |
| 85 |
} |
| 86 |
if (!abj404_is_local_debug_host()) { |
| 87 |
return; |
| 88 |
} |
| 89 |
// Intentionally render nothing: simulated-latency status is shown on the |
| 90 |
// plugin's Tools > Diagnostics card, not as an intrusive floating notice. |
| 91 |
} |
| 92 |
} |
| 93 |
|