Skip to content

Commit 2bd5a53

Browse files
committed
Revised solution
1 parent e72f969 commit 2bd5a53

File tree

1 file changed

+114
-7
lines changed

1 file changed

+114
-7
lines changed

src/optimize.cls.php

Lines changed: 114 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,117 @@ private function _maybe_js_delay()
554554
$this->html_foot .= '<script>' . File::read(LSCWP_DIR . self::LIB_FILE_JS_DELAY) . '</script>';
555555
}
556556

557+
private function round_to_hundred($number, $min = true){
558+
return round($number, -2, ( $min ? PHP_ROUND_HALF_DOWN : PHP_ROUND_HALF_UP ) );
559+
}
560+
561+
private function convert_to_api_v1($qs){
562+
// Replace css2? withs css?.
563+
$args = str_replace('css2?', 'css?', $qs);
564+
// Save if has display and remove it.
565+
$has_display = strpos($args, '&display=swap');
566+
$args = str_replace('&display=swap', '', $args);
567+
// Get families.
568+
$args = explode('?', $args);
569+
// Prepare new QS.
570+
$new_qs = $args[0].'?family=';
571+
// Work with families list.
572+
$args = $args[1];
573+
// Replace family= with ''.
574+
$args = str_replace('family=', '', $args);
575+
// Families to array.
576+
$args = explode('&', $args);
577+
// New families.
578+
$families = array();
579+
580+
// Work on each family.
581+
foreach($args as $arg){
582+
// Family does not have options.
583+
if(!strpos($arg, ':')){
584+
$families[] = $arg;
585+
}
586+
// Family have options.
587+
else{
588+
$add_data = '';
589+
$options = explode(':', $arg);
590+
// Convert font name.
591+
$add_data .= $options[0];
592+
593+
594+
// Convert font options.
595+
if(isset($options[1])){
596+
$add_values_array = [];
597+
$add_data .= ':';
598+
599+
if($options[1] == 'ital'){
600+
foreach( [100,200,300,400,500,600,700,800,900] as $weight ){
601+
$add_values_array[] = $weight;
602+
$add_values_array[] = $weight.'italic';
603+
}
604+
}
605+
else{
606+
$options = explode('@', $options[1]);
607+
608+
// Font modifiers and values
609+
$font_modifier = explode(',', $options[0]);
610+
$font_modifier_values = explode(';', $options[1]);
611+
612+
// Modify values to css1.
613+
foreach($font_modifier_values as $k => &$modifier_value){
614+
$modifier_value_array = explode(',', $modifier_value);
615+
616+
// Italic key.
617+
$italic_key = array_search('ital', $font_modifier);
618+
619+
foreach($font_modifier as $k_mod => $value_modifier){
620+
if($value_modifier === 'wght'){
621+
// Weight setting.
622+
$value = $modifier_value_array[$k_mod];
623+
624+
// If height has "..".
625+
if( strpos($value, '..' )){
626+
$limits = explode('..', $value);
627+
$limits[0] = $this->round_to_hundred($limits[0]);
628+
$limits[1] = $this->round_to_hundred($limits[1], false);
629+
630+
$value = [];
631+
for($i = $limits[0]; $i <= $limits[1]; $i = $i + 100 ){
632+
$value[] = $i;
633+
}
634+
}
635+
else{
636+
// If single value.
637+
$value = array($value);
638+
}
639+
640+
641+
foreach($value as $weight){
642+
// Test if italic is set to 1.
643+
$italic = ( $italic_key && $modifier_value_array[$italic_key] ? 'italic' : '' );
644+
645+
// Add value to family parameters.
646+
$add_values_array[] = $weight.$italic;
647+
}
648+
}
649+
}
650+
}
651+
}
652+
653+
$add_data .= implode(',', $add_values_array);
654+
}
655+
656+
$families[] = $add_data;
657+
}
658+
}
659+
660+
$new_qs .= implode('|', $families);
661+
if($has_display){
662+
$new_qs .= '&display=swap';
663+
}
664+
665+
return $new_qs;
666+
}
667+
557668
/**
558669
* Google font async
559670
*
@@ -575,7 +686,7 @@ private function _async_ggfonts()
575686
*
576687
* Could be multiple fonts
577688
*
578-
* CSS API V1
689+
* CSS API V1
579690
* <link rel='stylesheet' href='//fonts.googleapis.com/css?family=Open+Sans%3A400%2C600%2C700%2C800%2C300&#038;ver=4.9.8' type='text/css' media='all' />
580691
* <link rel='stylesheet' href='//fonts.googleapis.com/css?family=PT+Sans%3A400%2C700%7CPT+Sans+Narrow%3A400%7CMontserrat%3A600&#038;subset=latin&#038;ver=4.9.8' type='text/css' media='all' />
581692
* -> family: PT Sans:400,700|PT Sans Narrow:400|Montserrat:600
@@ -584,7 +695,7 @@ private function _async_ggfonts()
584695
* CSS API V2
585696
* <link rel='stylesheet' href='https://fonts.googleapis.com/css2?family=Manrope:wght@200..800&display=swap' />
586697
* <link rel='stylesheet' href='https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap' />
587-
* <link rel='stylesheet' href='https://fonts.googleapis.com/css2?family=Playwrite+ES+Deco:wght@100..400&family=Playwrite+US+Trad:wght@100..400&family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap' />
698+
* <link rel='stylesheet' href='https://fonts.googleapis.com/css2?family=Honk&family=Playwrite+DK+Uloopet:wght@100..400&family=Playwrite+HR:wght@100..400&family=Playwrite+HU:wght@100..400&display=swap' />
588699
*/
589700
$script = 'WebFontConfig={google:{families:[';
590701

@@ -595,11 +706,7 @@ private function _async_ggfonts()
595706

596707
// Try to convert API V2 to V1.
597708
if( strpos($qs, '/css2?') ){
598-
$qs = str_replace(
599-
array( 'css2?', 'ital,wght@', 'wght@', 'ital@', '0,', '1,', ';', '&family=' ),
600-
array( 'css?', '', '', '', 'bold', 'italic', ',', '|' ),
601-
$qs
602-
);
709+
$qs = $this->convert_to_api_v1($qs);
603710
}
604711

605712
$qs = parse_url($qs, PHP_URL_QUERY);

0 commit comments

Comments
 (0)