From 8e4b9d6d221c5bb2da62a445262fa1888e800412 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 4 Jun 2026 08:40:46 +0000 Subject: [PATCH] Fix Site Health false OPcache warning (#65395) --- src/wp-admin/includes/class-wp-debug-data.php | 20 +++++--- .../includes/class-wp-site-health.php | 8 +--- src/wp-admin/includes/file.php | 32 +++++++++++++ tests/phpunit/tests/admin/wpSiteHealth.php | 12 ++--- .../tests/filesystem/wpOpcacheIsEnabled.php | 46 +++++++++++++++++++ 5 files changed, 97 insertions(+), 21 deletions(-) create mode 100644 tests/phpunit/tests/filesystem/wpOpcacheIsEnabled.php diff --git a/src/wp-admin/includes/class-wp-debug-data.php b/src/wp-admin/includes/class-wp-debug-data.php index f2399b30550c9..0d18a7abd5116 100644 --- a/src/wp-admin/includes/class-wp-debug-data.php +++ b/src/wp-admin/includes/class-wp-debug-data.php @@ -475,13 +475,7 @@ private static function get_wp_server(): array { if ( function_exists( 'opcache_get_status' ) ) { $opcache_status = @opcache_get_status( false ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Warning emitted in failure case. - if ( false === $opcache_status ) { - $fields['opcode_cache'] = array( - 'label' => __( 'Opcode cache' ), - 'value' => __( 'Disabled by configuration' ), - 'debug' => 'not available', - ); - } else { + if ( is_array( $opcache_status ) ) { $fields['opcode_cache'] = array( 'label' => __( 'Opcode cache' ), 'value' => $opcache_status['opcache_enabled'] ? __( 'Enabled' ) : __( 'Disabled' ), @@ -539,6 +533,18 @@ private static function get_wp_server(): array { 'debug' => $opcache_status['cache_full'], ); } + } elseif ( wp_opcache_is_enabled() ) { + $fields['opcode_cache'] = array( + 'label' => __( 'Opcode cache' ), + 'value' => __( 'Enabled (statistics unavailable due to server configuration)' ), + 'debug' => true, + ); + } else { + $fields['opcode_cache'] = array( + 'label' => __( 'Opcode cache' ), + 'value' => __( 'Disabled' ), + 'debug' => false, + ); } } else { $fields['opcode_cache'] = array( diff --git a/src/wp-admin/includes/class-wp-site-health.php b/src/wp-admin/includes/class-wp-site-health.php index 75e046ef8ffa7..25d3d1d74272e 100644 --- a/src/wp-admin/includes/class-wp-site-health.php +++ b/src/wp-admin/includes/class-wp-site-health.php @@ -2809,13 +2809,7 @@ public function get_test_search_engine_visibility() { * @return array> The test result. */ public function get_test_opcode_cache(): array { - $opcode_cache_enabled = false; - if ( function_exists( 'opcache_get_status' ) ) { - $status = @opcache_get_status( false ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Warning emitted in failure case. - if ( $status && true === $status['opcache_enabled'] ) { - $opcode_cache_enabled = true; - } - } + $opcode_cache_enabled = wp_opcache_is_enabled(); $result = array( 'label' => __( 'Opcode cache is enabled' ), diff --git a/src/wp-admin/includes/file.php b/src/wp-admin/includes/file.php index 0c6d968ea02d3..49eb49b3e75cd 100644 --- a/src/wp-admin/includes/file.php +++ b/src/wp-admin/includes/file.php @@ -2694,6 +2694,38 @@ function wp_print_request_filesystem_credentials_modal() { instance->get_test_opcode_cache(); - $opcache_enabled = false; - if ( function_exists( 'opcache_get_status' ) ) { - $status = @opcache_get_status( false ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Warning emitted in failure case. - if ( $status && true === $status['opcache_enabled'] ) { - $opcache_enabled = true; - } - } + require_once ABSPATH . 'wp-admin/includes/file.php'; + + $opcache_enabled = wp_opcache_is_enabled(); if ( $opcache_enabled ) { $this->assertSame( 'good', $result['status'], 'When opcache is enabled, status should be "good".' ); diff --git a/tests/phpunit/tests/filesystem/wpOpcacheIsEnabled.php b/tests/phpunit/tests/filesystem/wpOpcacheIsEnabled.php new file mode 100644 index 0000000000000..5471c4f17a8b1 --- /dev/null +++ b/tests/phpunit/tests/filesystem/wpOpcacheIsEnabled.php @@ -0,0 +1,46 @@ +assertIsBool( wp_opcache_is_enabled() ); + } + + /** + * Tests that the helper matches expected detection for the current environment. + * + * @ticket 65395 + */ + public function test_wp_opcache_is_enabled_matches_environment() { + require_once ABSPATH . 'wp-admin/includes/file.php'; + + $expected = false; + + if ( function_exists( 'opcache_get_status' ) ) { + $status = @opcache_get_status( false ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Warning emitted when the API is restricted. + + if ( is_array( $status ) && array_key_exists( 'opcache_enabled', $status ) ) { + $expected = (bool) $status['opcache_enabled']; + } else { + $expected = wp_validate_boolean( ini_get( 'opcache.enable' ) ); + } + } + + $this->assertSame( $expected, wp_opcache_is_enabled() ); + } +}