*/ private $summary_source; /** * @param callable():array|null $summary_source Testable saved-summary reader. */ public function __construct( FindingFactory $findings, ?callable $summary_source = null ) { $this->findings = $findings; $this->summary_source = $summary_source ?? array( $this, 'wordpress_summary' ); } public function id(): string { // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText -- Stable registry machine ID. return 'wordpress.site_health'; } /** * @return array */ public function execute( string $timestamp ): array { try { $summary = call_user_func( $this->summary_source ); } catch ( Throwable $exception ) { return $this->error_finding( $timestamp ); } if ( ! is_array( $summary ) || ! isset( $summary['api_available'], $summary['cache_state'], $summary['counts'] ) || ! is_bool( $summary['api_available'] ) || ! is_string( $summary['cache_state'] ) || ! in_array( $summary['cache_state'], self::STATES, true ) || ! is_array( $summary['counts'] ) ) { return $this->error_finding( $timestamp ); } $objects = array(); $total = 0; foreach ( self::COUNT_KEYS as $state ) { $count = $summary['counts'][ $state ] ?? null; if ( ! is_int( $count ) || $count < 0 || $count > self::MAX_TESTS ) { return $this->error_finding( $timestamp ); } $total += $count; $objects[] = array( 'result' => $state, 'count' => $count, ); } if ( $total > self::MAX_TESTS ) { return $this->error_finding( $timestamp ); } $tests = array(); $test_counts = array_fill_keys( self::COUNT_KEYS, 0 ); $test_ids = array(); foreach ( is_array( $summary['tests'] ?? null ) ? $summary['tests'] : array() as $test ) { if ( ! is_array( $test ) || ! isset( $test['test'], $test['result'] ) || ! is_string( $test['test'] ) || ! is_string( $test['result'] ) || 1 !== preg_match( '/^[a-z][a-z0-9_]{1,63}$/', $test['test'] ) || ! in_array( $test['result'], self::COUNT_KEYS, true ) ) { return $this->error_finding( $timestamp ); } if ( isset( $test_ids[ $test['test'] ] ) || count( $tests ) >= self::MAX_TESTS ) { return $this->error_finding( $timestamp ); } $test_ids[ $test['test'] ] = true; ++$test_counts[ $test['result'] ]; $tests[] = array( 'test' => $test['test'], 'result' => $test['result'], ); } if ( count( $tests ) !== $total || $test_counts !== $summary['counts'] ) { return $this->error_finding( $timestamp ); } $current = $summary['api_available'] && 'current' === $summary['cache_state'] && $total > 0; if ( ! $current ) { $status = 'not_tested'; $coverage = 'none'; $observed = 0; } elseif ( $summary['counts']['critical'] > 0 ) { $status = 'fail'; $coverage = 'complete'; $observed = $total; } elseif ( $summary['counts']['recommended'] > 0 ) { $status = 'warning'; $coverage = 'complete'; $observed = $total; } else { $status = 'pass'; $coverage = 'complete'; $observed = $total; } return $this->findings->build( $this->id(), $status, array( 'objects' => array_merge( $tests, $objects ), 'observed_count' => $observed, 'total_count' => $total, 'truncated' => false, 'limitations' => array( 'A fixed no-write subset of current core Site Health callbacks was executed. Callback labels, descriptions, paths, versions, URLs and raw errors were not stored. External, authenticated REST, filesystem-credential and plugin-added callbacks were excluded.' ), ), $observed, $total, $coverage, $timestamp ); } /** * @return array{api_available:bool,cache_state:string,counts:array{good:int,recommended:int,critical:int},tests:list} */ private function wordpress_summary(): array { $counts = array( 'good' => 0, 'recommended' => 0, 'critical' => 0, ); $paths = new WordPressPathProvider(); $file = $paths->core_file( 'wp-admin/includes/class-wp-site-health.php' ); if ( ! is_readable( $file ) ) { return array( 'api_available' => false, 'cache_state' => 'missing', 'counts' => $counts, 'tests' => array(), ); } require_once $paths->core_file( 'wp-admin/includes/admin.php' ); $reflection = new ReflectionClass( 'WP_Site_Health' ); $health = $reflection->newInstanceWithoutConstructor(); $health->php_memory_limit = ini_get( 'memory_limit' ); $methods = array( 'wordpress_version', 'plugin_version', 'theme_version', 'php_version', 'php_extensions', 'php_default_timezone', 'php_sessions', 'sql_server', 'ssl_support', 'scheduled_events', 'http_requests', 'is_in_debug_mode', 'file_uploads', 'plugin_theme_auto_updates', 'available_updates_disk_space', 'insecure_registration', 'autoloaded_options', 'search_engine_visibility', 'opcode_cache', 'persistent_object_cache', ); $tests = array(); $deny_network = static function (): WP_Error { return new WP_Error( 'px320_site_health_network_blocked', 'Network is disabled for the local Site Health subset.' ); }; add_filter( 'pre_http_request', $deny_network, PHP_INT_MAX ); try { foreach ( $methods as $test_id ) { // Core discovers theme roots and writes a site transient on first use. // Run this callback only when WordPress has already built that cache. if ( 'theme_version' === $test_id && false === get_site_transient( 'theme_roots' ) ) { continue; } // This core test calls WordPress.org and writes a transient when its // normal weekly cache is absent. Use it only when WordPress already // has the response; the audit itself remains local and no-write. if ( 'php_version' === $test_id && false === get_site_transient( 'php_check_' . md5( PHP_VERSION ) ) ) { continue; } $method = 'get_test_' . $test_id; if ( ! method_exists( $health, $method ) || ! is_callable( array( $health, $method ) ) ) { continue; } try { $result = call_user_func( array( $health, $method ) ); } catch ( Throwable $exception ) { continue; } $state = is_array( $result ) && isset( $result['status'] ) && is_string( $result['status'] ) ? $result['status'] : ''; if ( ! in_array( $state, self::COUNT_KEYS, true ) ) { continue; } ++$counts[ $state ]; $tests[] = array( 'test' => $test_id, 'result' => $state, ); } } finally { remove_filter( 'pre_http_request', $deny_network, PHP_INT_MAX ); } return array( 'api_available' => true, 'cache_state' => array() === $tests ? 'missing' : 'current', 'counts' => $counts, 'tests' => $tests, ); } /** * @return array */ private function error_finding( string $timestamp ): array { return $this->findings->build( $this->id(), 'error', array( 'objects' => array(), 'observed_count' => 0, 'total_count' => 0, 'truncated' => false, 'limitations' => array( 'The local Site Health summary adapter failed; no transient value, callback result, issue content or exception was stored.' ), ), 0, 0, 'error', $timestamp ); } }