Exemplo n.º 1
0
 /**
  * Runs plugin
  */
 function run()
 {
     add_action('w3tc_flush_all', array($this, 'w3tc_flush_posts'), 1100, 1);
     add_action('w3tc_flush_post', array($this, 'w3tc_flush_post'), 1100, 1);
     add_action('w3tc_flushable_posts', '__return_true', 1100);
     add_action('w3tc_flush_posts', array($this, 'w3tc_flush_posts'), 1100);
     add_action('w3tc_flush_url', array($this, 'w3tc_flush_url'), 1100, 1);
     add_filter('w3tc_admin_bar_menu', array($this, 'w3tc_admin_bar_menu'));
     add_filter('cron_schedules', array($this, 'cron_schedules'));
     $o = Dispatcher::component('PgCache_ContentGrabber');
     add_filter('w3tc_footer_comment', array($o, 'w3tc_footer_comment'));
     add_action('w3tc_usage_statistics_of_request', array($o, 'w3tc_usage_statistics_of_request'), 10, 1);
     add_filter('w3tc_usage_statistics_metrics', array($this, 'w3tc_usage_statistics_metrics'));
     if ($this->_config->get_string('pgcache.engine') == 'file' || $this->_config->get_string('pgcache.engine') == 'file_generic') {
         add_action('w3_pgcache_cleanup', array($this, 'cleanup'));
     }
     add_action('w3_pgcache_prime', array($this, 'prime'));
     Util_AttachToActions::flush_posts_on_actions();
     add_filter('comment_cookie_lifetime', array($this, 'comment_cookie_lifetime'));
     if ($this->_config->get_string('pgcache.engine') == 'file_generic') {
         add_action('wp_logout', array($this, 'on_logout'), 0);
         add_action('wp_login', array($this, 'on_login'), 0);
     }
     if ($this->_config->get_boolean('pgcache.prime.post.enabled', false)) {
         add_action('publish_post', array($this, 'prime_post'), 30);
     }
     if (($this->_config->get_boolean('pgcache.late_init') || $this->_config->get_boolean('pgcache.late_caching')) && !is_admin()) {
         $o = Dispatcher::component('PgCache_ContentGrabber');
         add_action('init', array($o, 'delayed_cache_print'), 99999);
     }
     if (!$this->_config->get_boolean('pgcache.mirrors.enabled') && !Util_Environment::is_wpmu_subdomain()) {
         add_action('init', array($this, 'redirect_on_foreign_domain'));
     }
 }
