コード例 #1
0
 public function minify()
 {
     foreach ($this->scripts as $script) {
         if (preg_match('#^INLINE;#', $script)) {
             //Inline script
             $script = preg_replace('#^INLINE;#', '', $script);
             $script = rtrim($script, ";\n\t\r") . ';';
             //Add try-catch?
             if ($this->trycatch) {
                 $script = 'try{' . $script . '}catch(e){}';
             }
             $tmpscript = apply_filters('autoptimize_js_individual_script', $script, "");
             if (has_filter('autoptimize_js_individual_script') && !empty($tmpscript)) {
                 $script = $tmpscript;
                 $this->alreadyminified = true;
             }
             $this->jscode .= "\n" . $script;
         } else {
             //External script
             if ($script !== false && file_exists($script) && is_readable($script)) {
                 $scriptsrc = file_get_contents($script);
                 $scriptsrc = preg_replace('/\\x{EF}\\x{BB}\\x{BF}/', '', $scriptsrc);
                 $scriptsrc = rtrim($scriptsrc, ";\n\t\r") . ';';
                 //Add try-catch?
                 if ($this->trycatch) {
                     $scriptsrc = 'try{' . $scriptsrc . '}catch(e){}';
                 }
                 $tmpscriptsrc = apply_filters('autoptimize_js_individual_script', $scriptsrc, $script);
                 if (has_filter('autoptimize_js_individual_script') && !empty($tmpscriptsrc)) {
                     $scriptsrc = $tmpscriptsrc;
                     $this->alreadyminified = true;
                 } else {
                     if (strpos($script, "min.js") !== false && $this->inject_min_late === true) {
                         $scriptsrc = "%%INJECTLATER%%" . base64_encode($script) . "|" . md5($scriptsrc) . "%%INJECTLATER%%";
                     }
                 }
                 $this->jscode .= "\n" . $scriptsrc;
             }
             /*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->alreadyminified !== true) {
         if (class_exists('JSMin') && apply_filters('autoptimize_js_do_minify', true)) {
             if (@is_callable(array("JSMin", "minify"))) {
                 $tmp_jscode = trim(JSMin::minify($this->jscode));
                 if (!empty($tmp_jscode)) {
                     $this->jscode = $tmp_jscode;
                     unset($tmp_jscode);
                 }
                 $this->jscode = $this->inject_minified($this->jscode);
                 $this->jscode = apply_filters('autoptimize_js_after_minify', $this->jscode);
                 return true;
             } else {
                 $this->jscode = $this->inject_minified($this->jscode);
                 return false;
             }
         } else {
             $this->jscode = $this->inject_minified($this->jscode);
             return false;
         }
     }
     return true;
 }
コード例 #2
0
 public function getcontent()
 {
     // restore IE hacks
     $this->content = $this->restore_iehacks($this->content);
     // restore comments
     $this->content = $this->restore_comments($this->content);
     // restore noscript
     if (strpos($this->content, '%%NOSCRIPT%%') !== false) {
         $this->content = preg_replace_callback('#%%NOSCRIPT%%(.*?)%%NOSCRIPT%%#is', create_function('$matches', 'return stripslashes(base64_decode($matches[1]));'), $this->content);
     }
     // restore noptimize
     $this->content = $this->restore_noptimize($this->content);
     //Restore the full content
     if (!empty($this->restofcontent)) {
         $this->content .= $this->restofcontent;
         $this->restofcontent = '';
     }
     // Inject the new stylesheets
     $replaceTag = array("<title", "before");
     $replaceTag = apply_filters('autoptimize_filter_css_replacetag', $replaceTag);
     if ($this->inline == true) {
         foreach ($this->csscode as $media => $code) {
             $this->inject_in_html('<style type="text/css" media="' . $media . '">' . $code . '</style>', $replaceTag);
         }
     } else {
         if ($this->defer == true) {
             $deferredCssBlock = "<script>function lCss(url,media) {var d=document;var l=d.createElement('link');l.rel='stylesheet';l.type='text/css';l.href=url;l.media=media; d.getElementsByTagName('head')[0].appendChild(l);}function deferredCSS() {";
             $noScriptCssBlock = "<noscript>";
             $defer_inline_code = $this->defer_inline;
             $defer_inline_code = apply_filters('autoptimize_filter_css_defer_inline', $defer_inline_code);
             if (!empty($defer_inline_code)) {
                 $iCssHash = md5($defer_inline_code);
                 $iCssCache = new autoptimizeCache($iCssHash, 'css');
                 if ($iCssCache->check()) {
                     // we have the optimized inline CSS in cache
                     $defer_inline_code = $iCssCache->retrieve();
                 } else {
                     if (class_exists('Minify_CSS_Compressor')) {
                         $tmp_code = trim(Minify_CSS_Compressor::process($this->defer_inline));
                     } else {
                         if (class_exists('CSSmin')) {
                             $cssmin = new CSSmin();
                             $tmp_code = trim($cssmin->run($defer_inline_code));
                         }
                     }
                     if (!empty($tmp_code)) {
                         $defer_inline_code = $tmp_code;
                         $iCssCache->cache($defer_inline_code, "text/css");
                         unset($tmp_code);
                     }
                 }
                 $code_out = '<style type="text/css" media="all">' . $defer_inline_code . '</style>';
                 $this->inject_in_html($code_out, $replaceTag);
             }
         }
         foreach ($this->url as $media => $url) {
             $url = $this->url_replace_cdn($url);
             //Add the stylesheet either deferred (import at bottom) or normal links in head
             if ($this->defer == true) {
                 $deferredCssBlock .= "lCss('" . $url . "','" . $media . "');";
                 $noScriptCssBlock .= '<link type="text/css" media="' . $media . '" href="' . $url . '" rel="stylesheet" />';
             } else {
                 $this->inject_in_html('<link type="text/css" media="' . $media . '" href="' . $url . '" rel="stylesheet" />', $replaceTag);
             }
         }
         if ($this->defer == true) {
             $deferredCssBlock .= "}if(window.addEventListener){window.addEventListener('DOMContentLoaded',deferredCSS,false);}else{window.onload = deferredCSS;}</script>";
             $noScriptCssBlock .= "</noscript>";
             $this->inject_in_html($noScriptCssBlock, array('<title>', 'before'));
             $this->inject_in_html($deferredCssBlock, array('</body>', 'before'));
         }
     }
     //Return the modified stylesheet
     return $this->content;
 }
コード例 #3
0
 public function minify()
 {
     foreach ($this->scripts as $script) {
         if (preg_match('#^INLINE;#', $script)) {
             //Inline script
             $script = preg_replace('#^INLINE;#', '', $script);
             $script = rtrim($script, ";\n\t\r") . ';';
             //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);
                 $script = preg_replace('/\\x{EF}\\x{BB}\\x{BF}/', '', $script);
                 $script = rtrim($script, ";\n\t\r") . ';';
                 //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 (class_exists('JSMin') && apply_filters('autoptimize_js_do_minify', true)) {
         if (@is_callable(array(new JSMin(), "minify"))) {
             $tmp_jscode = trim(JSMin::minify($this->jscode));
             if (!empty($tmp_jscode)) {
                 $this->jscode = $tmp_jscode;
                 unset($tmp_jscode);
             }
             return true;
         } else {
             return false;
         }
     } else {
         return false;
     }
 }
コード例 #4
0
 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;
 }
コード例 #5
0
 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;
     }
 }