public function minify() { foreach ($this->css as $group) { list($media, $css) = $group; if (preg_match('#^INLINE;#', $css)) { //<style> $css = preg_replace('#^INLINE;#', '', $css); $css = $this->fixurls(ABSPATH . '/index.php', $css); } else { //<link> if ($css !== false && file_exists($css) && is_readable($css)) { $css = $this->fixurls($css, file_get_contents($css)); } else { //Couldn't read CSS. Maybe getpath isn't working? $css = ''; } } foreach ($media as $elem) { if (!isset($this->csscode[$elem])) { $this->csscode[$elem] = ''; } $this->csscode[$elem] .= "\n/*FILESTART*/" . $css; } } //Check for duplicate code $md5list = array(); $tmpcss = $this->csscode; foreach ($tmpcss as $media => $code) { $md5sum = md5($code); $medianame = $media; foreach ($md5list as $med => $sum) { //If same code if ($sum === $md5sum) { //Add the merged code $medianame = $med . ', ' . $media; $this->csscode[$medianame] = $code; $md5list[$medianame] = $md5list[$med]; unset($this->csscode[$med], $this->csscode[$media]); unset($md5list[$med]); } } $md5list[$medianame] = $md5sum; } unset($tmpcss); //Manage @imports, while is for recursive import management foreach ($this->csscode as &$thiscss) { //Flag to trigger import reconstitution $fiximports = false; while (preg_match_all('#@import.*(?:;|$)#Um', $thiscss, $matches)) { foreach ($matches[0] as $import) { $url = trim(preg_replace('#^.*((?:https?|ftp)://.*\\.css).*$#', '$1', $import), " \t\n\r\v\"'"); $path = $this->getpath($url); if (file_exists($path) && is_readable($path)) { $code = addcslashes($this->fixurls($path, file_get_contents($path)), "\\"); $thiscss = preg_replace('#(/\\*FILESTART\\*/.*)' . preg_quote($import, '#') . '#Us', '/*FILESTART2*/' . $code . '$1', $thiscss); } else { //getpath is not working? //Encode so preg_match doesn't see it $thiscss = str_replace($import, '/*IMPORT*/' . base64_encode($import) . '/*IMPORT*/', $thiscss); $fiximports = true; } } $thiscss = preg_replace('#/\\*FILESTART\\*/#', '', $thiscss); $thiscss = preg_replace('#/\\*FILESTART2\\*/#', '/*FILESTART*/', $thiscss); } //Recover imports if ($fiximports) { $thiscss = preg_replace('#/\\*IMPORT\\*/(.*)/\\*IMPORT\\*/#Use', 'base64_decode("$1")', $thiscss); } } unset($thiscss); //$this->csscode has all the uncompressed code now. $mhtmlcount = 0; foreach ($this->csscode as &$code) { //Check for already-minified code $hash = md5($code); $ccheck = new autoptimizeCache($hash, 'css'); if ($ccheck->check()) { $code = $ccheck->retrieve(); $this->hashmap[md5($code)] = $hash; continue; } unset($ccheck); $imgreplace = array(); //Do the imaging! if ($this->datauris == true && function_exists('base64_encode') && preg_match_all('#(background[^;}]*url\\((?!data)(.*)\\)[^;}]*)(?:;|$|})#Usm', $code, $matches)) { foreach ($matches[2] as $count => $quotedurl) { $url = trim($quotedurl, " \t\n\r\v\"'"); // fgo: if querystring, remove it from url if (strpos($url, '?') !== false) { $url = reset(explode('?', $url)); } $path = $this->getpath($url); // fgo: jpe?j should be jpe?g I guess + 5KB seems like a lot, lower to 2.5KB if ($path != false && preg_match('#\\.(jpe?g|png|gif|bmp)$#', $path) && file_exists($path) && is_readable($path) && filesize($path) <= 2560) { //It's an image //Get type $type = end(explode('.', $path)); switch ($type) { // fgo: jpeg and jpg case 'jpeg': $dataurihead = 'data:image/jpeg;base64,'; break; case 'jpg': $dataurihead = 'data:image/jpeg;base64,'; break; case 'gif': $dataurihead = 'data:image/gif;base64,'; break; case 'png': $dataurihead = 'data:image/png;base64,'; break; case 'bmp': $dataurihead = 'data:image/bmp;base64,'; break; default: $dataurihead = 'data:application/octet-stream;base64,'; } //Encode the data $base64data = base64_encode(file_get_contents($path)); //Add it to the list for replacement $imgreplace[$matches[1][$count]] = str_replace($quotedurl, $dataurihead . $base64data, $matches[1][$count]) . ";\n*" . str_replace($quotedurl, 'mhtml:%%MHTML%%!' . $mhtmlcount, $matches[1][$count]) . ";\n_" . $matches[1][$count] . ';'; //Store image on the mhtml document $this->mhtml .= "--_\r\nContent-Location:{$mhtmlcount}\r\nContent-Transfer-Encoding:base64\r\n\r\n{$base64data}\r\n"; $mhtmlcount++; } } //Replace the images $code = str_replace(array_keys($imgreplace), array_values($imgreplace), $code); } //Minify if ($this->yui == false && class_exists('Minify_CSS_Compressor')) { $code = trim(Minify_CSS_Compressor::process($code)); } elseif ($this->yui == true && autoptimizeYUI::available()) { $code = autoptimizeYUI::compress('css', $code); } $this->hashmap[md5($code)] = $hash; } unset($code); return true; }
public function minify() { foreach ($this->scripts as $script) { if (preg_match('#^INLINE;#', $script)) { //Inline script $script = preg_replace('#^INLINE;#', '', $script); //Add try-catch? if ($this->trycatch) { $script = 'try{' . $script . '}catch(e){}'; } $this->jscode .= "\n" . $script; } else { //External script if ($script !== false && file_exists($script) && is_readable($script)) { $script = file_get_contents($script); //Add try-catch? if ($this->trycatch) { $script = 'try{' . $script . '}catch(e){}'; } $this->jscode .= "\n" . $script; } /*else{ //Couldn't read JS. Maybe getpath isn't working? }*/ } } //Check for already-minified code $this->md5hash = md5($this->jscode); $ccheck = new autoptimizeCache($this->md5hash, 'js'); if ($ccheck->check()) { $this->jscode = $ccheck->retrieve(); return true; } unset($ccheck); //$this->jscode has all the uncompressed code now. if ($this->yui == false && class_exists('JSMin')) { $this->jscode = trim(JSMin::minify($this->jscode)); return true; } elseif ($this->yui == true && autoptimizeYUI::available()) { $this->jscode = autoptimizeYUI::compress('js', $this->jscode); return true; } else { return false; } }