'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) use ($content) { $upload_dir = wp_upload_dir(); $easyfonts_dir = $upload_dir['basedir'] . '/easyfonts'; if (!wp_mkdir_p($easyfonts_dir)) { return $content; } // Filter-based support toggle for Bunny Fonts $apply_bunnyfonts_filter = apply_filters('easyfonts_bunnyfonts', true); // List of supported font providers $font_providers = [ 'fonts.googleapis.com', ]; if ($apply_bunnyfonts_filter) { $font_providers[] = 'fonts.bunny.net'; } foreach ($html->find('link[rel=stylesheet]') as $link) { $href = $link->href; $matched = false; foreach ($font_providers as $provider) { if (strpos($href, $provider) !== false) { $matched = true; break; } } if (!$matched) continue; // Normalize URL if (strpos($href, '//') === 0) { $href = 'https:' . $href; } $decoded_url = rawurldecode(htmlspecialchars_decode($href)); $filename = substr(hash('sha256', $decoded_url), 0, 10) . '.css'; $local_css_path = $easyfonts_dir . '/' . $filename; if (!file_exists($local_css_path)) { $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); file_put_contents($local_css_path, $css); } $finalbasecss = get_base_url(); $link->href = $finalbasecss . '/easyfonts/' . $filename; } return $html; }); } 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