Site Health instead. * * Never run here: async tests (loopback, REST, page cache, wordpress.org, * background updates, HTTPS status, authorization header), the temp-backup * test (requests filesystem credentials) and performance-only tests. * * `php_version` can make a remote request on a cold cache (Serve Happy); the * tab's one-hour result cache is what keeps that rare, and a failure is shown * as "Couldn't verify". */ final class Core_Health_Passthrough { const GROUP = 'wp_site_health'; /** * Core direct-test ids to show, in display order. * * @var string[] */ const ALLOWLIST = [ 'insecure_registration', 'debug_enabled', 'wordpress_version', 'plugin_version', 'theme_version', 'php_version', 'sql_server', 'plugin_theme_auto_updates', ]; /** * The one place AO's wording for each passed-through test lives: * label, message when Good, and the message when core reports a problem * but gives nothing useful of its own. `%s` is replaced by the PHP version. * * @return array */ public static function wording(): array { return [ 'insecure_registration' => [ 'label' => __( 'New user registration', 'admin-optimizer' ), 'good' => __( 'Registration is closed, or new users get a low-privilege role.', 'admin-optimizer' ), 'fail' => __( 'Registration is open and new users get a privileged role.', 'admin-optimizer' ), ], 'debug_enabled' => [ 'label' => __( 'Debug mode', 'admin-optimizer' ), 'good' => __( 'Errors are not displayed to visitors.', 'admin-optimizer' ), 'fail' => __( 'Debug output may be shown to visitors.', 'admin-optimizer' ), ], 'wordpress_version' => [ 'label' => __( 'WordPress updates', 'admin-optimizer' ), 'good' => __( 'WordPress is up to date.', 'admin-optimizer' ), 'fail' => __( 'A WordPress update is available.', 'admin-optimizer' ), ], 'plugin_version' => [ 'label' => __( 'Plugin updates', 'admin-optimizer' ), 'good' => __( 'Plugins are up to date.', 'admin-optimizer' ), 'fail' => __( 'Plugin updates are available, or inactive plugins are installed. Remove plugins you no longer use.', 'admin-optimizer' ), ], 'theme_version' => [ 'label' => __( 'Theme updates', 'admin-optimizer' ), 'good' => __( 'Themes are up to date.', 'admin-optimizer' ), 'fail' => __( 'Theme updates are available, or no default theme is installed.', 'admin-optimizer' ), ], 'php_version' => [ 'label' => __( 'PHP version', 'admin-optimizer' ), /* translators: %s: PHP version. */ 'good' => __( 'PHP %s is supported.', 'admin-optimizer' ), /* translators: %s: PHP version. */ 'fail' => __( 'PHP %s is outdated and should be updated.', 'admin-optimizer' ), ], 'sql_server' => [ 'label' => __( 'Database server', 'admin-optimizer' ), 'good' => __( 'The database server version is supported.', 'admin-optimizer' ), 'fail' => __( 'The database server version should be updated.', 'admin-optimizer' ), ], 'plugin_theme_auto_updates' => [ 'label' => __( 'Plugin and theme auto-updates', 'admin-optimizer' ), 'good' => __( 'Auto-update settings are consistent.', 'admin-optimizer' ), 'fail' => __( 'Some plugin or theme auto-update settings need attention.', 'admin-optimizer' ), ], ]; } /** * Build a row's label and message from a core result, following the * wording rules. Pure, so every rule can be tested. * * @param string $id Core test id. * @param string $core_label Core's registry label for the test. * @param string $core_result Core's result label (a finding such as "You should remove inactive plugins"). * @param string $status Mapped row status (`ok`, `warning`, `critical`). * @param string $php_version PHP version for the PHP row. * * @return array{label:string,message:string} */ public static function build_wording( string $id, string $core_label, string $core_result, string $status, string $php_version ): array { $map = self::wording(); if ( ! isset( $map[ $id ] ) ) { // An allowlisted id without a map entry: core's own label, so a new core test is never blank. return [ 'label' => $core_label, 'message' => self::plain_sentence( $core_result ), ]; } $entry = $map[ $id ]; if ( 'ok' === $status ) { return [ 'label' => $entry['label'], 'message' => sprintf( $entry['good'], $php_version ), ]; } $sentence = self::plain_sentence( $core_result ); $same = static function ( string $a, string $b ): bool { return strtolower( trim( $a, " \t\n\r.!" ) ) === strtolower( trim( $b, " \t\n\r.!" ) ); }; // Core's finding is used only when it adds something. $use_core = '' !== $sentence && ! $same( $sentence, $entry['label'] ) && ! $same( $sentence, $core_label ) && ! $same( $sentence, sprintf( $entry['good'], $php_version ) ); return [ 'label' => $entry['label'], 'message' => $use_core ? $sentence : sprintf( $entry['fail'], $php_version ), ]; } /** * Core's text as one plain sentence: markup stripped, whitespace collapsed, * cut at the first sentence end, capped at 160 characters. * * @param string $text Core text. * * @return string */ private static function plain_sentence( string $text ): string { $text = trim( preg_replace( '/\s+/', ' ', wp_strip_all_tags( $text ) ) ); $parts = preg_split( '/(?<=[.!?])\s/', $text, 2 ); $text = trim( (string) ( $parts[0] ?? '' ) ); return mb_strlen( $text ) > 160 ? rtrim( mb_substr( $text, 0, 159 ) ) . '…' : $text; } /** * Keep only allowlisted direct tests, in allowlist order. Our own * `adminoptim_` tests (registered into the same list) are always excluded * so rows are not listed twice. * * @param array $direct `get_tests()['direct']`. * * @return array */ public static function filter_allowlisted( array $direct ): array { $kept = []; foreach ( self::ALLOWLIST as $id ) { if ( 0 === strpos( $id, Site_Health_Checks::CORE_TEST_PREFIX ) ) { continue; } if ( isset( $direct[ $id ] ) ) { $kept[ $id ] = $direct[ $id ]; } } return $kept; } /** * Map core's status to the row status: good -> ok, recommended -> * warning, critical -> critical; anything else is not trustworthy. * * @param mixed $status Core status. * * @return string */ public static function map_status( $status ): string { switch ( $status ) { case 'good': return 'ok'; case 'recommended': return 'warning'; case 'critical': return 'critical'; default: return 'unverified'; } } /** * Rows for the allowlisted core tests. * * @return array */ public static function rows(): array { return self::collect_rows( self::available_tests() ); } /** * Run the allowlisted tests found in `$available`. * * Each test is guarded and isolated: a thrown error becomes one * `unverified` row. `insecure_registration` and the debug test have a * built-in equivalent that is used when core does not offer them (older * WordPress, or unregistered), so those rows never silently disappear. * * @param array $available Test id => label and runner. * * @return array */ public static function collect_rows( array $available ): array { $rows = []; $priority = 10; foreach ( self::ALLOWLIST as $id ) { $row = null; if ( isset( $available[ $id ] ) ) { $row = self::run_core_test( $id, $available[ $id ] ); } elseif ( 'insecure_registration' === $id ) { $row = self::registration_fallback_row(); } elseif ( 'debug_enabled' === $id ) { $row = self::debug_fallback_row(); } if ( null !== $row ) { $row['priority'] = $priority; $rows[] = $row; } $priority += 10; } return $rows; } /** * Built-in equivalent of core's insecure-registration test (WP 7.0+): * open registration with a privileged default role. * * @param bool $open Whether anyone can register. * @param string $role Default role. * * @return string `critical` or `ok`. */ public static function evaluate_registration( bool $open, string $role ): string { return $open && in_array( $role, [ 'editor', 'administrator' ], true ) ? 'critical' : 'ok'; } /** * Built-in equivalent of core's debug-mode test: debug output on a * production site is Recommended, anything else is fine. * * @param bool $debug WP_DEBUG. * @param bool $display WP_DEBUG_DISPLAY. * @param string $environment wp_get_environment_type(). * * @return string `warning` or `ok`. */ public static function evaluate_debug( bool $debug, bool $display, string $environment ): string { return $debug && $display && 'production' === $environment ? 'warning' : 'ok'; } /** * Available core direct tests as id => label and runner. Empty when core's * Site Health class cannot be loaded. * * @return array */ private static function available_tests(): array { try { if ( ! class_exists( 'WP_Site_Health' ) && is_readable( ABSPATH . 'wp-admin/includes/class-wp-site-health.php' ) ) { require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php'; } if ( ! class_exists( 'WP_Site_Health' ) || ! method_exists( 'WP_Site_Health', 'get_tests' ) || ! method_exists( 'WP_Site_Health', 'get_instance' ) ) { return []; } $tests = \WP_Site_Health::get_tests(); if ( ! is_array( $tests ) || empty( $tests['direct'] ) || ! is_array( $tests['direct'] ) ) { return []; } $instance = \WP_Site_Health::get_instance(); $available = []; foreach ( self::filter_allowlisted( $tests['direct'] ) as $id => $test ) { $runner = self::resolve_runner( $instance, $test ); if ( null !== $runner ) { $available[ $id ] = [ 'label' => isset( $test['label'] ) && is_string( $test['label'] ) ? $test['label'] : $id, 'run' => $runner, ]; } } return $available; } catch ( \Throwable $e ) { return []; } } /** * Resolve a direct test to a callable the way core does: a string names a * `get_test_{name}` method, otherwise the value must itself be callable. * * @param object $instance WP_Site_Health instance. * @param mixed $test Test definition. * * @return callable|null */ private static function resolve_runner( $instance, $test ): ?callable { if ( ! is_array( $test ) || ! isset( $test['test'] ) ) { return null; } if ( is_string( $test['test'] ) ) { $method = 'get_test_' . $test['test']; if ( method_exists( $instance, $method ) && is_callable( [ $instance, $method ] ) ) { return [ $instance, $method ]; } } return is_callable( $test['test'] ) ? $test['test'] : null; } /** * Run one core test and turn its result into a row. * * @param string $id Core test id. * @param array $test `label` and `run`. * * @return array */ private static function run_core_test( string $id, array $test ): array { $row = [ 'id' => 'core-' . str_replace( '_', '-', $id ), 'label' => $test['label'], 'group' => self::GROUP, 'url' => admin_url( 'site-health.php' ), 'link_label' => __( 'Open Site Health', 'admin-optimizer' ), ]; try { $result = call_user_func( $test['run'] ); if ( ! is_array( $result ) ) { throw new \UnexpectedValueException( 'Unexpected Site Health result.' ); } $row['status'] = self::map_status( $result['status'] ?? null ); if ( 'unverified' === $row['status'] ) { $row['message'] = __( 'WordPress returned a result this tab does not recognise.', 'admin-optimizer' ); } else { $wording = self::build_wording( $id, $test['label'], isset( $result['label'] ) && is_string( $result['label'] ) ? $result['label'] : '', $row['status'], PHP_VERSION ); $row['label'] = $wording['label']; $row['message'] = $wording['message']; } } catch ( \Throwable $e ) { $row['status'] = 'unverified'; $row['message'] = __( 'This WordPress check could not be run on this server.', 'admin-optimizer' ); // Even a failed test keeps AO's label. $map = self::wording(); if ( isset( $map[ $id ] ) ) { $row['label'] = $map[ $id ]['label']; } } return $row; } /** * @return array */ private static function registration_fallback_row(): array { $status = self::evaluate_registration( (bool) get_option( 'users_can_register' ), (string) get_option( 'default_role' ) ); $entry = self::wording()['insecure_registration']; return [ 'id' => 'core-insecure-registration', 'label' => $entry['label'], 'status' => $status, 'message' => 'critical' === $status ? $entry['fail'] : $entry['good'], 'group' => self::GROUP, 'url' => admin_url( 'options-general.php' ), 'link_label' => __( 'Open General Settings', 'admin-optimizer' ), ]; } /** * @return array */ private static function debug_fallback_row(): array { $debug = defined( 'WP_DEBUG' ) && WP_DEBUG; $display = ! defined( 'WP_DEBUG_DISPLAY' ) || WP_DEBUG_DISPLAY; $status = self::evaluate_debug( $debug, (bool) $display, wp_get_environment_type() ); $entry = self::wording()['debug_enabled']; return [ 'id' => 'core-debug-enabled', 'label' => $entry['label'], 'status' => $status, 'message' => 'warning' === $status ? $entry['fail'] : $entry['good'], 'group' => self::GROUP, 'url' => admin_url( 'site-health.php' ), 'link_label' => __( 'Open Site Health', 'admin-optimizer' ), ]; } }