88use WP_Rocket \Engine \Media \PreloadFonts \Database \Queries \PreloadFonts as PFQuery ;
99use WP_Rocket \Engine \Media \PreloadFonts \Context \Context ;
1010use WP_Rocket \Engine \Optimization \UrlTrait ;
11+ use WP_Rocket \Engine \Support \CommentTrait ;
12+ use WP_Rocket \Engine \Common \Head \ElementTrait ;
1113
1214class Controller implements ControllerInterface {
1315 use UrlTrait;
16+ use CommentTrait;
17+ use ElementTrait;
1418
1519 /**
1620 * Options instance
@@ -55,13 +59,10 @@ public function __construct( Options_Data $options, PFQuery $query, Context $con
5559 * @return string
5660 */
5761 public function optimize ( string $ html , $ row ): string {
58- if ( ! $ row ->has_preload_fonts () ) {
62+ if ( ! $ this -> context -> is_allowed () || ! $ row ->has_preload_fonts () ) {
5963 return $ html ;
6064 }
61-
62- $ html = $ this ->preload_fonts ( $ html , $ row );
63-
64- return $ html ;
65+ return $ this ->add_meta_comment ( 'auto_preload_fonts ' , $ html );
6566 }
6667
6768 /**
@@ -72,6 +73,10 @@ public function optimize( string $html, $row ): string {
7273 * @return array
7374 */
7475 public function add_custom_data ( array $ data ): array {
76+ if ( ! $ this ->context ->is_allowed () ) {
77+ return $ data ;
78+ }
79+
7580 $ system_fonts = [
7681 'serif ' ,
7782 'sans-serif ' ,
@@ -121,45 +126,6 @@ public function add_custom_data( array $data ): array {
121126 return $ data ;
122127 }
123128
124- /**
125- * Preload Fonts in HTML.
126- *
127- * @param string $html HTML content.
128- * @param object $row Corresponding DB row.
129- *
130- * @return string
131- */
132- private function preload_fonts ( string $ html , $ row ): string {
133- if ( 'completed ' !== $ row ->status || empty ( $ row ->fonts ) || '[] ' === $ row ->fonts ) {
134- return $ html ;
135- }
136-
137- if ( ! preg_match ( '#</title\s*># ' , $ html , $ matches ) ) {
138- return $ html ;
139- }
140- $ title = $ matches [0 ];
141-
142- $ preload = $ title ;
143-
144- $ fonts = json_decode ( $ row ->fonts , true );
145-
146- if ( empty ( $ fonts ) ) {
147- return $ html ;
148- }
149-
150- foreach ( $ fonts as $ font ) {
151- $ preload .= $ this ->preload_tag ( $ font );
152- }
153-
154- $ replace = preg_replace ( '# ' . $ title . '# ' , $ preload , $ html , 1 );
155-
156- if ( null === $ replace ) {
157- return $ html ;
158- }
159-
160- return $ replace ;
161- }
162-
163129 /**
164130 * Checks if the font URL is from a third party.
165131 *
@@ -180,26 +146,46 @@ private function is_third_party_font( string $font_url ): bool {
180146 }
181147
182148 /**
183- * Generates the preload tag for a font.
184- *
185- * @param string $font Font URL.
149+ * Adds the preload fonts to the head tag.
186150 *
187- * @return string
151+ * @param array $items added to the head.
152+ * @return array Items to be added to the head.
188153 */
189- private function preload_tag ( string $ font ): string {
190- if ( ! $ this ->is_relative ( $ font ) ) {
191- $ crossorigin = $ this ->is_third_party_font ( $ font ) ? ' crossorigin ' : '' ;
192- $ tag = sprintf (
193- '<link rel="preload" data-rocket-preload as="font" href="%s"%s> ' ,
194- esc_url ( $ font ),
195- $ crossorigin
196- );
197- } else {
198- $ tag = sprintf (
199- '<link rel="preload" data-rocket-preload as="font" href="%s"> ' ,
200- $ font
201- );
154+ public function add_preload_fonts_in_head ( $ items ) {
155+ if ( ! $ this ->context ->is_allowed () ) {
156+ return $ items ;
157+ }
158+
159+ global $ wp ;
160+
161+ $ url = untrailingslashit ( home_url ( add_query_arg ( [], $ wp ->request ) ) );
162+ $ is_mobile = $ this ->context ->is_mobile_allowed ();
163+
164+ $ row = $ this ->query ->get_row ( $ url , $ is_mobile );
165+ if ( empty ( $ row ) || 'completed ' !== $ row ->status || empty ( $ row ->fonts ) || '[] ' === $ row ->fonts ) {
166+ return $ items ;
167+ }
168+
169+ $ fonts = json_decode ( $ row ->fonts , true );
170+
171+ if ( empty ( $ fonts ) ) {
172+ return $ items ;
202173 }
203- return $ tag ;
174+
175+ foreach ( $ fonts as $ font ) {
176+ $ item_args = [
177+ // 'id' => 'preload-font-' . md5( $font ), // Unique ID based on font URL.
178+ 'href ' => esc_url ( $ font ),
179+ 'as ' => 'font ' ,
180+ ];
181+
182+ if ( ! $ this ->is_relative ( $ font ) && $ this ->is_third_party_font ( $ font ) ) {
183+ $ item_args [2 ] = 'crossorigin ' ;
184+ }
185+
186+ $ items [] = $ this ->preload_link ( $ item_args );
187+ }
188+
189+ return $ items ;
204190 }
205191}
0 commit comments