add_query_arg( 'atb_health', $atb_buster, home_url( '/' ) ), 'rest' => add_query_arg( 'atb_health', $atb_buster, rest_url() ), ); $atb_failures = array(); $atb_checks = array(); foreach ( $atb_urls as $atb_label => $atb_url ) { $atb_start = microtime( true ); $atb_response = wp_remote_get( $atb_url, array( 'timeout' => self::PROBE_TIMEOUT, 'sslverify' => false, // Loopback to ourselves; local/self-signed certs must not fail the probe. ) ); $atb_time_ms = (int) round( ( microtime( true ) - $atb_start ) * 1000 ); if ( is_wp_error( $atb_response ) ) { $atb_failures[] = 'health probe (' . $atb_label . '): ' . $atb_response->get_error_message(); $atb_checks[ $atb_label ] = array( 'status' => 0, 'ok' => false, 'reason' => $atb_response->get_error_message(), 'time_ms' => $atb_time_ms, ); continue; } $atb_status = (int) wp_remote_retrieve_response_code( $atb_response ); $atb_verdict = self::evaluate( $atb_status, (string) wp_remote_retrieve_body( $atb_response ) ); if ( ! $atb_verdict['ok'] ) { $atb_failures[] = 'health probe (' . $atb_label . '): ' . $atb_verdict['reason']; } $atb_checks[ $atb_label ] = array( 'status' => $atb_status, 'ok' => $atb_verdict['ok'], 'reason' => $atb_verdict['reason'], 'time_ms' => $atb_time_ms, ); } return array( 'passed' => empty( $atb_failures ), 'failures' => $atb_failures, 'checks' => $atb_checks, ); } /** * Pure verdict on one probe response: 2xx and no fatal marker = ok. * * @param int $atb_code HTTP status code (0 when unavailable). * @param string $atb_body Response body. * @return array { ok: bool, reason: string } */ public static function evaluate( $atb_code, $atb_body ) { if ( $atb_code < 200 || $atb_code >= 300 ) { return array( 'ok' => false, 'reason' => 'unexpected HTTP status ' . $atb_code ); } foreach ( self::FATAL_MARKERS as $atb_marker ) { if ( false !== stripos( $atb_body, $atb_marker ) ) { return array( 'ok' => false, 'reason' => 'fatal-error marker found in response body' ); } } return array( 'ok' => true, 'reason' => '' ); } } }