Exemplo n.º 2
0
 /**
  * Returns blog_id by home url
  * If database not initialized yet - returns 0
  *
  * @return integer
  */
 public static function get_current_blog_data()
 {
     $host = Util_Environment::host();
     // subdomain
     if (Util_Environment::is_wpmu_subdomain()) {
         $blog_data = Util_WpmuBlogmap::try_get_current_blog_data($host);
         if (is_null($blog_data)) {
             $GLOBALS['w3tc_blogmap_register_new_item'] = $host;
         }
         return $blog_data;
     } else {
         // try subdir blog
         $url = $host . $_SERVER['REQUEST_URI'];
         $pos = strpos($url, '?');
         if ($pos !== false) {
             $url = substr($url, 0, $pos);
         }
         $url = rtrim($url, '/');
         $start_url = $url;
         for (;;) {
             $blog_data = Util_WpmuBlogmap::try_get_current_blog_data($url);
             if (!is_null($blog_data)) {
                 return $blog_data;
             }
             $pos = strrpos($url, '/');
             if ($pos === false) {
                 break;
             }
             $url = rtrim(substr($url, 0, $pos), '/');
         }
         $GLOBALS['w3tc_blogmap_register_new_item'] = $start_url;
         return null;
     }
 }
 /**
  * Remove script tags from the source
  *
  * @param string  $content
  * @param array   $files
  * @return void
  */
 function remove_scripts(&$content, $files)
 {
     $regexps = array();
     $home_url_regexp = Util_Environment::home_url_regexp();
     $path = '';
     if (Util_Environment::is_wpmu() && !Util_Environment::is_wpmu_subdomain()) {
         $path = ltrim(Util_Environment::home_url_uri(), '/');
     }
     foreach ($files as $file) {
         if ($path && strpos($file, $path) === 0) {
             $file = substr($file, strlen($path));
         }
         $this->replaced_scripts[] = $file;
         if (Util_Environment::is_url($file) && !preg_match('~' . $home_url_regexp . '~i', $file)) {
             // external JS files
             $regexps[] = Util_Environment::preg_quote($file);
         } else {
             // local JS files
             $file = ltrim($file, '/');
             if (home_url() == site_url() && ltrim(Util_Environment::site_url_uri(), '/') && strpos($file, ltrim(Util_Environment::site_url_uri(), '/')) === 0) {
                 $file = str_replace(ltrim(Util_Environment::site_url_uri(), '/'), '', $file);
             }
             $file = ltrim(preg_replace('~' . $home_url_regexp . '~i', '', $file), '/\\');
             $regexps[] = '(' . $home_url_regexp . ')?/?' . Util_Environment::preg_quote($file);
         }
     }
     foreach ($regexps as $regexp) {
         $content = preg_replace('~<script\\s+[^<>]*src=["\']?' . $regexp . '["\']?[^<>]*>\\s*</script>~Uis', '', $content);
     }
 }
 /**
  * Generates rules
  *
  * @param Config  $config
  * @return string
  */
 function rules_core_generate_nginx($config)
 {
     $cache_dir = Util_Rule::filename_to_uri(W3TC_CACHE_MINIFY_DIR);
     $first_regex_var = '$1';
     // for subdir - need to count subdir in url
     if (Util_Environment::is_wpmu() && !Util_Environment::is_wpmu_subdomain()) {
         // take into accont case when whole subdir wpmu is installed in subdir
         $home_uri = network_home_url('', 'relative');
         if (substr($cache_dir, 0, strlen($home_uri)) == $home_uri) {
             $cache_dir = $home_uri . '([a-z0-9]+/)?' . substr($cache_dir, strlen($home_uri));
         } else {
             $cache_dir = '(/[a-z0-9]+)?' . $cache_dir;
         }
         $first_regex_var = '$2';
     }
     $minify_uri = rtrim(network_site_url('', 'relative'), '/') . '/';
     $engine = $config->get_string('minify.engine');
     $browsercache = $config->get_boolean('browsercache.enabled');
     $compression = $browsercache && $config->get_boolean('browsercache.cssjs.compression') && !defined('W3TC_PAGECACHE_OUTPUT_COMPRESSION_OFF');
     $rules = '';
     $rules .= W3TC_MARKER_BEGIN_MINIFY_CORE . "\n";
     if ($engine == 'file') {
         $rules .= "set \$w3tc_enc \"\";\n";
         if ($compression) {
             $rules .= "if (\$http_accept_encoding ~ gzip) {\n";
             $rules .= "    set \$w3tc_enc .gzip;\n";
             $rules .= "}\n";
         }
         $rules .= "if (-f \$request_filename\$w3tc_enc) {\n";
         $rules .= "    rewrite (.*) \$1\$w3tc_enc break;\n";
         $rules .= "}\n";
     }
     $rules .= "rewrite ^{$cache_dir}/ {$minify_uri}index.php last;\n";
     $rules .= W3TC_MARKER_END_MINIFY_CORE . "\n";
     return $rules;
 }
		</table>

		<?php 
echo Util_Ui::button_config_save('pagecache_general');
?>
		<?php 
Util_Ui::postbox_footer();
?>

		<?php 
Util_Ui::postbox_header(__('Aliases', 'w3-total-cache'), '', 'mirrors');
?>
		<table class="form-table">
			<?php 
Util_Ui::config_item(array('key' => 'pgcache.mirrors.enabled', 'control' => 'checkbox', 'label' => __('Cache Alias Hostnames:', 'w3-total-cache'), 'checkbox_label' => __('Enable', 'w3-total-cache'), 'enabled' => !Util_Environment::is_wpmu_subdomain(), 'description' => __('If the same Wordpress content is accessed from different domains', 'w3-total-cache')));
Util_Ui::config_item(array('key' => 'pgcache.mirrors.home_urls', 'control' => 'textarea', 'label' => __('Additional Home URLs:', 'w3-total-cache'), 'enabled' => !Util_Environment::is_wpmu_subdomain(), 'description' => __('Specify full home URLs of your mirrors so that plugin will flush it\'s cache when content is changed. For example:<br /> http://my-site.com<br />http://www.my-site.com<br />https://my-site.com', 'w3-total-cache')));
?>
		</table>
		<?php 
echo Util_Ui::button_config_save('pagecache_aliases');
?>
		<?php 
Util_Ui::postbox_footer();
?>

		<?php 
Util_Ui::postbox_header(__('Cache Preload', 'w3-total-cache'), '', 'cache_preload');
?>
		<table class="form-table">
			<tr>
				<th colspan="2">
Exemplo n.º 6
0
}
?>
        </li>

        <li>
            <?php 
_e('Network mode:', 'w3-total-cache');
?>
            <?php 
if (Util_Environment::is_wpmu()) {
    ?>
            <code><?php 
    _e('Yes', 'w3-total-cache');
    ?>
 (<?php 
    echo Util_Environment::is_wpmu_subdomain() ? 'subdomain' : 'subdir';
    ?>
)</code>
            <?php 
} else {
    ?>
            <code><?php 
    _e('No', 'w3-total-cache');
    ?>
</code>
            <?php 
}
?>
        </li>
    </ul>
</div>
 /**
  * Generate rules related to prevent for media 404 error by WP
  *
  * @param Config  $config
  * @return string
  */
 private function rules_no404wp_generate_apache($config)
 {
     $a = $this->get_mime_types();
     $cssjs_types = $a['cssjs'];
     $html_types = $a['html'];
     $other_types = $a['other'];
     $extensions = array_merge(array_keys($cssjs_types), array_keys($html_types), array_keys($other_types));
     $permalink_structure = get_option('permalink_structure');
     $permalink_structure_ext = ltrim(strrchr($permalink_structure, '.'), '.');
     if ($permalink_structure_ext != '') {
         foreach ($extensions as $index => $extension) {
             if (strstr($extension, $permalink_structure_ext) !== false) {
                 $extensions[$index] = preg_replace('~\\|?' . Util_Environment::preg_quote($permalink_structure_ext) . '\\|?~', '', $extension);
             }
         }
     }
     $exceptions = $config->get_array('browsercache.no404wp.exceptions');
     $wp_uri = network_home_url('', 'relative');
     $wp_uri = rtrim($wp_uri, '/');
     $rules = '';
     $rules .= W3TC_MARKER_BEGIN_BROWSERCACHE_NO404WP . "\n";
     $rules .= "<IfModule mod_rewrite.c>\n";
     $rules .= "    RewriteEngine On\n";
     // in subdir - rewrite theme files and similar to upper folder if file exists
     if (Util_Environment::is_wpmu() && !Util_Environment::is_wpmu_subdomain()) {
         $rules .= "    RewriteCond %{REQUEST_FILENAME} !-f\n";
         $rules .= "    RewriteCond %{REQUEST_FILENAME} !-d\n";
         $rules .= "    RewriteCond %{REQUEST_URI} ^{$wp_uri}/([_0-9a-zA-Z-]+/)(.*\\.)(" . implode('|', $extensions) . ")\$ [NC]\n";
         $document_root = Util_Rule::apache_docroot_variable();
         $rules .= '    RewriteCond "' . $document_root . $wp_uri . '/%2%3" -f' . "\n";
         $rules .= "    RewriteRule .* {$wp_uri}/%2%3 [L]\n\n";
     }
     $rules .= "    RewriteCond %{REQUEST_FILENAME} !-f\n";
     $rules .= "    RewriteCond %{REQUEST_FILENAME} !-d\n";
     $imploded = implode('|', $exceptions);
     if (!empty($imploded)) {
         $rules .= "    RewriteCond %{REQUEST_URI} !(" . $imploded . ")\n";
     }
     $rules .= "    RewriteCond %{REQUEST_URI} \\.(" . implode('|', $extensions) . ")\$ [NC]\n";
     $rules .= "    RewriteRule .* - [L]\n";
     $rules .= "</IfModule>\n";
     $rules .= W3TC_MARKER_END_BROWSERCACHE_NO404WP . "\n";
     return $rules;
 }
Exemplo n.º 8
0
 /**
  * Returns the sitepath for multisite subfolder or subdomain path for multisite subdomain
  *
  * @return string
  */
 private function _get_multisite_url_identifier()
 {
     if (defined('DOMAIN_MAPPING') && DOMAIN_MAPPING) {
         $parsedUrl = parse_url(site_url());
         return $parsedUrl['host'];
     } elseif (Util_Environment::is_wpmu_subdomain()) {
         $parsedUrl = parse_url(Util_Environment::home_domain_root_url());
         $urlparts = explode('.', $parsedUrl['host']);
         if (sizeof($urlparts) > 2) {
             $subdomain = array_shift($urlparts);
             return trim($subdomain, '/');
         }
     }
     return trim(Util_Environment::site_url_uri(), '/');
 }
Exemplo n.º 9
0
 /**
  * Flush cache based on regex
  *
  * @param string  $regex
  */
 private function _flush_based_on_regex($regex)
 {
     if (Util_Environment::is_wpmu() && !Util_Environment::is_wpmu_subdomain()) {
         $domain = get_home_url();
         $parsed = parse_url($domain);
         $host = $parsed['host'];
         $path = isset($parsed['path']) ? '/' . trim($parsed['path'], '/') : '';
         $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR . '/' . $host . $path;
     } else {
         $flush_dir = W3TC_CACHE_PAGE_ENHANCED_DIR . '/' . Util_Environment::host();
     }
     $dir = @opendir($flush_dir);
     if ($dir) {
         while (($entry = @readdir($dir)) !== false) {
             if ($entry == '.' || $entry == '..') {
                 continue;
             }
             if (preg_match('/' . $regex . '/', basename($entry))) {
                 Util_File::rmdir($flush_dir . DIRECTORY_SEPARATOR . $entry);
             }
         }
         @closedir($dir);
     }
 }
Exemplo n.º 10
0
 /**
  * Init action
  *
  * @return void
  */
 function init()
 {
     // Load plugin text domain
     load_plugin_textdomain(W3TC_TEXT_DOMAIN, null, plugin_basename(W3TC_DIR) . '/languages/');
     if (is_multisite() && !is_network_admin()) {
         global $w3_current_blog_id, $current_blog;
         if ($w3_current_blog_id != $current_blog->blog_id && !isset($GLOBALS['w3tc_blogmap_register_new_item'])) {
             $url = Util_Environment::host_port() . $_SERVER['REQUEST_URI'];
             $pos = strpos($url, '?');
             if ($pos !== false) {
                 $url = substr($url, 0, $pos);
             }
             $GLOBALS['w3tc_blogmap_register_new_item'] = $url;
         }
     }
     if (isset($GLOBALS['w3tc_blogmap_register_new_item'])) {
         $do_redirect = false;
         // true value is a sign to just generate config cache
         if ($GLOBALS['w3tc_blogmap_register_new_item'] != 'cache_options') {
             if (Util_Environment::is_wpmu_subdomain()) {
                 $blog_home_url = $GLOBALS['w3tc_blogmap_register_new_item'];
             } else {
                 $home_url = rtrim(get_home_url(), '/');
                 if (substr($home_url, 0, 7) == 'http://') {
                     $home_url = substr($home_url, 7);
                 } else {
                     if (substr($home_url, 0, 8) == 'https://') {
                         $home_url = substr($home_url, 8);
                     }
                 }
                 if (substr($GLOBALS['w3tc_blogmap_register_new_item'], 0, strlen($home_url)) == $home_url) {
                     $blog_home_url = $home_url;
                 } else {
                     $blog_home_url = $GLOBALS['w3tc_blogmap_register_new_item'];
                 }
             }
             $do_redirect = Util_WpmuBlogmap::register_new_item($blog_home_url, $this->_config);
             // reset cache of blog_id
             global $w3_current_blog_id;
             $w3_current_blog_id = null;
             // change config to actual blog, it was master before
             $this->_config = new Config();
             // fix environment, potentially it's first request to a specific blog
             $environment = Dispatcher::component('Root_Environment');
             $environment->fix_on_event($this->_config, 'first_frontend', $this->_config);
         }
         // need to repeat request processing, since we was not able to realize
         // blog_id before so we are running with master config now.
         // redirect to the same url causes "redirect loop" error in browser,
         // so need to redirect to something a bit different
         if ($do_redirect) {
             if (strpos($_SERVER['REQUEST_URI'], '?') === false) {
                 Util_Environment::redirect_temp($_SERVER['REQUEST_URI'] . '?repeat=w3tc');
             } else {
                 if (strpos($_SERVER['REQUEST_URI'], 'repeat=w3tc') === false) {
                     Util_Environment::redirect_temp($_SERVER['REQUEST_URI'] . '&repeat=w3tc');
                 }
             }
         }
     }
     /**
      * Check for rewrite test request
      */
     $rewrite_test = Util_Request::get_boolean('w3tc_rewrite_test');
     if ($rewrite_test) {
         echo 'OK';
         exit;
     }
     $admin_bar = false;
     if (function_exists('is_admin_bar_showing')) {
         $admin_bar = is_admin_bar_showing();
     }
     if ($admin_bar) {
         add_action('wp_print_scripts', array($this, 'popup_script'));
     }
     // dont add system stuff to search results
     if (isset($_GET['repeat']) && $_GET['repeat'] == 'w3tc' || Util_Environment::is_preview_mode()) {
         header('X-Robots-Tag: noindex');
     }
 }
