From 11ae83e622a5da341a946945e68a621af24634ed Mon Sep 17 00:00:00 2001 From: midwestE Date: Tue, 7 Feb 2023 15:32:32 -0600 Subject: [PATCH 1/3] Clear aggregated posts on cron --- cloudflare.loader.php | 1 + src/WordPress/Hooks.php | 64 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/cloudflare.loader.php b/cloudflare.loader.php index c5305212..c8bf2668 100644 --- a/cloudflare.loader.php +++ b/cloudflare.loader.php @@ -43,6 +43,7 @@ } add_action('init', array($cloudflareHooks, 'initAutomaticPlatformOptimization')); +add_action('init', array($cloudflareHooks, 'initScheduleQueuePurge')); if (is_admin()) { //Register proxy AJAX endpoint diff --git a/src/WordPress/Hooks.php b/src/WordPress/Hooks.php index 065f29b1..3981c5ef 100644 --- a/src/WordPress/Hooks.php +++ b/src/WordPress/Hooks.php @@ -137,7 +137,71 @@ public function purgeCacheEverything() } } + public function initScheduleQueuePurge() + { + // Add a new interval for cron purge + add_filter('cron_schedules', function ($schedules) { + $default = array( + 'interval' => 60, + 'display' => __('Every Minute', 'cloudflare') + ); + $schedules['cloudflare_purge_cron_interval'] = apply_filters('cloudflare_purge_schedule', $default); + return $schedules; + }); + + // Schedule an action if it's not already scheduled + if (!wp_next_scheduled('cloudflare_cron_purge_queue')) { + // wp_schedule_event(time(), 'every_minute', 'cloudflare_cron_purge_queue'); + wp_schedule_event(time(), 'cloudflare_purge_cron_interval', 'cloudflare_cron_purge_queue'); + } + add_action('cloudflare_cron_purge_queue', array($this, 'cronPurgeQueue')); + } + + public function queuePostIdPurge($postIds) + { + $queued = get_option('cloudflare_related_urls', []); + foreach ($postIds as $postId) { + if (isset($queued[$postId])) { + continue; + } + $queued[$postId] = time(); + } + update_option('cloudflare_related_urls', $queued, false); + return true; + } + + public function cronPurgeQueue() + { + $queued = get_option('cloudflare_related_urls', []); + if (empty($queued)) { + return; + } + + // // need to rate limit purges 1200 requests every 5 minutes + // $now = time(); + // $start = get_option('cloudflare_related_urls_purge_window', $now); + + + + // $next_five_min = strtotime('+ 5 minutes', $now); + // //d(); + // update_option('cloudflare_related_urls_purge_window', $now); + // update_option('cloudflare_related_urls_next_purge_window', $next_five_min); + + + // // todo, purge oldest time() pages first + + // empty queue so next cron run doesn't get same urls + update_option('cloudflare_related_urls', [], false); + $this->purgeCacheByPostIds(array_keys($queued)); + } + public function purgeCacheByRelevantURLs($postIds) + { + return $this->queuePostIdPurge((array) $postIds); + } + + public function purgeCacheByPostIds($postIds) { if ($this->isPluginSpecificCacheEnabled() || $this->isAutomaticPlatformOptimizationEnabled()) { $wpDomainList = $this->integrationAPI->getDomainList(); From 6207cf95b13032485e67a61057991a84a88a29e1 Mon Sep 17 00:00:00 2001 From: midwestE Date: Tue, 7 Feb 2023 16:08:47 -0600 Subject: [PATCH 2/3] cleanup cron method names add filter for purge on cron --- cloudflare.loader.php | 4 +++- src/WordPress/Hooks.php | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/cloudflare.loader.php b/cloudflare.loader.php index c8bf2668..c026e6d6 100644 --- a/cloudflare.loader.php +++ b/cloudflare.loader.php @@ -43,7 +43,9 @@ } add_action('init', array($cloudflareHooks, 'initAutomaticPlatformOptimization')); -add_action('init', array($cloudflareHooks, 'initScheduleQueuePurge')); +if (apply_filters('cloudflare_purge_on_cron', false)) { + add_action('init', array($cloudflareHooks, 'initCronScheduleQueuePurge')); +} if (is_admin()) { //Register proxy AJAX endpoint diff --git a/src/WordPress/Hooks.php b/src/WordPress/Hooks.php index 3981c5ef..c6154db4 100644 --- a/src/WordPress/Hooks.php +++ b/src/WordPress/Hooks.php @@ -137,7 +137,7 @@ public function purgeCacheEverything() } } - public function initScheduleQueuePurge() + public function initCronScheduleQueuePurge() { // Add a new interval for cron purge add_filter('cron_schedules', function ($schedules) { @@ -157,7 +157,7 @@ public function initScheduleQueuePurge() add_action('cloudflare_cron_purge_queue', array($this, 'cronPurgeQueue')); } - public function queuePostIdPurge($postIds) + public function cronQueuePostIdPurge($postIds) { $queued = get_option('cloudflare_related_urls', []); foreach ($postIds as $postId) { @@ -198,7 +198,11 @@ public function cronPurgeQueue() public function purgeCacheByRelevantURLs($postIds) { - return $this->queuePostIdPurge((array) $postIds); + if (apply_filters('cloudflare_purge_on_cron', false)) { + return $this->cronQueuePostIdPurge((array) $postIds); + } else { + return $this->purgeCacheByPostIds($postIds); + } } public function purgeCacheByPostIds($postIds) From 554a47d26ca275c320f0bd58fd8a94cc8313cf75 Mon Sep 17 00:00:00 2001 From: midwestE Date: Wed, 8 Feb 2023 16:22:55 -0600 Subject: [PATCH 3/3] move hook check inside cron schedule method, rename schedules, remove comments --- cloudflare.loader.php | 4 +--- src/WordPress/Hooks.php | 24 ++++++------------------ 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/cloudflare.loader.php b/cloudflare.loader.php index c026e6d6..9aca2657 100644 --- a/cloudflare.loader.php +++ b/cloudflare.loader.php @@ -43,9 +43,7 @@ } add_action('init', array($cloudflareHooks, 'initAutomaticPlatformOptimization')); -if (apply_filters('cloudflare_purge_on_cron', false)) { - add_action('init', array($cloudflareHooks, 'initCronScheduleQueuePurge')); -} +add_action('init', array($cloudflareHooks, 'initCronScheduleQueuePurge')); if (is_admin()) { //Register proxy AJAX endpoint diff --git a/src/WordPress/Hooks.php b/src/WordPress/Hooks.php index c6154db4..a67c751f 100644 --- a/src/WordPress/Hooks.php +++ b/src/WordPress/Hooks.php @@ -139,20 +139,23 @@ public function purgeCacheEverything() public function initCronScheduleQueuePurge() { + if (!apply_filters('cloudflare_purge_on_cron', false)) { + return; + } + // Add a new interval for cron purge add_filter('cron_schedules', function ($schedules) { $default = array( 'interval' => 60, 'display' => __('Every Minute', 'cloudflare') ); - $schedules['cloudflare_purge_cron_interval'] = apply_filters('cloudflare_purge_schedule', $default); + $schedules['cloudflare_cron_purge_interval'] = apply_filters('cloudflare_cron_purge_interval', $default); return $schedules; }); // Schedule an action if it's not already scheduled if (!wp_next_scheduled('cloudflare_cron_purge_queue')) { - // wp_schedule_event(time(), 'every_minute', 'cloudflare_cron_purge_queue'); - wp_schedule_event(time(), 'cloudflare_purge_cron_interval', 'cloudflare_cron_purge_queue'); + wp_schedule_event(time(), 'cloudflare_cron_purge_interval', 'cloudflare_cron_purge_queue'); } add_action('cloudflare_cron_purge_queue', array($this, 'cronPurgeQueue')); } @@ -176,21 +179,6 @@ public function cronPurgeQueue() if (empty($queued)) { return; } - - // // need to rate limit purges 1200 requests every 5 minutes - // $now = time(); - // $start = get_option('cloudflare_related_urls_purge_window', $now); - - - - // $next_five_min = strtotime('+ 5 minutes', $now); - // //d(); - // update_option('cloudflare_related_urls_purge_window', $now); - // update_option('cloudflare_related_urls_next_purge_window', $next_five_min); - - - // // todo, purge oldest time() pages first - // empty queue so next cron run doesn't get same urls update_option('cloudflare_related_urls', [], false); $this->purgeCacheByPostIds(array_keys($queued));