/** * @param W3_Config $config * @param W3_Config $config_master * @param bool $do_merge if to merge with network main site * @return string */ public static function get_wordpress_appname($config, $config_master, $do_merge = true) { if (w3_is_network()) { if ($config_master->get_boolean('newrelic.use_network_wide_id')) { $appname = $config_master->get_string('newrelic.appname'); } else { $merge = $config->get_boolean('newrelic.merge_with_network'); $merge_name = ''; if ($do_merge && $merge && w3_get_blog_id() != 0) { $merge_name = ';' . $config_master->get_string('newrelic.appname'); } if (w3_get_blog_id() != 0 && !$config->get_boolean('common.force_master')) { $appname = $config->get_string('newrelic.appname', ''); if (empty($appname)) { $prefix = $config->get_string('newrelic.appname_prefix'); $appname = $prefix . trim(w3_get_home_domain() . w3_get_site_path(), '/'); } } else { if (w3_get_blog_id() != 0) { $prefix = $config->get_string('newrelic.appname_prefix'); $appname = $prefix . trim(w3_get_home_domain() . w3_get_site_path(), '/'); } else { $appname = $config->get_string('newrelic.appname'); } } $appname = $appname . $merge_name; } } else { $appname = $config->get_string('newrelic.appname'); } return $appname; }
/** * Return canonical rules * * @param bool $cdnftp * @return string */ public function canonical_without_location($cdnftp = false) { $rules = ''; switch (true) { case w3_is_apache(): case w3_is_litespeed(): $host = $cdnftp ? w3_get_home_domain() : '%{HTTP_HOST}'; $rules .= " <IfModule mod_rewrite.c>\n"; $rules .= " RewriteEngine On\n"; $rules .= " RewriteCond %{HTTPS} !=on\n"; $rules .= " RewriteRule .* - [E=CANONICAL:http://{$host}%{REQUEST_URI},NE]\n"; $rules .= " RewriteCond %{HTTPS} =on\n"; $rules .= " RewriteRule .* - [E=CANONICAL:https://{$host}%{REQUEST_URI},NE]\n"; $rules .= " </IfModule>\n"; $rules .= " <IfModule mod_headers.c>\n"; $rules .= ' Header set Link "<%{CANONICAL}e>; rel=\\"canonical\\""' . "\n"; $rules .= " </IfModule>\n"; break; case w3_is_nginx(): $home = $cdnftp ? w3_get_home_domain() : '$host'; $rules .= ' add_header Link "<$scheme://' . $home . '$uri>; rel=\\"canonical\\"";' . "\n"; break; } return $rules; }
/** * Generates rules for WP dir * * @return string */ function generate_rules_core_nginx() { $is_network = w3_is_network(); $is_vhost = w3_is_subdomain_install(); $base_path = w3_get_base_path(); $cache_dir = w3_path(W3TC_CACHE_FILE_PGCACHE_DIR); $permalink_structure = get_option('permalink_structure'); /** * Auto reject cookies */ $reject_cookies = array('comment_author', 'wp-postpass'); /** * Auto reject URIs */ $reject_uris = array('\\/wp-admin\\/', '\\/xmlrpc.php', '\\/wp-(app|cron|login|register|mail)\\.php'); /** * Reject cache for logged in users */ if ($this->_config->get_boolean('pgcache.reject.logged')) { $reject_cookies = array_merge($reject_cookies, array('wordpress_[a-f0-9]+', 'wordpress_logged_in')); } /** * Reject cache for home page */ if (!$this->_config->get_boolean('pgcache.cache.home')) { $reject_uris[] = '^(\\/|\\/index.php)$'; } /** * Reject cache for feeds */ if (!$this->_config->get_boolean('pgcache.cache.feed')) { $reject_uris[] = '\\/feed\\/'; } /** * Custom config */ $reject_cookies = array_merge($reject_cookies, $this->_config->get_array('pgcache.reject.cookie')); $reject_uris = array_merge($reject_uris, $this->_config->get_array('pgcache.reject.uri')); $reject_uris = array_map('w3_parse_path', $reject_uris); $reject_user_agents = array_merge(array(W3TC_POWERED_BY), $this->_config->get_array('pgcache.reject.ua')); $accept_uris = $this->_config->get_array('pgcache.accept.uri'); $accept_files = $this->_config->get_array('pgcache.accept.files'); /** * Generate rules */ $rules = ''; $rules .= W3TC_MARKER_BEGIN_PGCACHE_CORE . "\n"; $rules .= "rewrite ^(.*\\/)?w3tc_rewrite_test\$ \$1?w3tc_rewrite_test=1 last;\n"; /** * Check for mobile redirect */ if ($this->_config->get_boolean('mobile.enabled')) { $mobile_groups = $this->_config->get_array('mobile.rgroups'); foreach ($mobile_groups as $mobile_group => $mobile_config) { $mobile_enabled = isset($mobile_config['enabled']) ? (bool) $mobile_config['enabled'] : false; $mobile_agents = isset($mobile_config['agents']) ? (array) $mobile_config['agents'] : ''; $mobile_redirect = isset($mobile_config['redirect']) ? $mobile_config['redirect'] : ''; if ($mobile_enabled && count($mobile_agents) && $mobile_redirect) { $rules .= "if (\$http_user_agent ~* \"(" . implode('|', $mobile_agents) . ")\") {\n"; $rules .= " rewrite .* " . $mobile_redirect . " last;\n"; $rules .= "}\n"; } } } /** * Check for referrer redirect */ if ($this->_config->get_boolean('referrer.enabled')) { $referrer_groups = $this->_config->get_array('referrer.rgroups'); foreach ($referrer_groups as $referrer_group => $referrer_config) { $referrer_enabled = isset($referrer_config['enabled']) ? (bool) $referrer_config['enabled'] : false; $referrer_referrers = isset($referrer_config['referrers']) ? (array) $referrer_config['referrers'] : ''; $referrer_redirect = isset($referrer_config['redirect']) ? $referrer_config['redirect'] : ''; if ($referrer_enabled && count($referrer_referrers) && $referrer_redirect) { $rules .= "if (\$http_cookie ~* \"w3tc_referrer=.*(" . implode('|', $referrer_referrers) . ")\") {\n"; $rules .= " rewrite .* " . $referrer_redirect . " last;\n"; $rules .= "}\n"; } } } /** * Don't accept POSTs */ $rules .= "set \$w3tc_rewrite 1;\n"; $rules .= "if (\$request_method = POST) {\n"; $rules .= " set \$w3tc_rewrite 0;\n"; $rules .= "}\n"; /** * Query string should be empty */ $rules .= "if (\$query_string != \"\") {\n"; $rules .= " set \$w3tc_rewrite 0;\n"; $rules .= "}\n"; /** * Check hostname */ if ($this->_config->get_boolean('pgcache.check.domain')) { $rules .= "if (\$http_host != \"" . w3_get_home_domain() . "\") {\n"; $rules .= " set \$w3tc_rewrite 0;\n"; $rules .= "}\n"; } /** * Check permalink structure trailing slash */ if (substr($permalink_structure, -1) == '/') { if (!count($accept_uris)) { $rules .= "if (\$request_uri !~ \\/\$) {\n"; $rules .= " set \$w3tc_rewrite 0;\n"; $rules .= "}\n"; } else { $rules .= "set \$w3tc_rewrite2 1;\n"; $rules .= "if (\$request_uri !~ \\/\$) {\n"; $rules .= " set \$w3tc_rewrite2 0;\n"; $rules .= "}\n"; $rules .= "if (\$request_uri ~* \"(" . implode('|', $accept_uris) . ")\") {\n"; $rules .= " set \$w3tc_rewrite2 1;\n"; $rules .= "}\n"; $rules .= "if (\$w3tc_rewrite2 != 1) {\n"; $rules .= " set \$w3tc_rewrite 0;\n"; $rules .= "}\n"; } } /** * Check for rejected URIs */ if (!count($accept_files)) { $rules .= "if (\$request_uri ~* \"(" . implode('|', $reject_uris) . "\") {\n"; $rules .= " set \$w3tc_rewrite 0;\n"; $rules .= "}\n"; } else { $rules .= "set \$w3tc_rewrite3 1;\n"; $rules .= "if (\$request_uri ~* \"(" . implode('|', $reject_uris) . ")\") {\n"; $rules .= " set \$w3tc_rewrite3 0;\n"; $rules .= "}\n"; $rules .= "if (\$request_uri ~* \"(" . implode('|', array_map('w3_preg_quote', $accept_files)) . ")\") {\n"; $rules .= " set \$w3tc_rewrite3 1;\n"; $rules .= "}\n"; $rules .= "if (\$w3tc_rewrite3 != 1) {\n"; $rules .= " set \$w3tc_rewrite 0;\n"; $rules .= "}\n"; } /** * Check for rejected cookies */ $rules .= "if (\$http_cookie ~* \"(" . implode('|', array_map('w3_preg_quote', $reject_cookies)) . ")\") {\n"; $rules .= " set \$w3tc_rewrite 0;\n"; $rules .= "}\n"; /** * Check for rejected user agents */ if (count($reject_user_agents)) { $rules .= "if (\$http_user_agent ~* \"(" . implode('|', array_map('w3_preg_quote', $reject_user_agents)) . ")\") {\n"; $rules .= " set \$w3tc_rewrite 0;\n"; $rules .= "}\n"; } /** * Network mode rules */ if ($is_network) { /** * Detect domain */ $rules .= "set \$w3tc_domain \"\";\n"; $rules .= "if (\$http_host ~ ^(www\\.)?([a-z0-9\\-\\.]+\\.[a-z]+)\\.?(:[0-9]+)?\$) {\n"; $rules .= " set \$w3tc_domain \$2;\n"; $rules .= "}\n"; $replacement = '/w3tc-$w3tc_domain/'; /** * If VHOST is off, detect blogname from URI */ if (!$is_vhost) { $blognames = w3_get_blognames(); if (count($blognames)) { $rules .= "set \$w3tc_blogname \"\";\n"; $rules .= "if (\$request_uri ~ ^" . $base_path . "(" . implode('|', array_map('w3_preg_quote', $blognames)) . ")/) {\n"; $rules .= " set \$w3tc_blogname \$1.;\n"; $rules .= "}\n"; $replacement = '/w3tc-$w3tc_blogname$w3tc_domain/'; } } $cache_dir = preg_replace('~/w3tc.*?/~', $replacement, $cache_dir, 1); } /** * Check mobile groups */ $rules .= "set \$w3tc_ua \"\";\n"; if ($this->_config->get_boolean('mobile.enabled')) { $mobile_groups = array_reverse($this->_config->get_array('mobile.rgroups')); foreach ($mobile_groups as $mobile_group => $mobile_config) { $mobile_enabled = isset($mobile_config['enabled']) ? (bool) $mobile_config['enabled'] : false; $mobile_agents = isset($mobile_config['agents']) ? (array) $mobile_config['agents'] : ''; $mobile_redirect = isset($mobile_config['redirect']) ? $mobile_config['redirect'] : ''; if ($mobile_enabled && count($mobile_agents) && !$mobile_redirect) { $rules .= "if (\$http_user_agent ~* \"(" . implode('|', $mobile_agents) . ")\") {\n"; $rules .= " set \$w3tc_ua _" . $mobile_group . ";\n"; $rules .= "}\n"; } } } /** * Check referrer groups */ $rules .= "set \$w3tc_ref \"\";\n"; if ($this->_config->get_boolean('referrer.enabled')) { $referrer_groups = array_reverse($this->_config->get_array('referrer.rgroups')); foreach ($referrer_groups as $referrer_group => $referrer_config) { $referrer_enabled = isset($referrer_config['enabled']) ? (bool) $referrer_config['enabled'] : false; $referrer_referrers = isset($referrer_config['referrers']) ? (array) $referrer_config['referrers'] : ''; $referrer_redirect = isset($referrer_config['redirect']) ? $referrer_config['redirect'] : ''; if ($referrer_enabled && count($referrer_referrers) && !$referrer_redirect) { $rules .= "if (\$http_cookie ~* \"w3tc_referrer=.*(" . implode('|', $referrer_referrers) . ")\") {\n"; $rules .= " set \$w3tc_ref _" . $referrer_group . ";\n"; $rules .= "}\n"; } } } $rules .= "set \$w3tc_ssl \"\";\n"; if ($this->_config->get_boolean('pgcache.cache.ssl')) { $rules .= "if (\$scheme = https) {\n"; $rules .= " set \$w3tc_ssl _ssl;\n"; $rules .= "}\n"; } $rules .= "set \$w3tc_enc \"\";\n"; if ($this->_config->get_boolean('browsercache.enabled') && $this->_config->get_boolean('browsercache.html.compression')) { $rules .= "if (\$http_accept_encoding ~ gzip) {\n"; $rules .= " set \$w3tc_enc _gzip;\n"; $rules .= "}\n"; } $cache_path = str_replace(w3_get_document_root(), '', $cache_dir); $rules .= "set \$w3tc_ext \"\";\n"; $rules .= "if (-f \"\$document_root" . $cache_path . "/\$request_uri/_index\$w3tc_ua\$w3tc_ref\$w3tc_ssl.html\$w3tc_enc\") {\n"; $rules .= " set \$w3tc_ext .html;\n"; $rules .= "}\n"; if ($this->_config->get_boolean('pgcache.cache.feed')) { $rules .= "if (-f \"\$document_root" . $cache_path . "/\$request_uri/_index\$w3tc_ua\$w3tc_ref\$w3tc_ssl.xml\$w3tc_enc\") {\n"; $rules .= " set \$w3tc_ext .xml;\n"; $rules .= "}\n"; } $rules .= "if (\$w3tc_ext = \"\") {\n"; $rules .= " set \$w3tc_rewrite 0;\n"; $rules .= "}\n"; $rules .= "if (\$w3tc_rewrite = 1) {\n"; $rules .= " rewrite .* \"" . $cache_path . "/\$request_uri/_index\$w3tc_ua\$w3tc_ref\$w3tc_ssl\$w3tc_ext\$w3tc_enc\" last;\n"; $rules .= "}\n"; $rules .= W3TC_MARKER_END_PGCACHE_CORE . "\n"; return $rules; }
/** * Returns nginx canonical rule * @param $cdnftp * @return string */ public function generate_canonical_nginx($cdnftp) { $home = $cdnftp ? w3_get_home_domain() : '$host'; return ' add_header Link "<$scheme://' . $home . '$uri>; rel=\\"canonical\\"";' . "\n"; }
<input type="hidden" name="pgcache.cache.404" value="0" /> <label><input type="checkbox" name="pgcache.cache.404" value="1"<?php checked($this->_config->get_boolean('pgcache.cache.404'), true); ?> /> Cache 404 (not found) pages</label><br /> <span class="description">Reduce server load by caching 404 pages. If the disk enhanced method of disk caching is used, 404 pages will be returned with a 200 response code. Use at your own risk.</span> </th> </tr> <tr> <th> <input type="hidden" name="pgcache.check.domain" value="0" /> <label><input type="checkbox" name="pgcache.check.domain" value="1"<?php checked($this->_config->get_boolean('pgcache.check.domain'), true); ?> /> Cache requests only for <?php echo w3_get_home_domain(); ?> hostname</label><br /> <span class="description">Cache only requests with the same <acronym title="Uniform Resource Indicator">URL</acronym> as the site's <a href="options-general.php">site address</a>.</span> </th> </tr> <tr> <th> <input type="hidden" name="pgcache.reject.logged" value="0" /> <label><input type="checkbox" name="pgcache.reject.logged" value="1"<?php checked($this->_config->get_boolean('pgcache.reject.logged'), true); ?> /> Don't cache pages for logged in users</label><br /> <span class="description">Users that have signed in to NXTClass (e.g. administrators) will never view cached pages if enabled.</span> </th> </tr>
/** * Checks if can we do cache logic * * @param string $buffer * @return boolean */ function _can_cache2(&$buffer) { /** * Skip if caching is disabled */ if (!$this->_caching) { return false; } /** * Check for database error */ if (w3_is_database_error($buffer)) { $this->cache_reject_reason = 'Database error occurred'; return false; } /** * Check for DONOTCACHEPAGE constant */ if (defined('DONOTCACHEPAGE') && DONOTCACHEPAGE) { $this->cache_reject_reason = 'DONOTCACHEPAGE constant is defined'; return false; } /** * Check hostname */ if ((!w3_is_multisite() || w3_is_multisite() && !w3_force_master()) && $this->_config->get_boolean('pgcache.check.domain') && w3_get_host() != w3_get_home_domain()) { $this->cache_reject_reason = 'Hostname mismatch'; return false; } /** * Don't cache 404 pages */ if (!$this->_config->get_boolean('pgcache.cache.404') && function_exists('is_404') && is_404()) { $this->cache_reject_reason = 'Page is 404'; return false; } /** * Don't cache homepage */ if (!$this->_config->get_boolean('pgcache.cache.home') && function_exists('is_home') && is_home()) { $this->cache_reject_reason = is_front_page() && is_home() ? 'Page is front page' : 'Page is posts page'; return false; } /** * Don't cache front page */ if ($this->_config->get_boolean('pgcache.reject.front_page') && function_exists('is_front_page') && is_front_page() && !is_home()) { $this->cache_reject_reason = 'Page is front page'; return false; } /** * Don't cache feed */ if (!$this->_config->get_boolean('pgcache.cache.feed') && function_exists('is_feed') && is_feed()) { $this->cache_reject_reason = 'Page is feed'; return false; } /** * Check if page contains dynamic tags */ if ($this->_enhanced_mode && $this->_has_dynamic($buffer)) { $this->cache_reject_reason = 'Page contains dynamic tags (mfunc or mclude) can not be cached in enhanced mode'; return false; } return true; }
_e('Cache 404 (not found) pages', 'w3-total-cache'); ?> </label><br /> <span class="description"><?php _e('Reduce server load by caching 404 pages. If the disk enhanced method of disk caching is used, 404 pages will be returned with a 200 response code. Use at your own risk.', 'w3-total-cache'); ?> </span> </th> </tr> <tr> <th> <?php $this->checkbox('pgcache.check.domain', $disable_check_domain); ?> <?php echo sprintf(__('Cache requests only for %s site address', 'w3-total-cache'), w3_get_home_domain()); ?> </label><br /> <span class="description"><?php _e('Cache only requests with the same <acronym title="Uniform Resource Indicator">URL</acronym> as the site\'s <a href="options-general.php">site address</a>.', 'w3-total-cache'); ?> </span> </th> </tr> <tr> <th> <?php $this->checkbox('pgcache.reject.logged'); ?> <?php _e('Don\'t cache pages for logged in users', 'w3-total-cache');
protected function strings() { return array('general' => array('pgcache.engine' => __('Page cache method:', 'w3-total-cache'), 'pgcache.enabled' => __('Page cache:', 'w3-total-cache'), 'pgcache.debug' => __('Page Cache', 'w3-total-cache')), 'settings' => array('pgcache.cache.home' => get_option('show_on_front') == 'posts' ? __('Cache front page', 'w3-total-cache') : __('Cache posts page', 'w3-total-cache'), 'pgcache.reject.front_page' => __('Don\'t cache front page', 'w3-total-cache'), 'pgcache.cache.feed' => __('Cache feeds: site, categories, tags, comments', 'w3-total-cache'), 'pgcache.cache.ssl' => __('Cache <acronym title="Secure Socket Layer">SSL</acronym> (<acronym title="HyperText Transfer Protocol over SSL">https</acronym>) requests', 'w3-total-cache'), 'pgcache.cache.query' => __('Cache <acronym title="Uniform Resource Identifier">URI</acronym>s with query string variables', 'w3-total-cache'), 'pgcache.cache.404' => __('Cache 404 (not found) pages', 'w3-total-cache'), 'pgcache.check.domain' => sprintf(__('Cache requests only for %s site address', 'w3-total-cache'), w3_get_home_domain()), 'pgcache.reject.logged' => __('Don\'t cache pages for logged in users', 'w3-total-cache'), 'pgcache.reject.logged_roles' => __('Don\'t cache pages for following user roles', 'w3-total-cache'), 'pgcache.prime.enabled' => __('Automatically prime the page cache', 'w3-total-cache'), 'pgcache.prime.interval' => __('Update interval:', 'w3-total-cache'), 'pgcache.prime.limit' => __('Pages per interval:', 'w3-total-cache'), 'pgcache.prime.sitemap' => __('Sitemap <acronym title="Uniform Resource Indicator">URL</acronym>:', 'w3-total-cache'), 'pgcache.prime.post.enabled' => __('Preload the post cache upon publish events.', 'w3-total-cache'), 'pgcache.purge.front_page' => __('Front page', 'w3-total-cache'), 'pgcache.purge.home' => get_option('show_on_front') == 'posts' ? __('Front page', 'w3-total-cache') : __('Posts page', 'w3-total-cache'), 'pgcache.purge.post' => __('Post page', 'w3-total-cache'), 'pgcache.purge.feed.blog' => __('Blog feed', 'w3-total-cache'), 'pgcache.purge.comments' => __('Post comments pages', 'w3-total-cache'), 'pgcache.purge.author' => __('Post author pages', 'w3-total-cache'), 'pgcache.purge.terms' => __('Post terms pages', 'w3-total-cache'), 'pgcache.purge.feed.comments' => __('Post comments feed', 'w3-total-cache'), 'pgcache.purge.feed.author' => __('Post author feed', 'w3-total-cache'), 'pgcache.purge.feed.terms' => __('Post terms feeds', 'w3-total-cache'), 'pgcache.purge.archive.daily' => __('Daily archive pages', 'w3-total-cache'), 'pgcache.purge.archive.monthly' => __('Monthly archive pages', 'w3-total-cache'), 'pgcache.purge.archive.yearly' => __('Yearly archive pages', 'w3-total-cache'), 'pgcache.purge.feed.types' => __('Specify the feed types to purge:', 'w3-total-cache'), 'pgcache.purge.postpages_limit' => __('Purge Limit:', 'w3-total-cache'), 'pgcache.purge.pages' => __('Additional pages:', 'w3-total-cache'), 'pgcache.purge.sitemap_regex' => __('Purge sitemaps:', 'w3-total-cache'), 'pgcache.memcached.servers' => __('Memcached hostname:port / <acronym title="Internet Protocol">IP</acronym>:port:', 'w3-total-cache'), 'pgcache.compatibility' => __('Enable compatibility mode', 'w3-total-cache'), 'pgcache.remove_charset' => __('Disable UTF-8 blog charset support', 'w3-total-cache'), 'pgcache.reject.request_head' => __(' Disable caching of HEAD <acronym title="Hypertext Transfer Protocol">HTTP</acronym> requests', 'w3-total-cache'), 'pgcache.lifetime' => __('Maximum lifetime of cache objects:', 'w3-total-cache'), 'pgcache.file.gc' => __('Garbage collection interval:', 'w3-total-cache'), 'pgcache.comment_cookie_ttl' => __('Comment cookie lifetime:', 'w3-total-cache'), 'pgcache.accept.qs' => __('Accepted query strings:', 'w3-total-cache'), 'pgcache.reject.ua' => __('Rejected user agents:', 'w3-total-cache'), 'pgcache.reject.cookie' => __('Rejected cookies:', 'w3-total-cache'), 'pgcache.reject.uri' => __('Never cache the following pages:', 'w3-total-cache'), 'pgcache.accept.files' => __('Cache exception list:', 'w3-total-cache'), 'pgcache.accept.uri' => __('Non-trailing slash pages:', 'w3-total-cache'), 'pgcache.cache.headers' => __('Specify page headers:', 'w3-total-cache'), 'pgcache.cache.nginx_handle_xml' => __('Handle <acronym title="Extensible Markup Language">XML</acronym> mime type', 'w3-total-cache'))); }