Skip to content

Commit

Permalink
Auto-purge feeds (just getting started); see wpsharks/comet-cache#182
Browse files Browse the repository at this point in the history
  • Loading branch information
JasWSInc committed Aug 6, 2014
1 parent c2f2da7 commit 9ce3be3
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions quick-cache-pro/quick-cache-pro.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,7 @@ public function auto_purge_post_cache($id, $force = FALSE)
}
unset($_file); // Just a little housekeeping.

$counter += $this->auto_purge_xml_feeds_cache(); // If enabled and necessary.
$counter += $this->auto_purge_xml_sitemaps_cache(); // If enabled and necessary.
$counter += $this->auto_purge_home_page_cache(); // If enabled and necessary.
$counter += $this->auto_purge_posts_page_cache(); // If enabled and necessary.
Expand Down Expand Up @@ -1507,6 +1508,78 @@ public function auto_purge_post_cache_transition($new_status, $old_status, \WP_P
return apply_filters(__METHOD__, $counter, get_defined_vars());
}

/**
* Automatically purges cache files related to XML feeds.
*
* @since 14xxxx Working to improve compatibility with feeds.
*
* @return integer Total files purged by this routine (if any).
*
* @throws \exception If a purge failure occurs.
*
* @note Unlike many of the other `auto_` methods, this one is NOT currently
* attached to any hooks. However, it is called upon by {@link auto_purge_post_cache()}.
*
* @see auto_purge_post_cache()
*/
public function auto_purge_xml_feeds_cache()
{
$counter = 0; // Initialize.
$enqueued_notices = 0; // Initialize.

if(isset($this->cache[__FUNCTION__]))
return $counter; // Already did this.
$this->cache[__FUNCTION__] = -1;

if(!$this->options['enable'])
return $counter; // Nothing to do.

if(!$this->options['cache_purge_xml_sitemaps_enable'])
return $counter; // Nothing to do.

if(!$this->options['cache_purge_xml_sitemap_patterns'])
return $counter; // Nothing to do.

if(!is_dir($cache_dir = $this->cache_dir()))
return $counter; // Nothing to do.

$_this = $this; // Needed in the closure below.
$patterns = '(?:'.implode('|', array_map(function ($pattern) use ($_this)
{
$pattern = $_this->build_cache_path(home_url('/'.trim($pattern, '/')), '', '', // Convert to a cache path w/ possible wildcards.
$_this::CACHE_PATH_ALLOW_WILDCARDS | $_this::CACHE_PATH_NO_SCHEME | $_this::CACHE_PATH_NO_HOST
| $_this::CACHE_PATH_NO_PATH_INDEX | $_this::CACHE_PATH_NO_QUV | $_this::CACHE_PATH_NO_EXT);
return preg_replace('/\\\\\*/', '.*?', preg_quote($pattern, '/')); // Wildcards.

}, preg_split('/['."\r\n".']+/', $this->options['cache_purge_xml_sitemap_patterns'], NULL, PREG_SPLIT_NO_EMPTY))).')';
$cache_path_no_scheme_quv_ext = $this->build_cache_path(home_url('/'), '', '', $this::CACHE_PATH_NO_SCHEME | $this::CACHE_PATH_NO_PATH_INDEX | $this::CACHE_PATH_NO_QUV | $this::CACHE_PATH_NO_EXT);
$regex = '/^'.preg_quote($cache_dir, '/'). // Consider all schemes; all path paginations; and all possible variations.
'\/[^\/]+\/'.preg_quote($cache_path_no_scheme_quv_ext, '/').
'\/'.$patterns.'\./';

/** @var $_file \RecursiveDirectoryIterator For IDEs. */
foreach($this->dir_regex_iteration($cache_dir, $regex) as $_file) if($_file->isFile() || $_file->isLink())
{
if(strpos($_file->getSubpathname(), '/') === FALSE) continue;
// Don't delete files in the immediate directory; e.g. `qc-advanced-cache` or `.htaccess`, etc.
// Actual `http|https/...` cache files are nested. Files in the immediate directory are for other purposes.

if(!unlink($_file->getPathname())) // Throw exception if unable to delete.
throw new \exception(sprintf(__('Unable to auto-purge XML sitemap file: `%1$s`.', $this->text_domain), $_file->getPathname()));
$counter++; // Increment counter for each file purge.

if($enqueued_notices || !$this->options['change_notifications_enable'] || !is_admin())
continue; // Stop here; we already issued a notice, or this notice is N/A.

$this->enqueue_notice('<img src="'.esc_attr($this->url('/client-s/images/clear.png')).'" style="float:left; margin:0 10px 0 0; border:0;" />'.
__('<strong>Quick Cache:</strong> detected changes. Found XML sitemaps (auto-purging).', $this->text_domain));
$enqueued_notices++; // Notice counter.
}
unset($_file); // Just a little housekeeping.

return apply_filters(__METHOD__, $counter, get_defined_vars());
}

/**
* Automatically purges cache files related to XML sitemaps.
*
Expand Down

0 comments on commit 9ce3be3

Please sign in to comment.