/** * Fixes environment once event occurs * * @param Config $config * @param string $event * @param Config|null $old_config * @throws Util_Environment_Exceptions */ public function fix_on_event($config, $event, $old_config = null) { if ($config->get_boolean('cdn.enabled') && !Cdn_Util::is_engine_mirror($config->get_string('cdn.engine'))) { if ($old_config != null && $config->get_integer('cdn.queue.interval') != $old_config->get_integer('cdn.queue.interval')) { $this->unschedule_queue_process(); } if (!wp_next_scheduled('w3_cdn_cron_queue_process')) { wp_schedule_event(time(), 'w3_cdn_cron_queue_process', 'w3_cdn_cron_queue_process'); } } else { $this->unschedule_queue_process(); } if ($config->get_boolean('cdn.enabled') && $config->get_boolean('cdn.autoupload.enabled') && !Cdn_Util::is_engine_mirror($config->get_string('cdn.engine'))) { if ($old_config != null && $config->get_integer('cdn.autoupload.interval') != $old_config->get_integer('cdn.autoupload.interval')) { $this->unschedule_upload(); } if (!wp_next_scheduled('w3_cdn_cron_upload')) { wp_schedule_event(time(), 'w3_cdn_cron_upload', 'w3_cdn_cron_upload'); } } else { $this->unschedule_upload(); } $exs = new Util_Environment_Exceptions(); if ($config->get_boolean('cdn.enabled')) { try { $this->table_create($event == 'activate'); } catch (\Exception $ex) { $exs->push($ex); } } if (count($exs->exceptions()) > 0) { throw $exs; } }
/** * Generates directives for file cache dir * * @param Config $config * @return string */ private function rules_cache_generate_nginx($config) { $cache_dir = Util_Rule::filename_to_uri(W3TC_CACHE_MINIFY_DIR); $browsercache = $config->get_boolean('browsercache.enabled'); $compression = $browsercache && $config->get_boolean('browsercache.cssjs.compression'); $expires = $browsercache && $config->get_boolean('browsercache.cssjs.expires'); $lifetime = $browsercache ? $config->get_integer('browsercache.cssjs.lifetime') : 0; $cache_control = $browsercache && $config->get_boolean('browsercache.cssjs.cache.control'); $w3tc = $browsercache && $config->get_integer('browsercache.cssjs.w3tc'); $rules = ''; $rules .= W3TC_MARKER_BEGIN_MINIFY_CACHE . "\n"; $common_rules = ''; if ($expires) { $common_rules .= " expires modified " . $lifetime . "s;\n"; } if ($w3tc) { $common_rules .= " add_header X-Powered-By \"" . Util_Environment::w3tc_header($config) . "\";\n"; } if ($compression) { $common_rules .= " add_header Vary \"Accept-Encoding\";\n"; } if ($cache_control) { $cache_policy = $config->get_string('browsercache.cssjs.cache.policy'); switch ($cache_policy) { case 'cache': $common_rules .= " add_header Pragma \"public\";\n"; $common_rules .= " add_header Cache-Control \"public\";\n"; break; case 'cache_public_maxage': $common_rules .= " add_header Pragma \"public\";\n"; $common_rules .= " add_header Cache-Control \"max-age=" . $lifetime . ", public\";\n"; break; case 'cache_validation': $common_rules .= " add_header Pragma \"public\";\n"; $common_rules .= " add_header Cache-Control \"public, must-revalidate, proxy-revalidate\";\n"; break; case 'cache_noproxy': $common_rules .= " add_header Pragma \"public\";\n"; $common_rules .= " add_header Cache-Control \"private, must-revalidate\";\n"; break; case 'cache_maxage': $common_rules .= " add_header Pragma \"public\";\n"; $common_rules .= " add_header Cache-Control \"max-age=" . $lifetime . ", public, must-revalidate, proxy-revalidate\";\n"; break; case 'no_cache': $common_rules .= " add_header Pragma \"no-cache\";\n"; $common_rules .= " add_header Cache-Control \"max-age=0, private, no-store, no-cache, must-revalidate\";\n"; break; } } $rules .= "location ~ " . $cache_dir . ".*\\.js\$ {\n"; $rules .= " types {}\n"; $rules .= " default_type application/x-javascript;\n"; $rules .= $common_rules; $rules .= "}\n"; $rules .= "location ~ " . $cache_dir . ".*\\.css\$ {\n"; $rules .= " types {}\n"; $rules .= " default_type text/css;\n"; $rules .= $common_rules; $rules .= "}\n"; if ($compression) { $rules .= "location ~ " . $cache_dir . ".*js\\.gzip\$ {\n"; $rules .= " gzip off;\n"; $rules .= " types {}\n"; $rules .= " default_type application/x-javascript;\n"; $rules .= $common_rules; $rules .= " add_header Content-Encoding gzip;\n"; $rules .= "}\n"; $rules .= "location ~ " . $cache_dir . ".*css\\.gzip\$ {\n"; $rules .= " gzip off;\n"; $rules .= " types {}\n"; $rules .= " default_type text/css;\n"; $rules .= $common_rules; $rules .= " add_header Content-Encoding gzip;\n"; $rules .= "}\n"; } $rules .= W3TC_MARKER_END_MINIFY_CACHE . "\n"; return $rules; }
/** * Adds cache rules for type to &$rules * * @param Config $config * @param string $rules * @param array $mime_types * @param string $section * @return void */ private function _rules_cache_generate_nginx_for_type($config, &$rules, $mime_types, $section) { $expires = $config->get_boolean('browsercache.' . $section . '.expires'); $cache_control = $config->get_boolean('browsercache.' . $section . '.cache.control'); $w3tc = $config->get_boolean('browsercache.' . $section . '.w3tc'); if ($expires || $cache_control || $w3tc) { $lifetime = $config->get_integer('browsercache.' . $section . '.lifetime'); $extensions = array_keys($mime_types); // Remove ext from filesmatch if its the same as permalink extension $pext = strtolower(pathinfo(get_option('permalink_structure'), PATHINFO_EXTENSION)); if ($pext) { $extensions = $this->_remove_extension_from_list($extensions, $pext); } $rules .= "location ~ \\.(" . implode('|', $extensions) . ")\$ {\n"; if ($expires) { $rules .= " expires " . $lifetime . "s;\n"; } $add_header_rules = ''; if ($cache_control) { $cache_policy = $config->get_string('browsercache.' . $section . '.cache.policy'); switch ($cache_policy) { case 'cache': $add_header_rules .= " add_header Pragma \"public\";\n"; $add_header_rules .= " add_header Cache-Control \"public\";\n"; break; case 'cache_public_maxage': $add_header_rules .= " add_header Pragma \"public\";\n"; $add_header_rules .= " add_header Cache-Control \"max-age=" . $lifetime . ", public\";\n"; break; case 'cache_validation': $add_header_rules .= " add_header Pragma \"public\";\n"; $add_header_rules .= " add_header Cache-Control \"public, must-revalidate, proxy-revalidate\";\n"; break; case 'cache_noproxy': $add_header_rules .= " add_header Pragma \"public\";\n"; $add_header_rules .= " add_header Cache-Control \"private, must-revalidate\";\n"; break; case 'cache_maxage': $add_header_rules .= " add_header Pragma \"public\";\n"; $add_header_rules .= " add_header Cache-Control \"max-age=" . $lifetime . ", public, must-revalidate, proxy-revalidate\";\n"; break; case 'no_cache': $add_header_rules .= " add_header Pragma \"no-cache\";\n"; $add_header_rules .= " add_header Cache-Control \"max-age=0, private, no-store, no-cache, must-revalidate\";\n"; break; } } if ($w3tc) { $add_header_rules .= " add_header X-Powered-By \"" . Util_Environment::w3tc_header($config) . "\";\n"; } $rules .= $add_header_rules; $rules .= Dispatcher::on_browsercache_rules_generation_for_section($config, false, $section, $add_header_rules); if (!$config->get_boolean('browsercache.no404wp')) { $wp_uri = network_home_url('', 'relative'); $wp_uri = rtrim($wp_uri, '/'); $rules .= ' try_files $uri $uri/ $uri.html ' . $wp_uri . '/index.php?$args;' . "\n"; } $rules .= "}\n"; } }
/** * Generates directives for file cache dir * * @param Config $config * @return string */ private function rules_cache_generate_nginx($config) { $cache_root = Util_Environment::normalize_path(W3TC_CACHE_PAGE_ENHANCED_DIR); $cache_dir = rtrim(str_replace(Util_Environment::document_root(), '', $cache_root), '/'); if (Util_Environment::is_wpmu()) { $cache_dir = preg_replace('~/w3tc.*?/~', '/w3tc.*?/', $cache_dir, 1); } $browsercache = $config->get_boolean('browsercache.enabled'); $compression = $browsercache && $config->get_boolean('browsercache.html.compression'); $expires = $browsercache && $config->get_boolean('browsercache.html.expires'); $lifetime = $browsercache ? $config->get_integer('browsercache.html.lifetime') : 0; $cache_control = $browsercache && $config->get_boolean('browsercache.html.cache.control'); $w3tc = $browsercache && $config->get_integer('browsercache.html.w3tc'); $common_rules = ''; if ($expires) { $common_rules .= " expires modified " . $lifetime . "s;\n"; } if ($w3tc) { $common_rules .= " add_header X-Powered-By \"" . Util_Environment::w3tc_header($config) . "\";\n"; } if ($expires) { $common_rules .= " add_header Vary \"Accept-Encoding, Cookie\";\n"; } if ($cache_control) { $cache_policy = $config->get_string('browsercache.html.cache.policy'); switch ($cache_policy) { case 'cache': $common_rules .= " add_header Pragma \"public\";\n"; $common_rules .= " add_header Cache-Control \"public\";\n"; break; case 'cache_public_maxage': $common_rules .= " add_header Pragma \"public\";\n"; $common_rules .= " add_header Cache-Control \"max-age=" . $lifetime . ", public\";\n"; break; case 'cache_validation': $common_rules .= " add_header Pragma \"public\";\n"; $common_rules .= " add_header Cache-Control \"public, must-revalidate, proxy-revalidate\";\n"; break; case 'cache_noproxy': $common_rules .= " add_header Pragma \"public\";\n"; $common_rules .= " add_header Cache-Control \"private, must-revalidate\";\n"; break; case 'cache_maxage': $common_rules .= " add_header Pragma \"public\";\n"; $common_rules .= " add_header Cache-Control \"max-age=" . $lifetime . ", public, must-revalidate, proxy-revalidate\";\n"; break; case 'no_cache': $common_rules .= " add_header Pragma \"no-cache\";\n"; $common_rules .= " add_header Cache-Control \"max-age=0, private, no-store, no-cache, must-revalidate\";\n"; break; } } $rules = ''; $rules .= W3TC_MARKER_BEGIN_PGCACHE_CACHE . "\n"; $rules .= "location ~ " . $cache_dir . ".*html\$ {\n"; $rules .= $common_rules; $rules .= "}\n"; if ($compression) { $rules .= "location ~ " . $cache_dir . ".*gzip\$ {\n"; $rules .= " gzip off;\n"; $rules .= " types {}\n"; $rules .= " default_type text/html;\n"; $rules .= $common_rules; $rules .= " add_header Content-Encoding gzip;\n"; $rules .= "}\n"; } $rules .= W3TC_MARKER_END_PGCACHE_CACHE . "\n"; return $rules; }
/** * Check config file * * @param Config $config * @param Util_Environment_Exceptions $exs */ private function notify_no_config_present($config, $exs) { if ((file_exists(Config::util_config_filename(0, false)) || file_exists(Config::util_config_filename_legacy(0, false))) && $config->get_integer('common.instance_id', 0) != 0) { return; } $onclick = 'document.location.href=\'' . addslashes(wp_nonce_url('admin.php?page=w3tc_general&w3tc_save_options', 'w3tc')) . '\';'; $button = '<input type="button" class="button w3tc" ' . 'value="save the settings" onclick="' . $onclick . '" />'; $exs->push(new Util_Environment_Exception('<strong>W3 Total Cache:</strong> ' . 'Default settings are in use. The configuration file could ' . 'not be read or doesn\'t exist. Please ' . $button . ' to create the file.')); }