SpeedyCache '; foreach($fonts as $font_name => $url){ $url = esc_url($url); if(substr($url, 0, 2) === '//'){ $url = 'https:' . $url; } $response = wp_remote_get($url, array('user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36')); if(is_wp_error($response) || !is_array($response)){ continue; } $css = wp_remote_retrieve_body($response); if(is_wp_error($css) || empty($css)){ continue; } if(!file_exists(speedycache_cache_path('fonts'))){ @mkdir(speedycache_cache_path('fonts')); } if(!file_exists(speedycache_cache_path('fonts/index.html'))){ file_put_contents(speedycache_cache_path('fonts/index.html'), $html); } if(!file_exists(speedycache_cache_path('fonts/').$font_name)){ @mkdir(speedycache_cache_path('fonts/').$font_name); } if(!file_exists(speedycache_cache_path('fonts/').$font_name . '/index.html')){ file_put_contents(speedycache_cache_path('fonts/').$font_name . '/index.html', $html); } preg_match_all('/url\((.*?)\)/m', $response['body'], $urls); // Get URL from the CSS we got if(empty($urls) || empty($urls[1])){ continue; } foreach($urls[1] as $url){ $file_name = basename($url); if(file_exists(speedycache_cache_path('fonts/') . $font_name . '/' . $file_name)){ continue; } if(strpos($url, 'display=swap') === FALSE){ $url_to_hit = add_query_arg(array('display' => 'swap'), $url); } $response = wp_remote_get($url_to_hit); if(is_wp_error($response) || !is_array($response)){ continue; } $font = wp_remote_retrieve_body($response); if(is_wp_error($font) || empty($font)){ continue; } file_put_contents(speedycache_cache_path('fonts/').$font_name.'/'.$file_name, $font); // Creating the font file $css = str_replace($url, SPEEDYCACHE_CACHE_URL .'/'. SPEEDYCACHE_SERVER_HOST . '/fonts/'. $font_name .'/'. $file_name, $css); } if(file_exists(speedycache_cache_path('fonts/').$font_name.'/'.$font_name . '.css')){ return; } //If we need to add swap then either we failed to add display=swap to the url or it didnt return what we expected. if(strpos($css, 'swap') === FALSE){ $css = preg_replace('/(^@font-face\s{)/m', "$1\n font-display: swap;", $css); } file_put_contents(speedycache_cache_path('fonts/').$font_name.'/'.$font_name . '.css', $css); } } // Replaces font url to the local font url static function replace($content){ $cache_dir = speedycache_cache_path(); if(!is_dir($cache_dir . '/fonts')){ @mkdir($cache_dir . '/fonts', 0755, true); } $fonts = array_diff(@scandir($cache_dir . 'fonts'), array('..', '.')); if(empty($fonts)){ return $content; } // To remove any preload or dns-fetch or preconnect for google fonts preg_match_all('/]+)?href=(["\'])([^>]*?fonts\.(gstatic|googleapis)\.com.*?)\1.*?>/i', $content, $google_links, PREG_SET_ORDER); if(!empty($google_links)){ foreach($google_links as $google_link){ preg_match('/rel=(["\'])(.*?(preload|preconnect|dns-fetch).*?)\1/i', $google_link[2], $removeable_link); if(!empty($removeable_link)){ $content = str_replace($google, '', $html); } } } /** * Our Font css name is in md5(created from the font URL) and we dont have URL in this function to get * all the google fonts url to replace the fonts . */ preg_match_all('/