From 3f944861722c0b4d527ad86598106fefc2d39ff9 Mon Sep 17 00:00:00 2001 From: Gareth Bissland <5170216+gbissland@users.noreply.github.com> Date: Fri, 28 Feb 2025 06:40:38 +1300 Subject: [PATCH 1/4] Update cache-purge-helper.php Testing for WP-Umbrella Integration --- cache-purge-helper.php | 196 +++++++++++++++++++++++++---------------- 1 file changed, 122 insertions(+), 74 deletions(-) diff --git a/cache-purge-helper.php b/cache-purge-helper.php index f710b41..9ccd3da 100644 --- a/cache-purge-helper.php +++ b/cache-purge-helper.php @@ -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 @@ -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'); From 41ad723a6cfe3d831efe5af2565893d7748155fc Mon Sep 17 00:00:00 2001 From: Gareth Bissland <5170216+gbissland@users.noreply.github.com> Date: Fri, 28 Feb 2025 06:47:39 +1300 Subject: [PATCH 2/4] Update README.md Updated for WP-Umbrella --- README.md | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c73cf69..c011f54 100644 --- a/README.md +++ b/README.md @@ -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`. @@ -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 @@ -45,6 +55,13 @@ 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) @@ -52,3 +69,5 @@ Enable Secure WP Debug via toggle switch then add `define( 'WC_PHP_DEBUG', tru ### Note For detailed changes, see the `CHANGELOG.md` file. + + From 5240f29698103f41d69ab8baf159b55abd009baf Mon Sep 17 00:00:00 2001 From: Gareth Bissland <5170216+gbissland@users.noreply.github.com> Date: Fri, 28 Feb 2025 06:48:49 +1300 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51a18d4..0685261 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. From aeb8ef8c63af885f0a3ee57c319d8667548903a0 Mon Sep 17 00:00:00 2001 From: Gareth Bissland <5170216+gbissland@users.noreply.github.com> Date: Fri, 28 Feb 2025 06:50:27 +1300 Subject: [PATCH 4/4] Create release.yml --- .github/workflows/release.yml | 59 +++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..ddcf4e6 --- /dev/null +++ b/.github/workflows/release.yml @@ -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 }}