Skip to content

Commit

Permalink
Merge pull request #482 from cloudflare/dont-purge-feed-urls-if-not-a…
Browse files Browse the repository at this point in the history
…po-or-page-rule-override

don't purge feed URLs if cache everything not enabled
  • Loading branch information
jacobbednarz authored Jun 2, 2022
2 parents 746b2ee + 8ee53b0 commit 7ced9d6
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/WordPress/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,18 @@ public function purgeCacheByRelevantURLs($postIds)
$urls = array_values(array_filter(array_unique($urls)));

$activePageRules = $this->api->getPageRules($zoneTag, "active");
$hasCacheOverride = $this->pageRuleContains($activePageRules, "cache_level", "cache_everything");

// Should we not have a 'cache_everything' page rule override, feeds
// shouldn't be attempted to be purged as they are not cachable by
// default.
if (!$hasCacheOverride) {
$this->logger->debug("cache everything behaviour found, filtering out feeds URLs");
$urls = array_filter($urls, array($this, "pathIsForFeeds"));
}

// Fetch the page rules and should we not have any hints of cache
// all behaviour or APO, filter out the non-cacheable URLs.
$hasCacheOverride = $this->pageRuleContains($activePageRules, "cache_level", "cache_everything");
if (!$hasCacheOverride && !$this->isAutomaticPlatformOptimizationEnabled()) {
$this->logger->debug("cache everything behaviour and APO not found, filtering URLs to only be those that are cacheable by default");
$urls = array_filter($urls, array($this, "pathHasCachableFileExtension"));
Expand Down Expand Up @@ -529,6 +537,21 @@ private function pathHasCachableFileExtension($value)
return false;
}

/**
* pathIsForFeeds accepts a string URL and checks if the path matches any
* known feed paths such as "/feed", "/feed/", "/feed/rdf/", "/feed/rss/",
* "/feed/atom/", "/author/foo/feed", "/comments/feed", "/shop/feed",
* "/tag/.../feed/", etc.
*
* @param mixed $value
* @return bool
*/
private function pathIsForFeeds($value)
{
$parsed_url = parse_url($value, PHP_URL_PATH);
return (bool) preg_match('/\/feed(?:\/(?:atom\/?|r(?:df|ss)\/?)?)?$/', $parsed_url);
}

/**
* urlIsHTTPS determines if a scheme used for a URL is HTTPS.
*
Expand Down

0 comments on commit 7ced9d6

Please sign in to comment.