Exemple #1
0
 public function minify($url)
 {
     $this->url = $url;
     $cachFilePath = WPFC_WP_CONTENT_DIR . "/cache/wpfc-minified/" . md5($url);
     $cssLink = content_url() . "/cache/wpfc-minified/" . md5($url);
     if (is_dir($cachFilePath)) {
         if ($cssFiles = @scandir($cachFilePath, 1)) {
             if ($cssContent = $this->file_get_contents_curl($cssLink . "/" . $cssFiles[0])) {
                 return array("cachFilePath" => $cachFilePath, "cssContent" => $cssContent, "url" => $cssLink . "/" . $cssFiles[0], "realUrl" => $url);
             } else {
                 return false;
             }
         }
     } else {
         if ($cssContent = $this->file_get_contents_curl($url, "?v=" . time())) {
             $original_content_length = strlen($cssContent);
             $cssContent = $this->fixPathsInCssContent($cssContent, $url);
             if (isset($this->wpfc->options->wpFastestCacheMinifyCss) && $this->wpfc->options->wpFastestCacheMinifyCss) {
                 $cssContent = $this->_process($cssContent);
             }
             if (isset($this->wpfc->options->wpFastestCacheMinifyCssPowerFul) && $this->wpfc->options->wpFastestCacheMinifyCssPowerFul) {
                 if (class_exists("WpFastestCachePowerfulHtml")) {
                     $powerful_html = new WpFastestCachePowerfulHtml();
                     $cssContent = $powerful_html->minify_css($cssContent);
                 }
             }
             $cssContent = str_replace("", '', $cssContent);
             // If the content is empty, the file is not created. This breaks "combine css" feature
             if (strlen($cssContent) == 0 && $original_content_length > 0) {
                 return array("cssContent" => "", "url" => $url);
             }
             if (!is_dir($cachFilePath)) {
                 $prefix = time();
                 if ($this->wpfc->cdn) {
                     $cssContent = preg_replace_callback("/(url)\\(([^\\)]+)\\)/i", array($this->wpfc, 'cdn_replace_urls'), $cssContent);
                 }
                 $this->wpfc->createFolder($cachFilePath, $cssContent, "css", $prefix);
             }
             if ($cssFiles = @scandir($cachFilePath, 1)) {
                 return array("cachFilePath" => $cachFilePath, "cssContent" => $cssContent, "url" => $cssLink . "/" . $cssFiles[0], "realUrl" => $url);
             }
         }
     }
     return false;
 }
 public function minify($url)
 {
     $this->url = $url;
     $cachFilePath = WPFC_WP_CONTENT_DIR . "/cache/wpfc-minified/" . md5($url);
     $cssLink = content_url() . "/cache/wpfc-minified/" . md5($url);
     if (is_dir($cachFilePath)) {
         if ($cssFiles = @scandir($cachFilePath, 1)) {
             if ($cssContent = $this->file_get_contents_curl($cssLink . "/" . $cssFiles[0])) {
                 return array("cachFilePath" => $cachFilePath, "cssContent" => $cssContent, "url" => $cssLink . "/" . $cssFiles[0], "realUrl" => $url);
             } else {
                 return false;
             }
         }
     } else {
         if ($cssContent = $this->file_get_contents_curl($url . "?v=" . time())) {
             $cssContent = $this->fixPathsInCssContent($cssContent);
             if (isset($this->wpfc->options->wpFastestCacheMinifyCss) && $this->wpfc->options->wpFastestCacheMinifyCss) {
                 $cssContent = $this->_process($cssContent);
             }
             if (isset($this->wpfc->options->wpFastestCacheMinifyCssPowerFul) && $this->wpfc->options->wpFastestCacheMinifyCssPowerFul) {
                 if (class_exists("WpFastestCachePowerfulHtml")) {
                     $powerful_html = new WpFastestCachePowerfulHtml();
                     $cssContent = $powerful_html->minify_css($cssContent);
                 }
             }
             $cssContent = str_replace("", '', $cssContent);
             if (!is_dir($cachFilePath)) {
                 $prefix = time();
                 $this->wpfc->createFolder($cachFilePath, $cssContent, "css", $prefix);
             }
             if ($cssFiles = @scandir($cachFilePath, 1)) {
                 return array("cachFilePath" => $cachFilePath, "cssContent" => $cssContent, "url" => $cssLink . "/" . $cssFiles[0], "realUrl" => $url);
             }
         }
     }
     return false;
 }
 public function combineCss($wpfc, $minify = false)
 {
     if (count($this->getCssLinks()) > 0) {
         $prev = array("content" => "", "value" => array(), "name" => "");
         foreach ($this->getCssLinks() as $key => $value) {
             if ($href = $this->checkInternal($value)) {
                 if (preg_match("/\\.ttf/", $href)) {
                     continue;
                 }
                 if (strpos($this->getCssLinksExcept(), $href) === false && (preg_match("/media=[\\'\"]all[\\'\"]/", $value) || !preg_match("/media=/", $value))) {
                     $minifiedCss = $this->minify($href, $minify);
                     if ($minifiedCss) {
                         if ($minify && isset($wpfc->options->wpFastestCacheMinifyCssPowerFul)) {
                             $powerful_html = new WpFastestCachePowerfulHtml();
                             $minifiedCss["cssContent"] = $powerful_html->minify_css($minifiedCss["cssContent"]);
                         }
                         if (!is_dir($minifiedCss["cachFilePath"])) {
                             $prefix = time();
                             $wpfc->createFolder($minifiedCss["cachFilePath"], $minifiedCss["cssContent"], "css", $prefix);
                         }
                         if ($cssFiles = @scandir($minifiedCss["cachFilePath"], 1)) {
                             if ($cssContent = $this->file_get_contents_curl($minifiedCss["url"] . "/" . $cssFiles[0] . "?v=" . time())) {
                                 $prev["name"] .= $minifiedCss["realUrl"];
                                 $prev["content"] .= $cssContent . "\n";
                                 array_push($prev["value"], $value);
                             }
                         }
                     }
                 } else {
                     $prev["content"] = $this->fixRules($prev["content"]);
                     $this->mergeCss($wpfc, $prev);
                     $prev = array("content" => "", "value" => array());
                 }
             } else {
                 $prev["content"] = $this->fixRules($prev["content"]);
                 $this->mergeCss($wpfc, $prev);
                 $prev = array("content" => "", "value" => array());
             }
         }
         $prev["content"] = $this->fixRules($prev["content"]);
         $this->mergeCss($wpfc, $prev);
     }
     $this->html = preg_replace("/(<!-- )+/", "<!-- ", $this->html);
     $this->html = preg_replace("/( -->)+/", " -->", $this->html);
     return $this->html;
 }
