From 45247573db075b1282a924f5781944c5c0d54c4b Mon Sep 17 00:00:00 2001 From: Sneha Date: Thu, 13 Feb 2025 13:56:44 +0530 Subject: [PATCH 1/2] Feat: Handles opcache invalidation across different PHP SAPIs and configurations. --- src/wp-admin/includes/file.php | 54 ++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/src/wp-admin/includes/file.php b/src/wp-admin/includes/file.php index 152a1e2f21395..4b7a9066edbd5 100644 --- a/src/wp-admin/includes/file.php +++ b/src/wp-admin/includes/file.php @@ -2742,11 +2742,47 @@ function wp_opcache_invalidate( $filepath, $force = false ) { * @param bool $will_invalidate Whether WordPress will invalidate `$filepath`. Default true. * @param string $filepath The path to the PHP file to invalidate. */ - if ( apply_filters( 'wp_opcache_invalidate_file', true, $filepath ) ) { - return opcache_invalidate( $filepath, $force ); + if ( ! apply_filters( 'wp_opcache_invalidate_file', true, $filepath ) ) { + return false; } - return false; + $success = opcache_invalidate( $filepath, $force ); + + // Handle CLI scenario + if ( 'cli' === php_sapi_name() ) { + // Create a flag file to signal FPM processes + $flag_file = WP_CONTENT_DIR . '/opcache-reset-' . md5( $filepath ) . '.flag'; + file_put_contents( $flag_file, time() ); + } + + // Handle file cache scenario + if ( ini_get( 'opcache.file_cache' ) ) { + // If file cache is enabled, we need to ensure complete invalidation + if ( function_exists( 'opcache_reset' ) ) { + opcache_reset(); + $success = true; + } + } + + // Check for reset flags in FPM context + if ( 'cli' !== php_sapi_name() ) { + $flags = glob( WP_CONTENT_DIR . '/opcache-reset-*.flag' ); + if ( ! empty( $flags ) ) { + foreach ( $flags as $flag_file ) { + $flag_time = (int) file_get_contents( $flag_file ); + // If flag is recent (within last minute) + if ( time() - $flag_time < 60 ) { + if ( function_exists( 'opcache_reset' ) ) { + opcache_reset(); + $success = true; + } + } + @unlink( $flag_file ); + } + } + } + + return $success; } /** @@ -2782,6 +2818,18 @@ function wp_opcache_invalidate_directory( $dir ) { return; } + // Handle CLI scenario for directory invalidation + if ( 'cli' === php_sapi_name() ) { + $flag_file = WP_CONTENT_DIR . '/opcache-reset-dir-' . md5( $dir ) . '.flag'; + file_put_contents( $flag_file, time() ); + } + + // Handle file cache for directory invalidation + $has_file_cache = ini_get( 'opcache.file_cache' ); + if ( $has_file_cache && function_exists( 'opcache_reset' ) ) { + opcache_reset(); + } + /* * Recursively invalidate opcache of files in a directory. * From 5aecf9755bb449b4e7592c51605d46de8afc7bdd Mon Sep 17 00:00:00 2001 From: anand346 Date: Thu, 20 Feb 2025 18:53:18 +0530 Subject: [PATCH 2/2] Improved formatting of the code --- src/wp-admin/includes/file.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/wp-admin/includes/file.php b/src/wp-admin/includes/file.php index 4b7a9066edbd5..92e5fc0db6a16 100644 --- a/src/wp-admin/includes/file.php +++ b/src/wp-admin/includes/file.php @@ -2748,29 +2748,30 @@ function wp_opcache_invalidate( $filepath, $force = false ) { $success = opcache_invalidate( $filepath, $force ); - // Handle CLI scenario + // Handle CLI scenario. if ( 'cli' === php_sapi_name() ) { - // Create a flag file to signal FPM processes + // Create a flag file to signal FPM processes. $flag_file = WP_CONTENT_DIR . '/opcache-reset-' . md5( $filepath ) . '.flag'; file_put_contents( $flag_file, time() ); } - // Handle file cache scenario + // Handle file cache scenario. if ( ini_get( 'opcache.file_cache' ) ) { - // If file cache is enabled, we need to ensure complete invalidation + // If file cache is enabled, we need to ensure complete invalidation. if ( function_exists( 'opcache_reset' ) ) { opcache_reset(); $success = true; } } - // Check for reset flags in FPM context + // Check for reset flags in FPM context. if ( 'cli' !== php_sapi_name() ) { $flags = glob( WP_CONTENT_DIR . '/opcache-reset-*.flag' ); if ( ! empty( $flags ) ) { foreach ( $flags as $flag_file ) { $flag_time = (int) file_get_contents( $flag_file ); - // If flag is recent (within last minute) + + // If flag is recent (within last minute). if ( time() - $flag_time < 60 ) { if ( function_exists( 'opcache_reset' ) ) { opcache_reset();