Skip to content

Commit

Permalink
MU Domain Mapping compat. See: wpsharks/comet-cache#339 and wpsharks/…
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswsinc committed Jul 30, 2015
1 parent 1a8c81c commit eaa4dd8
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 98 deletions.
8 changes: 4 additions & 4 deletions src/includes/classes/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function wipeCache($args)

/*[pro strip-from="lite"]*/
if ($this->plugin->options['cache_clear_s2clean_enable']) {
if (function_exists('s2clean')) {
if ($this->plugin->functionIsPossible('s2clean')) {
$s2clean_counter = s2clean()->md_cache_clear();
}
}
Expand Down Expand Up @@ -117,7 +117,7 @@ protected function clearCache($args)

/*[pro strip-from="lite"]*/
if ($this->plugin->options['cache_clear_s2clean_enable']) {
if (function_exists('s2clean')) {
if ($this->plugin->functionIsPossible('s2clean')) {
$s2clean_counter = s2clean()->md_cache_clear();
}
}
Expand Down Expand Up @@ -157,7 +157,7 @@ protected function ajaxWipeCache($args)
$counter = $this->plugin->wipeCache(true);

if ($this->plugin->options['cache_clear_s2clean_enable']) {
if (function_exists('s2clean')) {
if ($this->plugin->functionIsPossible('s2clean')) {
$s2clean_counter = s2clean()->md_cache_clear();
}
}
Expand Down Expand Up @@ -197,7 +197,7 @@ protected function ajaxClearCache($args)
$counter = $this->plugin->clearCache(true);

if ($this->plugin->options['cache_clear_s2clean_enable']) {
if (function_exists('s2clean')) {
if ($this->plugin->functionIsPossible('s2clean')) {
$s2clean_counter = s2clean()->md_cache_clear();
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/includes/classes/AutoCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ protected function run()
$total_urls = $total_time = 0; // Initialize.

$network_home_url = rtrim(network_home_url(), '/');
$network_home_host = parse_url($network_home_url, PHP_URL_HOST);
$network_home_path = parse_url($network_home_url, PHP_URL_PATH);
$network_home_host = $this->plugin->parseUrl($network_home_url, PHP_URL_HOST);
$network_home_path = $this->plugin->parseUrl($network_home_url, PHP_URL_PATH);

$delay = (integer) $this->plugin->options['auto_cache_delay']; // In milliseconds.
$delay = $delay > 0 ? $delay * 1000 : 0; // Convert delay to microseconds for `usleep()`.
Expand All @@ -78,7 +78,7 @@ protected function run()

foreach ($blogs as $_blog) {
$_blog_sitemap_urls = $_blog_other_urls = $_blog_urls = array();
$_blog_url = 'http://'.$_blog->domain.'/'.trim($_blog->path, '/');
$_blog_url = 'http://'.$_blog->domain.'/'.trim($_blog->path, '/'); // @TODO Add support for `PATH_CURRENT_SITE`; i.e., the base directory.

if ($this->plugin->options['auto_cache_sitemap_url']) {
$_blog_sitemap_urls = $this->getSitemapUrlsDeep($_blog_url.'/'.$this->plugin->options['auto_cache_sitemap_url']);
Expand All @@ -91,7 +91,7 @@ protected function run()
shuffle($_blog_urls); // Randomize the order.

foreach ($_blog_urls as $_url) {
$total_urls++; // Counter.
++$total_urls; // Counter.

$this->autoCacheUrl($_url);

Expand Down
44 changes: 22 additions & 22 deletions src/includes/classes/CdnFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function __construct()

/* Host-related properties. */

$this->local_host = strtolower((string) parse_url(home_url(), PHP_URL_HOST));
$this->local_host = strtolower((string) $this->plugin->parseUrl(home_url(), PHP_URL_HOST));
$this->cdn_host = strtolower($this->plugin->options['cdn_host']);
$this->cdn_hosts = strtolower($this->plugin->options['cdn_hosts']);
$this->parseCdnHosts(); // Convert CDN hosts to an array.
Expand Down Expand Up @@ -348,44 +348,44 @@ public function contentFilter($string)
*
* @since 150422 Rewrite.
*
* @param string $url_uri_query Input URL|URI|query.
* @param string|null $scheme `NULL`, `http`, `https`, `login`, `login_post`, `admin`, or `relative`.
* @param bool $esc Defaults to a FALSE value; do not deal with HTML entities.
* @param string|null $for One of `null`, `head`, `body`, `foot`.
* @param string $url_uri_qsl Input URL, URI, or query string w/ a leading `?`.
* @param string|null $scheme `NULL`, `http`, `https`, `login`, `login_post`, `admin`, or `relative`.
* @param bool $esc Defaults to a FALSE value; do not deal with HTML entities.
* @param string|null $for One of `null`, `head`, `body`, `foot`.
*
* @return string The URL after having been filtered.
*
* @note This is only public for PHP v5.3 compat.
*/
public function filterUrl($url_uri_query, $scheme = null, $esc = false, $for = null)
public function filterUrl($url_uri_qsl, $scheme = null, $esc = false, $for = null)
{
if (!($url_uri_query = trim((string) $url_uri_query))) {
if (!($url_uri_qsl = trim((string) $url_uri_qsl))) {
return; // Unparseable.
}
$orig_url_uri_query = $url_uri_query; // Needed below.
$orig_url_uri_qsl = $url_uri_qsl; // Needed below.

if ($esc) { // If escaping, unescape the input value also.
$url_uri_query = wp_specialchars_decode($url_uri_query, ENT_QUOTES);
$url_uri_qsl = wp_specialchars_decode($url_uri_qsl, ENT_QUOTES);
}
if (!($local_file = $this->localFile($url_uri_query))) {
return $orig_url_uri_query; // Not a local file.
if (!($local_file = $this->localFile($url_uri_qsl))) {
return $orig_url_uri_qsl; // Not a local file.
}
if (empty($this->cdn_hosts[$local_file->host])) {
return $orig_url_uri_query; // Exclude; no host mapping.
return $orig_url_uri_qsl; // Exclude; no host mapping.
}
if (!in_array($local_file->extension, $this->cdn_whitelisted_extensions, true)) {
return $orig_url_uri_query; // Not a whitelisted extension.
return $orig_url_uri_qsl; // Not a whitelisted extension.
}
if ($this->cdn_blacklisted_extensions && in_array($local_file->extension, $this->cdn_blacklisted_extensions, true)) {
return $orig_url_uri_query; // Exclude; it's a blacklisted extension.
return $orig_url_uri_qsl; // Exclude; it's a blacklisted extension.
}
if ($this->cdn_whitelisted_uri_patterns && !preg_match($this->cdn_whitelisted_uri_patterns, $local_file->uri)) {
return $orig_url_uri_query; // Exclude; not a whitelisted URI pattern.
return $orig_url_uri_qsl; // Exclude; not a whitelisted URI pattern.
}
if ($this->cdn_blacklisted_uri_patterns && preg_match($this->cdn_blacklisted_uri_patterns, $local_file->uri)) {
return $orig_url_uri_query; // Exclude; it's a blacklisted URI pattern.
return $orig_url_uri_qsl; // Exclude; it's a blacklisted URI pattern.
}
if (!isset($scheme) && isset($local_file->scheme)) {
if (!isset($scheme) && isset($local_file->scheme) && $local_file->scheme !== '//') {
$scheme = $local_file->scheme; // Use original scheme.
}
$cdn_host = $this->cdn_hosts[$local_file->host][0];
Expand Down Expand Up @@ -417,18 +417,18 @@ public function filterUrl($url_uri_query, $scheme = null, $esc = false, $for = n
*
* @since 150422 Rewrite.
*
* @param string $url_uri_query Input URL|URI|query.
* @param string $url_uri_qsl Input URL, URI, or query string w/ a leading `?`.
*
* @return object|null An object with: `scheme`, `host`, `uri`, `extension` properties.
* This returns NULL for any URL that is not local, or does not lead to a file.
* Local, meaning that we have a CDN host mapping for the associated host/domain name.
*/
protected function localFile($url_uri_query)
protected function localFile($url_uri_qsl)
{
if (!($url_uri_query = trim((string) $url_uri_query))) {
if (!($url_uri_qsl = trim((string) $url_uri_qsl))) {
return; // Unparseable.
}
if (!($parsed = @parse_url($url_uri_query))) {
if (!($parsed = @$this->plugin->parseUrl($url_uri_qsl))) {
return; // Unparseable.
}
if (empty($parsed['host']) && empty($this->cdn_hosts[$this->local_host])) {
Expand Down Expand Up @@ -526,7 +526,7 @@ protected function parseCdnHosts()

if (empty($this->cdn_hosts[$this->local_host])) {
if ($this->cdn_host && (!is_multisite() || is_main_site())) {
$this->cdn_hosts[strtolower((string) parse_url(network_home_url(), PHP_URL_HOST))][] = $this->cdn_host;
$this->cdn_hosts[strtolower((string) $this->plugin->parseUrl(network_home_url(), PHP_URL_HOST))][] = $this->cdn_host;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/includes/classes/Conflicts.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected static function doCheck()
if (in_array($_active_plugin_slug, $conflicting_plugin_slugs, true)) {
if (in_array($_active_plugin_slug, array('quick-cache', 'quick-cache-pro'), true)) {
add_action('admin_init', function () use ($_active_plugin_basename) {
if (function_exists('deactivate_plugins')) {
if (function_exists('deactivate_plugins')) { // Can deactivate?
deactivate_plugins($_active_plugin_basename, true);
}
}, -1000);
Expand Down
4 changes: 2 additions & 2 deletions src/includes/classes/MenuPageOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,8 @@ public function __construct()
echo ' <h3>'.__('Multiple CDN Host Names for Domain Sharding and Multisite Networks (Optional)', SLUG_TD).'</h3>'."\n";
echo ' <p>'.sprintf(__('%1$s also supports multiple CDN Host Names for any given domain. Using multiple CDN Host Names (instead of just one, as seen above) is referred to as <strong><a href="http://zencache.com/r/domain-sharding/" target="_blank">Domain Sharding</a></strong> (<a href="http://zencache.com/r/domain-sharding/" target="_blank">click here to learn more</a>). If you configure multiple CDN Host Names (i.e., if you implement Domain Sharding), %1$s will use the first one that you list for static resources loaded in the HTML <code>&lt;head&gt;</code> section, the last one for static resources loaded in the footer, and it will choose one at random for all other static resource locations. Configuring multiple CDN Host Names can improve speed! This is a way for advanced site owners to work around concurrency limits in popular browsers; i.e., making it possible for browsers to download many more resources simultaneously, resulting in a faster overall completion time. In short, this tells the browser that your website will not be overloaded by concurrent requests, because static resources are in fact being served by a content-delivery network (i.e., multiple CDN host names). If you use this functionality for Domain Sharding, we suggest that you setup one CDN Distribution (aka: Pull Zone), and then create multiple CNAME records pointing to that distribution. You can enter each of your CNAMES in the field below, as instructed.', SLUG_TD), esc_html(NAME)).'</p>'."\n";
echo ' <p class="info" style="display:block;">'.sprintf(__('<strong>On WordPress Multisite Network installations</strong>, this field also allows you to configure different CDN Host Names for each domain (or sub-domain) that you run from a single installation of WordPress. For more information about configuring Static CDN Filters on a WordPress Multisite Network, see this tutorial: <a href="http://zencache.com/r/static-cdn-filters-for-wordpress-multisite-networks/" target="_blank">Static CDN Filters for WordPress Multisite Networks</a>.', SLUG_TD), esc_html(NAME)).'</p>'."\n";
echo ' <p style="margin-bottom:0;"><textarea name="'.esc_attr(GLOBAL_NS).'[saveOptions][cdn_hosts]" rows="5" spellcheck="false" autocomplete="off" placeholder="'.esc_attr('e.g., '.$this->plugin->httpHost().' = cdn1.'.$this->plugin->httpHost().', cdn2.'.$this->plugin->httpHost().', cdn3.'.$this->plugin->httpHost()).'" wrap="off" style="white-space:nowrap;">'.esc_textarea($this->plugin->options['cdn_hosts']).'</textarea></p>'."\n";
echo ' <p style="margin-top:0;">'.sprintf(__('<strong>↑ Syntax:</strong> This is a line-delimited list of domain mappings. Each line should start with your WordPress domain name (e.g., <code>%1$s</code>), followed by an <code>=</code> sign, followed by a comma-delimited list of CDN Host Names associated with the domain in that line. If you\'re running a Multisite Network installation of WordPress, you might have multiple configuration lines. Otherwise, you should only need one line to configure multiple CDN Host Names for a standard WordPress installation.', SLUG_TD), esc_html($this->plugin->httpHost())).'</p>'."\n";
echo ' <p style="margin-bottom:0;"><textarea name="'.esc_attr(GLOBAL_NS).'[saveOptions][cdn_hosts]" rows="5" spellcheck="false" autocomplete="off" placeholder="'.esc_attr('e.g., '.$this->plugin->httpHost(true).' = cdn1.'.$this->plugin->httpHost(true).', cdn2.'.$this->plugin->httpHost(true).', cdn3.'.$this->plugin->httpHost(true)).'" wrap="off" style="white-space:nowrap;">'.esc_textarea($this->plugin->options['cdn_hosts']).'</textarea></p>'."\n";
echo ' <p style="margin-top:0;">'.sprintf(__('<strong>↑ Syntax:</strong> This is a line-delimited list of domain mappings. Each line should start with your WordPress domain name (e.g., <code>%1$s</code>), followed by an <code>=</code> sign, followed by a comma-delimited list of CDN Host Names associated with the domain in that line. If you\'re running a Multisite Network installation of WordPress, you might have multiple configuration lines. Otherwise, you should only need one line to configure multiple CDN Host Names for a standard WordPress installation.', SLUG_TD), esc_html($this->plugin->httpHost(true))).'</p>'."\n";

echo ' <hr />'."\n";

Expand Down
28 changes: 19 additions & 9 deletions src/includes/closures/Shared/CacheDirUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
$self->cacheDir = function ($rel_path = '') use ($self) {
$rel_path = (string) $rel_path;

if (!($self instanceof AdvancedCache)) {
if (!$self->isAdvancedCache()) {
$cache_dir = $self->wpContentBaseDirTo($self->cache_sub_dir);
} elseif (defined('ZENCACHE_DIR') && \ZENCACHE_DIR) {
$cache_dir = \ZENCACHE_DIR;
Expand Down Expand Up @@ -122,8 +122,8 @@
}
$cache_dir = $self->nDirSeps($cache_dir);

if ($check_max_age && $self instanceof AdvancedCache) {
throw new \Exception(__('Requires an instance of Plugin.', SLUG_TD));
if ($check_max_age && $self->isAdvancedCache()) {
throw new \Exception(__('Invalid argument; isAdvancedCache!', SLUG_TD));
}
if ($check_max_age && !($max_age = strtotime('-'.$self->options['cache_max_age']))) {
return $counter; // Invalid cache expiration time.
Expand Down Expand Up @@ -251,11 +251,13 @@
*
* @param boolean $check_max_age Check max age? i.e., use purge behavior?
*
* @param boolean $___without_domain_mapping For internal use only.
*
* @return integer Total files deleted by this routine (if any).
*
* @throws \Exception If unable to delete a file for any reason.
*/
$self->deleteFilesFromHostCacheDir = function ($regex, $check_max_age = false) use ($self) {
$self->deleteFilesFromHostCacheDir = function ($regex, $check_max_age = false, $___without_domain_mapping = false) use ($self) {
$counter = 0; // Initialize.

if (!($regex = (string) $regex)) {
Expand All @@ -268,8 +270,8 @@
$host_base_dir_tokens = $self->hostBaseDirTokens();
$cache_dir = $self->nDirSeps($cache_dir);

if ($check_max_age && $self instanceof AdvancedCache) {
throw new \Exception(__('Requires an instance of Plugin.', SLUG_TD));
if ($check_max_age && $self->isAdvancedCache()) {
throw new \Exception(__('Invalid argument; isAdvancedCache!', SLUG_TD));
}
if ($check_max_age && !($max_age = strtotime('-'.$self->options['cache_max_age']))) {
return $counter; // Invalid cache expiration time.
Expand All @@ -290,11 +292,15 @@
In fact, it may improve performance since we are traversing each sub-directory separately;
i.e., we don't need to glob both `http` and `https` traffic into a single directory scan. */
$_host_url = $_host_scheme.'://'.$host.$host_base_dir_tokens;
$_host_cache_path_flags = CACHE_PATH_NO_PATH_INDEX | CACHE_PATH_NO_QUV | CACHE_PATH_NO_EXT;
$_host_cache_path = $self->buildCachePath($_host_url, '', '', $_host_cache_path_flags);
$_host_cache_dir = $self->nDirSeps($cache_dir.'/'.$_host_cache_path); // Normalize.
$_host_cache_path_flags = $___without_domain_mapping ? CACHE_PATH_NO_DOMAIN_MAPPING : 0;
$_host_cache_path_flags |= CACHE_PATH_NO_PATH_INDEX | CACHE_PATH_NO_QUV | CACHE_PATH_NO_EXT;

$_host_cache_path = $self->buildCachePath($_host_url, '', '', $_host_cache_path_flags);
$_host_cache_dir = $self->nDirSeps($cache_dir.'/'.$_host_cache_path); // Normalize.

if (!$_host_cache_dir || !is_dir($_host_cache_dir)) {
// @TODO: a multisite install may have a cache sub-directory.
// e.g., `http/example-com/child1` instead of `http/example-com`
continue; // Nothing to do.
}
$_host_cache_dir_tmp = $self->addTmpSuffix($_host_cache_dir); // Temporary directory.
Expand Down Expand Up @@ -388,6 +394,10 @@

$self->cacheUnlock($cache_lock);

// This runs one additional deletion scan for the unmapped variation.
if (!$___without_domain_mapping && is_multisite() && $self->canConsiderDomainMapping()) {
$counter += $self->deleteFilesFromHostCacheDir($regex, $check_max_age, true);
}
return $counter;
};

Expand Down
29 changes: 19 additions & 10 deletions src/includes/closures/Shared/CachePathConsts.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@
*/
const CACHE_PATH_DEFAULT = 0;

/**
* Allow a domain-mapped cache path.
*
* @since 15xxxx Improving multisite compat.
*
* @type int Part of a bitmask.
*/
const CACHE_PATH_NO_DOMAIN_MAPPING = 1;

/**
* Exclude scheme from cache path.
*
* @since 150422 Rewrite.
*
* @type int Part of a bitmask.
*/
const CACHE_PATH_NO_SCHEME = 1;
const CACHE_PATH_NO_SCHEME = 2;

/**
* Exclude host (i.e. domain name) from cache path.
Expand All @@ -29,7 +38,7 @@
*
* @type int Part of a bitmask.
*/
const CACHE_PATH_NO_HOST = 2;
const CACHE_PATH_NO_HOST = 4;

/**
* Exclude path from cache path.
Expand All @@ -38,7 +47,7 @@
*
* @type int Part of a bitmask.
*/
const CACHE_PATH_NO_PATH = 4;
const CACHE_PATH_NO_PATH = 8;

/**
* Exclude path index (i.e. no default `index`) from cache path.
Expand All @@ -47,7 +56,7 @@
*
* @type int Part of a bitmask.
*/
const CACHE_PATH_NO_PATH_INDEX = 8;
const CACHE_PATH_NO_PATH_INDEX = 16;

/**
* Exclude query, user & version salt from cache path.
Expand All @@ -56,7 +65,7 @@
*
* @type int Part of a bitmask.
*/
const CACHE_PATH_NO_QUV = 16;
const CACHE_PATH_NO_QUV = 32;

/**
* Exclude query string from cache path.
Expand All @@ -65,7 +74,7 @@
*
* @type int Part of a bitmask.
*/
const CACHE_PATH_NO_QUERY = 32;
const CACHE_PATH_NO_QUERY = 64;

/**
* Exclude user token from cache path.
Expand All @@ -74,7 +83,7 @@
*
* @type int Part of a bitmask.
*/
const CACHE_PATH_NO_USER = 64;
const CACHE_PATH_NO_USER = 128;

/**
* Exclude version salt from cache path.
Expand All @@ -83,7 +92,7 @@
*
* @type int Part of a bitmask.
*/
const CACHE_PATH_NO_VSALT = 128;
const CACHE_PATH_NO_VSALT = 256;

/**
* Exclude extension from cache path.
Expand All @@ -92,7 +101,7 @@
*
* @type int Part of a bitmask.
*/
const CACHE_PATH_NO_EXT = 256;
const CACHE_PATH_NO_EXT = 512;

/**
* Allow wildcards in the cache path.
Expand All @@ -101,7 +110,7 @@
*
* @type int Part of a bitmask.
*/
const CACHE_PATH_ALLOW_WILDCARDS = 512;
const CACHE_PATH_ALLOW_WILDCARDS = 1024;

/**
* Default cache path regex suffix frag.
Expand Down
Loading

0 comments on commit eaa4dd8

Please sign in to comment.