/** * Runs minify */ function process() { require_once W3TC_LIB_MINIFY_DIR . '/Minify.php'; require_once W3TC_LIB_MINIFY_DIR . '/HTTP/Encoder.php'; HTTP_Encoder::$encodeToIe6 = $this->_config->get_boolean('minify.comprss.ie6'); Minify::$uploaderHoursBehind = $this->_config->get_integer('minify.fixtime'); Minify::setCache($this->_get_cache()); $serve_options = $this->_config->get_array('minify.options'); $serve_options['maxAge'] = $this->_config->get_integer('minify.maxage'); $serve_options['encodeOutput'] = $this->_config->get_boolean('minify.compress'); $serve_options['postprocessor'] = array(&$this, 'postprocessor'); if (stripos(PHP_OS, 'win') === 0) { Minify::setDocRoot(); } foreach ($this->_config->get_array('minify.symlinks') as $link => $target) { $link = str_replace('//', realpath($_SERVER['DOCUMENT_ROOT']), $link); $link = strtr($link, '/', DIRECTORY_SEPARATOR); $serve_options['minifierOptions']['text/css']['symlinks'][$link] = realpath($target); } if ($this->_config->get_boolean('minify.debug')) { $serve_options['debug'] = true; } if ($this->_config->get('minify.debug')) { require_once W3TC_LIB_MINIFY_DIR . '/Minify/Logger.php'; Minify_Logger::setLogger($this); } if (isset($_GET['f']) || isset($_GET['gg']) && isset($_GET['g']) && isset($_GET['t'])) { if (isset($_GET['gg']) && isset($_GET['g']) && isset($_GET['t'])) { $serve_options['minApp']['groups'] = $this->get_groups($_GET['gg'], $_GET['t']); if ($_GET['t'] == 'js' && (in_array($_GET['g'], array('include', 'include-nb')) && $this->_config->get_boolean('minify.js.combine.header') || in_array($_GET['g'], array('include-footer', 'include-footer-nb')) && $this->_config->get_boolean('minify.js.combine.footer'))) { $serve_options['minifiers']['application/x-javascript'] = array($this, 'minify_stub'); } /** * Setup user-friendly cache ID for disk cache */ if ($this->_config->get_string('minify.engine') == 'file') { $cacheId = sprintf('%s.%s.%s', $_GET['gg'], $_GET['g'], $_GET['t']); Minify::setCacheId($cacheId); } } @header('Pragma: public'); try { Minify::serve('MinApp', $serve_options); } catch (Exception $exception) { printf('<strong>W3 Total Cache Error:</strong> Minify error: %s', $exception->getMessage()); } } else { die('This file cannot be accessed directly'); } }
/** * Runs minify * * @param string|null $file * * @return void */ function process($file = NULL) { w3_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; } $rewrite_test = W3_Request::get_string('test_file'); if ($rewrite_test) { $cache = $this->_get_cache(); header('Content-type: text/css'); if ($cache->store(basename($rewrite_test), 'content ok')) { if (function_exists('gzencode') && $this->_config->get_boolean('browsercache.enabled') && $this->_config->get_boolean('browsercache.cssjs.compression')) { if (!$cache->store(basename($rewrite_test) . '.gzip', gzencode('content ok'))) { echo 'error storing'; exit; } } if ($this->_config->get_string('minify.engine') != 'file') { if ($cache->fetch(basename($rewrite_test)) == 'content ok') { echo 'content ok'; } else { echo 'error storing'; } } else { echo 'retry'; } } else { echo 'error storing'; } exit; } if (is_null($file)) { $file = W3_Request::get_string('file'); } if (!$file) { $this->error('File param is missing', false); return; } // remove blog_id $levels = ''; if (defined('W3TC_BLOG_LEVELS')) { for ($n = 0; $n < W3TC_BLOG_LEVELS; $n++) { $levels .= '[0-9]+\\/'; } } if (preg_match('~^(' . $levels . '[0-9]+)\\/(.+)$~', $file, $matches)) { $file = $matches[2]; } // parse file $hash = ''; $matches = null; $location = ''; $type = ''; if (preg_match('~^' . MINIFY_AUTO_FILENAME_REGEX . '$~', $file, $matches)) { list(, $hash, $type) = $matches; } elseif (preg_match('~^' . MINIFY_MANUAL_FILENAME_REGEX . '$~', $file, $matches)) { list(, $theme, $template, $location, , , $type) = $matches; } else { $this->error(sprintf('Bad file param format: "%s"', $file), false); return; } w3_require_once(W3TC_LIB_MINIFY_DIR . '/Minify.php'); w3_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 */ w3_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_custom_files($hash, $type); } else { $_GET['g'] = $location; $serve_options['minApp']['groups'] = $this->get_groups($theme, $template, $type); } /** * Set minifier */ $w3_minifier = w3_instance('W3_Minifier'); if ($type == 'js') { $minifier_type = 'application/x-javascript'; switch (true) { case ($hash || $location == 'include') && $this->_config->get_boolean('minify.js.combine.header'): case $location == 'include-body' && $this->_config->get_boolean('minify.js.combine.body'): case $location == 'include-footer' && $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()); } if (!$this->_error_occurred && $this->_config_admin->get_boolean('notes.minify_error')) { $error_file = $this->_config_admin->get_string('minify.error.file'); if ($error_file == $file) { $this->_config_admin->set('notes.minify_error', false); $this->_config_admin->save(); } } }
/** * Runs minify * * @return void */ 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', false); return; } $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), false); return; } 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 */ $w3_minifier =& w3_instance('W3_Minifier'); 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()); } }
/** * Serves friendly minify url with minified contents * * This actually does a remote get from Minify's dir with appropriate `?f` * query variable built from persistently stored detected enqueued files. * All minified contents as well as headers should be preserved. * * Serving friendly minify urls like this cause a lot of overheads, but is * safer to use than server rewrite rules. * * @uses wp_remote_get() * @return void */ public function serve($query) { $group_handle = isset($_GET['min_group']) ? $this->_sanitize_request($_GET['min_group']) : ''; $type = isset($_GET['min_type']) && in_array($_GET['min_type'], array('js', 'css')) ? $_GET['min_type'] : ''; $bid = isset($_GET['blog']) ? (int) $_GET['blog'] : 0; if (empty($group_handle) || empty($type) || empty($bid)) { // if missing any important data, do not proceed return; } global $blog_id; if ($bid != $blog_id) { // if not on the correct blog, do not proceed return; } $group = $this->_detector->get_group($group_handle); if (false == $group) { // this should not happen, but just in case it does echo sprintf(__('Minify group %s not found.', $this->_domain), $group_handle); exit; } // load Minify class based on default or custom Minify directory $min_dir = $this->_main->get_min_dir(); if (empty($min_dir) || !file_exists($min_dir . 'config.php')) { // if Minify directory is not valid or Minify library is not found // we can not continue _e('Invalid Minify directory, please check Minify settings.', $this->_domain); exit; } // prepare input for Minify to handle $_GET['f'] = $group['string']; if ($this->_options['enable_debug'] == 'yes') { // add debug flag if needed $_GET['debug'] = 1; } else { // minified contents are often served compressed so it's best to turn off // error reporting to avoid content encoding error, unless debug is // enabled. This is useful when Minify doesn't catch all the notices // (for example when the cache directory is not writable). error_reporting(0); } // load Minify classes define('MINIFY_MIN_DIR', $min_dir); require_once MINIFY_MIN_DIR . '/config.php'; require_once $min_libPath . '/Minify/Loader.php'; Minify_Loader::register(); // set some optional for the Minify application based on current settings Minify::$uploaderHoursBehind = $min_uploaderHoursBehind; // set cache file name (id) and cache path if needed Minify::setCacheId('minify-b' . $blog_id . '-' . $group_handle . '.' . $type); Minify::setCache(isset($min_cachePath) ? $min_cachePath : '', $min_cacheFileLocking); if ($min_documentRoot) { $_SERVER['DOCUMENT_ROOT'] = $min_documentRoot; Minify::$isDocRootSet = true; } // set serve options for each file type if needed $min_serveOptions['minifierOptions']['text/css']['symlinks'] = $min_symlinks; foreach ($min_symlinks as $uri => $target) { $min_serveOptions['minApp']['allowDirs'][] = $target; } if ($min_allowDebugFlag) { // allow debugging Minify application $min_serveOptions['debug'] = Minify_DebugDetector::shouldDebugRequest($_COOKIE, $_GET, $_SERVER['REQUEST_URI']); } if ($min_errorLogger) { // log Minify error if allowed if (true === $min_errorLogger) { $min_errorLogger = FirePHP::getInstance(true); } Minify_Logger::setLogger($min_errorLogger); } // serve minified contents, on the fly or from cache $min_serveController = new Minify_Controller_MinApp(); Minify::serve($min_serveController, $min_serveOptions); // for a proper response headers exit; }