PluginProbe
EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant / 2.0.1
EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant v2.0.1
2.0.4 2.0.3 2.0.2 2.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2 1.3 2.0.0
easyfonts / src / Frontend / SmartPreload.php

SmartPreload.php in EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant 2.0.1, at src/Frontend/SmartPreload.php

112 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Smart preload.
4 *
5 * Emits <link rel="preload"> for the font variants the user has marked to
6 * preload (Fonts screen → per-variant Preload toggle). Results are deduped by
7 * file and capped, and the output buffer positions them ABOVE the consolidated
8 * stylesheet so the browser starts fetching them as early as possible.
9 *
10 * When "smart preload" is enabled, the beacon auto-marks above-the-fold variants
11 * as preloaded after a real visit (see RestApi::beacon), so this populates with
12 * sensible defaults that the user can then refine.
13 *
14 * @package EasyFonts
15 */
16
17 namespace EasyFonts\Frontend;
18
19 use EasyFonts\Fonts\Registry;
20 use EasyFonts\Fonts\Storage;
21 use EasyFonts\Settings;
22
23 defined( 'ABSPATH' ) || exit;
24
25 /**
26 * Builds deduped preload link markup.
27 */
28 class SmartPreload {
29
30 /**
31 * @var Registry
32 */
33 private Registry $registry;
34
35 /**
36 * @var Storage
37 */
38 private Storage $storage;
39
40 /**
41 * Constructor.
42 */
43 public function __construct() {
44 $this->registry = new Registry();
45 $this->storage = new Storage();
46 }
47
48 /**
49 * Build preload markup for the current request.
50 *
51 * @return string
52 */
53 public function build(): string {
54 if ( ! Settings::get( 'smart_preload', 1 ) ) {
55 return '';
56 }
57
58 $targets = $this->registry->preload_targets();
59
60 if ( empty( $targets ) ) {
61 return '';
62 }
63
64 $links = '';
65 $seen = array();
66 $count = 0;
67
68 foreach ( $targets as $t ) {
69 if ( $count >= 6 ) {
70 break; // Hard cap — preloading too much hurts more than it helps.
71 }
72
73 $file = $t['file'];
74
75 if ( '' === $file || isset( $seen[ $file ] ) || ! $this->storage->exists( $file ) ) {
76 continue;
77 }
78
79 $seen[ $file ] = true;
80
81 $url = add_query_arg( 'ver', Settings::buster(), $this->storage->url( $file ) );
82 $links .= sprintf(
83 '<link rel="preload" href="%s" as="font" type="%s" crossorigin>' . "\n",
84 esc_url( $url ),
85 esc_attr( $this->mime_for( $file ) )
86 );
87 $count++;
88 }
89
90 return $links;
91 }
92
93 /**
94 * MIME for a font file.
95 *
96 * @param string $file Filename.
97 * @return string
98 */
99 private function mime_for( string $file ): string {
100 $ext = strtolower( (string) pathinfo( $file, PATHINFO_EXTENSION ) );
101
102 $map = array(
103 'woff2' => 'font/woff2',
104 'woff' => 'font/woff',
105 'ttf' => 'font/ttf',
106 'otf' => 'font/otf',
107 );
108
109 return $map[ $ext ] ?? 'font/woff2';
110 }
111 }
112