From 2020b1cff016d2e41639b4c105a1b2da41a45562 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 26 Feb 2025 22:08:29 -0600 Subject: [PATCH] Initial commit --- README.md | 1 + cloudflare-cache-purger.php | 299 ++++++++++++++++++++++++++++++++++++ 2 files changed, 300 insertions(+) create mode 100644 README.md create mode 100644 cloudflare-cache-purger.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..5f3f5f3 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# cloudflare-cache-purger diff --git a/cloudflare-cache-purger.php b/cloudflare-cache-purger.php new file mode 100644 index 0000000..bf97ed5 --- /dev/null +++ b/cloudflare-cache-purger.php @@ -0,0 +1,299 @@ +load_settings(); + + // Register hooks to purge cache. + $this->register_hooks(); + + // Add admin menu. + add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); + add_action( 'admin_init', array( $this, 'register_settings' ) ); + } + + /** + * Load settings from WordPress options. + */ + private function load_settings() { + $this->api_token = get_option( 'cloudflare_cache_purger_api_token', '' ); + $this->zone_id = get_option( 'cloudflare_cache_purger_zone_id', '' ); + $this->debug_mode = get_option( 'cloudflare_cache_purger_debug', false ); + } + + /** + * Register hooks that will trigger cache purge. + */ + private function register_hooks() { + // Hooks specified in requirements. + add_action( 'pending_to_publish', array( $this, 'purge_cache' ) ); + add_action( 'publish_to_trash', array( $this, 'purge_cache' ) ); + add_action( 'future_to_publish', array( $this, 'purge_cache' ) ); + add_action( 'draft_to_publish', array( $this, 'purge_cache' ) ); + add_action( 'delete_attachment', array( $this, 'purge_cache' ) ); + add_action( 'autoptimize_action_cachepurged', array( $this, 'purge_cache' ) ); + add_action( 'switch_theme', array( $this, 'purge_cache' ) ); + add_action( 'customize_save_after', array( $this, 'purge_cache' ) ); + } + + /** + * Write debug information to a log file in wp-content. + * + * @param string $message The debug message. + */ + private function debug_log( $message ) { + $log_file = WP_CONTENT_DIR . '/cloudflare-cache-purger-debug.log'; + $formatted_message = '[' . date( 'Y-m-d H:i:s' ) . '] ' . $message . "\n"; + file_put_contents( $log_file, $formatted_message, FILE_APPEND ); + } + + /** + * Purge the Cloudflare cache. + */ + public function purge_cache() { + // Check if credentials are set. + if ( empty( $this->api_token ) || empty( $this->zone_id ) ) { + error_log( 'Cloudflare Cache Purger: API credentials not set.' ); + if ( $this->debug_mode ) { + $this->debug_log( 'API credentials not set.' ); + } + return; + } + + // Prepare request to Cloudflare API. + $url = "https://api.cloudflare.com/client/v4/zones/{$this->zone_id}/purge_cache"; + $headers = array( + 'Authorization: Bearer ' . $this->api_token, + 'Content-Type: application/json' + ); + + // Purge everything. + $data = json_encode( array( 'purge_everything' => true ) ); + + // Log the purge event if debug is enabled. + if ( $this->debug_mode ) { + $this->debug_log( "Initiating cache purge. URL: $url, Data: $data" ); + } + + // Initialize cURL. + $ch = curl_init(); + curl_setopt( $ch, CURLOPT_URL, $url ); + curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' ); + curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); + curl_setopt( $ch, CURLOPT_POSTFIELDS, $data ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + + // Execute request. + $result = curl_exec( $ch ); + $http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); + + // Close cURL. + curl_close( $ch ); + + // Log API response if debug is enabled. + if ( $this->debug_mode ) { + $this->debug_log( "API response: HTTP Code: $http_code, Result: $result" ); + } + + // Log result to error_log and show admin notices. + if ( $http_code >= 200 && $http_code < 300 ) { + error_log( 'Cloudflare Cache Purger: Cache successfully purged.' ); + if ( is_admin() ) { + add_action( 'admin_notices', function() { + echo '

