)#is',
function ($style_match) use (&$removed_font_files) {
$css = $style_match[3];
$css = preg_replace_callback(
'#(?:/\*\s*[^*]*\*/\s*)?@font-face\s*\{[^{}]*\}#is',
function ($font_match) use (&$removed_font_files) {
$rule = $font_match[0];
if (!preg_match(
'#font-family\s*:\s*([\'"]?)Open\s+Sans\1#i',
$rule
)) {
return $rule;
}
if (!preg_match(
'#unicode-range\s*:\s*([^;}]+)#i',
$rule,
$range_match
)) {
return $rule;
}
$unicode_range = strtoupper(
preg_replace('/\s+/', '', $range_match[1])
);
// Basic Latin
$is_latin = strpos($unicode_range, 'U+0000-00FF') !== false;
// Basic Cyrillic
$is_cyrillic = strpos($unicode_range, 'U+0400-045F') !== false;
// Cyrillic-ext (нужен для казахских букв Ә Ғ Қ Ң Ө Ұ Ү Һ І)
$cyrillic_ext_markers = [
'U+0460-052F',
'U+1C80-1C88',
'U+20B4',
'U+2DE0-2DFF',
'U+A640-A69F',
'U+FE2E-FE2F',
];
$is_cyrillic_ext = false;
foreach ($cyrillic_ext_markers as $marker) {
if (strpos($unicode_range, $marker) !== false) {
$is_cyrillic_ext = true;
break;
}
}
if ($is_latin || $is_cyrillic || $is_cyrillic_ext) {
return $rule;
}
if (preg_match_all(
'#url\(\s*([\'"]?)(.*?)\1\s*\)#i',
$rule,
$url_matches
)) {
foreach ($url_matches[2] as $font_url) {
$path = wp_parse_url(
html_entity_decode($font_url, ENT_QUOTES | ENT_HTML5, 'UTF-8'),
PHP_URL_PATH
);
if ($path) {
$removed_font_files[] = basename($path);
}
}
}
return '';
},
$css
);
return $style_match[1] . $css . $style_match[4];
},
$html
);
$removed_font_files = array_values(array_unique($removed_font_files));
if (!empty($removed_font_files)) {
$html = preg_replace_callback(
'#]*>#i',
function ($matches) use ($removed_font_files) {
$link = $matches[0];
if (
!preg_match('#\brel\s*=\s*([\'"])preload\1#i', $link) ||
!preg_match('#\bas\s*=\s*([\'"])font\1#i', $link)
) {
return $link;
}
if (!preg_match('#\bhref\s*=\s*([\'"])(.*?)\1#i', $link, $href_match)) {
return $link;
}
$path = wp_parse_url(
html_entity_decode($href_match[2], ENT_QUOTES | ENT_HTML5, 'UTF-8'),
PHP_URL_PATH
);
if (!$path) {
return $link;
}
if (in_array(basename($path), $removed_font_files, true)) {
return '';
}
return $link;
},
$html
);
}
return $html;
}, 999);