Skip to content

Instantly share code, notes, and snippets.

@tnog
Last active April 27, 2021 20:17
Show Gist options
  • Save tnog/56fff72aa5113a8b4212902e4ed240e6 to your computer and use it in GitHub Desktop.
Save tnog/56fff72aa5113a8b4212902e4ed240e6 to your computer and use it in GitHub Desktop.
<?php
/**
* Clears entire WP Engine cache
* @return void
*/
public function wpeCacheFlush() {
// Only initiates a cache clear if the $cleared property is false
if (!self::$cleared) {
// Don't cause a fatal error if there is no WpeCommon class
if (!class_exists('WpeCommon')) {
return false;
}
if (method_exists('WpeCommon', 'purge_memcached')) {
\WpeCommon::purge_memcached();
AdminNotice::log('WpeCommon::purge_memcached triggered');
}
if (method_exists('WpeCommon', 'clear_maxcdn_cache')) {
\WpeCommon::clear_maxcdn_cache();
}
if (method_exists('WpeCommon', 'purge_varnish_cache')) {
\WpeCommon::purge_varnish_cache();
AdminNotice::log('WpeCommon::purge_varnish_cache triggered');
}
global $wp_object_cache;
// Check for valid cache. Sometimes this is broken -- we don't know why! -- and it crashes when we flush.
// If there's no cache, we don't need to flush anyway.
$error = '';
if ($wp_object_cache && is_object($wp_object_cache)) {
try {
wp_cache_flush();
} catch (\Exception $ex) {
$error = "Warning: error flushing WordPress object cache: " . $ex->getMessage();
}
}
// Log our response to the debug.log
if (!empty($error)) {
AdminNotice::displayError($error);
AdminNotice::log($error);
} else {
$success = __('All WP Engine caches have been cleared successfully.', 'to4-framework');
AdminNotice::log($success);
AdminNotice::displaySuccess($success);
}
self::$cleared = true;
}
}
/**
* Trigger entire WPE cache include memcache, Varnish, and object cache,
* and CF cache if on live WPE site, and if cache clearing is enabled in settings
* @param string $log_message (optional) Add additional message for debugging
* @param boolean $rest (optional) Setting to true enables CF cache clearing for custom REST API endpoints
*/
public function globalCacheFlush($log_message = null, $rest = false) {
// Send cache setting status and what type of cache clear has been triggered to admin_notice
self::logCacheClearStatus();
AdminNotice::log($log_message);
//AdminNotice::displayInfo($log_message);
// We'll only perform this on WPE servers if cache purge is enabled in our settings
if ( (1 == $this->isThisWpe()) && (1 == self::$cachePurgeOptions) ) {
// Trigger cache clear
$this->wpeCacheFlush();
$this->cfCacheFlushAll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment