/** * Check if plugin is locked * * @return boolean */ function locked() { static $locked = null; if ($locked === null) { if (w3_is_wpmu()) { if (isset($_GET['sitewide'])) { $locked = false; } else { global $blog_id; $blogs = get_blog_list(); foreach ($blogs as $blog) { if ($blog['blog_id'] != $blog_id) { $active_plugins = get_blog_option($blog['blog_id'], 'active_plugins'); if (in_array(W3TC_FILE, $active_plugins)) { $locked = true; break; } } } } } else { $locked = false; } } return $locked; }
/** * Returns if there is multisite mode * * @return boolean */ function w3_is_network() { return w3_is_wpmu() || w3_is_multisite(); }
/** * Activate plugin action * * @return void */ function activate() { require_once W3TC_INC_DIR . '/functions/activation.php'; /** * Disable buggy sitewide activation in WPMU and WP 3.0 */ if (w3_is_wpmu() && isset($_GET['sitewide']) || w3_is_multisite() && isset($_GET['networkwide'])) { w3_network_activate_error(); } /** * Check installation files */ $files = array(W3TC_INSTALL_FILE_ADVANCED_CACHE, W3TC_INSTALL_FILE_DB, W3TC_INSTALL_FILE_OBJECT_CACHE); $nonexistent_files = array(); foreach ($files as $file) { if (!file_exists($file)) { $nonexistent_files[] = $file; } } if (count($nonexistent_files)) { $error = sprintf('Unfortunately core file(s): (<strong>%s</strong>) are missing, so activation will fail. Please re-start the installation process from the beginning.', implode(', ', $nonexistent_files)); w3_activate_error($error); } if (!@is_dir(W3TC_CONTENT_DIR) && !@mkdir(W3TC_CONTENT_DIR)) { w3_writable_error(W3TC_CONTENT_DIR); } if (!@is_dir(W3TC_CACHE_FILE_DBCACHE_DIR) && !@mkdir(W3TC_CACHE_FILE_DBCACHE_DIR)) { w3_writable_error(W3TC_CACHE_FILE_DBCACHE_DIR); } if (!@is_dir(W3TC_CACHE_FILE_OBJECTCACHE_DIR) && !@mkdir(W3TC_CACHE_FILE_OBJECTCACHE_DIR)) { w3_writable_error(W3TC_CACHE_FILE_OBJECTCACHE_DIR); } if (!@is_dir(W3TC_CACHE_FILE_PGCACHE_DIR) && !@mkdir(W3TC_CACHE_FILE_PGCACHE_DIR)) { w3_writable_error(W3TC_CACHE_FILE_PGCACHE_DIR); } if (!@is_dir(W3TC_CACHE_FILE_MINIFY_DIR) && !@mkdir(W3TC_CACHE_FILE_MINIFY_DIR)) { w3_writable_error(W3TC_CACHE_FILE_MINIFY_DIR); } if (!@is_dir(W3TC_LOG_DIR) && !@mkdir(W3TC_LOG_DIR)) { w3_writable_error(W3TC_LOG_DIR); } if (!@is_dir(W3TC_TMP_DIR) && !@mkdir(W3TC_TMP_DIR)) { w3_writable_error(W3TC_TMP_DIR); } if (w3_is_network() && file_exists(W3TC_CONFIG_MASTER_PATH)) { /** * For multisite load master config */ $this->_config->load_master(); if (!$this->_config->save(false)) { w3_writable_error(W3TC_CONFIG_PATH); } } elseif (!file_exists(W3TC_CONFIG_PATH)) { /** * Set default settings */ $this->_config->set_defaults(); /** * If config doesn't exist enable preview mode */ if (!$this->_config->save(true)) { w3_writable_error(W3TC_CONFIG_PREVIEW_PATH); } } /** * Save blognames into file */ if (w3_is_network() && !w3_is_subdomain_install()) { if (!w3_save_blognames()) { w3_writable_error(W3TC_BLOGNAMES_PATH); } } delete_option('w3tc_request_data'); add_option('w3tc_request_data', '', null, 'no'); }
/** * Detect WPMU blogname * * @return string */ function w3_get_blogname() { static $blogname = null; if ($blogname === null) { if (w3_is_wpmu()) { $domain = w3_get_domain($_SERVER['HTTP_HOST']); if (w3_is_vhost()) { $blogname = $domain; } else { $uri = $_SERVER['REQUEST_URI']; $site_path = w3_get_site_path(); if ($site_path != '' && strpos($uri, $site_path) === 0) { $uri = substr_replace($uri, '/', 0, strlen($site_path)); } $blogname = w3_get_blogname_from_uri($uri); if ($blogname != '') { $blogname = $blogname . '.' . $domain; } else { $blogname = $domain; } } } else { $blogname = ''; } } return $blogname; }
/** * Generates rules for WP dir * * @return string */ function generate_rules_core() { global $w3_reserved_blognames; /** * 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).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_user_agents = $this->_config->get_array('pgcache.reject.ua'); $accept_files = $this->_config->get_array('pgcache.accept.files'); /** * WPMU support */ $is_wpmu = w3_is_wpmu(); $is_vhost = w3_is_vhost(); /** * Generate directives */ $rules = ''; $rules .= "# BEGIN W3 Total Cache\n"; $setenvif_rules = ''; if ($is_wpmu) { $setenvif_rules .= " SetEnvIfNoCase Host ^(www\\.)?([a-z0-9\\-\\.]+\\.[a-z]+)\\.?(:[0-9]+)?\$ DOMAIN=\$2\n"; if (!$is_vhost) { $setenvif_rules .= " SetEnvIfNoCase Request_URI ^" . w3_get_site_path() . "([a-z0-9\\-]+)/ BLOGNAME=\$1\n"; } } $compression = $this->_config->get_string('pgcache.compression'); if ($compression != '') { $compressions = array(); if (stristr($compression, 'gzip') !== false) { $compressions[] = 'gzip'; } if (stristr($compression, 'deflate') !== false) { $compressions[] = 'deflate'; } if (count($compressions)) { $setenvif_rules .= " SetEnvIfNoCase Accept-Encoding (" . implode('|', $compressions) . ") APPEND_EXT=.\$1\n"; } } if ($setenvif_rules != '') { $rules .= "<IfModule mod_setenvif.c>\n" . ($setenvif_rules .= "</IfModule>\n"); } $rules .= "<IfModule mod_rewrite.c>\n"; $rules .= " RewriteEngine On\n"; $mobile_redirect = $this->_config->get_string('pgcache.mobile.redirect'); if ($mobile_redirect != '') { $mobile_agents = $this->_config->get_array('pgcache.mobile.agents'); $rules .= " RewriteCond %{HTTP_USER_AGENT} (" . implode('|', array_map('w3_preg_quote', $mobile_agents)) . ") [NC]\n"; $rules .= " RewriteRule .* " . $mobile_redirect . " [R,L]\n"; } $rules .= " RewriteCond %{REQUEST_URI} \\/\$\n"; $rules .= " RewriteCond %{REQUEST_URI} !(" . implode('|', $reject_uris) . ")"; if (count($accept_files)) { $rules .= " [OR]\n RewriteCond %{REQUEST_URI} (" . implode('|', array_map('w3_preg_quote', $accept_files)) . ") [NC]\n"; } else { $rules .= "\n"; } $rules .= " RewriteCond %{REQUEST_METHOD} !=POST\n"; $rules .= " RewriteCond %{QUERY_STRING} =\"\"\n"; $rules .= " RewriteCond %{HTTP_COOKIE} !(" . implode('|', array_map('w3_preg_quote', $reject_cookies)) . ") [NC]\n"; if (count($reject_user_agents)) { $rules .= " RewriteCond %{HTTP_USER_AGENT} !(" . implode('|', array_map('w3_preg_quote', $reject_user_agents)) . ") [NC]\n"; } if ($is_wpmu) { if ($is_vhost) { $replacement = '/w3tc-%{ENV:DOMAIN}/'; } else { $rules .= " RewriteCond %{ENV:BLOGNAME} !^(" . implode('|', $w3_reserved_blognames) . ")\$\n"; $rules .= " RewriteCond %{ENV:BLOGNAME} !-f\n"; $rules .= " RewriteCond %{ENV:BLOGNAME} !-d\n"; $replacement = '/w3tc-%{ENV:BLOGNAME}.%{ENV:DOMAIN}/'; } $cache_dir = preg_replace('~/w3tc.*/~U', $replacement, W3TC_CACHE_FILE_PGCACHE_DIR, 1); } else { $cache_dir = W3TC_CACHE_FILE_PGCACHE_DIR; } $rules .= " RewriteCond " . w3_path($cache_dir) . "/\$1/_default_.html%{ENV:APPEND_EXT} -f\n"; $rules .= " RewriteRule (.*) " . str_replace(ABSPATH, '', $cache_dir) . "/\$1/_default_.html%{ENV:APPEND_EXT} [L]\n"; $rules .= "</IfModule>\n"; $rules .= "# END W3 Total Cache\n\n"; if (!$this->check_rules_wp()) { $rules .= "# BEGIN WordPress\n"; $rules .= "<IfModule mod_rewrite.c>\n"; $rules .= " RewriteEngine On\n"; $rules .= " RewriteCond %{REQUEST_FILENAME} !-f\n"; $rules .= " RewriteCond %{REQUEST_FILENAME} !-d\n"; $rules .= " RewriteRule .* index.php [L]\n"; $rules .= "</IfModule>\n"; $rules .= "# END WordPress\n\n"; } return $rules; }
/** * Options save action */ function options_save() { require_once W3TC_LIB_W3_DIR . '/Request.php'; /** * Redirect params */ $params = array(); /** * Read config */ $config =& new W3_Config(); $config->read_request(); /** * General tab */ if ($this->_tab == 'general') { $debug = W3_Request::get_array('debug'); $config->set('dbcache.debug', in_array('dbcache', $debug)); $config->set('pgcache.debug', in_array('pgcache', $debug)); $config->set('minify.debug', in_array('minify', $debug)); $config->set('cdn.debug', in_array('cdn', $debug)); /** * Page cache tab */ if ($config->get_boolean('pgcache.enabled') && $config->get_string('pgcache.engine') == 'file_pgcache' && get_option('permalink_structure') == '') { $this->redirect(array('error' => 'fancy_permalinks_disabled')); } /** * Show notification when CDN enabled */ if ($this->_config->get_boolean('cdn.enabled') == false && $config->get_boolean('cdn.enabled') == true && $config->get_string('cdn.engine') != 'mirror') { $config->set('notes.cdn_upload', true); } } /** * Minify tab */ if ($this->_tab == 'minify') { $groups = $this->minify_get_groups(); $js_group = W3_Request::get_string('js_group'); $js_files = W3_Request::get_array('js_files'); $css_group = W3_Request::get_string('css_group'); $css_files = W3_Request::get_array('css_files'); $js_groups = array(); $css_groups = array(); foreach ($js_files as $group => $locations) { if (!array_key_exists($group, $groups)) { continue; } foreach ((array) $locations as $location => $files) { switch ($location) { case 'include': $js_groups[$group][$location]['blocking'] = true; break; case 'include-nb': $js_groups[$group][$location]['blocking'] = false; break; case 'include-footer': $js_groups[$group][$location]['blocking'] = true; break; case 'include-footer-nb': $js_groups[$group][$location]['blocking'] = false; break; } foreach ((array) $files as $file) { if (!empty($file)) { $js_groups[$group][$location]['files'][] = w3_normalize_file($file); } } } } foreach ($css_files as $group => $locations) { if (!array_key_exists($group, $groups)) { continue; } foreach ((array) $locations as $location => $files) { foreach ((array) $files as $file) { if (!empty($file)) { $css_groups[$group][$location]['files'][] = w3_normalize_file($file); } } } } $config->set('minify.js.groups', $js_groups); $config->set('minify.css.groups', $css_groups); $params = array_merge($params, array('js_group' => $js_group, 'css_group' => $css_group)); } /** * Handle settings change that require pgcache and minify empty */ $pgcache_dependencies = array('pgcache.debug', 'dbcache.enabled', 'minify.enabled', 'cdn.enabled'); if ($config->get_boolean('dbcache.enabled')) { $pgcache_dependencies = array_merge($pgcache_dependencies, array('dbcache.debug')); } if ($config->get_boolean('minify.enabled')) { $pgcache_dependencies = array_merge($pgcache_dependencies, array('minify.debug', 'minify.rewrite', 'minify.options', 'minify.html.enable', 'minify.html.reject.admin', 'minify.html.inline.css', 'minify.html.inline.js', 'minify.html.strip.crlf', 'minify.css.enable', 'minify.css.groups', 'minify.js.enable', 'minify.js.groups')); } if ($config->get_boolean('cdn.enabled')) { $pgcache_dependencies = array_merge($pgcache_dependencies, array('cdn.debug', 'cdn.engine', 'cdn.includes.enable', 'cdn.includes.files', 'cdn.theme.enable', 'cdn.theme.files', 'cdn.minify.enable', 'cdn.custom.enable', 'cdn.custom.files', 'cdn.ftp.domain', 'cdn.s3.bucket', 'cdn.cf.id', 'cdn.cf.cname')); } $minify_dependencies = array('minify.debug', 'minify.css.combine', 'minify.css.strip.comments', 'minify.css.strip.crlf', 'minify.css.groups', 'minify.js.combine.header', 'minify.js.combine.footer', 'minify.js.strip.comments', 'minify.js.strip.crlf', 'minify.js.groups'); $old_pgcache_dependencies_values = array(); $new_pgcache_dependencies_values = array(); $old_minify_dependencies_values = array(); $new_minify_dependencies_values = array(); foreach ($pgcache_dependencies as $pgcache_dependency) { $old_pgcache_dependencies_values[] = $this->_config->get($pgcache_dependency); $new_pgcache_dependencies_values[] = $config->get($pgcache_dependency); } foreach ($minify_dependencies as $minify_dependency) { $old_minify_dependencies_values[] = $this->_config->get($minify_dependency); $new_minify_dependencies_values[] = $config->get($minify_dependency); } if ($this->_config->get_boolean('pgcache.enabled') && serialize($old_pgcache_dependencies_values) != serialize($new_pgcache_dependencies_values)) { $config->set('notes.need_empty_pgcache', true); } if ($this->_config->get_boolean('minify.enabled') && serialize($old_minify_dependencies_values) != serialize($new_minify_dependencies_values)) { $config->set('notes.need_empty_minify', true); } /** * Save config */ if ($config->save()) { require_once W3TC_LIB_W3_DIR . '/Plugin/PgCache.php'; require_once W3TC_LIB_W3_DIR . '/Plugin/DbCache.php'; require_once W3TC_LIB_W3_DIR . '/Plugin/Cdn.php'; $w3_plugin_pgcache =& W3_Plugin_PgCache::instance(); $w3_plugin_dbcache =& W3_Plugin_DbCache::instance(); $w3_plugin_cdn =& W3_Plugin_Cdn::instance(); if (W3TC_PHP5) { require_once W3TC_LIB_W3_DIR . '/Plugin/Minify.php'; $w3_plugin_minify =& W3_Plugin_Minify::instance(); } /** * Empty caches on engine change or cache enable/disable */ if ($this->_config->get_string('pgcache.engine') != $config->get_string('pgcache.engine') || $this->_config->get_string('pgcache.enabled') != $config->get_string('pgcache.enabled')) { $this->flush_pgcache(); } if ($this->_config->get_string('dbcache.engine') != $config->get_string('dbcache.engine') || $this->_config->get_string('dbcache.enabled') != $config->get_string('dbcache.enabled')) { $this->flush_dbcache(); } if ($this->_config->get_string('minify.engine') != $config->get_string('minify.engine') || $this->_config->get_string('minify.enabled') != $config->get_string('minify.enabled')) { $this->flush_minify(); } /** * Unschedule events if changed file gc interval */ if ($this->_config->get_boolean('pgcache.file.gc') != $config->get_boolean('pgcache.file.gc')) { $w3_plugin_pgcache->unschedule(); } if ($this->_config->get_boolean('dbcache.file.gc') != $config->get_boolean('dbcache.file.gc')) { $w3_plugin_dbcache->unschedule(); } if (W3TC_PHP5 && $this->_config->get_boolean('minify.file.gc') != $config->get_boolean('minify.file.gc')) { $w3_plugin_minify->unschedule(); } $this->_config->load(); /** * Schedule events */ $w3_plugin_pgcache->schedule(); $w3_plugin_dbcache->schedule(); $w3_plugin_cdn->schedule(); if (W3TC_PHP5) { $w3_plugin_minify->schedule(); } /** * Update support us option */ $this->link_update(); /** * Auto upload minify files to CDN */ if ($this->_tab == 'minify' && $this->_config->get_boolean('minify.upload') && $this->_config->get_boolean('cdn.enabled') && $this->_config->get_string('cdn.engine') != 'mirror') { $this->cdn_upload_minify(); } /** * Write page cache rewrite rules */ if ($this->_tab == 'general' || $this->_tab == 'pgcache') { $is_wpmu = w3_is_wpmu(); if ($this->_config->get_boolean('pgcache.enabled') && $this->_config->get_string('pgcache.engine') == 'file_pgcache') { if (!$is_wpmu) { $w3_plugin_pgcache->write_rules_core(); } $w3_plugin_pgcache->write_rules_cache(); } else { if (!$is_wpmu) { $w3_plugin_pgcache->remove_rules_core(); } $w3_plugin_pgcache->remove_rules_cache(); } } /** * Write minify rewrite rules */ if (W3TC_PHP5 && ($this->_tab == 'general' || $this->_tab == 'minify')) { if ($this->_config->get_boolean('minify.enabled') && $this->_config->get_boolean('minify.rewrite')) { $w3_plugin_minify->write_rules(); } else { require_once W3TC_DIR . '/lib/W3/Plugin/Minify.php'; $w3_plugin_minify->remove_rules(); } } $this->redirect(array_merge($params, array('note' => 'config_save'))); } else { $this->redirect(array_merge($params, array('error' => 'config_save'))); } }