Last active
January 27, 2024 14:05
-
-
Save Digiover/f3a30849f621cdb97e3346b300711e74 to your computer and use it in GitHub Desktop.
WordPress: Clear PHP opcode caches before updates are applied, ease the updating process (https://www.saotn.org/wordpress-plugin-flush-php-opcache/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Clear PHP opcode caches | |
* Plugin URI: https://www.saotn.org/wordpress-plugin-flush-php-opcache/ | |
* Donate URI: https://www.paypal.me/jreilink | |
* Description: Purges various PHP opcode and user caches. Currently it tries to clear / purge / flush PHP OPcache and WinCache caches from your web server's memory. This should ease WordPress updates and plugin activation / deactivation. | |
* Network: True | |
* Version: 1.2 | |
* Author: Jan Reilink | |
* Author URI: https://www.saotn.org | |
* License: GPLv2 | |
*/ | |
require_once( ABSPATH .'/wp-config.php' ); | |
function clear_memcached_cache() { | |
global $memcached_servers; | |
if( isset($memcached_servers) ) { | |
list( $socket, $port ) = explode( ":", $memcached_servers["default"][0] ); | |
$m = new Memcached(); | |
// $m->addServer( '/path/to/my/wordpress-site/temp/memcached.sock', 0 ); | |
$m->addServer( $socket, $port); | |
if ( false === $m->flush() ) { | |
return false; | |
} | |
else { | |
return true; | |
} | |
} | |
} | |
function clear_iis_wincache() { | |
if( ! function_exists( 'wincache_ucache_get' ) ) { | |
return; | |
} | |
if( ! wincache_ucache_clear() ) { | |
return false; | |
} | |
else { | |
return true; | |
} | |
} | |
function clear_php_opcache() { | |
if( ! extension_loaded( 'Zend OPcache' ) ) { | |
return; | |
} | |
$opcache_status = opcache_get_status(); | |
if( false === $opcache_status["opcache_enabled"] ) { | |
error_log( 'extension loaded but OPcache not enabled' ); | |
return; | |
} | |
if( ! opcache_reset() ) { | |
return false; | |
} | |
else { | |
/** | |
* opcache_reset() is performed, now try to clear the | |
* file cache. | |
* Please note: http://stackoverflow.com/a/23587079/1297898 | |
* "Opcache does not evict invalid items from memory - they | |
* stay there until the pool is full at which point the | |
* memory is completely cleared" | |
*/ | |
foreach( $opcache_status['scripts'] as $key => $data ) { | |
$dirs[dirname( $key )][basename( $key )] = $data; | |
opcache_invalidate( $data['full_path'] , $force = true ); | |
} | |
return true; | |
} | |
} | |
function is_iis() { | |
$software = strtolower( $_SERVER["SERVER_SOFTWARE"] ); | |
if( false !== strpos( $software, "microsoft-iis" ) ) | |
return true; | |
else | |
return false; | |
} | |
function clear_caches() { | |
if( is_iis() ) { | |
if ( clear_iis_wincache() ) { | |
error_log( 'WinCache user cache cleared.' ); | |
} | |
else { | |
error_log( 'Clearing WinCache user cache opcode cache failed.' ); | |
} | |
} | |
if( clear_memcached_cache() ) { | |
error_log( 'Memcached flushed' ); | |
} | |
if( clear_php_opcache() ) { | |
error_log( 'PHP OPcache opcode cache cleared.' ); | |
} | |
else { | |
error_log( 'Clearing PHP OPcache opcode cache failed.' ); | |
} | |
} | |
add_filter( 'upgrader_pre_install', 'clear_caches', 10, 2 ); | |
// cpoc = Clear PHP Opcode Caches | |
add_filter( 'plugin_row_meta', 'cpoc_plugin_row_meta', 10, 2 ); | |
function cpoc_plugin_row_meta($links, $file) { | |
if ( !preg_match('/clear-opcode-caches.php$/', $file ) ) { | |
return $links; | |
} | |
$links[] = sprintf( | |
'<a target="_blank" href="https://paypal.me/jreilink" title="Donate to Jan Reilink / Sysadmins of the North">%s</a>', | |
__( 'Donate' ) | |
); | |
return $links; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment