Skip to content

Commit

Permalink
Merge pull request #1 from weavedigitalstudio/dev
Browse files Browse the repository at this point in the history
🚀 Merge dev into main for latest updates
  • Loading branch information
gbissland authored Feb 27, 2025
2 parents 1a50580 + aeb8ef8 commit 1d06625
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 82 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Build Plugin Release
on:
push:
tags:
- "*"
jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Get the version
id: get_version
run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
tools: composer

# Uncomment if you have Composer dependencies
# - name: Install dependencies
# run: composer install --no-dev --optimize-autoloader

- name: Create build directory
run: mkdir -p build/weave-cache-purge-helper

- name: Copy plugin files to build directory
run: |
rsync -av --exclude='.git' \
--exclude='.github' \
--exclude='build' \
--exclude='.gitignore' \
--exclude='README.md' \
--exclude='CHANGELOG.md' \
--exclude='composer.json' \
--exclude='composer.lock' \
--exclude='.DS_Store' \
--exclude='.nova' \
--exclude='node_modules' \
--exclude='package.json' \
--exclude='package-lock.json' \
--exclude='phpcs.xml' \
--exclude='phpunit.xml' \
--exclude='tests' \
. build/weave-cache-purge-helper/
- name: Create zip file
run: cd build && zip -r weave-cache-purge-helper-${{ env.VERSION }}.zip weave-cache-purge-helper

- name: Create GitHub release
uses: softprops/action-gh-release@v1
with:
files: build/weave-cache-purge-helper-${{ env.VERSION }}.zip
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# Changelog

## [1.0.1] - 2024-06-30
## [1.2.0] - 2025-02-27
### Added
- Integration with WP-Umbrella's cache system to properly clear caches after WP-Umbrella updates.
- Improved cache clearing sequence to prevent 404 errors (Beaver Builder first, then Nginx/LiteSpeed).
- Enhanced logging for WP-Umbrella triggered events.

### Changed
- Refactored cache clearing logic to ensure proper order of operations.
- Improved compatibility checks for both Nginx Helper and LiteSpeed Cache.

### Fixed
- Resolved 404 errors on Beaver Builder assets by ensuring caches are cleared in the correct order.
- Fixed potential issues with cache synchronization between WP-Umbrella, Beaver Builder, and Nginx/LiteSpeed.

## [1.1.1] - 2024-06-30
### Changed
- Refactored plugin initialization to reduce redundant log messages.
- Added `wcph_init_hooks` function to initialize hooks once during the `init` action.
Expand Down
33 changes: 26 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Weave Cache Purge Helper

This is a fork of the Cache Purge Helper plugin, tailored for use by Weave Digital. It includes additional hooks for Beaver Builder and ACF to trigger NGINX Helper and LiteSpeed Cache plugin purges.
This is a fork of the Cache Purge Helper plugin, tailored for use by Weave Digital on GridPane. It includes additional hooks for Beaver Builder, ACF, and WP-Umbrella to trigger NGINX Helper and LiteSpeed Cache plugin purges.

## Changes from Original Plugin

### Added
- Integration ACF Options pages update to trigger cache purges.
- Integration with ACF Options pages updates to trigger cache purges.
- Integration with WP-Umbrella to ensure proper cache clearing after plugin updates.
- Proper cache clearing sequence (Beaver Builder first, then Nginx/LiteSpeed) to prevent 404 errors.

### Changed
- Logging prefix updated from `cphp` to `wcph`.
Expand All @@ -23,12 +25,20 @@ This fork is based on the Cache Purge Helper plugin by Paul Stoute, Jordan Trask
* Jeff Cleverley - [GridPane](https://gridpane.com)
* Gareth Bissland - [Weave Digital Studio](https://weave.co.nz) (Fork Author)

## Installation
## Installation from GitHub

### Manual Install
1. Extract the zip file.
2. Upload the extracted folder to the `/wp-content/plugins/` directory on your WordPress installation.
3. Activate the plugin from the Plugins page.
When installing this plugin from GitHub:

1. Go to the [Releases](https://github.com/weavedigitalstudio/weave-cache-purge-helper/releases) page
2. Download the latest release ZIP file
3. Extract the ZIP file on your computer
4. Rename the extracted folder to remove the version number
(e.g., from `weave-cache-purge-helper-1.1.0` to `weave-cache-purge-helper`)
5. Create a new ZIP file from the renamed folder
6. In your WordPress admin panel, go to Plugins → Add New → Upload Plugin
7. Upload your new ZIP file and activate the plugin

**Note**: The folder renaming step is necessary for WordPress to properly handle plugin updates and functionality.

## Logging

Expand All @@ -45,10 +55,19 @@ define( 'WC_PHP_DEBUG', true ); // Custom logging

Enable Secure WP Debug via toggle switch then add `define( 'WC_PHP_DEBUG', true );` to your website user-configs.php

## Cache Clearing Order

This plugin ensures that caches are cleared in the correct order:
1. First: Beaver Builder cache is cleared
2. Second: Nginx Helper or LiteSpeed cache is cleared

This sequence prevents 404 errors that can occur when Nginx serves cached HTML that references old, non-existent Beaver Builder asset files.

## Fork Information
* Original Plugin URI: [Cache Purge Helper on GitHub](https://github.com/managingwp/cache-purge-helper)
* Author of the Fork: Gareth Bissland - [GitHub](https://github.com/gbissland)

### Note
For detailed changes, see the `CHANGELOG.md` file.


196 changes: 122 additions & 74 deletions cache-purge-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/**
* Plugin Name: Weave Cache Purge Helper
* Plugin URI: https://github.com/weavedigitalstudio/weave-cache-purge-helper/
* Description: Fork of Cache Purge Helper for Weave Digital Use. Adds additional WordPress and BB, ACF hooks to trigger NGINX Helper / lscache plugin purges.
* Version: 1.1.1
* Description: Fork of Cache Purge Helper for Weave Digital Use. Adds additional WordPress, BB, ACF, and WP-Umbrella hooks to trigger cache purges in the correct order.
* Version: 1.2.0
* Author: Gareth Bissland, Paul Stoute, Jordan Trask, Jeff Cleverley
* Author URI: https://weave.co.nz
* Text Domain: weave-cache-purge-helper
Expand All @@ -20,96 +20,144 @@
* @package weave-cache-purge-helper
*/


if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

/**
* Log to WordPress Debug Log
*/
function wcph_write_log($log) {
if ( defined('WP_DEBUG') && WP_DEBUG === true && defined('WC_PHP_DEBUG') && WC_PHP_DEBUG === true ) {
if ( is_array($log) || is_object($log) ) {
error_log(print_r($log, true));
} else {
error_log($log);
}
}
}

/**
* Purge Cache Function
*/
function wcph_purge() {
$called_action_hook = current_filter();
wcph_write_log('[' . date('Y-m-d H:i:s') . '] ' . __('wcph - initiated on ', 'weave-cache-purge-helper') . $called_action_hook);

// Default WordPress Cache Purge
wp_cache_flush();
$purge_executed = false;
$called_action_hook = current_filter();
wcph_write_log('[' . date('Y-m-d H:i:s') . '] ' . __('wcph - initiated on ', 'weave-cache-purge-helper') . $called_action_hook);

// Default WordPress Cache Purge
wp_cache_flush();

// FIRST: Clear Beaver Builder cache if it's active
if (defined('FL_BUILDER_VERSION') && class_exists('FLBuilderModel')) {
wcph_write_log(__('wcph - Clearing Beaver Builder cache.', 'weave-cache-purge-helper'));
FLBuilderModel::delete_asset_cache_for_all_posts();
wcph_write_log(__('wcph - Beaver Builder cache cleared.', 'weave-cache-purge-helper'));
}

// SECOND: Clear Nginx/Litespeed cache
$purge_executed = false;

// Check and Purge Nginx Helper Cache
if ( function_exists( 'is_plugin_active' ) && is_plugin_active('nginx-helper/nginx-helper.php') ) {
global $nginx_purger;
if ( isset( $nginx_purger ) && is_object( $nginx_purger ) ) {
wcph_write_log(__('wcph - nginx-helper plugin detected, purging cache.', 'weave-cache-purge-helper'));
$nginx_purger->purge_all();
$purge_executed = true; // Mark purge as executed
}
}

// Check and Purge LiteSpeed Cache (if no other purge executed)
if ( !$purge_executed && function_exists('litespeed_purge_all') ) {
wcph_write_log(__('wcph - litespeed-cache plugin detected, purging cache.', 'weave-cache-purge-helper'));
do_action('litespeed_purge_all');
$purge_executed = true; // Mark purge as executed
}

// If no supported cache plugin was detected, log a message
if ( !$purge_executed ) {
wcph_write_log(__('wcph - No supported cache plugin detected. No cache purge executed.', 'weave-cache-purge-helper'));
} else {
wcph_write_log(__('wcph - cache purge completed.', 'weave-cache-purge-helper'));
}
}

// Check and Purge Nginx Helper Cache
if ( function_exists( 'is_plugin_active' ) && is_plugin_active('nginx-helper/nginx-helper.php') ) {
global $nginx_purger;
if ( isset( $nginx_purger ) && is_object( $nginx_purger ) ) {
wcph_write_log(__('wcph - nginx-helper plugin detected, purging cache.', 'weave-cache-purge-helper'));
$nginx_purger->purge_all();
$purge_executed = true; // Mark purge as executed
}
}
// WP-Umbrella Integration
if (file_exists(WP_PLUGIN_DIR . '/wp-health')) {
include_once WP_PLUGIN_DIR . '/wp-health/src/Helpers/GodTransient.php';
include_once WP_PLUGIN_DIR . '/wp-health/src/God/ErrorHandler.php';

// Check and Purge LiteSpeed Cache (if no other purge executed)
if ( !$purge_executed && function_exists('litespeed_purge_all') ) {
wcph_write_log(__('wcph - litespeed-cache plugin detected, purging cache.', 'weave-cache-purge-helper'));
do_action('litespeed_purge_all');
$purge_executed = true; // Mark purge as executed
}
include_once ABSPATH . 'wp-admin/includes/plugin.php';

// If no supported cache plugin was detected, log a message
if ( !$purge_executed ) {
wcph_write_log(__('wcph - No supported cache plugin detected. No cache purge executed.', 'weave-cache-purge-helper'));
} else {
wcph_write_log(__('wcph - cache purge completed.', 'weave-cache-purge-helper'));
}
}
if (class_exists("\WPUmbrella\Core\Collections\CacheCollectionItem") &&
is_plugin_active('wp-health/wp-health.php')) {

/**
* Log to WordPress Debug Log
*/
function wcph_write_log($log) {
if ( defined('WP_DEBUG') && WP_DEBUG === true && defined('WC_PHP_DEBUG') && WC_PHP_DEBUG === true ) {
if ( is_array($log) || is_object($log) ) {
error_log(print_r($log, true));
} else {
error_log($log);
}
}
class WPUmbrellaNginxHelper implements \WPUmbrella\Core\Collections\CacheCollectionItem
{
public static function isAvailable()
{
if (!function_exists('is_plugin_active')) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

// Check for Nginx Helper OR LiteSpeed Cache
return is_plugin_active('nginx-helper/nginx-helper.php') ||
is_plugin_active('litespeed-cache/litespeed-cache.php') ||
class_exists('\Nginx_Helper');
}

public function clear()
{
wcph_write_log('[' . date('Y-m-d H:i:s') . '] ' . __('wcph - WP Umbrella triggered cache clear', 'weave-cache-purge-helper'));
wcph_purge();
wcph_write_log('[' . date('Y-m-d H:i:s') . '] ' . __('wcph - WP Umbrella cache clear completed', 'weave-cache-purge-helper'));
}
}

add_filter('wp_umbrella_cache_compatibilities', 'wpu_add_cache_compatibilities');
function wpu_add_cache_compatibilities($classes)
{
$classes[] = '\WPUmbrellaNginxHelper';
return $classes;
}
}
}

/**
* Initialize Plugin Hooks
*/
function wcph_init_hooks() {
static $initialized = false;

if ($initialized) {
return;
}

$initialized = true;

add_action('upgrader_process_complete', 'wcph_purge', 10, 0);
add_action('activated_plugin', 'wcph_purge', 10, 0);
add_action('deactivated_plugin', 'wcph_purge', 10, 0);
add_action('switch_theme', 'wcph_purge', 10, 0);

// Beaver Builder
if ( defined('FL_BUILDER_VERSION') ) {
add_action('fl_builder_cache_cleared', 'wcph_purge', 10, 0);
add_action('fl_builder_after_save_layout', 'wcph_purge', 10, 0);
add_action('fl_builder_after_save_user_template', 'wcph_purge', 10, 0);
}

// ACF Options Page Purge
if ( function_exists('acf') ) {
add_action('acf/save_post', function($post_id) {
if ($post_id === 'options') {
wcph_write_log(__('wcph - ACF options page saved, clearing Beaver Builder cache.', 'weave-cache-purge-helper'));
FLBuilderModel::delete_asset_cache_for_all_posts();
wcph_write_log(__('wcph - Beaver Builder cache cleared.', 'weave-cache-purge-helper'));

wcph_purge();
}
});
}
static $initialized = false;
if ($initialized) {
return;
}
$initialized = true;

add_action('upgrader_process_complete', 'wcph_purge', 10, 0);
add_action('activated_plugin', 'wcph_purge', 10, 0);
add_action('deactivated_plugin', 'wcph_purge', 10, 0);
add_action('switch_theme', 'wcph_purge', 10, 0);

// Beaver Builder
if ( defined('FL_BUILDER_VERSION') ) {
add_action('fl_builder_cache_cleared', 'wcph_purge', 10, 0);
add_action('fl_builder_after_save_layout', 'wcph_purge', 10, 0);
add_action('fl_builder_after_save_user_template', 'wcph_purge', 10, 0);
}

// ACF Options Page Purge
if ( function_exists('acf') ) {
add_action('acf/save_post', function($post_id) {
if ($post_id === 'options') {
wcph_write_log(__('wcph - ACF options page saved, clearing Beaver Builder cache.', 'weave-cache-purge-helper'));
if (defined('FL_BUILDER_VERSION') && class_exists('FLBuilderModel')) {
FLBuilderModel::delete_asset_cache_for_all_posts();
wcph_write_log(__('wcph - Beaver Builder cache cleared.', 'weave-cache-purge-helper'));
}
wcph_purge();
}
});
}
}

add_action('init', 'wcph_init_hooks');

0 comments on commit 1d06625

Please sign in to comment.