CombineV2.php
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
namespace WP_Rocket\Engine\Optimization\GoogleFonts;
use WP_Rocket\Engine\Optimization\RegexTrait;
use WP_Rocket\Logger\Logger;
/**
* Combine v2 Google Fonts
*
* @since 3.8
*/
class CombineV2 extends AbstractGFOptimization {
use RegexTrait;
/**
* Combines multiple Google Fonts (API v2) links into one
*
* @since 3.8
*
* @param string $html HTML content.
*
* @return string
*/
public function optimize( $html ): string {
Logger::info( 'GOOGLE FONTS COMBINE-V2 PROCESS STARTED.', [ 'GF combine process' ] );
$processed_tags = [];
$html_nocomments = $this->hide_comments( $html );
$font_tags = $this->find( '<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])(?<url>(?:https?:)?\/\/fonts\.googleapis\.com\/css2(?:(?!\1).)+)\1)(?:\s+[^>]*)?>', $html_nocomments );
if ( ! $font_tags ) {
Logger::debug( 'No v2 Google Fonts found.', [ 'GF combine process' ] );
$this->has_google_fonts = false;
return $html;
}
$this->has_google_fonts = true;
$num_tags = count( $font_tags );
Logger::debug(
"Found {$num_tags} v2 Google Fonts.",
[
'GF combine process',
'tags' => $font_tags,
]
);
$families = [];
foreach ( $font_tags as $tag ) {
$parsed_families = $this->parse( $tag );
if ( ! empty( $parsed_families ) ) {
$processed_tags[] = $tag;
$families = array_merge( $families, $parsed_families );
}
}
if ( empty( $families ) ) {
Logger::debug( 'No v2 Google Fonts left to combine.', [ 'GF combine process' ] );
return $html;
}
$families = array_unique( $families );
$combined_tag = $this->get_optimized_markup( $this->get_combined_url( $families ) );
$html = preg_replace( '@<\/title>@i', '$0' . $combined_tag, $html, 1 );
foreach ( $processed_tags as $font ) {
$html = str_replace( $font[0], '', $html );
}
Logger::info(
'V2 Google Fonts successfully combined.',
[
'GF combine process',
'url' => $combined_tag,
]
);
return $html;
}
/**
* Parses found matches to extract fonts and subsets.
*
* @since 3.8
*
* @param array $tag A Google Font v2 url.
*
* @return array
*/
private function parse( array $tag ): array {
if ( false !== strpos( $tag['url'], 'text=' ) ) {
Logger::debug( 'GOOGLEFONTS V2 COMBINE: ' . $tag['url'] . ' SKIPPED TO PRESERVE "text" ATTRIBUTE.' );
return [];
}
$url_pattern = '#(family=[A-Za-z0-9;:,=%&\+\@\.]+)$#';
$display_pattern = '#&display=(?:swap|auto|block|fallback|optional)#';
$decoded_url = html_entity_decode( $tag['url'] );
$query = wp_parse_url( $decoded_url, PHP_URL_QUERY );
if ( empty( $query ) ) {
return [];
}
if ( ! preg_match_all( $url_pattern, $query, $matches, PREG_PATTERN_ORDER ) ) {
return [];
}
$families = [];
foreach ( $matches[1] as $family ) {
$families[] = preg_replace( $display_pattern, '', $family );
}
return $families;
}
/**
* Returns the combined Google fonts URL
*
* @since 3.9.1
*
* @param array $families Array with all Google V2 families.
*
* @return string
*/
private function get_combined_url( array $families ): string {
$display = $this->get_font_display_value();
return esc_url( "https://fonts.googleapis.com/css2{$this->get_concatenated_families( $families )}&display={$display}" );
}
/**
* Get a string of the concatenated font family queries.
*
* @since 3.8
*
* @param array $families Array with all Google V2 families.
*
* @return string
*/
private function get_concatenated_families( array $families ): string {
$families_string = '?';
foreach ( $families as $family ) {
$families_string .= $family . '&';
}
return rtrim( $families_string, '&?' );
}
}