PluginProbe
OPcache Reset / 2.1.3
OPcache Reset v2.1.3
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.1.3, at opcache-reset.php

88 lines 2.7 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.1.2
7 * Author: Danila Vershinin
8 * Author URI: https://www.getpagespeed.com/
9 * License: GPL2
10 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
11 */
12
13 // Make sure we don't expose any info if called directly
14 if ( !function_exists( 'add_action' ) ) {
15 echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
16 exit;
17 }
18
19 if ( is_admin() ) {
20 if (! (defined( 'DOING_AJAX' ) && DOING_AJAX)) {
21 // We are in admin mode: notices and such are handled here
22 require_once(__DIR__ . '/opcache-admin.php');
23 }
24 }
25
26 /**
27 * Reset OPCache using different approach depending on the caller context (e.g. cron vs. web)
28 */
29 function gps_opcache_reset() {
30
31 if( ! function_exists('opcache_reset') ) {
32 // Bail if extension is not loaded
33 return;
34 }
35
36
37 if (empty( ini_get( 'opcache.enable' ))) {
38 // Do not try doing anything if OPcache is loaded but disabled
39 return;
40 }
41
42 if ( ! empty( ini_get( 'opcache.restrict_api' ) ) && strpos( __FILE__, ini_get( 'opcache.restrict_api' ) ) !== 0 ) {
43 return;
44 }
45
46 // https://www.getpagespeed.com/server-setup/php/zend-opcache
47 // Follow the principles of reliable file cache clearing from this article
48 $fileCacheDir = ini_get( 'opcache.file_cache' );
49 // Check if file cache is enabled and delete it if enabled
50 if ( $fileCacheDir && is_writable( $fileCacheDir ) ) {
51 // check if we can create subdirectory in the parent directory.
52 // Normally, it's ~/.cache so we can
53 $cacheDir = dirname($fileCacheDir);
54 if (! is_writable($cacheDir)) {
55 $fileCacheDir = null;
56 }
57 }
58
59 if ($fileCacheDir && file_exists($fileCacheDir)) {
60 // move it out of the way to avoid race conditions
61 shell_exec("mv ${fileCacheDir} ${fileCacheDir}.rm");
62 }
63
64 // We are in PHP-FPM context and not file cache only
65 if (php_sapi_name() !== 'cli' && ! ini_get( 'opcache.file_cache_only' )) {
66 opcache_reset();
67 }
68
69 if ($fileCacheDir) {
70 // if file cache only, do not use opcache:reset as it cannot clear file caches and yield error in such case
71 if (! ini_get( 'opcache.file_cache_only' )) {
72 shell_exec('php -d opcache.enable_cli=0 -d opcache.file_cache=/tmp $(which cachetool) opcache:reset');
73 }
74 if ( file_exists( "${fileCacheDir}.rm" )) {
75 shell_exec( "rm -rf ${fileCacheDir}.rm" );
76 }
77 // make sure OPcache directory is re-created
78 shell_exec( "mkdir -p ${fileCacheDir}" );
79 }
80
81 }
82
83
84 add_action( 'upgrader_process_complete', 'gps_opcache_reset', PHP_INT_MAX - 1, 2 );
85
86
87
88