Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature purge on cron #504

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cloudflare.loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
}

add_action('init', array($cloudflareHooks, 'initAutomaticPlatformOptimization'));
add_action('init', array($cloudflareHooks, 'initCronScheduleQueuePurge'));

if (is_admin()) {
//Register proxy AJAX endpoint
Expand Down
56 changes: 56 additions & 0 deletions src/WordPress/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,63 @@ 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_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(), 'cloudflare_cron_purge_interval', 'cloudflare_cron_purge_queue');
}
add_action('cloudflare_cron_purge_queue', array($this, 'cronPurgeQueue'));
}

public function cronQueuePostIdPurge($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;
}
// 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)
{
if (apply_filters('cloudflare_purge_on_cron', false)) {
return $this->cronQueuePostIdPurge((array) $postIds);
} else {
return $this->purgeCacheByPostIds($postIds);
}
}

public function purgeCacheByPostIds($postIds)
{
if ($this->isPluginSpecificCacheEnabled() || $this->isAutomaticPlatformOptimizationEnabled()) {
$wpDomainList = $this->integrationAPI->getDomainList();
Expand Down