1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Processes typography-related fields |
4
|
|
|
* and generates the google-font link. |
5
|
|
|
* |
6
|
|
|
* @package Kirki |
7
|
|
|
* @category Core |
8
|
|
|
* @author Aristeides Stathopoulos |
9
|
|
|
* @copyright Copyright (c) 2017, Aristeides Stathopoulos |
10
|
|
|
* @license http://opensource.org/licenses/https://opensource.org/licenses/MIT |
11
|
|
|
* @since 1.0 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Manages the way Google Fonts are enqueued. |
16
|
|
|
*/ |
17
|
|
|
final class Kirki_Fonts_Google { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The Kirki_Fonts_Google instance. |
21
|
|
|
* We use the singleton pattern here to avoid loading the google-font array multiple times. |
22
|
|
|
* This is mostly a performance tweak. |
23
|
|
|
* |
24
|
|
|
* @access private |
25
|
|
|
* @var null|object |
26
|
|
|
*/ |
27
|
|
|
private static $instance = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* If set to true, forces loading ALL variants. |
31
|
|
|
* |
32
|
|
|
* @static |
33
|
|
|
* @access public |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
public static $force_load_all_variants = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The array of fonts |
40
|
|
|
* |
41
|
|
|
* @access public |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
public $fonts = array(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* An array of all google fonts. |
48
|
|
|
* |
49
|
|
|
* @access private |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private $google_fonts = array(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The class constructor. |
56
|
|
|
*/ |
57
|
|
|
private function __construct() { |
58
|
|
|
|
59
|
|
|
$config = apply_filters( 'kirki_config', array() ); |
|
|
|
|
60
|
|
|
|
61
|
|
|
// If we have set $config['disable_google_fonts'] to true then do not proceed any further. |
62
|
|
|
if ( isset( $config['disable_google_fonts'] ) && true === $config['disable_google_fonts'] ) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
add_action( 'wp_ajax_kirki_fonts_google_all_get', array( $this, 'get_googlefonts_json' ) ); |
|
|
|
|
67
|
|
|
add_action( 'wp_ajax_noprinv_kirki_fonts_google_all_get', array( $this, 'get_googlefonts_json' ) ); |
68
|
|
|
add_action( 'wp_ajax_kirki_fonts_standard_all_get', array( $this, 'get_strandardfonts_json' ) ); |
69
|
|
|
add_action( 'wp_ajax_noprinv_kirki_fonts_standard_all_get', array( $this, 'get_strandardfonts_json' ) ); |
70
|
|
|
|
71
|
|
|
// Populate the array of google fonts. |
72
|
|
|
$this->google_fonts = Kirki_Fonts::get_google_fonts(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the one, true instance of this class. |
77
|
|
|
* Prevents performance issues since this is only loaded once. |
78
|
|
|
* |
79
|
|
|
* @return object Kirki_Fonts_Google |
80
|
|
|
*/ |
81
|
|
|
public static function get_instance() { |
82
|
|
|
if ( null === self::$instance ) { |
83
|
|
|
self::$instance = new Kirki_Fonts_Google(); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
return self::$instance; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Processes the arguments of a field |
90
|
|
|
* determines if it's a typography field |
91
|
|
|
* and if it is, then takes appropriate actions. |
92
|
|
|
* |
93
|
|
|
* @param array $args The field arguments. |
94
|
|
|
*/ |
95
|
|
|
public function generate_google_font( $args ) { |
96
|
|
|
|
97
|
|
|
// Process typography fields. |
98
|
|
|
if ( isset( $args['type'] ) && 'kirki-typography' === $args['type'] ) { |
99
|
|
|
|
100
|
|
|
// Get the value. |
101
|
|
|
$value = Kirki_Values::get_sanitized_field_value( $args ); |
102
|
|
|
|
103
|
|
|
// If we don't have a font-family then we can skip this. |
104
|
|
|
if ( ! isset( $value['font-family'] ) ) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// If not a google-font, then we can skip this. |
109
|
|
|
if ( ! Kirki_Fonts::is_google_font( $value['font-family'] ) ) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Set a default value for variants. |
114
|
|
|
if ( ! isset( $value['variant'] ) ) { |
115
|
|
|
$value['variant'] = 'regular'; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Add the requested google-font. |
119
|
|
|
if ( ! isset( $this->fonts[ $value['font-family'] ] ) ) { |
120
|
|
|
$this->fonts[ $value['font-family'] ] = array(); |
121
|
|
|
} |
122
|
|
|
if ( ! in_array( $value['variant'], $this->fonts[ $value['font-family'] ], true ) ) { |
123
|
|
|
$this->fonts[ $value['font-family'] ][] = $value['variant']; |
124
|
|
|
} |
125
|
|
|
// Are we force-loading all variants? |
126
|
|
|
if ( true === self::$force_load_all_variants ) { |
127
|
|
|
$all_variants = Kirki_Fonts::get_all_variants(); |
128
|
|
|
$args['choices']['variant'] = array_keys( $all_variants ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ( ! empty( $args['choices']['variant'] ) && is_array( $args['choices']['variant'] ) ) { |
132
|
|
|
foreach ( $args['choices']['variant'] as $extra_variant ) { |
133
|
|
|
$this->fonts[ $value['font-family'] ][] = $extra_variant; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
return; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// Process non-typography fields. |
140
|
|
|
if ( isset( $args['output'] ) && is_array( $args['output'] ) ) { |
141
|
|
|
foreach ( $args['output'] as $output ) { |
142
|
|
|
|
143
|
|
|
// If we don't have a typography-related output argument we can skip this. |
144
|
|
|
if ( ! isset( $output['property'] ) || ! in_array( $output['property'], array( 'font-family', 'font-weight' ), true ) ) { |
145
|
|
|
continue; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// Get the value. |
149
|
|
|
$value = Kirki_Values::get_sanitized_field_value( $args ); |
150
|
|
|
|
151
|
|
|
if ( is_string( $value ) ) { |
152
|
|
|
if ( 'font-family' === $output['property'] ) { |
153
|
|
|
if ( ! array_key_exists( $value, $this->fonts ) ) { |
154
|
|
|
$this->fonts[ $value ] = array(); |
155
|
|
|
} |
156
|
|
|
} elseif ( 'font-weight' === $output['property'] ) { |
157
|
|
|
foreach ( $this->fonts as $font => $variants ) { |
158
|
|
|
if ( ! in_array( $value, $variants, true ) ) { |
159
|
|
|
$this->fonts[ $font ][] = $value; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} // End foreach(). |
165
|
|
|
} // End if(). |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Determines the vbalidity of the selected font as well as its properties. |
170
|
|
|
* This is vital to make sure that the google-font script that we'll generate later |
171
|
|
|
* does not contain any invalid options. |
172
|
|
|
*/ |
173
|
|
|
public function process_fonts() { |
174
|
|
|
|
175
|
|
|
// Early exit if font-family is empty. |
176
|
|
|
if ( empty( $this->fonts ) ) { |
177
|
|
|
return; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
foreach ( $this->fonts as $font => $variants ) { |
181
|
|
|
|
182
|
|
|
// Determine if this is indeed a google font or not. |
183
|
|
|
// If it's not, then just remove it from the array. |
184
|
|
|
if ( ! array_key_exists( $font, $this->google_fonts ) ) { |
185
|
|
|
unset( $this->fonts[ $font ] ); |
186
|
|
|
continue; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// Get all valid font variants for this font. |
190
|
|
|
$font_variants = array(); |
191
|
|
|
if ( isset( $this->google_fonts[ $font ]['variants'] ) ) { |
192
|
|
|
$font_variants = $this->google_fonts[ $font ]['variants']; |
193
|
|
|
} |
194
|
|
|
foreach ( $variants as $variant ) { |
195
|
|
|
|
196
|
|
|
// If this is not a valid variant for this font-family |
197
|
|
|
// then unset it and move on to the next one. |
198
|
|
|
if ( ! in_array( $variant, $font_variants, true ) ) { |
199
|
|
|
$variant_key = array_search( $variant, $this->fonts[ $font ], true ); |
200
|
|
|
unset( $this->fonts[ $font ][ $variant_key ] ); |
201
|
|
|
continue; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Gets the googlefonts JSON file. |
209
|
|
|
* |
210
|
|
|
* @since 3.0.17 |
211
|
|
|
* @return void |
212
|
|
|
*/ |
213
|
|
|
public function get_googlefonts_json() { |
214
|
|
|
include wp_normalize_path( dirname( __FILE__ ) . '/webfonts.json' ); |
|
|
|
|
215
|
|
|
wp_die(); |
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Get the standard fonts JSON. |
220
|
|
|
* |
221
|
|
|
* @since 3.0.17 |
222
|
|
|
* @return void |
223
|
|
|
*/ |
224
|
|
|
public function get_strandardfonts_json() { |
225
|
|
|
echo wp_json_encode( Kirki_Fonts::get_standard_fonts() ); // WPCS: XSS ok. |
|
|
|
|
226
|
|
|
wp_die(); |
|
|
|
|
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|