admin-toolbar.php
10 months ago
admin.php
7 months ago
cache.php
1 year ago
cdn.php
1 year ago
clearing-specific-pages.php
2 years ago
cli.php
4 years ago
column.php
6 years ago
css-utilities.php
2 years ago
index.html
11 years ago
js-utilities.php
1 year ago
preload.php
1 year ago
single-preload.php
2 years ago
varnish.php
2 years ago
wp-polls.php
9 years ago
js-utilities.php
341 lines
| 1 | <?php |
| 2 | class JsUtilities{ |
| 3 | private $wpfc; |
| 4 | private $html = ""; |
| 5 | private $jsLinks = array(); |
| 6 | private $jsLinksExcept = ""; |
| 7 | private $url = ""; |
| 8 | private $minify; |
| 9 | |
| 10 | public function __construct($wpfc, $html, $minify = false){ |
| 11 | |
| 12 | //$this->html = preg_replace("/\s+/", " ", ((string) $html)); |
| 13 | $this->minify = $minify; |
| 14 | $this->wpfc = $wpfc; |
| 15 | $this->html = $html; |
| 16 | |
| 17 | $this->setJsLinksExcept(); |
| 18 | $this->setJsLinks(); |
| 19 | } |
| 20 | |
| 21 | public function check_exclude($js_url = false){ |
| 22 | if($js_url){ |
| 23 | foreach((array)$this->wpfc->exclude_rules as $key => $value){ |
| 24 | |
| 25 | if(isset($value->prefix) && $value->prefix && $value->type == "js"){ |
| 26 | if($value->prefix == "contain"){ |
| 27 | $preg_match_rule = preg_quote($value->content, "/"); |
| 28 | } |
| 29 | |
| 30 | if(preg_match("/".$preg_match_rule."/i", $js_url)){ |
| 31 | return true; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public function combine_js(){ |
| 39 | if(count($this->jsLinks) > 0){ |
| 40 | $prev_content = ""; |
| 41 | foreach($this->jsLinks as $key => $value){ |
| 42 | $script_tag = substr($this->html, $value["start"], ($value["end"] - $value["start"] + 1)); |
| 43 | |
| 44 | if(!preg_match("/<script[^>]+json[^>]+>.+/", $script_tag) && !preg_match("/<script[^>]+text\/template[^>]+>.+/", $script_tag)){ |
| 45 | if($href = $this->checkInternal($script_tag)){ |
| 46 | if(strpos($this->jsLinksExcept, $href) === false){ |
| 47 | if($key == 0 && $this->check_exclude($href)){ |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | if($key > 0 && $this->check_exclude($href)){ |
| 52 | $this->mergeJs($prev_content, $this->jsLinks[$key - 1]); |
| 53 | $prev_content = ""; |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | if(preg_match("/data-no-minify/i", $script_tag) || preg_match("/type\s*=\s*[\"\']\s*module\s*[\"\']/i", $script_tag)){ |
| 58 | if($key > 0 && $prev_content){ |
| 59 | $this->mergeJs($prev_content, $this->jsLinks[$key - 1]); |
| 60 | $prev_content = ""; |
| 61 | continue; |
| 62 | }else{ |
| 63 | continue; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | $minifiedJs = $this->minify($href); |
| 68 | |
| 69 | if($minifiedJs){ |
| 70 | if(!is_dir($minifiedJs["cachFilePath"])){ |
| 71 | $this->wpfc->createFolder($minifiedJs["cachFilePath"], $minifiedJs["jsContent"], "js"); |
| 72 | } |
| 73 | |
| 74 | if($jsFiles = @scandir($minifiedJs["cachFilePath"], 1)){ |
| 75 | |
| 76 | $jsFiles[0] = preg_replace("/\.gz$/", "", $jsFiles[0]); |
| 77 | |
| 78 | if($jsContent = $this->file_get_contents_curl($minifiedJs["url"]."/".$jsFiles[0]."?v=".time())){ |
| 79 | |
| 80 | if($key > 0 && preg_match("/^[\"\']use strict[\"\']/i", $jsContent)){ |
| 81 | $this->mergeJs($prev_content, $this->jsLinks[$key - 1]); |
| 82 | $prev_content = ""; |
| 83 | }else{ |
| 84 | $prev_content = $jsContent."\n".$prev_content; |
| 85 | |
| 86 | $script_tag = "<!-- ".$script_tag." -->"; |
| 87 | |
| 88 | if(($key + 1) == count($this->jsLinks)){ |
| 89 | $this->mergeJs($prev_content, $value, true); |
| 90 | $prev_content = ""; |
| 91 | }else{ |
| 92 | $this->html = substr_replace($this->html, $script_tag, $value["start"], ($value["end"] - $value["start"] + 1)); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | }else{ |
| 98 | if($key > 0 && $prev_content){ |
| 99 | $this->mergeJs($prev_content, $this->jsLinks[$key - 1]); |
| 100 | $prev_content = ""; |
| 101 | } |
| 102 | } |
| 103 | }else{ |
| 104 | if($key > 0 && $prev_content){ |
| 105 | $this->mergeJs($prev_content, $this->jsLinks[$key - 1]); |
| 106 | $prev_content = ""; |
| 107 | } |
| 108 | } |
| 109 | }else{ |
| 110 | if($key > 0 && $prev_content){ |
| 111 | $this->mergeJs($prev_content, $this->jsLinks[$key - 1]); |
| 112 | $prev_content = ""; |
| 113 | } |
| 114 | } |
| 115 | }else{ |
| 116 | if($key > 0 && $prev_content){ |
| 117 | $this->mergeJs($prev_content, $this->jsLinks[$key - 1]); |
| 118 | $prev_content = ""; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return $this->html; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | public function setJsLinks(){ |
| 129 | $data = $this->html; |
| 130 | $script_list = array(); |
| 131 | $script_start_index = false; |
| 132 | |
| 133 | for($i = 0; $i < strlen( $data ); $i++) { |
| 134 | if(isset($data[$i-6])){ |
| 135 | if(substr($data, $i-6, 7) == "<script"){ |
| 136 | $script_start_index = $i-6; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if(isset($data[$i-8])){ |
| 141 | if($script_start_index){ |
| 142 | if(substr($data, $i-8, 9) == "</script>"){ |
| 143 | array_push($script_list, array("start" => $script_start_index, "end" => $i)); |
| 144 | $script_start_index = false; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | if(count($script_list) > 0){ |
| 150 | $this->jsLinks = array_reverse($script_list); |
| 151 | } |
| 152 | |
| 153 | // to update jsLinksExcept |
| 154 | foreach($this->jsLinks as $key => $value){ |
| 155 | $script_tag = substr($this->html, $value["start"], ($value["end"] - $value["start"] + 1)); |
| 156 | |
| 157 | if(preg_match("/wp-spamshield\/js\/jscripts\.php/i", $script_tag)){ |
| 158 | $this->jsLinksExcept = $this->jsLinksExcept.$script_tag; |
| 159 | } |
| 160 | |
| 161 | //amazonjs/components/js/jquery-tmpl/jquery.tmpl.min.js?ver=1.0.0pre |
| 162 | if(preg_match("/jquery-tmpl\/jquery\.tmpl\.min\.js/i", $script_tag)){ |
| 163 | $this->jsLinksExcept = $this->jsLinksExcept.$script_tag; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | public function setJsLinksExcept(){ |
| 169 | $comment_tags = $this->find_tags("<!--", "-->"); |
| 170 | $document_write = $this->find_tags("document.write(", ")"); |
| 171 | |
| 172 | foreach ($comment_tags as $key => $value) { |
| 173 | if(preg_match("/<script/i", $value["text"]) && preg_match("/<\/script/i", $value["text"])){ |
| 174 | $this->jsLinksExcept = $value["text"].$this->jsLinksExcept; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | foreach ($document_write as $key => $value) { |
| 179 | $this->jsLinksExcept = $value["text"].$this->jsLinksExcept; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | public function minify($url){ |
| 184 | $this->url = $url; |
| 185 | |
| 186 | $md5 = $this->wpfc->create_name($url); |
| 187 | |
| 188 | $cachFilePath = $this->wpfc->getWpContentDir("/cache/wpfc-minified")."/".$md5; |
| 189 | $jsLink = $this->convert_path_to_link($cachFilePath); |
| 190 | |
| 191 | |
| 192 | if(is_dir($cachFilePath)){ |
| 193 | return array("cachFilePath" => $cachFilePath, "jsContent" => "", "url" => $jsLink); |
| 194 | }else{ |
| 195 | if($js = $this->file_get_contents_curl($url)){ |
| 196 | |
| 197 | if($this->minify){ |
| 198 | if(class_exists("WpFastestCachePowerfulHtml")){ |
| 199 | $powerful_html = new WpFastestCachePowerfulHtml(); |
| 200 | $js = $powerful_html->minify_js($js); |
| 201 | }else{ |
| 202 | $js = "\n// source --> ".$url." \n".$js; |
| 203 | } |
| 204 | }else{ |
| 205 | $js = "\n// source --> ".$url." \n".$js; |
| 206 | } |
| 207 | |
| 208 | return array("cachFilePath" => $cachFilePath, "jsContent" => $js, "url" => $jsLink); |
| 209 | } |
| 210 | } |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | public function checkInternal($link){ |
| 215 | $httpHost = str_replace("www.", "", $_SERVER["HTTP_HOST"]); |
| 216 | |
| 217 | if(preg_match("/^<script[^\>]+\>/i", $link, $script)){ |
| 218 | if(preg_match("/src=[\"\'](.*?)[\"\']/", $script[0], $src)){ |
| 219 | if(preg_match("/alexa\.com\/site\_stats/i", $src[1])){ |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | if(preg_match("/^\/[^\/]/", $src[1])){ |
| 224 | return $src[1]; |
| 225 | } |
| 226 | |
| 227 | if(preg_match("/".preg_quote($httpHost, "/")."/i", $src[1])){ |
| 228 | //<script src="https://server1.opentracker.net/?site=www.site.com"></script> |
| 229 | if(preg_match("/[\?\=].*".preg_quote($httpHost, "/")."/i", $src[1])){ |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | return $src[1]; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | public function mergeJs($js_content, $value, $last = false){ |
| 243 | $name = md5($js_content); |
| 244 | |
| 245 | $name = base_convert(crc32($name), 20, 36); |
| 246 | |
| 247 | $cachFilePath = $this->wpfc->getWpContentDir("/cache/wpfc-minified")."/".$name; |
| 248 | $jsLink = $this->convert_path_to_link($cachFilePath); |
| 249 | |
| 250 | if(!is_dir($cachFilePath)){ |
| 251 | $this->wpfc->createFolder($cachFilePath, $js_content, "js"); |
| 252 | } |
| 253 | |
| 254 | if(is_dir($cachFilePath)){ |
| 255 | if($jsFiles = @scandir($cachFilePath, 1)){ |
| 256 | |
| 257 | $newLink = "<script src='".$jsLink."/".$jsFiles[0]."' type=\"text/javascript\"></script>"; |
| 258 | |
| 259 | $script_tag = substr($this->html, $value["start"], ($value["end"] - $value["start"] + 1)); |
| 260 | |
| 261 | if($last){ |
| 262 | $script_tag = $newLink."\n<!-- ".$script_tag." -->\n"; |
| 263 | }else{ |
| 264 | $script_tag = $newLink."\n".$script_tag; |
| 265 | } |
| 266 | |
| 267 | $this->html = substr_replace($this->html, $script_tag, $value["start"], ($value["end"] - $value["start"] + 1)); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | public function convert_path_to_link($path){ |
| 273 | preg_match("/\/cache\/.+/", $path, $out); |
| 274 | $prefixLink = str_replace(array("http:", "https:"), "", WPFC_WP_CONTENT_URL); |
| 275 | |
| 276 | return $prefixLink.$out[0]; |
| 277 | } |
| 278 | |
| 279 | public function file_get_contents_curl($url) { |
| 280 | if($data = $this->wpfc->read_file($url)){ |
| 281 | return $data; |
| 282 | } |
| 283 | |
| 284 | if(!preg_match("/\.php$/", $url)){ |
| 285 | $url = $url."?v=".time(); |
| 286 | } |
| 287 | |
| 288 | if(preg_match("/^\/[^\/]/", $url)){ |
| 289 | $url = home_url().$url; |
| 290 | } |
| 291 | |
| 292 | $url = preg_replace("/^\/\//", "http://", $url); |
| 293 | |
| 294 | $response = wp_remote_get($url, array('timeout' => 10 ) ); |
| 295 | |
| 296 | if ( !$response || is_wp_error( $response ) ) { |
| 297 | return false; |
| 298 | }else{ |
| 299 | if(wp_remote_retrieve_response_code($response) == 200){ |
| 300 | $data = wp_remote_retrieve_body( $response ); |
| 301 | |
| 302 | if(preg_match("/<\/\s*html\s*>\s*$/i", $data)){ |
| 303 | return false; |
| 304 | }else{ |
| 305 | return $data; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | public function find_tags($start_string, $end_string){ |
| 312 | $data = $this->html; |
| 313 | |
| 314 | $list = array(); |
| 315 | $start_index = false; |
| 316 | $end_index = false; |
| 317 | |
| 318 | for($i = 0; $i < strlen( $data ); $i++) { |
| 319 | if(substr($data, $i, strlen($start_string)) == $start_string){ |
| 320 | $start_index = $i; |
| 321 | } |
| 322 | |
| 323 | if($start_index && $i > $start_index){ |
| 324 | if(substr($data, $i, strlen($end_string)) == $end_string){ |
| 325 | $end_index = $i + strlen($end_string)-1; |
| 326 | $text = substr($data, $start_index, ($end_index-$start_index + 1)); |
| 327 | |
| 328 | |
| 329 | array_push($list, array("start" => $start_index, "end" => $end_index, "text" => $text)); |
| 330 | |
| 331 | |
| 332 | $start_index = false; |
| 333 | $end_index = false; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | return $list; |
| 339 | } |
| 340 | } |
| 341 | ?> |