Skip to content

Instantly share code, notes, and snippets.

@neilgee
Last active December 19, 2021 13:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neilgee/4a251b24f65a70849ab122afa6dfac0f to your computer and use it in GitHub Desktop.
Save neilgee/4a251b24f65a70849ab122afa6dfac0f to your computer and use it in GitHub Desktop.
Purge RunCloud Cache On Extra Beaver Hooks
<?php
/**
* Plugin Name: Runcloud BeaverCache Purge Helper
* Plugin URI: https://gist.github.com/neilgee/4a251b24f65a70849ab122afa6dfac0fp
* Description: Adding additional hooks to trigger RunCloud or lscache plugin purges
* Version: 0.1
* Author: Paul Stoute, Jordan Trask, Jeff Cleverly, Neil Gowran
* Author URI: https://github.com/jordantrizz/cache-purge-helper
* Text Domain: runcloud-beaver-cache-purge-helper
* Domain Path: /languages
* Requires at least: 3.0
* Tested up to: 5.4
*
* @link https://wpbeaches.com
* @since 0.1
* @package runcloud-beaver-cache-purge-helper
*/
/* Purge Cache Function
*
* RunCloud have either RunCloud Hub(nginx) or LiteSpeed server caches, this will check for the existance of either and flush it based on any triggered actions at the bottom of the gist
*/
function runcloud_beavercache_purge_helper() {
// Purge WordPress Cache
$called_action_hook = current_filter();
write_log('rbcph - initiated');
write_log('rbcph - running on '. $called_action_hook );
write_log('rbcph - flushing WordPress Cache first');
wp_cache_flush();
// If RunCloud plugin is enabled, purge cache.
write_log('rbcph - checking for RunCloud plugin');
if ( class_exists('RunCloud_Hub') && is_callable( [ 'RunCloud_Hub', 'purge_cache_all_noaction' ] ) ) {
RunCloud_Hub::purge_cache_all_noaction();
write_log('rbcph - flushing RunCloud cache out');
} else {
write_log('rbcph - No RunCloud Plugin here');
}
// If litespeed-cache plugin is enabled, purge cache.
write_log('rbcph - checking for litespeed-cache plugin');
if ( is_plugin_active('litespeed-cache/litespeed-cache.php') ) {
write_log('rbcph - litespeed-cache plugin installed, running do_action(\'litespeed_purge_all\');');
do_action( 'litespeed_purge_all' );
} else {
write_log('rbcph - litespeed-cache plugin not installed or detected');
}
write_log('rbcph - end of runcloud_beavercache_purge_helper function');
}
/* Log to WordPress Debug Log */
if ( ! function_exists('write_log')) {
function write_log ( $log ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
}
}
// Beaver Builder Actions - when to flush
add_action( 'fl_builder_cache_cleared', 'runcloud_beavercache_purge_helper', 10, 3 );
add_action( 'fl_builder_after_save_layout', 'runcloud_beavercache_purge_helper', 10, 3 );
add_action( 'fl_builder_after_save_user_template', 'runcloud_beavercache_purge_helper', 10, 3 );
add_action( 'upgrader_process_complete', 'runcloud_beavercache_purge_helper', 10, 3 );
// AutoOptimizer
add_action( 'autoptimize_action_cachepurged','runcloud_beavercache_purge_helper', 10, 3 ); // Need to document this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment