Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/wp-admin/includes/class-wp-debug-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 1 addition & 7 deletions src/wp-admin/includes/class-wp-site-health.php
Original file line number Diff line number Diff line change
Expand Up @@ -2809,13 +2809,7 @@ public function get_test_search_engine_visibility() {
* @return array<string, string|array<string, string>> 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' ),
Expand Down
32 changes: 32 additions & 0 deletions src/wp-admin/includes/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -2694,6 +2694,38 @@ function wp_print_request_filesystem_credentials_modal() {
<?php
}

/**
* Determines whether the Zend OPcache extension is enabled.
*
* Uses opcache_get_status() when the host allows OPcache API calls. When
* opcache_get_status() is unavailable (for example, when opcache.restrict_api
* blocks the current script), falls back to the opcache.enable INI setting.
*
* @since 7.1.0
*
* @link https://www.php.net/manual/en/function.opcache-get-status.php
* @link https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.restrict-api
*
* @return bool True if OPcache is enabled, false otherwise.
*/
function wp_opcache_is_enabled() {
if ( ! function_exists( 'opcache_get_status' ) ) {
return false;
}

$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 ) ) {
return (bool) $status['opcache_enabled'];
}

/*
* opcache_get_status() returns false when OPcache is disabled or when the
* opcache.restrict_api INI directive blocks API access for this script.
*/
return wp_validate_boolean( ini_get( 'opcache.enable' ) );
}

/**
* Attempts to clear the opcode cache for an individual PHP file.
*
Expand Down
12 changes: 5 additions & 7 deletions tests/phpunit/tests/admin/wpSiteHealth.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,19 +684,17 @@ public function test_get_test_opcode_cache_return_structure() {
* Covers: opcache enabled, disabled, not available, and opcache_get_status() returns false.
*
* @ticket 63697
* @ticket 65395
*
* @covers ::get_test_opcode_cache()
* @covers ::wp_opcache_is_enabled
*/
public function test_get_test_opcode_cache_result_by_environment() {
$result = $this->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".' );
Expand Down
46 changes: 46 additions & 0 deletions tests/phpunit/tests/filesystem/wpOpcacheIsEnabled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* Tests wp_opcache_is_enabled().
*
* @group file
* @group filesystem
*
* @covers ::wp_opcache_is_enabled
*/
class Tests_Filesystem_WpOpcacheIsEnabled extends WP_UnitTestCase {

/**
* Ensures the helper is available and returns a boolean.
*
* @ticket 65395
*/
public function test_wp_opcache_is_enabled_returns_bool() {
require_once ABSPATH . 'wp-admin/includes/file.php';

$this->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() );
}
}
Loading