admin-toolbar.php
5 years ago
admin.php
3 years ago
cache.php
3 years ago
cdn.php
3 years ago
clearing-specific-pages.php
3 years ago
cli.php
3 years ago
column.php
6 years ago
css-utilities.php
3 years ago
index.html
11 years ago
js-utilities.php
3 years ago
preload.php
3 years ago
single-preload.php
3 years ago
wp-polls.php
9 years ago
css-utilities.php
756 lines
| 1 | <?php |
| 2 | class CssUtilities{ |
| 3 | private $html = ""; |
| 4 | private $tags = array(); |
| 5 | private $except = ""; |
| 6 | private $wpfc; |
| 7 | |
| 8 | public function __construct($wpfc, $html){ |
| 9 | |
| 10 | $this->wpfc = $wpfc; |
| 11 | $this->html = $html; |
| 12 | $this->set_except_tags(); |
| 13 | $this->set_tags(); |
| 14 | $this->tags_reorder(); |
| 15 | } |
| 16 | |
| 17 | public function check_exclude($css_url = false){ |
| 18 | if($css_url){ |
| 19 | // to exclude the css source of elementor which is /elementor/css/post-[number].css to avoid increasing the size of minified sources |
| 20 | if(preg_match("/\/elementor\/css\/post-\d+\.css/i", $css_url)){ |
| 21 | return true; |
| 22 | } |
| 23 | |
| 24 | foreach((array)$this->wpfc->exclude_rules as $key => $value){ |
| 25 | |
| 26 | if(isset($value->prefix) && $value->prefix && $value->type == "css"){ |
| 27 | if($value->prefix == "contain"){ |
| 28 | $preg_match_rule = preg_quote($value->content, "/"); |
| 29 | } |
| 30 | |
| 31 | if(preg_match("/".$preg_match_rule."/i", $css_url)){ |
| 32 | return true; |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | public function combineCss(){ |
| 40 | $all = array(); |
| 41 | $group = array(); |
| 42 | |
| 43 | foreach ($this->tags as $key => $value) { |
| 44 | if(preg_match("/<link/i", $value["text"])){ |
| 45 | |
| 46 | if($this->except){ |
| 47 | if(strpos($this->except, $value["text"]) !== false){ |
| 48 | array_push($all, $group); |
| 49 | $group = array(); |
| 50 | continue; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if(!$this->checkInternal($value["text"])){ |
| 55 | array_push($all, $group); |
| 56 | $group = array(); |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | if($this->check_exclude($value["text"])){ |
| 61 | array_push($all, $group); |
| 62 | $group = array(); |
| 63 | continue; |
| 64 | } |
| 65 | |
| 66 | if(count($group) > 0){ |
| 67 | if($group[0]["media"] == $value["media"]){ |
| 68 | array_push($group, $value); |
| 69 | }else{ |
| 70 | array_push($all, $group); |
| 71 | $group = array(); |
| 72 | array_push($group, $value); |
| 73 | } |
| 74 | }else{ |
| 75 | array_push($group, $value); |
| 76 | } |
| 77 | |
| 78 | if($value === end($this->tags)){ |
| 79 | array_push($all, $group); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | } |
| 84 | |
| 85 | if(preg_match("/<style/i", $value["text"])){ |
| 86 | if(count($group) > 0){ |
| 87 | array_push($all, $group); |
| 88 | $group = array(); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if(count($all) > 0){ |
| 94 | $all = array_reverse($all); |
| 95 | |
| 96 | foreach ($all as $group_key => $group_value) { |
| 97 | if(count($group_value) > 0){ |
| 98 | |
| 99 | $combined_css = ""; |
| 100 | $combined_name = $this->wpfc->create_name($group_value); |
| 101 | $combined_link = ""; |
| 102 | |
| 103 | $cachFilePath = $this->wpfc->getWpContentDir("/cache/wpfc-minified")."/".$combined_name; |
| 104 | $cssLink = $this->convert_path_to_link($cachFilePath); |
| 105 | |
| 106 | if(is_dir($cachFilePath)){ |
| 107 | if($cssFiles = @scandir($cachFilePath, 1)){ |
| 108 | $combined_link = '<link rel="stylesheet" type="text/css" href="'.$cssLink."/".$cssFiles[0].'" media="'.$group_value[0]["media"].'"/>'; |
| 109 | |
| 110 | if($css_content = $this->wpfc->read_file($cssLink."/".$cssFiles[0])){ |
| 111 | |
| 112 | $css_content = $this->apply_filter($css_content); |
| 113 | |
| 114 | $combined_link = $this->to_inline($combined_link, $css_content); |
| 115 | } |
| 116 | } |
| 117 | }else{ |
| 118 | $combined_css = $this->create_content(array_reverse($group_value)); |
| 119 | $combined_css = $this->fix_charset($combined_css); |
| 120 | |
| 121 | if($combined_css){ |
| 122 | |
| 123 | if($this->wpfc->cdn){ |
| 124 | $combined_css = preg_replace_callback("/(url)\(([^\)]+)\)/i", array($this->wpfc, 'cdn_replace_urls'), $combined_css); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | $this->wpfc->createFolder($cachFilePath, $combined_css, "css"); |
| 129 | |
| 130 | if(is_dir($cachFilePath)){ |
| 131 | if($cssFiles = @scandir($cachFilePath, 1)){ |
| 132 | $combined_link = '<link rel="stylesheet" type="text/css" href="'.$cssLink."/".$cssFiles[0].'" media="'.$group_value[0]["media"].'"/>'; |
| 133 | |
| 134 | $combined_css = $this->apply_filter($combined_css); |
| 135 | |
| 136 | $combined_link = $this->to_inline($combined_link, $combined_css); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if($combined_link){ |
| 143 | foreach (array_reverse($group_value) as $tag_key => $tag_value) { |
| 144 | $text = substr($this->html, $tag_value["start"], ($tag_value["end"]-$tag_value["start"] + 1)); |
| 145 | |
| 146 | if($tag_key > 0){ |
| 147 | $this->html = substr_replace($this->html, "<!-- ".$text." -->", $tag_value["start"], ($tag_value["end"] - $tag_value["start"] + 1)); |
| 148 | }else{ |
| 149 | $this->html = substr_replace($this->html, "<!-- ".$text." -->"."\n".$combined_link, $tag_value["start"], ($tag_value["end"] - $tag_value["start"] + 1)); |
| 150 | |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return $this->html; |
| 159 | } |
| 160 | |
| 161 | public function create_content($group_value){ |
| 162 | $combined_css = ""; |
| 163 | foreach ($group_value as $tag_key => $tag_value) { |
| 164 | $minifiedCss = $this->minify($tag_value["href"]); |
| 165 | |
| 166 | if($minifiedCss){ |
| 167 | $combined_css = $minifiedCss["cssContent"].$combined_css; |
| 168 | }else{ |
| 169 | return false; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return $combined_css; |
| 174 | } |
| 175 | |
| 176 | public function minifyCss(){ |
| 177 | $data = $this->html; |
| 178 | |
| 179 | if(count($this->tags) > 0){ |
| 180 | foreach (array_reverse($this->tags) as $key => $value) { |
| 181 | $text = substr($data, $value["start"], ($value["end"]-$value["start"] + 1)); |
| 182 | |
| 183 | if(preg_match("/<link/i", $text)){ |
| 184 | if($href = $this->checkInternal($text)){ |
| 185 | if($this->check_exclude($href)){ |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | $minifiedCss = $this->minify($href); |
| 190 | |
| 191 | if($minifiedCss){ |
| 192 | $prefixLink = str_replace(array("http:", "https:"), "", $minifiedCss["url"]); |
| 193 | $text = preg_replace("/href\=[\"\'][^\"\']+[\"\']/", "href='".$prefixLink."'", $text); |
| 194 | |
| 195 | |
| 196 | $minifiedCss["cssContent"] = $this->apply_filter($minifiedCss["cssContent"]); |
| 197 | |
| 198 | |
| 199 | $text = $this->to_inline($text, $minifiedCss["cssContent"]); |
| 200 | |
| 201 | $this->html = substr_replace($this->html, $text, $value["start"], ($value["end"] - $value["start"] + 1)); |
| 202 | |
| 203 | } |
| 204 | |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return $this->html; |
| 211 | } |
| 212 | |
| 213 | public function to_inline($link, $css_content){ |
| 214 | if(!isset($GLOBALS["wp_fastest_cache_options"]->wpFastestCacheRenderBlocking)){ |
| 215 | return $link; |
| 216 | } |
| 217 | |
| 218 | if(!preg_match("/\smedia\=[\'\"]all[\'\"]/i", $link)){ |
| 219 | return $link; |
| 220 | } |
| 221 | |
| 222 | if(isset($css_content["11000"])){ |
| 223 | return $link; |
| 224 | } |
| 225 | |
| 226 | $link = "<style>".$css_content."</style>"; |
| 227 | |
| 228 | return $link; |
| 229 | } |
| 230 | |
| 231 | public function tags_reorder(){ |
| 232 | $sorter = array(); |
| 233 | $ret = array(); |
| 234 | |
| 235 | foreach ($this->tags as $ii => $va) { |
| 236 | $sorter[$ii] = $va['start']; |
| 237 | } |
| 238 | |
| 239 | asort($sorter); |
| 240 | |
| 241 | foreach ($sorter as $ii => $va) { |
| 242 | $ret[$ii] = $this->tags[$ii]; |
| 243 | } |
| 244 | |
| 245 | $this->tags = $ret; |
| 246 | } |
| 247 | |
| 248 | public function set_except_tags(){ |
| 249 | $comment_tags = $this->find_tags("<!--", "-->"); |
| 250 | |
| 251 | foreach ($comment_tags as $key => $value) { |
| 252 | $this->except = $value["text"].$this->except; |
| 253 | } |
| 254 | |
| 255 | // to execute if html contains <noscript> tag |
| 256 | if(preg_match("/<noscript/i", $this->html)){ |
| 257 | $noscript_tags = $this->find_tags("<noscript", "</noscript>"); |
| 258 | |
| 259 | foreach ($noscript_tags as $key => $value) { |
| 260 | $this->except = $value["text"].$this->except; |
| 261 | |
| 262 | if(isset($GLOBALS["wp_fastest_cache_options"]->wpFastestCacheLazyLoad)){ |
| 263 | // to set noscript for lazy load |
| 264 | // <noscript><img src="http://google.com/image.jpg"></noscript> |
| 265 | $GLOBALS["wp_fastest_cache"]->noscript = $value["text"].$GLOBALS["wp_fastest_cache"]->noscript; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // $("head").append( "<link rel='stylesheet' id='ms-fonts' href='//fonts.googleapis.com/css?family=Exo+2:regular' type='text/css' media='all' />" ); |
| 271 | $script_tags = $this->find_tags("<script", "</script>"); |
| 272 | |
| 273 | foreach ($script_tags as $key => $value) { |
| 274 | $link_tags = $this->find_tags("<link", ">", $value["text"]); |
| 275 | |
| 276 | if(count($link_tags) > 0){ |
| 277 | $this->except = $value["text"].$this->except; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | public function set_tags(){ |
| 283 | $style_tags = $this->find_tags("<style", "</style>"); |
| 284 | $this->tags = array_merge($this->tags, $style_tags); |
| 285 | |
| 286 | $link_tags = $this->find_tags("<link", ">"); |
| 287 | |
| 288 | foreach ($link_tags as $key => $value) { |
| 289 | //<link rel='stylesheet' id='avada-dynamic-css-css' href='/wp-content/uploads/avada-styles/avada-9.css?timestamp=1485306359&ver=4.7.2' type='text/css' media='all' /> |
| 290 | if(preg_match("/avada-dynamic-css-css/", $value["text"])){ |
| 291 | continue; |
| 292 | } |
| 293 | |
| 294 | preg_match("/media\=[\'\"]([^\'\"]+)[\'\"]/", $value["text"], $media); |
| 295 | preg_match("/href\=[\'\"]([^\'\"]+)[\'\"]/", $value["text"], $href); |
| 296 | |
| 297 | $media[1] = (isset($media[1]) && $media[1]) ? trim($media[1]) : ""; |
| 298 | $value["media"] = (isset($media[1]) && $media[1]) ? $media[1] : "all"; |
| 299 | |
| 300 | if(isset($href[1])){ |
| 301 | $href[1] = trim($href[1]); |
| 302 | $value["href"] = (isset($href[1]) && $href[1]) ? $href[1] : ""; |
| 303 | |
| 304 | if(preg_match("/href\s*\=/i", $value["text"])){ |
| 305 | if(preg_match("/rel\s*\=\s*[\'\"]\s*stylesheet\s*[\'\"]/i", $value["text"])){ |
| 306 | array_push($this->tags, $value); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | public function find_tags($start_string, $end_string, $source = false){ |
| 314 | if($source){ |
| 315 | $data = $source; |
| 316 | }else{ |
| 317 | $data = $this->html; |
| 318 | } |
| 319 | |
| 320 | $list = array(); |
| 321 | $start_index = false; |
| 322 | $end_index = false; |
| 323 | |
| 324 | for($i = 0; $i < strlen( $data ); $i++) { |
| 325 | if(substr($data, $i, strlen($start_string)) == $start_string){ |
| 326 | $start_index = $i; |
| 327 | } |
| 328 | |
| 329 | if($start_index && $i > $start_index){ |
| 330 | if(substr($data, $i, strlen($end_string)) == $end_string){ |
| 331 | $end_index = $i + strlen($end_string)-1; |
| 332 | $text = substr($data, $start_index, ($end_index-$start_index + 1)); |
| 333 | |
| 334 | |
| 335 | array_push($list, array("start" => $start_index, "end" => $end_index, "text" => $text)); |
| 336 | |
| 337 | |
| 338 | $start_index = false; |
| 339 | $end_index = false; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | return $list; |
| 345 | } |
| 346 | |
| 347 | public function minify($url){ |
| 348 | $this->url = $url; |
| 349 | $md5 = $this->wpfc->create_name($url); |
| 350 | |
| 351 | $cachFilePath = $this->wpfc->getWpContentDir("/cache/wpfc-minified")."/".$md5; |
| 352 | $cssLink = $this->convert_path_to_link($cachFilePath); |
| 353 | |
| 354 | if(is_dir($cachFilePath)){ |
| 355 | if($cssFiles = @scandir($cachFilePath, 1)){ |
| 356 | if($cssContent = $this->file_get_contents_curl($cssLink."/".$cssFiles[0])){ |
| 357 | |
| 358 | |
| 359 | $cssContent = $this->apply_filter($cssContent); |
| 360 | |
| 361 | |
| 362 | return array("cachFilePath" => $cachFilePath, "cssContent" => $cssContent, "url" => $cssLink."/".$cssFiles[0], "realUrl" => $url); |
| 363 | }else{ |
| 364 | return false; |
| 365 | } |
| 366 | } |
| 367 | }else{ |
| 368 | if($cssContent = $this->file_get_contents_curl($url, "?v=".time())){ |
| 369 | |
| 370 | $original_content_length = strlen($cssContent); |
| 371 | |
| 372 | if(isset($this->wpfc->options->wpFastestCacheMinifyCss) && $this->wpfc->options->wpFastestCacheMinifyCss){ |
| 373 | $cssContent = $this->_process($cssContent); |
| 374 | } |
| 375 | |
| 376 | $cssContent = $this->fixPathsInCssContent($cssContent, $url); |
| 377 | |
| 378 | $cssContent = $this->apply_filter($cssContent); |
| 379 | |
| 380 | if(isset($this->wpfc->options->wpFastestCacheMinifyCssPowerFul) && $this->wpfc->options->wpFastestCacheMinifyCssPowerFul){ |
| 381 | if(class_exists("WpFastestCachePowerfulHtml")){ |
| 382 | $powerful_html = new WpFastestCachePowerfulHtml(); |
| 383 | $cssContent = $powerful_html->minify_css($cssContent); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | |
| 388 | $cssContent = str_replace("\xEF\xBB\xBF", '', $cssContent); |
| 389 | |
| 390 | // If the content is empty, the file is not created. This breaks "combine css" feature |
| 391 | if(strlen($cssContent) == 0 && $original_content_length > 0){ |
| 392 | return array("cssContent" => "", "url" => $url); |
| 393 | } |
| 394 | |
| 395 | if(!is_dir($cachFilePath)){ |
| 396 | if($this->wpfc->cdn){ |
| 397 | $cssContent = preg_replace_callback("/(url)\(([^\)]+)\)/i", array($this->wpfc, 'cdn_replace_urls'), $cssContent); |
| 398 | } |
| 399 | |
| 400 | $this->wpfc->createFolder($cachFilePath, $cssContent, "css"); |
| 401 | } |
| 402 | |
| 403 | if($cssFiles = @scandir($cachFilePath, 1)){ |
| 404 | return array("cachFilePath" => $cachFilePath, "cssContent" => $cssContent, "url" => $cssLink."/".$cssFiles[0], "realUrl" => $url); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | return false; |
| 409 | } |
| 410 | |
| 411 | public function fixPathsInCssContent($css, $url){ |
| 412 | $this->url_for_fix = $url; |
| 413 | |
| 414 | $css = preg_replace("/@import\s+[\"\']([^\;\"\'\)]+)[\"\'];/", "@import url($1);", $css); |
| 415 | $css = preg_replace_callback("/url\(([^\)\n]*)\)/", array($this, 'newImgPath'), $css); |
| 416 | $css = preg_replace_callback('/@import\s+url\(([^\)]+)\);/i', array($this, 'fix_import_rules'), $css); |
| 417 | $css = $this->fix_charset($css); |
| 418 | |
| 419 | return $css; |
| 420 | } |
| 421 | |
| 422 | public function svg_to_file($source){ |
| 423 | return $source; |
| 424 | |
| 425 | if(preg_match("/base64\,/", $source)){ |
| 426 | $is_base64 = true; |
| 427 | }else{ |
| 428 | $is_base64 = false; |
| 429 | } |
| 430 | |
| 431 | if(preg_match("/\,(%3Csvg|<svg)/", $source) || $is_base64){ |
| 432 | $source = preg_replace("/\"|\'/", "", $source); |
| 433 | $source = preg_replace("/data[^\,]+\,/", "", $source); |
| 434 | |
| 435 | if($is_base64){ |
| 436 | $source = base64_decode($source); |
| 437 | }else{ |
| 438 | $source = rawurldecode($source); |
| 439 | } |
| 440 | |
| 441 | |
| 442 | $md5 = $this->wpfc->create_name($source); |
| 443 | $cachFilePath = $this->wpfc->getWpContentDir("/cache/wpfc-minified")."/svg-".$md5; |
| 444 | |
| 445 | $this->wpfc->createFolder($cachFilePath, $source, "svg"); |
| 446 | |
| 447 | if(is_dir($cachFilePath)){ |
| 448 | if($cssFiles = @scandir($cachFilePath, 1)){ |
| 449 | $source = $this->convert_path_to_link($cachFilePath."/".$cssFiles[0]); |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | return $source; |
| 455 | } |
| 456 | |
| 457 | public function woff_to_file($source){ |
| 458 | return $source; |
| 459 | |
| 460 | // url("data:application/x-font-woff;charset=utf-8;base64,d09GRgABAAAA") |
| 461 | if(preg_match("/base64\,/", $source)){ |
| 462 | $is_base64 = true; |
| 463 | }else{ |
| 464 | $is_base64 = false; |
| 465 | } |
| 466 | |
| 467 | if($is_base64){ |
| 468 | // not to use preg_match() for the speed |
| 469 | $source = strstr($source, 'base64,'); |
| 470 | $source = str_replace("base64,", "", $source); |
| 471 | $source = trim($source); |
| 472 | $source = str_replace(array("'", '"'), "", $source); |
| 473 | |
| 474 | $md5 = $this->wpfc->create_name($source); |
| 475 | $cachFilePath = $this->wpfc->getWpContentDir("/cache/wpfc-minified")."/woff-".$md5; |
| 476 | |
| 477 | $this->wpfc->createFolder($cachFilePath, $source, "woff"); |
| 478 | |
| 479 | if(is_dir($cachFilePath)){ |
| 480 | if($cssFiles = @scandir($cachFilePath, 1)){ |
| 481 | $link = $this->convert_path_to_link($cachFilePath."/".$cssFiles[0]); |
| 482 | |
| 483 | return $link; |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | return $source; |
| 489 | } |
| 490 | |
| 491 | |
| 492 | public function newImgPath($matches){ |
| 493 | $matches[1] = trim($matches[1]); |
| 494 | |
| 495 | if(preg_match("/data\:font\/opentype/i", $matches[1])){ |
| 496 | $matches[1] = $matches[1]; |
| 497 | }else if(preg_match("/data\:application\/x-font-woff/i", $matches[1])){ |
| 498 | $matches[1] = $this->woff_to_file($matches[1]); |
| 499 | }else if(preg_match("/data\:image\/svg\+xml/i", $matches[1])){ |
| 500 | $matches[1] = $this->svg_to_file($matches[1]); |
| 501 | }else{ |
| 502 | $matches[1] = str_replace(array("\"","'"), "", $matches[1]); |
| 503 | $matches[1] = trim($matches[1]); |
| 504 | |
| 505 | if(!$matches[1]){ |
| 506 | $matches[1] = ""; |
| 507 | }else if(preg_match("/^\#/", $matches[1])){ |
| 508 | $matches[1] = $matches[1]; |
| 509 | }else if(preg_match("/^(\/\/|http|\/\/fonts|data:image|data:application)/", $matches[1])){ |
| 510 | if(preg_match("/fonts\.googleapis\.com/", $matches[1])){ // for safari browser |
| 511 | $matches[1] = '"'.$matches[1].'"'; |
| 512 | }else{ |
| 513 | $matches[1] = $matches[1]; |
| 514 | } |
| 515 | }else if(preg_match("/^\//", $matches[1])){ |
| 516 | $homeUrl = str_replace(array("http:", "https:"), "", home_url()); |
| 517 | $matches[1] = $homeUrl.$matches[1]; |
| 518 | }else if(preg_match("/^\.\/.+/i", $matches[1])){ |
| 519 | //$matches[1] = str_replace("./", get_template_directory_uri()."/", $matches[1]); |
| 520 | $matches[1] = str_replace("./", dirname($this->url_for_fix)."/", $matches[1]); |
| 521 | }else if(preg_match("/^(?P<up>(\.\.\/)+)(?P<name>.+)/", $matches[1], $out)){ |
| 522 | $count = strlen($out["up"])/3; |
| 523 | $url = dirname($this->url); |
| 524 | for($i = 1; $i <= $count; $i++){ |
| 525 | $url = substr($url, 0, strrpos($url, "/")); |
| 526 | } |
| 527 | $url = str_replace(array("http:", "https:"), "", $url); |
| 528 | $matches[1] = $url."/".$out["name"]; |
| 529 | }else{ |
| 530 | $url = str_replace(array("http:", "https:"), "", dirname($this->url)); |
| 531 | $matches[1] = $url."/".$matches[1]; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | return "url(".$matches[1].")"; |
| 536 | } |
| 537 | |
| 538 | public function apply_filter($content){ |
| 539 | return apply_filters('wpfc_css_content', $content, null, null); |
| 540 | } |
| 541 | |
| 542 | public function fix_charset($css){ |
| 543 | preg_match_all('/@charset[^\;]+\;/i', $css, $charsets); |
| 544 | if(count($charsets[0]) > 0){ |
| 545 | $css = preg_replace('/@charset[^\;]+\;/i', "", $css); |
| 546 | foreach($charsets[0] as $charset){ |
| 547 | $css = $charset."\n".$css; |
| 548 | } |
| 549 | } |
| 550 | return $css; |
| 551 | } |
| 552 | |
| 553 | public function fix_import_rules($matches){ |
| 554 | if($this->is_internal_css($matches[1])){ |
| 555 | if($cssContent = $this->file_get_contents_curl($matches[1], "?v=".time())){ |
| 556 | $tmp_url = $this->url; |
| 557 | $this->url = $matches[1]; |
| 558 | $cssContent = $this->fixPathsInCssContent($cssContent, $matches[1]); |
| 559 | $this->url = $tmp_url; |
| 560 | |
| 561 | // to minify again because of the @import css sources |
| 562 | if(isset($this->wpfc->options->wpFastestCacheMinifyCss) && $this->wpfc->options->wpFastestCacheMinifyCss){ |
| 563 | $cssContent = $this->_process($cssContent); |
| 564 | } |
| 565 | |
| 566 | return $cssContent; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | return $matches[0]; |
| 571 | } |
| 572 | |
| 573 | protected $_inHack = false; |
| 574 | |
| 575 | protected function _process($css){ |
| 576 | $css = preg_replace("/^\s+/m", "", ((string) $css)); |
| 577 | $css = str_replace("\r", "", $css); |
| 578 | |
| 579 | $css = preg_replace_callback('@\\s*/\\*([\\s\\S]*?)\\*/\\s*@' |
| 580 | ,array($this, '_commentCB'), $css); |
| 581 | |
| 582 | //to remove empty chars from url() |
| 583 | $css = preg_replace("/url\((\s+)([^\)]+)(\s+)\)/", "url($2)", $css); |
| 584 | |
| 585 | return trim($css); |
| 586 | } |
| 587 | |
| 588 | protected function _commentCB($m){ |
| 589 | $hasSurroundingWs = (trim($m[0]) !== $m[1]); |
| 590 | $m = $m[1]; |
| 591 | // $m is the comment content w/o the surrounding tokens, |
| 592 | // but the return value will replace the entire comment. |
| 593 | if ($m === 'keep') { |
| 594 | return '/**/'; |
| 595 | } |
| 596 | if ($m === '" "') { |
| 597 | // component of http://tantek.com/CSS/Examples/midpass.html |
| 598 | return '/*" "*/'; |
| 599 | } |
| 600 | if (preg_match('@";\\}\\s*\\}/\\*\\s+@', $m)) { |
| 601 | // component of http://tantek.com/CSS/Examples/midpass.html |
| 602 | return '/*";}}/* */'; |
| 603 | } |
| 604 | if ($this->_inHack) { |
| 605 | // inversion: feeding only to one browser |
| 606 | if (preg_match('@ |
| 607 | ^/ # comment started like /*/ |
| 608 | \\s* |
| 609 | (\\S[\\s\\S]+?) # has at least some non-ws content |
| 610 | \\s* |
| 611 | /\\* # ends like /*/ or /**/ |
| 612 | @x', $m, $n)) { |
| 613 | // end hack mode after this comment, but preserve the hack and comment content |
| 614 | $this->_inHack = false; |
| 615 | return "/*/{$n[1]}/**/"; |
| 616 | } |
| 617 | } |
| 618 | if (substr($m, -1) === '\\') { // comment ends like \*/ |
| 619 | // begin hack mode and preserve hack |
| 620 | $this->_inHack = true; |
| 621 | return '/*\\*/'; |
| 622 | } |
| 623 | if ($m !== '' && $m[0] === '/') { // comment looks like /*/ foo */ |
| 624 | // begin hack mode and preserve hack |
| 625 | $this->_inHack = true; |
| 626 | return '/*/*/'; |
| 627 | } |
| 628 | if ($this->_inHack) { |
| 629 | // a regular comment ends hack mode but should be preserved |
| 630 | $this->_inHack = false; |
| 631 | return '/**/'; |
| 632 | } |
| 633 | // Issue 107: if there's any surrounding whitespace, it may be important, so |
| 634 | // replace the comment with a single space |
| 635 | return $hasSurroundingWs // remove all other comments |
| 636 | ? ' ' |
| 637 | : ''; |
| 638 | } |
| 639 | |
| 640 | public function checkInternal($link){ |
| 641 | $httpHost = str_replace("www.", "", $_SERVER["HTTP_HOST"]); |
| 642 | |
| 643 | if(preg_match("/href=[\"\'](.*?)[\"\']/", $link, $href)){ |
| 644 | |
| 645 | if(preg_match("/^\/[^\/]/", $href[1])){ |
| 646 | return $href[1]; |
| 647 | } |
| 648 | |
| 649 | if(@strpos($href[1], $httpHost)){ |
| 650 | return $href[1]; |
| 651 | } |
| 652 | |
| 653 | // if(preg_match("/fonts\.googleapis\.com/i", $href[1])){ |
| 654 | // //http://fonts.googleapis.com/css?family=Raleway%3A400%2C600 |
| 655 | // if(preg_match("/Raleway/i", $href[1])){ |
| 656 | // return false; |
| 657 | // } |
| 658 | |
| 659 | // return $href[1]; |
| 660 | // } |
| 661 | } |
| 662 | return false; |
| 663 | } |
| 664 | |
| 665 | public function is_internal_css($url){ |
| 666 | $http_host = trim($_SERVER["HTTP_HOST"], "www."); |
| 667 | |
| 668 | $url = trim($url); |
| 669 | $url = trim($url, "'"); |
| 670 | $url = trim($url, '"'); |
| 671 | |
| 672 | $url = str_replace(array("http://", "https://"), "", $url); |
| 673 | |
| 674 | $url = trim($url, '//'); |
| 675 | $url = trim($url, 'www.'); |
| 676 | |
| 677 | if($url){ |
| 678 | if(preg_match("/".$http_host."/i", $url)){ |
| 679 | return true; |
| 680 | } |
| 681 | |
| 682 | // if(preg_match("/fonts\.googleapis\.com/i", $url)){ |
| 683 | // //http://fonts.googleapis.com/css?family=Raleway%3A400%2C600 |
| 684 | // if(preg_match("/Raleway/i", $url)){ |
| 685 | // return false; |
| 686 | // } |
| 687 | |
| 688 | // return true; |
| 689 | // } |
| 690 | } |
| 691 | |
| 692 | return false; |
| 693 | } |
| 694 | |
| 695 | public function convert_path_to_link($path){ |
| 696 | preg_match("/\/cache\/.+/", $path, $out); |
| 697 | $prefixLink = str_replace(array("http:", "https:"), "", WPFC_WP_CONTENT_URL); |
| 698 | |
| 699 | return $prefixLink.$out[0]; |
| 700 | } |
| 701 | |
| 702 | public function file_get_contents_curl($url, $version = ""){ |
| 703 | if($data = $this->wpfc->read_file($url)){ |
| 704 | return $data; |
| 705 | } |
| 706 | |
| 707 | $url = str_replace('&', '&', $url); |
| 708 | |
| 709 | if(preg_match("/\.php\?/i", $url)){ |
| 710 | $version = ""; |
| 711 | } |
| 712 | |
| 713 | if(preg_match("/(fonts\.googleapis\.com|iire-social-icons)/i", $url)){ |
| 714 | $version = ""; |
| 715 | $url = str_replace(array("'",'"'), "", $url); |
| 716 | } |
| 717 | |
| 718 | $url = $url.$version; |
| 719 | |
| 720 | if(preg_match("/^\/[^\/]/", $url)){ |
| 721 | $url = get_option("home").$url; |
| 722 | } |
| 723 | |
| 724 | if(preg_match("/http\:\/\//i", home_url())){ |
| 725 | $url = preg_replace("/^\/\//", "http://", $url); |
| 726 | }else if(preg_match("/https\:\/\//i", home_url())){ |
| 727 | $url = preg_replace("/^\/\//", "https://", $url); |
| 728 | } |
| 729 | |
| 730 | //$response = wp_remote_get($url, array('timeout' => 10, 'headers' => array("cache-control" => array("no-store, no-cache, must-revalidate", "post-check=0, pre-check=0")))); |
| 731 | $response = wp_remote_get($url, array('timeout' => 10, 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36')); |
| 732 | |
| 733 | if ( !$response || is_wp_error( $response ) ) { |
| 734 | return false; |
| 735 | }else{ |
| 736 | if(wp_remote_retrieve_response_code($response) == 200){ |
| 737 | $data = wp_remote_retrieve_body( $response ); |
| 738 | |
| 739 | if(preg_match("/\<\!DOCTYPE/i", $data) || preg_match("/<\/\s*html\s*>/i", $data)){ |
| 740 | return false; |
| 741 | }else if(!$data){ |
| 742 | return "/* empty */"; |
| 743 | }else{ |
| 744 | return $data; |
| 745 | } |
| 746 | }else if(wp_remote_retrieve_response_code($response) == 404){ |
| 747 | if(preg_match("/\.css/", $url)){ |
| 748 | return "/*404*/"; |
| 749 | }else{ |
| 750 | return "<!-- 404 -->"; |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | ?> |