/** * Returns true if we can minify * * @param string $buffer * @return string */ function can_minify2($buffer) { /** * Check for database error */ if (Util_Content::is_database_error($buffer)) { $this->minify_reject_reason = 'Database Error occurred'; return false; } /** * Check for DONOTMINIFY constant */ if (defined('DONOTMINIFY') && DONOTMINIFY) { $this->minify_reject_reason = 'DONOTMINIFY constant is defined'; return false; } /** * Check feed minify */ if ($this->_config->get_boolean('minify.html.reject.feed') && function_exists('is_feed') && is_feed()) { $this->minify_reject_reason = 'Feed is rejected'; return false; } return true; }
/** * Returns headers for file * * @param array $file CDN file array * @return array */ function _get_headers($file, $block_expires = false) { $local_path = $file['local_path']; $mime_type = Util_Mime::get_mime_type($local_path); $last_modified = time(); $link = $file['original_url']; $headers = array('Content-Type' => $mime_type, 'Last-Modified' => Util_Content::http_date($last_modified), 'Access-Control-Allow-Origin' => '*', 'Link' => '<' . $link . '>; rel="canonical"'); if (isset($this->cache_config[$mime_type])) { if ($this->cache_config[$mime_type]['etag']) { $headers['ETag'] = '"' . @md5_file($local_path) . '"'; } if ($this->cache_config[$mime_type]['w3tc']) { $headers['X-Powered-By'] = Util_Environment::w3tc_header($this->_config); } $expires_set = false; if (!$block_expires && $this->cache_config[$mime_type]['expires']) { $headers['Expires'] = Util_Content::http_date(time() + $this->cache_config[$mime_type]['lifetime']); $expires_set = true; } switch ($this->cache_config[$mime_type]['cache_control']) { case 'cache': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'public')); break; case 'cache_public_maxage': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => ($expires_set ? '' : 'max-age=' . $this->cache_config[$mime_type]['lifetime'] . ', ') . 'public')); break; case 'cache_validation': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'public, must-revalidate, proxy-revalidate')); break; case 'cache_noproxy': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'private, must-revalidate')); break; case 'cache_maxage': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => ($expires_set ? '' : 'max-age=' . $this->cache_config[$mime_type]['lifetime'] . ', ') . 'public, must-revalidate, proxy-revalidate')); break; case 'no_cache': $headers = array_merge($headers, array('Pragma' => 'no-cache', 'Cache-Control' => 'max-age=0, private, no-store, no-cache, must-revalidate')); break; } } return $headers; }
function get_replaced_urls() { $strings = array(); if (count($this->_replaced_urls)) { $strings[] = "Replaced URLs:"; foreach ($this->_replaced_urls as $old_url => $new_url) { $strings[] = sprintf("%s => %s", Util_Content::escape_comment($old_url), Util_Content::escape_comment($new_url)); } } return $strings; }
/** * Output buffer callback * * @param string $buffer * @return mixed */ function ob_callback($buffer) { if ($buffer != '' && Util_Content::is_html($buffer)) { $domain_url_regexp = Util_Environment::home_domain_root_url_regexp(); $buffer = preg_replace_callback('~(href|src|action|extsrc|asyncsrc|w3tc_load_js\\()=?([\'"])((' . $domain_url_regexp . ')?(/[^\'"]*\\.([a-z-_]+)(\\?[^\'"]*)?))[\'"]~Ui', array($this, 'link_replace_callback'), $buffer); } return $buffer; }
/** * Output buffering callback * * @param string $buffer * @return string */ function ob_callback($buffer) { global $wpdb; global $w3_late_caching_succeeded; if ($w3_late_caching_succeeded) { return $buffer; } if (Util_Content::is_database_error($buffer)) { status_header(503); } else { if (Util_Content::can_print_comment($buffer)) { /** * Add footer comment */ $date = date_i18n('Y-m-d H:i:s'); $host = !empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost'; if (Util_Environment::is_preview_mode()) { $buffer .= "\r\n<!-- W3 Total Cache used in preview mode -->"; } if ($this->_config->get_string('common.support') != '' || $this->_config->get_boolean('common.tweeted')) { $buffer .= sprintf("\r\n<!-- Served from: %s @ %s by W3 Total Cache -->", Util_Content::escape_comment($host), $date); } else { $strings = array(); $strings = apply_filters('w3tc_footer_comment', $strings); $buffer .= "\r\n<!-- Performance optimized by W3 Total Cache. Learn more: https://www.w3-edge.com/products/\r\n"; if (count($strings)) { $buffer .= "\r\n" . Util_Content::escape_comment(implode("\r\n", $strings)) . "\r\n"; } $buffer .= sprintf("\r\n Served from: %s @ %s by W3 Total Cache -->", Util_Content::escape_comment($host), $date); } } $buffer = Util_Bus::do_ob_callbacks(array('minify', 'newrelic', 'cdn', 'browsercache', 'pagecache'), $buffer); } return $buffer; }
/** * Sends headers * * @param boolean $is_404 * @param string $etag * @param integer $time * @param string $compression * @param array $custom_headers * @return boolean */ function _send_headers($is_404, $time, $etag, $compression, $custom_headers = array()) { $exit = false; $headers = is_array($custom_headers) ? $custom_headers : array(); $curr_time = time(); $bc_lifetime = $this->_config->get_integer('browsercache.html.lifetime'); $expires = (is_null($time) ? $curr_time : $time) + $bc_lifetime; $max_age = $expires > $curr_time ? $expires - $curr_time : 0; if ($is_404) { /** * Add 404 header */ $headers = array_merge($headers, array('Status' => 'HTTP/1.1 404 Not Found')); } elseif (!is_null($time) && $this->_check_modified_since($time) || $this->_check_match($etag)) { /** * Add 304 header */ $headers = array_merge($headers, array('Status' => 'HTTP/1.1 304 Not Modified')); /** * Don't send content if it isn't modified */ $exit = true; } if ($this->_config->get_boolean('browsercache.enabled')) { if ($this->_config->get_boolean('browsercache.html.last_modified')) { $headers = array_merge($headers, array('Last-Modified' => Util_Content::http_date($time))); } if ($this->_config->get_boolean('browsercache.html.expires')) { $headers = array_merge($headers, array('Expires' => Util_Content::http_date($expires))); } if ($this->_config->get_boolean('browsercache.html.cache.control')) { switch ($this->_config->get_string('browsercache.html.cache.policy')) { case 'cache': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'public')); break; case 'cache_public_maxage': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => sprintf('max-age=%d, public', $max_age))); break; case 'cache_validation': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'public, must-revalidate, proxy-revalidate')); break; case 'cache_noproxy': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => 'private, must-revalidate')); break; case 'cache_maxage': $headers = array_merge($headers, array('Pragma' => 'public', 'Cache-Control' => sprintf('max-age=%d, public, must-revalidate, proxy-revalidate', $max_age))); break; case 'no_cache': $headers = array_merge($headers, array('Pragma' => 'no-cache', 'Cache-Control' => 'max-age=0, private, no-store, no-cache, must-revalidate')); break; } } if ($this->_config->get_boolean('browsercache.html.etag')) { $headers = array_merge($headers, array('ETag' => '"' . $etag . '"')); } if ($this->_config->get_boolean('browsercache.html.w3tc')) { $headers = array_merge($headers, array('X-Powered-By' => Util_Environment::w3tc_header())); } } $vary = ''; //compressed && UAG if ($compression && $this->_get_mobile_group()) { $vary = 'Accept-Encoding,User-Agent,Cookie'; $headers = array_merge($headers, array('Content-Encoding' => $compression)); //compressed } elseif ($compression) { $vary = 'Accept-Encoding'; $headers = array_merge($headers, array('Content-Encoding' => $compression)); //uncompressed && UAG } elseif ($this->_get_mobile_group()) { $vary = 'User-Agent,Cookie'; } //Add Cookie to vary if user logged in and not previously set if (!$this->_check_logged_in() && strpos($vary, 'Cookie') === false) { if ($vary) { $vary .= ',Cookie'; } else { $vary = 'Cookie'; } } /** * Add vary header */ if ($vary) { $headers = array_merge($headers, array('Vary' => $vary)); } /** * Disable caching for preview mode */ if (Util_Environment::is_preview_mode()) { $headers = array_merge($headers, array('Pragma' => 'private', 'Cache-Control' => 'private')); } /** * Send headers to client */ $result = $this->_headers($headers); if ($exit) { exit; } return $result; }