Exemplo n.º 11
0
 private function do_flush()
 {
     if (!is_network_admin()) {
         $queued_urls[get_home_url() . '/.*'] = '*';
         // add mirror urls
         $queued_urls = Util_PageUrls::complement_with_mirror_urls($queued_urls);
         foreach ($queued_urls as $url => $nothing) {
             $this->_purge($url);
         }
     } else {
         // todo: remove. doesnt work for all caches.
         // replace with tool to flush network
         global $wpdb;
         $protocall = Util_Environment::is_https() ? 'https://' : 'http://';
         // If WPMU Domain Mapping plugin is installed and active
         if (defined('SUNRISE_LOADED') && SUNRISE_LOADED && isset($wpdb->dmtable) && !empty($wpdb->dmtable)) {
             $blogs = $wpdb->get_results("SELECT {$wpdb->blogs}.domain, {$wpdb->blogs}.path, {$wpdb->dmtable}.domain AS mapped_domain\n                                    FROM {$wpdb->dmtable}\n                                    RIGHT JOIN {$wpdb->blogs} ON {$wpdb->dmtable}.blog_id = {$wpdb->blogs}.blog_id\n                                    WHERE site_id = {$wpdb->siteid}\n                                    AND spam = 0\n                                    AND deleted = 0\n                                    AND archived = '0'\n                                   ");
             foreach ($blogs as $blog) {
                 if (!isset($blog->mapped_domain)) {
                     $url = $protocall . $blog->domain . (strlen($blog->path) > 1 ? '/' . trim($blog->path, '/') : '') . '/.*';
                 } else {
                     $url = $protocall . $blog->mapped_domain . '/.*';
                 }
                 $this->_purge($url);
             }
         } else {
             if (!Util_Environment::is_wpmu_subdomain()) {
                 $this->_purge(get_home_url() . '/.*');
             } else {
                 $blogs = $wpdb->get_results("\n                                        SELECT domain, path\n                                        FROM {$wpdb->blogs}\n                                        WHERE site_id = '{$wpdb->siteid}'\n                                        AND spam = 0\n                                        AND deleted = 0\n                                        AND archived = '0'\n                                    ");
                 foreach ($blogs as $blog) {
                     $url = $protocall . $blog->domain . (strlen($blog->path) > 1 ? '/' . trim($blog->path, '/') : '') . '/.*';
                     $this->_purge($url);
                 }
             }
         }
     }
 }
Exemplo n.º 12
0
 /**
  * Returns if there is multisite mode
  *
  * @return boolean
  */
 public static function is_wpmu()
 {
     static $wpmu = null;
     if ($wpmu === null) {
         $wpmu = file_exists(ABSPATH . 'wpmu-settings.php') || defined('MULTISITE') && MULTISITE || defined('SUNRISE') || Util_Environment::is_wpmu_subdomain();
     }
     return $wpmu;
 }
Exemplo n.º 13
0
 public static function complement_with_mirror_urls($queued_urls)
 {
     $config = Dispatcher::config();
     if (!$config->get_boolean('pgcache.mirrors.enabled') || Util_Environment::is_wpmu_subdomain()) {
         return $queued_urls;
     }
     $home_urls = $config->get_array('pgcache.mirrors.home_urls');
     $url_prefix = trailingslashit(get_home_url());
     $urls = array_keys($queued_urls);
     foreach ($urls as $url) {
         if (substr($url, 0, strlen($url_prefix)) != $url_prefix) {
             continue;
         }
         foreach ($home_urls as $home) {
             if (!empty($home)) {
                 $mirror_url = $home . substr($url, strlen($url_prefix));
                 $queued_urls[$mirror_url] = '*';
             }
         }
     }
     return $queued_urls;
 }