APC
3 days ago
Events
3 days ago
Purger
3 days ago
assets
3 days ago
AHSC_Apc.php
3 days ago
AHSC_Check.php
3 days ago
AHSC_Config.php
3 days ago
AHSC_Dboptimization.php
3 days ago
AHSC_Functions.php
3 days ago
AHSC_HtmlOptimizer.php
5 months ago
AHSC_Lazyload.php
3 days ago
AHSC_Preconnect.php
5 months ago
AHSC_Static.php
4 months ago
AHSC_Version.php
5 months ago
AHSC_Warmer.php
3 days ago
AHSC_XmlRPC.php
5 months ago
index.php
5 months ago
AHSC_Preconnect.php
99 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | if(isset(AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_dns_preconnect']) |
| 6 | && AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS']['ahsc_dns_preconnect']!=false) { |
| 7 | |
| 8 | function ahsc_dns_prefetch_to_preconnect( $urls, $relation_type ) { |
| 9 | global $wp_scripts, $wp_styles; |
| 10 | $unique_urls = array(); |
| 11 | |
| 12 | foreach ( array( $wp_scripts, $wp_styles ) as $dependencies ) { |
| 13 | if ( $dependencies instanceof WP_Dependencies && ! empty( $dependencies->queue ) ) { |
| 14 | foreach ( $dependencies->queue as $handle ) { |
| 15 | |
| 16 | if ( ! isset( $dependencies->registered[ $handle ] ) ) { |
| 17 | continue; |
| 18 | } |
| 19 | |
| 20 | $dependency = $dependencies->registered[ $handle ]; |
| 21 | $parsed = wp_parse_url( $dependency->src ); |
| 22 | |
| 23 | if ( ! empty( $parsed['host'] ) && ! in_array( $parsed['host'], $unique_urls ) && isset($_SERVER['SERVER_NAME']) && $parsed['host'] !== $_SERVER['SERVER_NAME'] ) { |
| 24 | if ( 'preconnect' === $relation_type ) { |
| 25 | $unique_urls[] = array( |
| 26 | 'href' => $parsed['scheme'] . '://' . $parsed['host'], |
| 27 | 'crossorigin', |
| 28 | ); |
| 29 | } else { |
| 30 | $unique_urls[] = array( |
| 31 | 'href' => $parsed['scheme'] . '://' . $parsed['host'], |
| 32 | ); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | $site_option=get_site_option( AHSC_CONSTANT['ARUBA_HISPEED_CACHE_OPTIONS_NAME'] ); |
| 40 | $option = ($site_option)?$site_option: AHSC_OPTIONS_LIST; |
| 41 | if(isset($option['ahsc_dns_preconnect_domains']) && $option['ahsc_dns_preconnect_domains']!==""){ |
| 42 | foreach($option['ahsc_dns_preconnect_domains'] as $custom_domain){ |
| 43 | |
| 44 | $custom_domain_parsed=wp_parse_url( esc_url($custom_domain,array( |
| 45 | 'https' |
| 46 | )) ); |
| 47 | if(array_search($custom_domain,$unique_urls)===false && isset( $custom_domain_parsed['host'] ) && isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST']!==$custom_domain_parsed['host']) { |
| 48 | if ( isset( $custom_domain_parsed['scheme'] ) && isset( $custom_domain_parsed['host'] ) ) { |
| 49 | |
| 50 | // $unique_urls[] = array( |
| 51 | // 'href' => $custom_domain_parsed['scheme'] . '://' . $custom_domain_parsed['host'], |
| 52 | // ); |
| 53 | |
| 54 | // Use scheme and host coming from the original URL |
| 55 | $scheme = $custom_domain_parsed['scheme']; |
| 56 | $host = $custom_domain_parsed['host']; |
| 57 | // echo "<script>console.log('scheme:', " . json_encode($scheme) . ");</script>"; |
| 58 | |
| 59 | // Added sample bug to simulate the issue |
| 60 | // $bug_suffix = (rand(0,1) === 1) ? 'https' : 'http'; |
| 61 | // $href = $host . $bug_suffix; |
| 62 | |
| 63 | // If the host is malformed fix it |
| 64 | $host_fixed = preg_replace('/http[s]?$/i', '', $host); |
| 65 | |
| 66 | // If something goes wrong and the host becomes empty, skip it |
| 67 | if ( $host_fixed === '' ) { |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | // Build href based on the type of hint |
| 72 | if ('dns-prefetch' === $relation_type) { |
| 73 | $href = '//' . $host_fixed; |
| 74 | } else { |
| 75 | $href = $scheme . '://' . $host_fixed; |
| 76 | } |
| 77 | |
| 78 | $unique_urls[] = array( |
| 79 | 'href' => $href, |
| 80 | ); |
| 81 | |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if ( 'preconnect' === $relation_type || 'dns-prefetch' === $relation_type) { |
| 90 | $urls = $unique_urls; |
| 91 | } |
| 92 | return $urls; |
| 93 | } |
| 94 | if(!is_admin()){ |
| 95 | add_filter( 'wp_resource_hints', 'ahsc_dns_prefetch_to_preconnect', 0, 2 ); |
| 96 | } |
| 97 | }else{ |
| 98 | remove_action('wp_head', 'wp_resource_hints', 2); |
| 99 | } |