Exemple #4
0
 public function mergeCss($wpfc, $prev)
 {
     if (count($prev["value"]) > 0) {
         $name = "";
         foreach ($prev["value"] as $prevKey => $prevValue) {
             if ($prevKey == count($prev["value"]) - 1) {
                 $name = md5($prev["name"]);
                 $cachFilePath = WPFC_WP_CONTENT_DIR . "/cache/wpfc-minified/" . $name;
                 $prev["content"] = $this->fixRules($prev["content"]);
                 if (isset($this->wpfc->options->wpFastestCacheMinifyCss) && $this->wpfc->options->wpFastestCacheMinifyCss) {
                     $prev["content"] = $this->_process($prev["content"]);
                 }
                 if (isset($this->wpfc->options->wpFastestCacheMinifyCssPowerFul) && $this->wpfc->options->wpFastestCacheMinifyCssPowerFul) {
                     $powerful_html = new WpFastestCachePowerfulHtml();
                     $prev["content"] = $powerful_html->minify_css($prev["content"]);
                 }
                 /* 
                 	The css files are saved in the previous function.
                 	If only one css file is in the array, there is need to save again.
                 */
                 if (count($prev["value"]) == 1) {
                     if ($cssFiles = @scandir($cachFilePath, 1)) {
                         file_put_contents($cachFilePath . "/" . $cssFiles[0], $prev["content"]);
                     }
                 } else {
                     if (!is_dir($cachFilePath)) {
                         $wpfc->createFolder($cachFilePath, $prev["content"], "css", time());
                     }
                 }
                 if ($cssFiles = @scandir($cachFilePath, 1)) {
                     $prefixLink = str_replace(array("http:", "https:"), "", content_url());
                     $newLink = "<link rel='stylesheet' href='" . $prefixLink . "/cache/wpfc-minified/" . $name . "/" . $cssFiles[0] . "' type='text/css' media='all' />";
                     $this->html = $wpfc->replaceLink($prevValue, "<!-- " . $prevValue . " -->" . "\n" . $newLink, $this->html);
                 }
             } else {
                 $name .= $prevValue;
                 $this->html = $wpfc->replaceLink($prevValue, "<!-- " . $prevValue . " -->", $this->html);
             }
         }
     }
 }