Skip to content

Commit

Permalink
Multisite compat.
Browse files Browse the repository at this point in the history
See: #169
  • Loading branch information
JasWSInc committed Jun 17, 2014
1 parent b283568 commit 3eaedf3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 55 deletions.
33 changes: 18 additions & 15 deletions quick-cache/includes/share.php
Expand Up @@ -1026,23 +1026,26 @@ public function apply_filters($hook, $value)
public $htaccess_deny = "<IfModule authz_core_module>\n\tRequire all denied\n</IfModule>\n<IfModule !authz_core_module>\n\tdeny from all\n</IfModule>";
}

/**
* Polyfill for {@link \__()}.
*
* @since 140422 First documented version.
*
* @param string $string String to translate.
* @param string $text_domain Plugin text domain.
*
* @return string Possibly translated string.
*/
function __($string, $text_domain)
if(!function_exists('\\'.__NAMESPACE__.'\\__'))
{
static $exists; // Static cache.
if(($exists || function_exists('__')) && ($exists = TRUE))
return \__($string, $text_domain);
/**
* Polyfill for {@link \__()}.
*
* @since 140422 First documented version.
*
* @param string $string String to translate.
* @param string $text_domain Plugin text domain.
*
* @return string Possibly translated string.
*/
function __($string, $text_domain)
{
static $exists; // Static cache.
if(($exists || function_exists('__')) && ($exists = TRUE))
return \__($string, $text_domain);

return $string; // Not possible (yet).
return $string; // Not possible (yet).
}
}
}
}
81 changes: 41 additions & 40 deletions quick-cache/quick-cache.inc.php
Expand Up @@ -732,18 +732,18 @@ public function clear_cache($manually = FALSE)
{
$counter = 0; // Initialize.

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

// @TODO When set_time_limit() is disabled by PHP configuration, display a warning message to users upon plugin activation
@set_time_limit(1800); // In case of HUGE sites w/ a very large directory. Errors are ignored in case `set_time_limit()` is disabled.
if(!is_dir($cache_dir = $this->cache_dir()))
return $counter; // Nothing to do.

$url = 'http://'.$_SERVER['HTTP_HOST'].$this->host_base_dir_tokens();
$cache_path_no_scheme_quv_ext = $this->url_to_cache_path($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 paths; and all possible variations.
'\/[^\/]+\/'.preg_quote($cache_path_no_scheme_quv_ext, '/').
'(?:\/index)?[.\/]/';

// @TODO When set_time_limit() is disabled by PHP configuration, display a warning message to users upon plugin activation
@set_time_limit(1800); // In case of HUGE sites w/ a very large directory. Errors are ignored in case `set_time_limit()` is disabled.

/** @var $_dir_file \RecursiveDirectoryIterator For IDEs. */
foreach($this->dir_regex_iteration($cache_dir, $regex) as $_dir_file)
{
Expand Down Expand Up @@ -776,9 +776,10 @@ public function purge_cache()
{
$counter = 0; // Initialize.

$cache_dir = $this->cache_dir(); // Current cache directory.
$max_age = strtotime('-'.$this->options['cache_max_age']);
if(!is_dir($cache_dir)) return $counter; // Nothing to do.
if(!is_dir($cache_dir = $this->cache_dir()))
return $counter; // Nothing to do.

$max_age = strtotime('-'.$this->options['cache_max_age']);

// @TODO When set_time_limit() is disabled by PHP configuration, display a warning message to users upon plugin activation
@set_time_limit(1800); // In case of HUGE sites w/ a very large directory. Errors are ignored in case `set_time_limit()` is disabled.
Expand Down Expand Up @@ -912,9 +913,13 @@ public function auto_purge_post_cache($id, $force = FALSE)
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $counter; // Nothing to do.

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

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

if(!$post_status)
if(!($post_status = get_post_status($id)))
return $counter; // Nothing to do.

if($post_status === 'auto-draft')
Expand All @@ -932,16 +937,6 @@ public function auto_purge_post_cache($id, $force = FALSE)
if($post_status === 'trash' && !$force)
return $counter; // Nothing to do.

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

$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 & applicable.
$counter += $this->auto_purge_post_terms_cache($id, $force); // If enabled and applicable.

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

if(($type = get_post_type($id)) && ($type = get_post_type_object($type)) && !empty($type->labels->singular_name))
$type_singular_name = $type->labels->singular_name; // Singular name for the post type.
else $type_singular_name = __('Post', $this->text_domain); // Default value.
Expand Down Expand Up @@ -971,6 +966,11 @@ public function auto_purge_post_cache($id, $force = FALSE)
}
unset($_file); // Just a little housekeeping.

$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.
$counter += $this->auto_purge_post_terms_cache($id, $force); // If enabled and necessary.

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

Expand Down Expand Up @@ -1044,19 +1044,21 @@ public function auto_purge_xml_sitemaps_cache()
if(!$this->options['enable'])
return $counter; // Nothing to do.

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

$patterns = '(?:'.implode('|', array_map(function ($pattern)
{
$pattern = trim($pattern, '/');
$pattern = str_replace('.', '-', $pattern);
$pattern = preg_quote($pattern, '/'); // Escape.
return preg_replace('/\\\\\*/', '.*?', $pattern); // Wildcards.

}, preg_split('/['."\r\n".']+/', 'sitemap*xml', NULL, PREG_SPLIT_NO_EMPTY))).')';
$cache_path_no_scheme_quv_ext = $this->url_to_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.'/';
'\/'.$patterns.'\./';

/** @var $_file \RecursiveDirectoryIterator For IDEs. */
foreach($this->dir_regex_iteration($cache_dir, $regex) as $_file) if($_file->isFile() || $_file->isLink())
Expand All @@ -1073,7 +1075,7 @@ public function auto_purge_xml_sitemaps_cache()
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 sitemap cache file(s) for the site (auto-purging).', $this->text_domain));
__('<strong>Quick Cache:</strong> detected changes. Found XML sitemaps (auto-purging).', $this->text_domain));
$enqueued_notices++; // Notice counter.
}
unset($_file); // Just a little housekeeping.
Expand Down Expand Up @@ -1110,8 +1112,8 @@ public function auto_purge_home_page_cache()
if(!$this->options['cache_purge_home_page_enable'])
return $counter; // Nothing to do.

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

$cache_path_no_scheme_quv_ext = $this->url_to_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.
Expand Down Expand Up @@ -1170,8 +1172,8 @@ public function auto_purge_posts_page_cache()
if(!$this->options['cache_purge_posts_page_enable'])
return $counter; // Nothing to do.

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

$show_on_front = get_option('show_on_front');
$page_for_posts = get_option('page_for_posts');
Expand Down Expand Up @@ -1253,8 +1255,8 @@ public function auto_purge_author_page_cache($post_ID, \WP_Post $post_after, \WP
if(!$this->options['cache_purge_author_page_enable'])
return $counter; // Nothing to do.

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

/*
* If we're changing the post author AND
Expand All @@ -1274,20 +1276,19 @@ public function auto_purge_author_page_cache($post_ID, \WP_Post $post_after, \WP
$authors[] = (integer)$post_before->post_author;
$authors[] = (integer)$post_after->post_author;
}
elseif(($post_before->post_status === 'publish' || $post_before->post_status === 'private') ||
($post_after->post_status === 'publish' || $post_after->post_status === 'private')
else if(($post_before->post_status === 'publish' || $post_before->post_status === 'private') ||
($post_after->post_status === 'publish' || $post_after->post_status === 'private')
)
$authors[] = (integer)$post_after->post_author;
else
return $counter; // Nothing to do.

// Get author posts URL and display name
foreach($authors as $_author_id)
else return $counter; // Nothing to do in this scenario.

foreach($authors as $_author_id) // Get author posts URL and display name.
{
$authors_to_purge[$_author_id]['posts_url'] = get_author_posts_url($_author_id);
$authors_to_purge[$_author_id]['display_name'] = get_the_author_meta('display_name', $_author_id);
}
unset($_author_id);
unset($_author_id); // Housekeeping.

foreach($authors_to_purge as $_author)
{
Expand Down Expand Up @@ -1363,6 +1364,9 @@ public function auto_purge_post_terms_cache($id, $force = FALSE)
!$this->options['cache_purge_term_other_enable']
) return $counter; // Nothing to do.

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

$post_status = get_post_status($id); // Cache this.

if($post_status === 'auto-draft')
Expand All @@ -1377,9 +1381,6 @@ public function auto_purge_post_terms_cache($id, $force = FALSE)
if($post_status === 'future' && !$force)
return $counter; // Nothing to do.

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

/*
* Build an array of available taxonomies for this post (as taxonomy objects)
*/
Expand Down

0 comments on commit 3eaedf3

Please sign in to comment.