@@ -554,6 +554,120 @@ private function _maybe_js_delay()
554
554
$ this ->html_foot .= '<script> ' . File::read (LSCWP_DIR . self ::LIB_FILE_JS_DELAY ) . '</script> ' ;
555
555
}
556
556
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
+ // Has italic only.
600
+ if ($ options [1 ] == 'ital ' ){
601
+ foreach ( [100 ,200 ,300 ,400 ,500 ,600 ,700 ,800 ,900 ] as $ weight ){
602
+ $ add_values_array [] = $ weight ;
603
+ $ add_values_array [] = $ weight .'italic ' ;
604
+ }
605
+ }
606
+ else {
607
+ // Font_modifier and Font_modifier_values.
608
+ $ options = explode ('@ ' , $ options [1 ]);
609
+
610
+ // Font modifiers and values
611
+ $ font_modifier = explode (', ' , $ options [0 ]);
612
+ $ font_modifier_values = explode ('; ' , $ options [1 ]);
613
+
614
+ // Go through each values.
615
+ foreach ($ font_modifier_values as $ k => &$ modifier_value ){
616
+ // Get array of values for each combination.
617
+ $ modifier_value_array = explode (', ' , $ modifier_value );
618
+
619
+ // Italic key.
620
+ $ italic_key = array_search ('ital ' , $ font_modifier );
621
+
622
+ foreach ($ font_modifier as $ k_mod => $ value_modifier ){
623
+ if ($ value_modifier === 'wght ' ){
624
+ // Weight setting.
625
+ $ value = $ modifier_value_array [$ k_mod ];
626
+
627
+ // If height has "..".
628
+ if ( strpos ($ value , '.. ' )){
629
+ $ limits = explode ('.. ' , $ value );
630
+ $ limits [0 ] = $ this ->round_to_hundred ($ limits [0 ]);
631
+ $ limits [1 ] = $ this ->round_to_hundred ($ limits [1 ], false );
632
+
633
+ $ value = [];
634
+ for ($ i = $ limits [0 ]; $ i <= $ limits [1 ]; $ i = $ i + 100 ){
635
+ $ value [] = $ i ;
636
+ }
637
+ }
638
+ else {
639
+ // If single value.
640
+ $ value = array ($ value );
641
+ }
642
+
643
+
644
+ foreach ($ value as $ weight ){
645
+ // Test if italic is set to 1.
646
+ $ italic = ( $ italic_key && $ modifier_value_array [$ italic_key ] ? 'italic ' : '' );
647
+
648
+ // Add value to family parameters.
649
+ $ add_values_array [] = $ weight .$ italic ;
650
+ }
651
+ }
652
+ }
653
+ }
654
+ }
655
+
656
+ $ add_data .= implode (', ' , $ add_values_array );
657
+ }
658
+
659
+ $ families [] = $ add_data ;
660
+ }
661
+ }
662
+
663
+ $ new_qs .= implode ('| ' , $ families );
664
+ if ($ has_display ){
665
+ $ new_qs .= '&display=swap ' ;
666
+ }
667
+
668
+ return $ new_qs ;
669
+ }
670
+
557
671
/**
558
672
* Google font async
559
673
*
@@ -575,17 +689,29 @@ private function _async_ggfonts()
575
689
*
576
690
* Could be multiple fonts
577
691
*
692
+ * CSS API V1
578
693
* <link rel='stylesheet' href='//fonts.googleapis.com/css?family=Open+Sans%3A400%2C600%2C700%2C800%2C300&ver=4.9.8' type='text/css' media='all' />
579
694
* <link rel='stylesheet' href='//fonts.googleapis.com/css?family=PT+Sans%3A400%2C700%7CPT+Sans+Narrow%3A400%7CMontserrat%3A600&subset=latin&ver=4.9.8' type='text/css' media='all' />
580
695
* -> family: PT Sans:400,700|PT Sans Narrow:400|Montserrat:600
581
696
* <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,300,300italic,400italic,600,700,900&subset=latin%2Clatin-ext' />
697
+ *
698
+ * CSS API V2
699
+ * <link rel='stylesheet' href='https://fonts.googleapis.com/css2?family=Manrope:[email protected] &display=swap' />
700
+ * <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' />
701
+ * <link rel='stylesheet' href='https://fonts.googleapis.com/css2?family=Honk&family=Playwrite+DK+Uloopet:[email protected] &family=Playwrite+HR:[email protected] &family=Playwrite+HU:[email protected] &display=swap' />
582
702
*/
583
703
$ script = 'WebFontConfig={google:{families:[ ' ;
584
704
585
705
$ families = array ();
586
706
foreach ($ this ->_ggfonts_urls as $ v ) {
587
707
$ qs = wp_specialchars_decode ($ v );
588
708
$ qs = urldecode ($ qs );
709
+
710
+ // Try to convert API V2 to V1.
711
+ if ( strpos ($ qs , '/css2? ' ) ){
712
+ $ qs = $ this ->convert_to_api_v1 ($ qs );
713
+ }
714
+
589
715
$ qs = parse_url ($ qs , PHP_URL_QUERY );
590
716
parse_str ($ qs , $ qs );
591
717
0 commit comments