PluginProbe
OPcache Reset / 2.3.0
OPcache Reset v2.3.0
trunk 1.0.0 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.3.0 2.4.0 2.4.1 2.4.2 2.4.3
opcache-reset / opcache-reset.php

opcache-reset.php in OPcache Reset 2.3.0, at opcache-reset.php

100 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: OPcache Reset
4 * Plugin URI: http://wordpress.org/plugins/opcache-reset/
5 * Description: Automatic reset of OPcache
6 * Version: 2.3.0
7 * Requires at least: 5.0
8 * Requires PHP: 7.4
9 * Author: Danila Vershinin
10 * Author URI: https://www.getpagespeed.com/
11 * License: GPL2
12 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
13 */
14
15 // Make sure we don't expose any info if called directly
16 if ( ! function_exists( 'add_action' ) ) {
17 echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
18 exit;
19 }
20
21 if ( is_admin() ) {
22 if ( ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
23 // We are in admin mode: notices and such are handled here
24 require_once __DIR__ . '/opcache-admin.php';
25 }
26 }
27
28 /**
29 * Reset OPCache using different approach depending on the caller context (e.g. cron vs. web)
30 */
31 function gps_opcache_reset() {
32
33 if ( ! function_exists( 'opcache_reset' ) ) {
34 // Bail if extension is not loaded
35 return;
36 }
37
38 if ( empty( ini_get( 'opcache.enable' ) ) ) {
39 // Do not try doing anything if OPcache is loaded but disabled
40 return;
41 }
42
43 if ( ! empty( ini_get( 'opcache.restrict_api' ) ) && strpos( __FILE__, ini_get( 'opcache.restrict_api' ) ) !== 0 ) {
44 return;
45 }
46
47 // https://www.getpagespeed.com/server-setup/php/zend-opcache
48 // Follow the principles of reliable file cache clearing from this article
49 $file_cache_dir = ini_get( 'opcache.file_cache' );
50 // Check if file cache is enabled and delete it if enabled
51 if ( $file_cache_dir && is_writable( $file_cache_dir ) ) {
52 // check if we can create subdirectory in the parent directory.
53 // Normally, it's ~/.cache so we can
54 $cache_dir = dirname( $file_cache_dir );
55 if ( ! is_writable( $cache_dir ) ) {
56 $file_cache_dir = null;
57 }
58 }
59
60 if ( $file_cache_dir && file_exists( $file_cache_dir ) ) {
61 // move it out of the way to avoid race conditions
62 shell_exec( "mv {$file_cache_dir} {$file_cache_dir}.rm" );
63 }
64
65 // We are in PHP-FPM context and not file cache only
66 if ( php_sapi_name() !== 'cli' && ! ini_get( 'opcache.file_cache_only' ) ) {
67 opcache_reset();
68 }
69
70 if ( $file_cache_dir ) {
71 if ( file_exists( "{$file_cache_dir}.rm" ) ) {
72 shell_exec( "rm -rf {$file_cache_dir}.rm" );
73 }
74 // make sure OPcache directory is re-created
75 shell_exec( "mkdir -p {$file_cache_dir}" );
76 }
77
78 // Irrespective of the context, we should attempt to clear the memory-based OPCache, because this script may be called from CLI
79 // and can't be aware of whether PHP-FPM is running with file_cache_only => On
80 // Since the script may be called via cron or PHP-FPM with clear_env settings, the PATH may not be set properly, so we need to set it
81 $username = getenv( 'USER' );
82 $path = "/home/{$username}/.local/bin:/usr/bin:/bin";
83 $system_path = getenv( 'PATH' );
84 if ( $system_path ) {
85 $path .= ':' . $system_path;
86 }
87 $cmd = 'php -d opcache.enable_cli=0 -d opcache.file_cache_only=0 -d opcache.file_cache=/tmp $(which cachetool) opcache:reset';
88 // Run cachetool with the adjusted PATH
89 shell_exec( "PATH={$path} {$cmd} 2>&1" );
90 }
91
92
93 // Reset OPcache after plugin/theme/core updates (files are modified on disk).
94 // Note: Activation/deactivation/theme-switch don't need cache clear since files aren't modified.
95 add_action( 'upgrader_process_complete', 'gps_opcache_reset', PHP_INT_MAX - 1, 2 );
96
97 // Reset OPcache after a plugin is deleted/uninstalled (files are removed from disk).
98 // Important when opcache.validate_timestamps=0 to prevent serving cached deleted files.
99 add_action( 'deleted_plugin', 'gps_opcache_reset', PHP_INT_MAX - 1, 2 );
100