/** * Minifies HTML * * @param string $html * @return string */ function minify_html(&$html) { require_once W3TC_LIB_W3_DIR . '/Minifier.php'; $w3_minifier =& W3_Minifier::instance(); $ignored_comments = $this->_config->get_array('minify.html.comments.ignore'); if (count($ignored_comments)) { require_once W3TC_LIB_MINIFY_DIR . '/Minify/IgnoredCommentPreserver.php'; $ignored_comments_preserver =& new Minify_IgnoredCommentPreserver(); $ignored_comments_preserver->setIgnoredComments($ignored_comments); $ignored_comments_preserver->search($html); } if ($this->_config->get_boolean('minify.html.inline.js')) { $js_engine = $this->_config->get_string('minify.js.engine'); if (!$w3_minifier->exists($js_engine) || !$w3_minifier->available($js_engine)) { $js_engine = 'js'; } $js_minifier = $w3_minifier->get_minifier($js_engine); $js_options = $w3_minifier->get_options($js_engine); $w3_minifier->init($js_engine); require_once W3TC_LIB_MINIFY_DIR . '/Minify/Inline.php'; require_once W3TC_LIB_MINIFY_DIR . '/Minify/Inline/JavaScript.php'; $html = Minify_Inline_JavaScript::minify($html, $js_minifier, $js_options); } if ($this->_config->get_boolean('minify.html.inline.css')) { $css_engine = $this->_config->get_string('minify.css.engine'); if (!$w3_minifier->exists($css_engine) || !$w3_minifier->available($css_engine)) { $css_engine = 'css'; } $css_minifier = $w3_minifier->get_minifier($css_engine); $css_options = $w3_minifier->get_options($css_engine); $w3_minifier->init($css_engine); require_once W3TC_LIB_MINIFY_DIR . '/Minify/Inline.php'; require_once W3TC_LIB_MINIFY_DIR . '/Minify/Inline/CSS.php'; $html = Minify_Inline_CSS::minify($html, $css_minifier, $css_options); } $engine = $this->_config->get_string('minify.html.engine'); if (!$w3_minifier->exists($engine) || !$w3_minifier->available($engine)) { $engine = 'html'; } if (function_exists('is_feed') && is_feed()) { $engine .= 'xml'; } $minifier = $w3_minifier->get_minifier($engine); $options = $w3_minifier->get_options($engine); $w3_minifier->init($engine); $html = call_user_func($minifier, $html, $options); if (isset($ignored_comments_preserver)) { $ignored_comments_preserver->replace($html); } }
/** * Runs minify */ function process() { require_once W3TC_LIB_W3_DIR . '/Request.php'; /** * Check for rewrite test request */ $rewrite_test = W3_Request::get_boolean('w3tc_rewrite_test'); if ($rewrite_test) { echo 'OK'; exit; } $file = W3_Request::get_string('file'); if (!$file) { $this->error('File param is missing'); } $hash = ''; $matches = null; if (preg_match('~^([a-f0-9]+)\\.[a-f0-9]+\\.(css|js)$~', $file, $matches)) { list(, $hash, $type) = $matches; } elseif (preg_match('~^([a-f0-9]+)\\/(.+)\\.(include(\\-(footer|body))?(-nb)?)\\.[a-f0-9]+\\.(css|js)$~', $file, $matches)) { list(, $theme, $template, $location, , , , $type) = $matches; } else { $this->error(sprintf('Bad file param format: "%s"', $file)); } require_once W3TC_LIB_MINIFY_DIR . '/Minify.php'; require_once W3TC_LIB_MINIFY_DIR . '/HTTP/Encoder.php'; /** * Fix DOCUMENT_ROOT */ $_SERVER['DOCUMENT_ROOT'] = w3_get_document_root(); /** * Set cache engine */ Minify::setCache($this->_get_cache()); /** * Set cache ID */ $cache_id = $this->get_cache_id($file); Minify::setCacheId($cache_id); /** * Set logger */ require_once W3TC_LIB_MINIFY_DIR . '/Minify/Logger.php'; Minify_Logger::setLogger(array(&$this, 'error')); /** * Set options */ $browsercache = $this->_config->get_boolean('browsercache.enabled'); $serve_options = array_merge($this->_config->get_array('minify.options'), array('debug' => $this->_config->get_boolean('minify.debug'), 'maxAge' => $this->_config->get_integer('browsercache.cssjs.lifetime'), 'encodeOutput' => $browsercache && $this->_config->get_boolean('browsercache.cssjs.compression'), 'bubbleCssImports' => $this->_config->get_string('minify.css.imports') == 'bubble', 'processCssImports' => $this->_config->get_string('minify.css.imports') == 'process', 'cacheHeaders' => array('use_etag' => $browsercache && $this->_config->get_boolean('browsercache.cssjs.etag'), 'expires_enabled' => $browsercache && $this->_config->get_boolean('browsercache.cssjs.expires'), 'cacheheaders_enabled' => $browsercache && $this->_config->get_boolean('browsercache.cssjs.cache.control'), 'cacheheaders' => $this->_config->get_string('browsercache.cssjs.cache.policy')))); /** * Set sources */ if ($hash) { $_GET['f'] = $this->get_files($hash, $type); } else { $_GET['g'] = $location; $serve_options['minApp']['groups'] = $this->get_groups($theme, $template, $type); } /** * Set minifier */ require_once W3TC_LIB_W3_DIR . '/Minifier.php'; $w3_minifier =& W3_Minifier::instance(); if ($type == 'js') { $minifier_type = 'application/x-javascript'; switch (true) { case ($hash || $location == 'include' || $location == 'include-nb') && $this->_config->get_boolean('minify.js.combine.header'): case ($location == 'include-body' || $location == 'include-body-nb') && $this->_config->get_boolean('minify.js.combine.body'): case ($location == 'include-footer' || $location == 'include-footer-nb') && $this->_config->get_boolean('minify.js.combine.footer'): $engine = 'combinejs'; break; default: $engine = $this->_config->get_string('minify.js.engine'); if (!$w3_minifier->exists($engine) || !$w3_minifier->available($engine)) { $engine = 'js'; } break; } } elseif ($type == 'css') { $minifier_type = 'text/css'; if (($hash || $location == 'include') && $this->_config->get_boolean('minify.css.combine')) { $engine = 'combinecss'; } else { $engine = $this->_config->get_string('minify.css.engine'); if (!$w3_minifier->exists($engine) || !$w3_minifier->available($engine)) { $engine = 'css'; } } } /** * Initialize minifier */ $w3_minifier->init($engine); $serve_options['minifiers'][$minifier_type] = $w3_minifier->get_minifier($engine); $serve_options['minifierOptions'][$minifier_type] = $w3_minifier->get_options($engine); /** * Send X-Powered-By header */ if ($browsercache && $this->_config->get_boolean('browsercache.cssjs.w3tc')) { @header('X-Powered-By: ' . W3TC_POWERED_BY); } /** * Minify! */ try { Minify::serve('MinApp', $serve_options); } catch (Exception $exception) { $this->error($exception->getMessage()); } }