'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',)); if (is_wp_error($response)) { add_settings_error($function_name, 'http_error', $response->get_error_message(), 'error'); return false; } return wp_remote_retrieve_body($response); } function easyfont_process_html_with_dom($content, $callback) { // using simple html dom $html = hgfl_str_get_html($content, false, true, 'UTF-8', false, PHP_EOL, ' '); if (empty($html)) { return $content; } $callback($html); $content = $html->save(); return $content; } function easyfonts_process_font_face_declarations($css, $easyfonts_dir) { if (preg_match_all('/@font-face\s*\{([^}]+)\}/', $css, $matches)) { foreach ($matches[1] as $match) { if (preg_match_all('/src\s*:\s*([^;]+);/', $match, $srcs)) { foreach ($srcs[1] as $src) { if (preg_match('/url\(([^)]+)\)/', $src, $url_match)) { $font_url = trim($url_match[1], "'\""); $font_filename = hash('sha256', $font_url, false); $font_filename = substr($font_filename, 0, 10) . '.' . pathinfo($font_url, PATHINFO_EXTENSION); if (!file_exists($easyfonts_dir . '/' . $font_filename)) { $font_data = wp_get_remote_file($font_url); // Save the font file to the 'wp-content/uploads/easyfonts' directory file_put_contents($easyfonts_dir . '/' . $font_filename, $font_data); } // Replace the src property with a local reference to the downloaded font file $finalbase = get_base_url(); $css = str_replace($src, "url('" . $finalbase . "/easyfonts/" . $font_filename . "')", $css); } } } } } return $css; } function easyfont_process_content_link_tag($content) { return easyfont_process_html_with_dom($content, function($html) { // Check if the 'easyfonts' directory exists and is writable $easyfonts_dir = wp_upload_dir() ['basedir'] . '/easyfonts'; if (!wp_mkdir_p($easyfonts_dir)) { return $content; } // Find all tags with a rel attribute of 'stylesheet' and a href attribute that points to a Google Fonts stylesheet foreach ($html->find('link[rel=stylesheet][href*=fonts.googleapis.com]') as $link) { $url = $link->href; if(strpos($url,'//') === 0){ $url = 'https:'.$url; } $decoded_url = rawurldecode(htmlspecialchars_decode($url)); // Generate a local filename for the stylesheet $filename = hash('sha256', $decoded_url, false); $filename = substr($filename, 0, 10) . '.css'; if (!file_exists($easyfonts_dir . '/' . $filename)) { $css = wp_get_remote_file($decoded_url); if (!preg_match_all('/@font-face\s*\{([^}]+)\}/', $css, $matches)) continue; $css = easyfonts_process_font_face_declarations($css, $easyfonts_dir); // Save the modified stylesheet to the 'wp-content/uploads/easyfonts' directory file_put_contents($easyfonts_dir . '/' . $filename, $css); } $finalbasecss = get_base_url(); // Replace the href attribute with a local reference to the downloaded stylesheet $link->href = $finalbasecss . '/easyfonts/' . $filename; } // Return the modified content }); } function easyfont_process_content_import($content) { // using simple html dom return easyfont_process_html_with_dom($content, function($html) { // Check if the 'easyfonts' directory exists and is writable $easyfonts_dir = wp_upload_dir() ['basedir'] . '/easyfonts'; if (!wp_mkdir_p($easyfonts_dir)) { return $content; } // Find all