Skip to content

Commit

Permalink
Auto purge Home Page cache and Posts Page cache when necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
raamdev committed Dec 24, 2013
1 parent d2f9697 commit 359ade0
Showing 1 changed file with 107 additions and 9 deletions.
116 changes: 107 additions & 9 deletions quick-cache/quick-cache.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,22 @@ public function setup()
load_plugin_textdomain($this->text_domain);

$this->default_options = array( // Default options.
'version' => $this->version,
'version' => $this->version,

'crons_setup' => '0', // `0` or timestamp.
'crons_setup' => '0', // `0` or timestamp.

'enable' => '0', // `0|1`.
'debugging_enable' => '1', // `0|1`.
'allow_browser_cache' => '0', // `0|1`.
'enable' => '0', // `0|1`.
'debugging_enable' => '1', // `0|1`.
'cache_purge_home_page_enable' => '1', // `0|1`.
'cache_purge_posts_page_enable' => '1', // `0|1`.
'allow_browser_cache' => '0', // `0|1`.

'cache_dir' => '/wp-content/cache', // Relative to `ABSPATH`.
'cache_max_age' => '7 days', // `strtotime()` compatible.
'cache_dir' => '/wp-content/cache', // Relative to `ABSPATH`.
'cache_max_age' => '7 days', // `strtotime()` compatible.

'get_requests' => '0', // `0|1`.
'get_requests' => '0', // `0|1`.

'uninstall_on_deactivation' => '0' // `0|1`.
'uninstall_on_deactivation' => '0' // `0|1`.
); // Default options are merged with those defined by the site owner.
$options = (is_array($options = get_option(__NAMESPACE__.'_options'))) ? $options : array();
if(is_multisite() && is_array($site_options = get_site_option(__NAMESPACE__.'_options')))
Expand Down Expand Up @@ -473,10 +475,16 @@ public function auto_purge_post_cache($id)
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $counter; // Nothing to do.

if ( get_post_status ( $id ) == 'auto-draft' )
return $counter; // Nothing to do.

$cache_dir = ABSPATH.$this->options['cache_dir'];

if(!is_dir($cache_dir)) return $counter; // Nothing to do.

$counter += $this->auto_purge_home_page_cache(); // If enabled and necessary.
$counter += $this->auto_purge_posts_page_cache(); // If enabled & applicable.

if(!($permalink = get_permalink($id))) return $counter; // Nothing we can do.

if(!($parts = parse_url($permalink)) || empty($parts['path']))
Expand Down Expand Up @@ -508,6 +516,96 @@ public function auto_purge_post_cache($id)
return apply_filters(__METHOD__, $counter, get_defined_vars());
}

public function auto_purge_home_page_cache()
{
$counter = 0; // Initialize.

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

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

$cache_dir = ABSPATH.$this->options['cache_dir'];

if(!is_dir($cache_dir)) return $counter; // Nothing to do.

if(!($parts = parse_url(home_url('/'))) || empty($parts['path']))
return $counter; // Nothing we can do.

$http_host_nps = preg_replace('/\:[0-9]+$/', '', $_SERVER['HTTP_HOST']);
$md5_2 = md5($http_host_nps.$parts['path'].((!empty($parts['query'])) ? '?'.$parts['query'] : ''));

foreach((array)glob($cache_dir.'/qc-c-*-'.$md5_2.'-*', GLOB_NOSORT) as $_file) if($_file && is_file($_file))
{
if(!unlink($_file)) // If file deletion fails; stop here w/ exception.
throw new \exception(sprintf(__('Unable to auto-purge: `%1$s`.', $this->text_domain), $_file));
$counter++; // Increment counter for each file purge.

if(!empty($_notices) || !is_admin()) // Change notifications cannot be turned off in the lite version.
continue; // Stop here; we already issued a notice, or this notice is N/A.

$_notices = (is_array($_notices = get_option(__NAMESPACE__.'_notices'))) ? $_notices : array();
$_notices[] = '<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 cache file(s) for the designated "Home Page" (auto-purging).', $this->text_domain);
update_option(__NAMESPACE__.'_notices', $_notices);
}
unset($_file, $_notices); // Just a little housekeeping.

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

public function auto_purge_posts_page_cache()
{
$counter = 0; // Initialize.

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

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

$cache_dir = ABSPATH.$this->options['cache_dir'];

if(!is_dir($cache_dir)) return $counter; // Nothing to do.

$show_on_front = get_option('show_on_front');
$page_for_posts = get_option('page_for_posts');

if(!in_array($show_on_front, array('posts', 'page'), TRUE))
return $counter; // Nothing we can do in this case.

if($show_on_front === 'page' && !$page_for_posts)
return $counter; // Nothing we can do.

if($show_on_front === 'posts') $posts_page = home_url('/');
else if($show_on_front === 'page') $posts_page = get_permalink($page_for_posts);

if(empty($posts_page) || !($parts = parse_url($posts_page)) || empty($parts['path']))
return $counter; // Nothing we can do.

$http_host_nps = preg_replace('/\:[0-9]+$/', '', $_SERVER['HTTP_HOST']);
$md5_2 = md5($http_host_nps.$parts['path'].((!empty($parts['query'])) ? '?'.$parts['query'] : ''));

foreach((array)glob($cache_dir.'/qc-c-*-'.$md5_2.'-*', GLOB_NOSORT) as $_file) if($_file && is_file($_file))
{
if(!unlink($_file)) // If file deletion fails; stop here w/ exception.
throw new \exception(sprintf(__('Unable to auto-purge: `%1$s`.', $this->text_domain), $_file));
$counter++; // Increment counter for each file purge.

if(!empty($_notices) || !is_admin()) // Change notifications cannot be turned off in the lite version.
continue; // Stop here; we already issued a notice, or this notice is N/A.

$_notices = (is_array($_notices = get_option(__NAMESPACE__.'_notices'))) ? $_notices : array();
$_notices[] = '<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 cache file(s) for the designated "Posts Page" (auto-purging).', $this->text_domain);
update_option(__NAMESPACE__.'_notices', $_notices);
}
unset($_file, $_notices); // Just a little housekeeping.

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

public function auto_purge_comment_post_cache($id)
{
$counter = 0; // Initialize.
Expand Down

3 comments on commit 359ade0

@raamdev
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JasWSInc I'm adding these two features (auto purge Home Page cache and auto purge Posts Page cache) to Quick Cache Lite from Quick Cache Pro, minus the option to disable them. When you get a moment, could you please review this and let me know if everything looks good? I tested this and it works as expected.

@raamdev
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See also #40.

@jaswrks
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks awesome to me. Nice!

Please sign in to comment.