Cloudflare cache has been purged successfully.

'; + } ); + } + } else { + error_log( 'Cloudflare Cache Purger: Failed to purge cache. Error: ' . $result ); + if ( is_admin() ) { + add_action( 'admin_notices', function() use ( $result ) { + $error_message = json_decode( $result, true ); + $message = isset( $error_message['errors'][0]['message'] ) ? $error_message['errors'][0]['message'] : 'Unknown error'; + echo '

Failed to purge Cloudflare cache: ' . esc_html( $message ) . '

'; + } ); + } + } + } + + /** + * Add admin menu. + */ + public function add_admin_menu() { + add_options_page( + 'Cloudflare Cache Purger Settings', + 'Cloudflare Cache', + 'manage_options', + 'cloudflare-cache-purger', + array( $this, 'settings_page' ) + ); + } + + /** + * Register plugin settings. + */ + public function register_settings() { + register_setting( 'cloudflare_cache_purger', 'cloudflare_cache_purger_api_token' ); + register_setting( 'cloudflare_cache_purger', 'cloudflare_cache_purger_zone_id' ); + register_setting( 'cloudflare_cache_purger', 'cloudflare_cache_purger_debug' ); + + add_settings_section( + 'cloudflare_cache_purger_section', + 'Cloudflare API Settings', + array( $this, 'settings_section_callback' ), + 'cloudflare-cache-purger' + ); + + add_settings_field( + 'cloudflare_cache_purger_api_token', + 'Cloudflare API Token', + array( $this, 'api_token_field_callback' ), + 'cloudflare-cache-purger', + 'cloudflare_cache_purger_section' + ); + + add_settings_field( + 'cloudflare_cache_purger_zone_id', + 'Cloudflare Zone ID', + array( $this, 'zone_id_field_callback' ), + 'cloudflare-cache-purger', + 'cloudflare_cache_purger_section' + ); + + add_settings_field( + 'cloudflare_cache_purger_debug', + 'Enable Debug Logging', + array( $this, 'debug_field_callback' ), + 'cloudflare-cache-purger', + 'cloudflare_cache_purger_section' + ); + } + + /** + * Settings section description. + */ + public function settings_section_callback() { + echo '

Enter your Cloudflare API credentials to enable automatic cache purging.

'; + echo '

You can find your Zone ID in the Cloudflare dashboard under "Overview" > "API" section.

'; + echo '

Create an API token with the "Zone.Cache Purge" permission in the Cloudflare dashboard under "My Profile" > "API Tokens".

'; + } + + /** + * API token field callback. + */ + public function api_token_field_callback() { + $api_token = get_option( 'cloudflare_cache_purger_api_token', '' ); + echo ''; + echo '

Your Cloudflare API token. Create one with "Zone.Cache Purge" permission.

'; + } + + /** + * Zone ID field callback. + */ + public function zone_id_field_callback() { + $zone_id = get_option( 'cloudflare_cache_purger_zone_id', '' ); + echo ''; + echo '

Your Cloudflare Zone ID for the domain.

'; + } + + /** + * Debug field callback. + */ + public function debug_field_callback() { + $debug = get_option( 'cloudflare_cache_purger_debug', false ); + ?> + /> +

Enable debug logging. Debug messages will be saved to wp-content/cloudflare-cache-purger-debug.log.

+ +
+

+
+ +
+ +
+

Manual Cache Purge

+

Click the button below to manually purge the Cloudflare cache.

+
+ + + +
+
+ +
+

Automatic Purge Events

+

This plugin automatically purges the Cloudflare cache when any of these WordPress events occur:

+
    +
  • When a post is edited
  • +
  • When a post is saved
  • +
  • When a post is deleted
  • +
  • When an attachment is deleted
  • +
  • When Autoptimize purges its cache
  • +
  • When a theme is switched
  • +
  • When customizer changes are saved
  • +
+
+
+ purge_cache(); + echo '

Manual cache purge initiated.

'; + } + } +} + +// Initialize the plugin. +$cloudflare_cache_purger = new Cloudflare_Cache_Purger();