Async.php
2 months ago
Downloader.php
5 months ago
Embed.php
2 months ago
Fonts.php
5 months ago
Google.php
2 months ago
Async.php
187 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Adds the Webfont Loader to load fonts asynchronously. |
| 4 | * |
| 5 | * @package kirki-framework/module-webfonts |
| 6 | * @author Themeum |
| 7 | * @copyright Copyright (c) 2023, Themeum |
| 8 | * @license https://opensource.org/licenses/MIT |
| 9 | * @since 1.0.0 |
| 10 | */ |
| 11 | |
| 12 | namespace Kirki\Module\Webfonts; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | use Kirki\URL; |
| 19 | |
| 20 | /**3. |
| 21 | * Manages the way Google Fonts are enqueued. |
| 22 | */ |
| 23 | final class Async |
| 24 | { |
| 25 | |
| 26 | /** |
| 27 | * Only load the webfont script if this is true. |
| 28 | * |
| 29 | * @static |
| 30 | * @access public |
| 31 | * @since 1.0.0 |
| 32 | * @var bool |
| 33 | */ |
| 34 | public static $load = false; |
| 35 | |
| 36 | /** |
| 37 | * The config ID. |
| 38 | * |
| 39 | * @access protected |
| 40 | * @since 1.0.0 |
| 41 | * @var string |
| 42 | */ |
| 43 | protected $config_id; |
| 44 | |
| 45 | /** |
| 46 | * The \Kirki\Module\Webfonts object. |
| 47 | * |
| 48 | * @access protected |
| 49 | * @since 1.0.0 |
| 50 | * @var object |
| 51 | */ |
| 52 | protected $webfonts; |
| 53 | |
| 54 | /** |
| 55 | * The \Kirki\Module\Webfonts\Google object. |
| 56 | * |
| 57 | * @access protected |
| 58 | * @since 1.0.0 |
| 59 | * @var object |
| 60 | */ |
| 61 | protected $googlefonts; |
| 62 | |
| 63 | /** |
| 64 | * Fonts to load. |
| 65 | * |
| 66 | * @access protected |
| 67 | * @since 1.0.0 |
| 68 | * @var array |
| 69 | */ |
| 70 | protected $fonts_to_load = []; |
| 71 | |
| 72 | /** |
| 73 | * Constructor. |
| 74 | * |
| 75 | * @access public |
| 76 | * @since 1.0.0 |
| 77 | * @param string $config_id The config-ID. |
| 78 | * @param object $webfonts The \Kirki\Module\Webfonts object. |
| 79 | * @param object $googlefonts The \Kirki\Module\Webfonts\Google object. |
| 80 | * @param array $args Extra args we want to pass. |
| 81 | */ |
| 82 | public function __construct($config_id, $webfonts, $googlefonts, $args = []) |
| 83 | { |
| 84 | $this->config_id = $config_id; |
| 85 | $this->webfonts = $webfonts; |
| 86 | $this->googlefonts = $googlefonts; |
| 87 | |
| 88 | add_action('wp_head', [$this, 'webfont_loader']); |
| 89 | add_action('wp_head', [$this, 'webfont_loader_script'], 30); |
| 90 | |
| 91 | // Add these in the dashboard to support editor-styles. |
| 92 | add_action('admin_enqueue_scripts', [$this, 'webfont_loader']); |
| 93 | add_action('admin_enqueue_scripts', [$this, 'webfont_loader_script'], 30); |
| 94 | |
| 95 | // add_filter( 'wp_resource_hints', [ $this, 'resource_hints' ], 10, 2 ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Add preconnect for Google Fonts. |
| 100 | * |
| 101 | * @access public |
| 102 | * @param array $urls URLs to print for resource hints. |
| 103 | * @param string $relation_type The relation type the URLs are printed. |
| 104 | * @return array $urls URLs to print for resource hints. |
| 105 | */ |
| 106 | public function resource_hints($urls, $relation_type) |
| 107 | { |
| 108 | $fonts_to_load = $this->googlefonts->fonts; |
| 109 | |
| 110 | if (!empty($fonts_to_load) && 'preconnect' === $relation_type) { |
| 111 | $urls[] = [ |
| 112 | 'href' => 'https://fonts.gstatic.com', |
| 113 | 'crossorigin', |
| 114 | ]; |
| 115 | } |
| 116 | return $urls; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Webfont Loader for Google Fonts. |
| 121 | * |
| 122 | * @access public |
| 123 | * @since 1.0.0 |
| 124 | */ |
| 125 | public function webfont_loader() |
| 126 | { |
| 127 | global $wp_customize; |
| 128 | |
| 129 | // Go through our fields and populate $this->fonts. |
| 130 | $this->webfonts->loop_fields($this->config_id); |
| 131 | |
| 132 | $this->googlefonts->fonts = apply_filters('kirki_enqueue_google_fonts', $this->googlefonts->fonts); |
| 133 | |
| 134 | // Goes through $this->fonts and adds or removes things as needed. |
| 135 | $this->googlefonts->process_fonts(); |
| 136 | |
| 137 | $subset = apply_filters('kirki_googlefonts_subset', 'cyrillic,cyrillic-ext,devanagari,greek,greek-ext,khmer,latin,latin-ext,vietnamese,hebrew,arabic,bengali,gujarati,tamil,telugu,thai'); |
| 138 | |
| 139 | foreach ($this->googlefonts->fonts as $font => $weights) { |
| 140 | foreach ($weights as $key => $value) { |
| 141 | if ('italic' === $value) { |
| 142 | $weights[$key] = '400i'; |
| 143 | } else { |
| 144 | $weights[$key] = str_replace(['regular', 'bold', 'italic'], ['400', '', 'i'], $value); |
| 145 | } |
| 146 | } |
| 147 | $this->fonts_to_load[] = $font . ':' . join(',', $weights) . ':' . $subset; |
| 148 | } |
| 149 | if (!empty($this->fonts_to_load)) { |
| 150 | self::$load = true; |
| 151 | } |
| 152 | |
| 153 | if (self::$load || $wp_customize || is_customize_preview()) { |
| 154 | wp_enqueue_script('webfont-loader', URL::get_from_path(dirname(__DIR__) . '/assets/scripts/vendor-typekit/webfontloader.js'), [], '3.0.28', true); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Webfont Loader script for Google Fonts. |
| 160 | * |
| 161 | * @access public |
| 162 | * @since 1.0.0 |
| 163 | */ |
| 164 | public function webfont_loader_script() |
| 165 | { |
| 166 | if (!empty($this->fonts_to_load)) { |
| 167 | wp_add_inline_script( |
| 168 | 'webfont-loader', |
| 169 | 'if(typeof WebFont!=="undefined"){WebFont.load({google:{families:[\'' . join('\', \'', $this->fonts_to_load) . '\']}});}', |
| 170 | 'after' |
| 171 | ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Set the $load property of this object. |
| 177 | * |
| 178 | * @access public |
| 179 | * @since 1.0.0 |
| 180 | * @param bool $load Set to false to disable loading. |
| 181 | * @return void |
| 182 | */ |
| 183 | public function set_load($load) |
| 184 | { |
| 185 | self::$load = $load; |
| 186 | } |
| 187 | } |