'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;
}
$getcss = wp_remote_retrieve_body($response);
return $getcss;
}
function wp_get_remote_font($font_url) {
$backtrace = debug_backtrace();
$function_name = isset($backtrace[1]['function']) ? $backtrace[1]['function'] : '';
$font_response = wp_remote_get($font_url, array('user-agent' => '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($font_response)) {
add_settings_error($function_name, 'http_error', $font_response->get_error_message(), 'error');
return false;
}
$getfont_data = wp_remote_retrieve_body($font_response);
return $getfont_data;
}
function easyfont_process_content_link_tag($content) {
$html = hgfl_str_get_html($content, false, true, 'UTF-8', false, PHP_EOL, ' ');
// 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_css($decoded_url);
if (!preg_match_all('/@font-face\s*\{([^}]+)\}/', $css, $matches)) continue;
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_font($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);
}
}
}
}
}
// Save the modified stylesheet to the 'wp-content/uploads/easyfonts' directory
file_put_contents($easyfonts_dir . '/' . $filename, $css);
}
$finalbasecss = get_basecss_url();
// Replace the href attribute with a local reference to the downloaded stylesheet
$link->href = $finalbasecss . '/easyfonts/' . $filename;
}
// Return the modified content
return $html->save();
}
function easyfont_process_content_import($content) {
// using simple html dom
$html = hgfl_str_get_html($content, false, true, 'UTF-8', false, PHP_EOL, ' ');
// 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