/** * Minify Javascript * * @param string $js Javascript to be minified * @return string */ public static function minify($js) { $jsmin = new VesTempcp_JSMin($js); return $jsmin->min(); }
public function compressJsCss($assets = array()) { $request = Mage::app()->getFrontController()->getRequest(); $module = $request->getModuleName(); $controller = $request->getControllerName(); $action = $request->getActionName(); //If Cart Page or Checkout Page do not compress if ($module == 'checkout' && ($controller == 'cart' || $controller == 'onepage')) { return $assets; } $enable_compress_css = $this->getConfig()->get("enable_compress_css"); $enable_compress_js = $this->getConfig()->get("enable_compress_js"); $tmp_css = array(); $tmp_js = array(); $tmp_assets = array(); $js_excludes = $excludes = array(); if ($enable_compress_js) { $exclude_js_files = $this->getConfig()->get("exclude_js_files"); $exclude_js_files = trim($exclude_js_files); $js_excludes = explode(",", $exclude_js_files); if (!empty($js_excludes)) { foreach ($js_excludes as $k => $item) { $js_excludes[$k] = trim($item); } } } if ($enable_compress_css) { $exclude_css_files = $this->getConfig()->get("exclude_css_files"); $exclude_css_files = trim($exclude_css_files); $excludes = explode(",", $exclude_css_files); if (!empty($excludes)) { foreach ($excludes as $k => $item) { $excludes[$k] = trim($item); } } } foreach ($assets as $key => $item) { if (!isset($item['name'])) { continue; } $if = !empty($item['if']) ? $item['if'] : ''; $params = !empty($item['params']) ? $item['params'] : ''; switch ($item['type']) { case 'js': // js/*.js if ($enable_compress_js) { if (!in_array("js/" . $item['name'], $js_excludes)) { $tmp_js[] = array("dir" => Mage::getBaseDir() . "/js/" . $item['name'], "url" => Mage::getBaseUrl('js') . $item['name']); } else { $tmp_assets[] = $item; } } else { $tmp_assets[] = $item; } break; case 'skin_js': // skin/*/*.js if ($enable_compress_js) { if (!in_array("skin_js/" . $item['name'], $js_excludes)) { if (file_exists($this->getThemeDir() . "/" . $item['name'])) { $tmp_js[] = array("dir" => $this->getThemeDir() . "/" . $item['name'], "url" => $this->getThemeURL() . "/" . $item['name']); } elseif ($skin_js = $this->getSkinJs($item['name'])) { $tmp_js[] = array("dir" => $skin_js['dir'], "url" => $skin_js['url']); } } else { $tmp_assets[] = $item; } } else { $tmp_assets[] = $item; } break; case 'skin_css': if ($enable_compress_css) { if (!in_array("skin_css/" . $item['name'], $excludes) && (!$item['params'] || $item['params'] == 'media="all"') && file_exists($this->getThemeDir() . "/" . $item['name'])) { $tmp_css[] = array("dir" => $this->getThemeDir() . "/" . $item['name'], "url" => $this->getThemeURL() . "/" . $item['name']); } else { $tmp_assets[] = $item; } } else { $tmp_assets[] = $item; } break; case 'js_css': if ($enable_compress_css) { if (!in_array("js_css/" . $item['name'], $excludes) && (!$item['params'] || $item['params'] == 'media="all"')) { $tmp_css[] = array("dir" => Mage::getBaseDir() . "/js/" . $item['name'], "url" => Mage::getBaseUrl('js') . $item['name']); } else { $tmp_assets[] = $item; } } else { $tmp_assets[] = $item; } break; default: $tmp_assets[] = $item; break; } } /*Compress Js*/ if (!empty($tmp_js) && trim($enable_compress_js)) { $pcache = new VesTempcp_Cache(); $pcache->setExtension('js'); if ($enable_compress_js == 'compress-merge') { $all = ''; $aKey = md5(serialize($tmp_js) . serialize($js_excludes) . Mage::getBaseUrl()); if (!$pcache->isExisted($aKey)) { foreach ($tmp_js as $key => $file) { if (is_array($file)) { $content = $pcache->read($file['dir']); if (!empty($content)) { //$content = VesTempcp_JSMin::minify($content); $all .= ";" . $content; } } } $all = VesTempcp_JSMin::minify($all); $pcache->set($aKey, $all); } $tmp_assets["skin_js/cache/" . md5($key) . ".js"] = array("type" => "skin_js", "name" => "cache/" . $aKey . ".js", "params" => '', "if" => "", "cond" => ""); } else { foreach ($tmp_js as $key => $file) { if (is_array($file)) { $check_existed = false; if (!$pcache->isExisted(md5($key))) { $content = $pcache->read($file['dir']); if (!empty($content)) { $content = VesTempcp_JSMin::minify($content); $pcache->set(md5($key), $content); $check_existed = true; } } else { $check_existed = true; } if ($check_existed) { $tmp_assets["skin_js/cache/" . md5($key) . ".js"] = array("type" => "skin_js", "name" => "cache/" . md5($key) . ".js", "params" => '', "if" => "", "cond" => ""); } } } } } /*Compress css*/ if (!empty($tmp_css) && trim($enable_compress_css)) { $pcache = new VesTempcp_Cache(); $pcache->setExtension('css'); if ($enable_compress_css == 'compress-merge') { $all = ''; $aKey = md5(serialize($tmp_css) . serialize($excludes) . Mage::getBaseUrl()); if (!$pcache->isExisted($aKey)) { foreach ($tmp_css as $key => $file) { if (is_array($file)) { $content = $pcache->read($file['dir']); if (!empty($content)) { $content = VesTempcp_Csscompressor::process($content, $file['url']); $all .= $content; } } } $pcache->set($aKey, $all); } $tmp_assets["skin_css/cache/" . md5($key) . ".css"] = array("type" => "skin_css", "name" => "cache/" . $aKey . ".css", "params" => 'media="all"', "if" => "", "cond" => ""); } else { foreach ($tmp_css as $key => $file) { if (is_array($file)) { $check_existed = false; if (!$pcache->isExisted(md5($key))) { $content = $pcache->read($file['dir']); if (!empty($content)) { $content = VesTempcp_Csscompressor::process($content, $file['url']); $pcache->set(md5($key), $content); $check_existed = true; } } else { $check_existed = true; } if ($check_existed) { $tmp_assets["skin_css/cache/" . md5($key) . ".css"] = array("type" => "skin_css", "name" => "cache/" . md5($key) . ".css", "params" => 'media="all"', "if" => "", "cond" => ""); } } } } } if (!empty($tmp_assets)) { return $tmp_assets; } return $assets; }