delay-js.php
1 year ago
delay-jswithjs.php
1 year ago
lazyload.js
3 years ago
lazyload.min.js
2 years ago
delay-js.php
639 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; // Exit if accessed directly. |
| 4 | } |
| 5 | function cwvpsb_get_atts_array($atts_string) { |
| 6 | |
| 7 | if(!empty($atts_string)) { |
| 8 | $atts_array = array_map( |
| 9 | function(array $attribute) { |
| 10 | return $attribute['value']; |
| 11 | }, |
| 12 | wp_kses_hair($atts_string, wp_allowed_protocols()) |
| 13 | ); |
| 14 | return $atts_array; |
| 15 | } |
| 16 | return false; |
| 17 | } |
| 18 | |
| 19 | function cwvpsb_get_atts_string($atts_array) { |
| 20 | |
| 21 | if(!empty($atts_array)) { |
| 22 | $assigned_atts_array = array_map( |
| 23 | function($name, $value) { |
| 24 | if($value === '' || $value === null || $value == "null" ) { |
| 25 | return $name; |
| 26 | } |
| 27 | return sprintf('%s="%s"', $name, esc_attr($value)); |
| 28 | }, |
| 29 | array_keys($atts_array), |
| 30 | $atts_array |
| 31 | ); |
| 32 | $atts_string = implode(' ', $assigned_atts_array); |
| 33 | return $atts_string; |
| 34 | } |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | function cwvpsb_delay_js_main() { |
| 39 | |
| 40 | $is_admin = current_user_can('manage_options'); |
| 41 | |
| 42 | if(is_admin() || $is_admin){ |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | if ( function_exists('is_checkout') && is_checkout() || (function_exists('is_feed')&& is_feed()) ) { |
| 47 | return; |
| 48 | } |
| 49 | if( class_exists( 'next_article_layout' ) ) { |
| 50 | return ; |
| 51 | } |
| 52 | |
| 53 | if ( function_exists('elementor_load_plugin_textdomain') && \Elementor\Plugin::$instance->preview->is_preview_mode() ) { |
| 54 | return; |
| 55 | } |
| 56 | if(cwvpsb_wprocket_lazyjs()){ |
| 57 | add_filter('rocket_delay_js_exclusions', 'cwvpsb_add_rocket_delay_js_exclusions'); |
| 58 | return; |
| 59 | } |
| 60 | add_filter('cwvpsb_complete_html_after_dom_loaded', 'cwvpsb_delay_js_html', 2); |
| 61 | add_filter('cwvpsb_complete_html_after_dom_loaded', 'cwvpsb_remove_js_query_param', 99); |
| 62 | add_action('wp_footer', 'cwvpsb_delay_js_load', PHP_INT_MAX); |
| 63 | } |
| 64 | add_action('wp', 'cwvpsb_delay_js_main'); |
| 65 | |
| 66 | function cwvpsb_delay_js_html($html) { |
| 67 | |
| 68 | if (function_exists('is_amp_endpoint') && is_amp_endpoint()) { |
| 69 | return $html; |
| 70 | } |
| 71 | |
| 72 | if (function_exists('is_feed') && is_feed()) { |
| 73 | return $html; |
| 74 | } |
| 75 | |
| 76 | if (function_exists('is_checkout') && is_checkout()) { |
| 77 | return $html; |
| 78 | } |
| 79 | |
| 80 | $html_no_comments = $html;//preg_replace('/<!--(.*)-->/Uis', '', $html); |
| 81 | preg_match_all('#(<script\s?([^>]+)?\/?>)(.*?)<\/script>#is', $html_no_comments, $matches); |
| 82 | if(!isset($matches[0])) { |
| 83 | return $html; |
| 84 | } |
| 85 | $combined_ex_js_arr = array(); |
| 86 | foreach($matches[0] as $i => $tag) { |
| 87 | $atts_array = !empty($matches[2][$i]) ? cwvpsb_get_atts_array($matches[2][$i]) : array(); |
| 88 | if(isset($atts_array['type']) && stripos($atts_array['type'], 'javascript') == false || |
| 89 | isset($atts_array['id']) && stripos($atts_array['id'], 'corewvps-mergejsfile') !== false || |
| 90 | isset($atts_array['id']) && stripos($atts_array['id'], 'corewvps-cc') !== false |
| 91 | ) { |
| 92 | continue; |
| 93 | } |
| 94 | $delay_flag = false; |
| 95 | $excluded_scripts = array( |
| 96 | 'cwvpsb-delayed-scripts', |
| 97 | ); |
| 98 | |
| 99 | if(!empty($excluded_scripts)) { |
| 100 | foreach($excluded_scripts as $excluded_script) { |
| 101 | if(strpos($tag, $excluded_script) !== false) { |
| 102 | continue 2; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | // Fix for recaptcha |
| 107 | if(strpos($tag,'recaptcha') !== false) { |
| 108 | continue 1; |
| 109 | } |
| 110 | // Fix for google analytics |
| 111 | if((strpos($tag,'google-analytics') !== false) || (strpos($tag,'googletagmanager') !== false)) { |
| 112 | continue 1; |
| 113 | } |
| 114 | |
| 115 | $delay_flag = true; |
| 116 | if(!empty($atts_array['type'])) { |
| 117 | $atts_array['data-cwvpsb-type'] = $atts_array['type']; |
| 118 | } |
| 119 | |
| 120 | $atts_array['type'] = 'cwvpsbdelayedscript'; |
| 121 | if(!empty($atts_array['src'])) { |
| 122 | $atts_array['defer'] = 'defer'; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | $include = true; |
| 127 | if(isset($atts_array['src'])){ |
| 128 | $regex = cwvpsb_delay_exclude_js(); |
| 129 | |
| 130 | if($regex && preg_match( '#(' . $regex . ')#', $atts_array['src'] )){ |
| 131 | $combined_ex_js_arr[] = $atts_array['src']; |
| 132 | //$html = str_replace($tag, '', $html); |
| 133 | $include = false; |
| 134 | } |
| 135 | } |
| 136 | if($include && isset($atts_array['id'])){ |
| 137 | $regex = cwvpsb_delay_exclude_js(); |
| 138 | $file_path = $atts_array['id']; |
| 139 | if($regex && preg_match( '#(' . $regex . ')#', $file_path)){ |
| 140 | $include = false; |
| 141 | } |
| 142 | } |
| 143 | if($include && isset($matches[3][$i])){ |
| 144 | $regex = cwvpsb_delay_exclude_js(); |
| 145 | $file_path = $matches[3][$i]; |
| 146 | if($regex && preg_match( '#(' . $regex . ')#', $file_path)){ |
| 147 | $include = false; |
| 148 | } |
| 149 | } |
| 150 | if(isset($atts_array['src']) && !$include){ |
| 151 | $include = true; |
| 152 | } |
| 153 | if($delay_flag && $include ) {// |
| 154 | |
| 155 | $delayed_atts_string = cwvpsb_get_atts_string($atts_array); |
| 156 | $delayed_tag = sprintf('<script %1$s>', $delayed_atts_string) . (!empty($matches[3][$i]) ? $matches[3][$i] : '') .'</script>'; |
| 157 | $html = str_replace($tag, $delayed_tag, $html); |
| 158 | continue; |
| 159 | } |
| 160 | } |
| 161 | /*if($combined_ex_js_arr){ |
| 162 | $html = cwvpsb_combine_js_files($combined_ex_js_arr, $html); |
| 163 | }*/ |
| 164 | return $html; |
| 165 | } |
| 166 | function cwvpsb_remove_js_query_param($html){ |
| 167 | |
| 168 | |
| 169 | $html = preg_replace('/type="cwvpsbdelayedscript"\s+src="(.*?)\.js\?(.*?)"/', 'type="cwvpsbdelayedscript" src="$1.js"', $html); |
| 170 | if(preg_match('/<link(.*?)rel="cwvpsbdelayedstyle"(.*?)href="(.*?)\.css\?(.*?)"(.*?)>/m',$html)){ |
| 171 | $html = preg_replace('/<link(.*?)rel="cwvpsbdelayedstyle"(.*?)href="(.*?)\.css\?(.*?)"(.*?)>/', '<link$1rel="cwvpsbdelayedstyle"$2href="$3.css"$5>', $html); |
| 172 | } |
| 173 | return $html; |
| 174 | } |
| 175 | |
| 176 | function cwvpsb_delay_exclude_js(){ |
| 177 | $settings = cwvpsb_defaults(); |
| 178 | $inputs['exclude_js'] = $settings['exclude_delay_js']; |
| 179 | if ( ! empty( $inputs['exclude_js'] ) ) { |
| 180 | if ( ! is_array( $inputs['exclude_js'] ) ) { |
| 181 | $inputs['exclude_js'] = explode( "\n", $inputs['exclude_js'] ); |
| 182 | } |
| 183 | $inputs['exclude_js'] = array_map( 'trim', $inputs['exclude_js'] ); |
| 184 | //$inputs['exclude_js'] = array_map( 'cwvpsb_sanitize_js', $inputs['exclude_js'] ); |
| 185 | $inputs['exclude_js'] = (array) array_filter( $inputs['exclude_js'] ); |
| 186 | $inputs['exclude_js'] = array_unique( $inputs['exclude_js'] ); |
| 187 | } else { |
| 188 | $inputs['exclude_js'] = array(); |
| 189 | } |
| 190 | $excluded_files = array(); |
| 191 | if($inputs['exclude_js']){ |
| 192 | foreach ( $inputs['exclude_js'] as $i => $excluded_file ) { |
| 193 | // Escape characters for future use in regex pattern. |
| 194 | $excluded_files[ $i ] = str_replace( '#', '\#', $excluded_file ); |
| 195 | } |
| 196 | } |
| 197 | if(is_array($excluded_files)){ |
| 198 | return implode( '|', $excluded_files ); |
| 199 | }else{ |
| 200 | return ''; |
| 201 | } |
| 202 | } |
| 203 | add_action( 'wp_enqueue_scripts', 'cwvpsb_scripts_styles' , 99999); |
| 204 | function cwvpsb_scripts_styles(){ |
| 205 | |
| 206 | global $wp_scripts; |
| 207 | $wp_scripts->all_deps($wp_scripts->queue); |
| 208 | |
| 209 | $uniqueid = get_transient( CWVPSB_CACHE_NAME ); |
| 210 | global $wp; |
| 211 | $url = home_url( $wp->request ); |
| 212 | $filename = md5($url.$uniqueid); |
| 213 | $user_dirname = CWVPSB_JS_EXCLUDE_CACHE_DIR; |
| 214 | $user_urlname = CWVPSB_JS_EXCLUDE_CACHE_URL; |
| 215 | |
| 216 | |
| 217 | if(!file_exists($user_dirname.'/'.$filename.'.js')){ |
| 218 | $combined_ex_js_arr= array(); |
| 219 | $jscontent = ''; |
| 220 | $regex = cwvpsb_delay_exclude_js(); |
| 221 | include_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; |
| 222 | include_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; |
| 223 | if (!class_exists('WP_Filesystem_Direct')) { |
| 224 | return false; |
| 225 | } |
| 226 | $wp_scripts->all_deps($wp_scripts->queue); |
| 227 | foreach( $wp_scripts->to_do as $key=>$handle) |
| 228 | { |
| 229 | $localize = $localize_handle = ''; |
| 230 | //$src = strtok($wp_scripts->registered[$handle]->src, '?'); |
| 231 | if($regex && preg_match( '#(' . $regex . ')#', $wp_scripts->registered[$handle]->src )){ |
| 232 | $localize_handle = $handle; |
| 233 | } |
| 234 | if($regex && preg_match( '#(' . $regex . ')#', $handle )){ |
| 235 | $localize_handle = $handle; |
| 236 | } |
| 237 | if($localize_handle){ |
| 238 | if(@array_key_exists('data', $wp_scripts->registered[$handle]->extra)) { |
| 239 | $localize = $wp_scripts->registered[$handle]->extra['data'] . ';'; |
| 240 | } |
| 241 | $file_url = $wp_scripts->registered[$handle]->src; |
| 242 | $parse_url = parse_url($file_url); |
| 243 | $file_path = str_replace(array(get_site_url(),'?'.@$parse_url['query']),array(ABSPATH,''),$file_url); |
| 244 | |
| 245 | if(substr( $file_path, 0, 13 ) === "/wp-includes/"){ |
| 246 | $file_path = ABSPATH.$file_path; |
| 247 | } |
| 248 | $wp_filesystem = new WP_Filesystem_Direct(null); |
| 249 | $js = $wp_filesystem->get_contents($file_path); |
| 250 | unset($wp_filesystem); |
| 251 | if (empty($js)) { |
| 252 | $request = wp_remote_get($file_url); |
| 253 | $js = wp_remote_retrieve_body($request); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | //$combined_ex_js_arr[$handle] = ; |
| 258 | $jscontent .= "\n/*File: $file_url*/\n".$localize.$js; |
| 259 | |
| 260 | //wp_deregister_script($handle); |
| 261 | } |
| 262 | } |
| 263 | if($jscontent){ |
| 264 | $fileSystem = new WP_Filesystem_Direct( new StdClass() ); |
| 265 | if(!file_exists($user_dirname)) wp_mkdir_p($user_dirname); |
| 266 | $fileSystem->put_contents($user_dirname.'/'.$filename.'.js', $jscontent, 644 ); |
| 267 | unset($fileSystem); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | |
| 272 | $uniqueid = get_transient( CWVPSB_CACHE_NAME ); |
| 273 | global $wp; |
| 274 | $url = home_url( $wp->request ); |
| 275 | $filename = md5($url.$uniqueid); |
| 276 | $user_dirname = CWVPSB_JS_EXCLUDE_CACHE_DIR; |
| 277 | $user_urlname = CWVPSB_JS_EXCLUDE_CACHE_URL; |
| 278 | |
| 279 | if(file_exists($user_dirname.'/'.$filename.'.js')){ |
| 280 | wp_register_script('corewvps-mergejsfile', $user_urlname.'/'.$filename.'.js', array(), CWVPSB_VERSION, true); |
| 281 | wp_enqueue_script('corewvps-mergejsfile'); |
| 282 | } |
| 283 | |
| 284 | } |
| 285 | add_filter( 'script_loader_src', 'cwvpsb_remove_css_js_version', 9999, 2 ); |
| 286 | function cwvpsb_remove_css_js_version($src, $handle ){ |
| 287 | $handles_with_version = [ 'corewvps-mergejsfile', 'corewvps-cc','corewvps-mergecssfile' ]; |
| 288 | if ( strpos( $src, 'ver=' ) && in_array( $handle, $handles_with_version, true ) ){ |
| 289 | //$src = remove_query_arg( 'ver', $src ); |
| 290 | } |
| 291 | $src = add_query_arg( 'time', time(), $src ); |
| 292 | return $src; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | function cwvpsb_merge_js_scripts(){ |
| 297 | global $wp_scripts; |
| 298 | $wp_scripts->all_deps($wp_scripts->queue); |
| 299 | |
| 300 | $uniqueid = get_transient( CWVPSB_CACHE_NAME ); |
| 301 | global $wp; |
| 302 | $url = home_url( $wp->request ); |
| 303 | $filename = md5($url.$uniqueid); |
| 304 | $user_dirname = CWVPSB_JS_MERGE_FILE_CACHE_DIR; |
| 305 | $user_urlname = CWVPSB_JS_MERGE_FILE_CACHE_CACHE_URL; |
| 306 | |
| 307 | if(!file_exists($user_dirname.'/'.$filename.'.js')){ |
| 308 | $combined_ex_js_arr= array(); |
| 309 | $jscontent = ''; |
| 310 | $regex = cwvpsb_delay_exclude_js(); |
| 311 | include_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; |
| 312 | include_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; |
| 313 | if (!class_exists('WP_Filesystem_Direct')) { |
| 314 | return false; |
| 315 | } |
| 316 | $wp_scripts->all_deps($wp_scripts->queue); |
| 317 | foreach( $wp_scripts->to_do as $key=>$handle) |
| 318 | { |
| 319 | $localize = ''; |
| 320 | $localize_handle = 'cwvpsb-merged-js'; |
| 321 | //$src = strtok($wp_scripts->registered[$handle]->src, '?'); |
| 322 | if($regex && preg_match( '#(' . $regex . ')#', $wp_scripts->registered[$handle]->src )){ |
| 323 | $localize_handle = $handle; |
| 324 | } |
| 325 | if($regex && preg_match( '#(' . $regex . ')#', $handle )){ |
| 326 | $localize_handle = $handle; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | if($localize_handle == 'cwvpsb-merged-js'){ |
| 331 | if(@array_key_exists('data', $wp_scripts->registered[$handle]->extra)) { |
| 332 | $localize = $wp_scripts->registered[$handle]->extra['data'] . ';'; |
| 333 | } |
| 334 | $file_url = $wp_scripts->registered[$handle]->src; |
| 335 | $parse_url = parse_url($file_url); |
| 336 | $file_path = str_replace(array(get_site_url(),'?'.@$parse_url['query']),array(ABSPATH,''),$file_url); |
| 337 | |
| 338 | if(substr( $file_path, 0, 13 ) === "/wp-includes/"){ |
| 339 | $file_path = ABSPATH.$file_path; |
| 340 | } |
| 341 | $wp_filesystem = new WP_Filesystem_Direct(null); |
| 342 | if(file_exists($file_path)){ |
| 343 | $js = $wp_filesystem->get_contents($file_path); |
| 344 | } |
| 345 | unset($wp_filesystem); |
| 346 | if (empty($js)) { |
| 347 | $request = wp_remote_get($file_url); |
| 348 | $js = wp_remote_retrieve_body($request); |
| 349 | } |
| 350 | |
| 351 | |
| 352 | //$combined_ex_js_arr[$handle] = ; |
| 353 | $jscontent .= "\n/*File: $file_url*/\n".$localize.$js; |
| 354 | |
| 355 | //wp_deregister_script($handle); |
| 356 | } |
| 357 | } |
| 358 | if($jscontent){ |
| 359 | $fileSystem = new WP_Filesystem_Direct( new StdClass() ); |
| 360 | if(!file_exists($user_dirname)) wp_mkdir_p($user_dirname); |
| 361 | $fileSystem->put_contents($user_dirname.'/'.$filename.'.js', $jscontent, 644 ); |
| 362 | unset($fileSystem); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | |
| 367 | $uniqueid = get_transient( CWVPSB_CACHE_NAME ); |
| 368 | global $wp; |
| 369 | $url = home_url( $wp->request ); |
| 370 | $filename = md5($url.$uniqueid); |
| 371 | $user_dirname = CWVPSB_JS_MERGE_FILE_CACHE_DIR; |
| 372 | $user_urlname = CWVPSB_JS_MERGE_FILE_CACHE_CACHE_URL; |
| 373 | |
| 374 | if(file_exists($user_dirname.'/'.$filename.'.js')){ |
| 375 | wp_register_script('corewvps-delayjs-mergedfile', $user_urlname.'/'.$filename.'.js', array(), CWVPSB_VERSION, true); |
| 376 | wp_enqueue_script('corewvps-delayjs-mergedfile'); |
| 377 | } |
| 378 | |
| 379 | } |
| 380 | |
| 381 | function cwvpsb_sanitize_js( $file ) { |
| 382 | $file = preg_replace( '#\?.*$#', '', $file ); |
| 383 | $ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) ); |
| 384 | return ( 'js' === $ext ) ? trim( $file ) : false; |
| 385 | } |
| 386 | |
| 387 | function cwvpsb_delay_js_load() { |
| 388 | echo '<script id="cwvpsb-delayed-scripts" data-two-no-delay="true"> |
| 389 | cwvpsbUserInteractions=["keydown","mousemove","wheel","touchmove","touchstart","touchend","touchcancel","touchforcechange"],cwvpsbDelayedScripts={normal:[],defer:[],async:[],jquery:[]},jQueriesArray=[];var cwvpsbDOMLoaded=!1; |
| 390 | function cwvpsbTriggerDOMListener(){cwvpsbUserInteractions.forEach(function(e){window.removeEventListener(e,cwvpsbTriggerDOMListener,{passive:!0})}),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",cwvpsbTriggerDelayedScripts):cwvpsbTriggerDelayedScripts()} |
| 391 | |
| 392 | var time = Date.now; |
| 393 | var ccfw_loaded = false; |
| 394 | function calculate_load_times() { |
| 395 | // Check performance support |
| 396 | if (performance === undefined) { |
| 397 | console.log("= Calculate Load Times: performance NOT supported"); |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | // Get a list of "resource" performance entries |
| 402 | var resources_length=0; |
| 403 | var resources = performance.getEntriesByType("resource"); |
| 404 | if (resources === undefined || resources.length <= 0) { |
| 405 | console.log("= Calculate Load Times: there are NO `resource` performance records"); |
| 406 | } |
| 407 | if(resources.length) |
| 408 | { |
| 409 | resources_length=resources.length; |
| 410 | } |
| 411 | |
| 412 | let is_last_resource = 0; |
| 413 | for (var i=0; i < resources.length; i++) { |
| 414 | if(resources[i].responseEnd>0){ |
| 415 | is_last_resource = is_last_resource + 1; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | let uag = navigator.userAgent; |
| 420 | let gpat = /Google Page Speed Insights/gm; |
| 421 | let gres = uag.match(gpat); |
| 422 | let cpat = /Chrome-Lighthouse/gm; |
| 423 | let cres = uag.match(cpat); |
| 424 | let wait_till=300; |
| 425 | let new_ua = "Mozilla/5.0 (Linux; Android 11; moto g power (2022)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Mobile Safari/537.36"; |
| 426 | if(gres || cres || uag==new_ua){ |
| 427 | wait_till = 3000; |
| 428 | } |
| 429 | if(is_last_resource==resources.length){ |
| 430 | setTimeout(function(){ |
| 431 | cwvpsbTriggerDelayedScripts(); |
| 432 | },wait_till); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | window.addEventListener("load", function(e) { |
| 437 | console.log("load complete"); |
| 438 | setTimeout(function(){ |
| 439 | calculate_load_times(); |
| 440 | },800); |
| 441 | }); |
| 442 | |
| 443 | async function cwvpsbTriggerDelayedScripts() { |
| 444 | if(ccfw_loaded){ return ;} |
| 445 | ctl(),cwvpsbDelayEventListeners(), cwvpsbDelayJQueryReady(), cwvpsbProcessDocumentWrite(), cwvpsbSortDelayedScripts(), cwvpsbPreloadDelayedScripts(),await cwvpsbLoadDelayedScripts(cwvpsbDelayedScripts.jquery), await cwvpsbLoadDelayedScripts(cwvpsbDelayedScripts.normal), await cwvpsbLoadDelayedScripts(cwvpsbDelayedScripts.defer), await cwvpsbLoadDelayedScripts(cwvpsbDelayedScripts.async), await cwvpsbTriggerEventListeners() |
| 446 | } |
| 447 | |
| 448 | function cwvpsbDelayEventListeners() { |
| 449 | let e = {}; |
| 450 | |
| 451 | function t(t, n) { |
| 452 | function r(n) { |
| 453 | return e[t].delayedEvents.indexOf(n) >= 0 ? "cwvpsb-" + n : n |
| 454 | } |
| 455 | e[t] || (e[t] = { |
| 456 | originalFunctions: { |
| 457 | add: t.addEventListener, |
| 458 | remove: t.removeEventListener |
| 459 | }, |
| 460 | delayedEvents: [] |
| 461 | }, t.addEventListener = function() { |
| 462 | arguments[0] = r(arguments[0]), e[t].originalFunctions.add.apply(t, arguments) |
| 463 | }, t.removeEventListener = function() { |
| 464 | arguments[0] = r(arguments[0]), e[t].originalFunctions.remove.apply(t, arguments) |
| 465 | }), e[t].delayedEvents.push(n) |
| 466 | } |
| 467 | |
| 468 | function n(e, t) { |
| 469 | const n = e[t]; |
| 470 | Object.defineProperty(e, t, { |
| 471 | get: n || function() {}, |
| 472 | set: function(n) { |
| 473 | e["cwvpsb" + t] = n |
| 474 | } |
| 475 | }) |
| 476 | } |
| 477 | t(document, "DOMContentLoaded"), t(window, "DOMContentLoaded"), t(window, "load"), t(window, "pageshow"), t(document, "readystatechange"), n(document, "onreadystatechange"), n(window, "onload"), n(window, "onpageshow") |
| 478 | } |
| 479 | |
| 480 | function cwvpsbDelayJQueryReady() { |
| 481 | let e = window.jQuery; |
| 482 | Object.defineProperty(window, "jQuery", { |
| 483 | get: () => e, |
| 484 | set(t) { |
| 485 | if (t && t.fn && !jQueriesArray.includes(t)) { |
| 486 | t.fn.ready = t.fn.init.prototype.ready = function(e) { |
| 487 | cwvpsbDOMLoaded ? e.bind(document)(t) : document.addEventListener("cwvpsb-DOMContentLoaded", function() { |
| 488 | e.bind(document)(t) |
| 489 | }) |
| 490 | }; |
| 491 | const e = t.fn.on; |
| 492 | t.fn.on = t.fn.init.prototype.on = function() { |
| 493 | if (this[0] === window) { |
| 494 | function t(e) { |
| 495 | return e.split(" ").map(e => "load" === e || 0 === e.indexOf("load.") ? "cwvpsb-jquery-load" : e).join(" ") |
| 496 | } |
| 497 | "string" == typeof arguments[0] || arguments[0] instanceof String ? arguments[0] = t(arguments[0]) : "object" == typeof arguments[0] && Object.keys(arguments[0]).forEach(function(e) { |
| 498 | delete Object.assign(arguments[0], { |
| 499 | [t(e)]: arguments[0][e] |
| 500 | })[e] |
| 501 | }) |
| 502 | } |
| 503 | return e.apply(this, arguments), this |
| 504 | }, jQueriesArray.push(t) |
| 505 | } |
| 506 | e = t |
| 507 | } |
| 508 | }) |
| 509 | } |
| 510 | |
| 511 | function cwvpsbProcessDocumentWrite() { |
| 512 | const e = new Map; |
| 513 | document.write = document.writeln = function(t) { |
| 514 | var n = document.currentScript, |
| 515 | r = document.createRange(); |
| 516 | let a = e.get(n); |
| 517 | void 0 === a && (a = n.nextSibling, e.set(n, a)); |
| 518 | var o = document.createDocumentFragment(); |
| 519 | r.setStart(o, 0), o.appendChild(r.createContextualFragment(t)), n.parentElement.insertBefore(o, a) |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | function cwvpsbSortDelayedScripts() { |
| 524 | document.querySelectorAll("script[type=cwvpsbdelayedscript]").forEach(function(e) { |
| 525 | e.hasAttribute("src")&&(e.getAttribute("src").match("jquery.min.js")||e.getAttribute("src").match("jquery-migrate.min.js"))?cwvpsbDelayedScripts.jquery.push(e):e.hasAttribute("src")?e.hasAttribute("defer")&&!1!==e.defer?cwvpsbDelayedScripts.defer.push(e):e.hasAttribute("async")&&!1!==e.async?cwvpsbDelayedScripts.async.push(e):cwvpsbDelayedScripts.normal.push(e):cwvpsbDelayedScripts.normal.push(e); |
| 526 | }) |
| 527 | } |
| 528 | |
| 529 | function cwvpsbPreloadDelayedScripts() { |
| 530 | var e = document.createDocumentFragment(); |
| 531 | [...cwvpsbDelayedScripts.normal, ...cwvpsbDelayedScripts.defer, ...cwvpsbDelayedScripts.async].forEach(function(t) { |
| 532 | var n = removeVersionFromLink(t.getAttribute("src")); |
| 533 | if (n) { |
| 534 | t.setAttribute("src", n); |
| 535 | var r = document.createElement("link"); |
| 536 | r.href = n, r.rel = "preload", r.as = "script", e.appendChild(r) |
| 537 | } |
| 538 | }), document.head.appendChild(e) |
| 539 | } |
| 540 | async function cwvpsbLoadDelayedScripts(e) { |
| 541 | var t = e.shift(); |
| 542 | return t ? (await cwvpsbReplaceScript(t), cwvpsbLoadDelayedScripts(e)) : Promise.resolve() |
| 543 | } |
| 544 | async function cwvpsbReplaceScript(e) { |
| 545 | return await cwvpsbNextFrame(), new Promise(function(t) { |
| 546 | const n = document.createElement("script"); |
| 547 | [...e.attributes].forEach(function(e) { |
| 548 | let t = e.nodeName; |
| 549 | "type" !== t && ("data-type" === t && (t = "type"), n.setAttribute(t, e.nodeValue)) |
| 550 | }), e.hasAttribute("src") ? (n.addEventListener("load", t), n.addEventListener("error", t)) : (n.text = e.text, t()), e.parentNode.replaceChild(n, e) |
| 551 | }) |
| 552 | } |
| 553 | |
| 554 | function ctl(){ |
| 555 | var cssEle = document.querySelectorAll("link[rel=cwvpsbdelayedstyle]"); |
| 556 | for(var i=0; i <= cssEle.length;i++){ |
| 557 | if(cssEle[i]){ |
| 558 | cssEle[i].href = removeVersionFromLink(cssEle[i].href); |
| 559 | cssEle[i].rel = "stylesheet"; |
| 560 | cssEle[i].type = "text/css"; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | |
| 565 | var cssEle = document.querySelectorAll("style[type=cwvpsbdelayedstyle]"); |
| 566 | for(var i=0; i <= cssEle.length;i++){ |
| 567 | if(cssEle[i]){ |
| 568 | cssEle[i].type = "text/css"; |
| 569 | } |
| 570 | } |
| 571 | ccfw_loaded=true; |
| 572 | } |
| 573 | function removeVersionFromLink(link) |
| 574 | { |
| 575 | if(cwvpbIsValidUrl(link)) |
| 576 | { |
| 577 | const url = new URL(cwvpbFormatLink(link)); |
| 578 | url.searchParams.delete("ver"); |
| 579 | url.searchParams.delete("time"); |
| 580 | return url.href; |
| 581 | } |
| 582 | return link; |
| 583 | } |
| 584 | |
| 585 | function cwvpbIsValidUrl(urlString) |
| 586 | { |
| 587 | if(urlString){ |
| 588 | var expression =/[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi; |
| 589 | var regex = new RegExp(expression); |
| 590 | return urlString.match(regex); |
| 591 | } |
| 592 | return false; |
| 593 | } |
| 594 | function cwvpbFormatLink(link) |
| 595 | { |
| 596 | let http_check=link.match("http:"); |
| 597 | let https_check=link.match("https:"); |
| 598 | if(!http_check && !https_check) |
| 599 | { |
| 600 | return location.protocol+link; |
| 601 | } |
| 602 | return link; |
| 603 | } |
| 604 | |
| 605 | async function cwvpsbTriggerEventListeners() { |
| 606 | cwvpsbDOMLoaded = !0, await cwvpsbNextFrame(), document.dispatchEvent(new Event("cwvpsb-DOMContentLoaded")), await cwvpsbNextFrame(), window.dispatchEvent(new Event("cwvpsb-DOMContentLoaded")), await cwvpsbNextFrame(), document.dispatchEvent(new Event("cwvpsb-readystatechange")), await cwvpsbNextFrame(), document.cwvpsbonreadystatechange && document.cwvpsbonreadystatechange(), await cwvpsbNextFrame(), window.dispatchEvent(new Event("cwvpsb-load")), await cwvpsbNextFrame(), window.cwvpsbonload && window.cwvpsbonload(), await cwvpsbNextFrame(), jQueriesArray.forEach(function(e) { |
| 607 | e(window).trigger("cwvpsb-jquery-load") |
| 608 | }), window.dispatchEvent(new Event("cwvpsb-pageshow")), await cwvpsbNextFrame(), window.cwvpsbonpageshow && window.cwvpsbonpageshow() |
| 609 | } |
| 610 | async function cwvpsbNextFrame() { |
| 611 | return new Promise(function(e) { |
| 612 | requestAnimationFrame(e) |
| 613 | }) |
| 614 | } |
| 615 | cwvpsbUserInteractions.forEach(function(e) { |
| 616 | window.addEventListener(e, cwvpsbTriggerDOMListener, { |
| 617 | passive: !0 |
| 618 | }) |
| 619 | });</script>'; |
| 620 | } |
| 621 | |
| 622 | function cwvpsb_wprocket_lazyjs() |
| 623 | { |
| 624 | if(defined('WP_ROCKET_VERSION')) |
| 625 | { |
| 626 | $cwvpsb_wprocket_options=get_option('wp_rocket_settings',null); |
| 627 | |
| 628 | if(isset($cwvpsb_wprocket_options['defer_all_js']) && $cwvpsb_wprocket_options['defer_all_js']==1) |
| 629 | { |
| 630 | return true; |
| 631 | } |
| 632 | } |
| 633 | return false; |
| 634 | } |
| 635 | |
| 636 | function cwvpsb_add_rocket_delay_js_exclusions( $patterns ) { |
| 637 | $patterns[] = 'cwvpsb-delayed-scripts'; |
| 638 | return $patterns; |
| 639 | } |