1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Adds the Webfont Loader to load fonts asyncronously. |
4
|
|
|
* |
5
|
|
|
* @package Kirki |
6
|
|
|
* @category Core |
7
|
|
|
* @author Aristeides Stathopoulos |
8
|
|
|
* @copyright Copyright (c) 2017, Aristeides Stathopoulos |
9
|
|
|
* @license http://opensource.org/licenses/https://opensource.org/licenses/MIT |
10
|
|
|
* @since 3.0 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Manages the way Google Fonts are enqueued. |
15
|
|
|
*/ |
16
|
|
|
final class Kirki_Modules_Webfonts_Async { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The config ID. |
20
|
|
|
* |
21
|
|
|
* @access protected |
22
|
|
|
* @since 3.0.0 |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $config_id; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The Kirki_Modules_Webfonts object. |
29
|
|
|
* |
30
|
|
|
* @access protected |
31
|
|
|
* @since 3.0.0 |
32
|
|
|
* @var object |
33
|
|
|
*/ |
34
|
|
|
protected $webfonts; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The Kirki_Fonts_Google object. |
38
|
|
|
* |
39
|
|
|
* @access protected |
40
|
|
|
* @since 3.0.0 |
41
|
|
|
* @var object |
42
|
|
|
*/ |
43
|
|
|
protected $googlefonts; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor. |
47
|
|
|
* |
48
|
|
|
* @access public |
49
|
|
|
* @since 3.0 |
50
|
|
|
* @param string $config_id The config-ID. |
51
|
|
|
* @param object $webfonts The Kirki_Modules_Webfonts object. |
52
|
|
|
* @param object $googlefonts The Kirki_Fonts_Google object. |
53
|
|
|
* @param array $args Extra args we want to pass. |
54
|
|
|
*/ |
55
|
|
|
public function __construct( $config_id, $webfonts, $googlefonts, $args = array() ) { |
|
|
|
|
56
|
|
|
|
57
|
|
|
$this->config_id = $config_id; |
58
|
|
|
$this->webfonts = $webfonts; |
59
|
|
|
$this->googlefonts = $googlefonts; |
60
|
|
|
|
61
|
|
|
add_action( 'wp_head', array( $this, 'webfont_loader' ) ); |
62
|
|
|
|
63
|
|
|
add_filter( 'wp_resource_hints', array( $this, 'resource_hints' ), 10, 2 ); |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Add preconnect for Google Fonts. |
69
|
|
|
* |
70
|
|
|
* @access public |
71
|
|
|
* @param array $urls URLs to print for resource hints. |
72
|
|
|
* @param string $relation_type The relation type the URLs are printed. |
73
|
|
|
* @return array $urls URLs to print for resource hints. |
74
|
|
|
*/ |
75
|
|
|
public function resource_hints( $urls, $relation_type ) { |
76
|
|
|
|
77
|
|
|
$fonts_to_load = $this->googlefonts->fonts; |
78
|
|
|
|
79
|
|
|
if ( ! empty( $fonts_to_load ) && 'preconnect' === $relation_type ) { |
80
|
|
|
$urls[] = array( |
81
|
|
|
'href' => 'https://fonts.gstatic.com', |
82
|
|
|
'crossorigin', |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
return $urls; |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Webfont Loader for Google Fonts. |
91
|
|
|
* |
92
|
|
|
* @access public |
93
|
|
|
* @since 3.0.0 |
94
|
|
|
*/ |
95
|
|
|
public function webfont_loader() { |
96
|
|
|
|
97
|
|
|
// Go through our fields and populate $this->fonts. |
98
|
|
|
$this->webfonts->loop_fields( $this->config_id ); |
99
|
|
|
|
100
|
|
|
$this->googlefonts->fonts = apply_filters( 'kirki_enqueue_google_fonts', $this->googlefonts->fonts ); |
101
|
|
|
|
102
|
|
|
// Goes through $this->fonts and adds or removes things as needed. |
103
|
|
|
$this->googlefonts->process_fonts(); |
104
|
|
|
|
105
|
|
|
$fonts_to_load = array(); |
106
|
|
|
foreach ( $this->googlefonts->fonts as $font => $weights ) { |
107
|
|
|
foreach ( $weights as $key => $value ) { |
108
|
|
|
if ( 'italic' === $value ) { |
109
|
|
|
$weights[ $key ] = '400i'; |
110
|
|
|
} else { |
111
|
|
|
$weights[ $key ] = str_replace( array( 'regular', 'bold', 'italic' ), array( '400', '', 'i' ), $value ); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
$fonts_to_load[] = $font . ':' . join( ',', $weights ) . ':cyrillic,cyrillic-ext,devanagari,greek,greek-ext,khmer,latin,latin-ext,vietnamese,hebrew,arabic,bengali,gujarati,tamil,telugu,thai'; |
115
|
|
|
} |
116
|
|
|
wp_enqueue_script( 'webfont-loader', 'https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js', array(), KIRKI_VERSION ); |
117
|
|
|
if ( empty( $fonts_to_load ) ) { |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
wp_add_inline_script( |
121
|
|
|
'webfont-loader', |
122
|
|
|
'WebFont.load({google:{families:[\'' . join( '\', \'', $fonts_to_load ) . '\']}});', |
123
|
|
|
'after' |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.