|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Override field methods |
|
4
|
|
|
* |
|
5
|
|
|
* @package Kirki |
|
6
|
|
|
* @subpackage Controls |
|
7
|
|
|
* @copyright Copyright (c) 2017, Aristeides Stathopoulos |
|
8
|
|
|
* @license http://opensource.org/licenses/https://opensource.org/licenses/MIT |
|
9
|
|
|
* @since 2.2.7 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Field overrides. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Kirki_Field_Typography extends Kirki_Field { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Sets the control type. |
|
19
|
|
|
* |
|
20
|
|
|
* @access protected |
|
21
|
|
|
*/ |
|
22
|
|
|
protected function set_type() { |
|
23
|
|
|
|
|
24
|
|
|
$this->type = 'kirki-typography'; |
|
25
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The class constructor. |
|
30
|
|
|
* Parses and sanitizes all field arguments. |
|
31
|
|
|
* Then it adds the field to Kirki::$fields. |
|
32
|
|
|
* |
|
33
|
|
|
* @access public |
|
34
|
|
|
* @param string $config_id The ID of the config we want to use. |
|
35
|
|
|
* Defaults to "global". |
|
36
|
|
|
* Configs are handled by the Kirki_Config class. |
|
37
|
|
|
* @param array $args The arguments of the field. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct( $config_id = 'global', $args = array() ) { |
|
40
|
|
|
parent::__construct( $config_id, $args ); |
|
41
|
|
|
$this->set_default(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Sets the default value. |
|
46
|
|
|
* |
|
47
|
|
|
* @access protected |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function set_default() { |
|
50
|
|
|
|
|
51
|
|
|
// Accomodate the use of font-weight and convert to variant. |
|
52
|
|
|
if ( isset( $this->default['font-weight'] ) ) { |
|
53
|
|
|
$this->default['variant'] = ( 'regular' === $this->default['font-weight'] ) ? 400 : (string) intval( $this->default['font-weight'] ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Make sure letter-spacing has units. |
|
57
|
|
|
if ( isset( $this->default['letter-spacing'] ) && is_numeric( $this->default['letter-spacing'] ) && $this->default['letter-spacing'] ) { |
|
58
|
|
|
$this->default['letter-spacing'] .= 'px'; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Make sure we use "subsets" instead of "subset". |
|
62
|
|
|
if ( isset( $this->default['subset'] ) && ! empty( $this->default['subset'] ) && ( ! isset( $this->default['subsets'] ) || empty( $this->default['subsets'] ) ) ) { |
|
63
|
|
|
$this->default['subsets'] = $this->default['subset']; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Sets the $sanitize_callback |
|
69
|
|
|
* |
|
70
|
|
|
* @access protected |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function set_sanitize_callback() { |
|
73
|
|
|
|
|
74
|
|
|
// If a custom sanitize_callback has been defined, |
|
75
|
|
|
// then we don't need to proceed any further. |
|
76
|
|
|
if ( ! empty( $this->sanitize_callback ) ) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
$this->sanitize_callback = array( __CLASS__, 'sanitize' ); |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Sets the $js_vars |
|
85
|
|
|
* |
|
86
|
|
|
* @access protected |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function set_js_vars() { |
|
89
|
|
|
|
|
90
|
|
|
if ( ! is_array( $this->js_vars ) ) { |
|
91
|
|
|
$this->js_vars = array(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Check if transport is set to auto. |
|
95
|
|
|
// If not, then skip the auto-calculations and exit early. |
|
96
|
|
|
if ( 'auto' !== $this->transport ) { |
|
97
|
|
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// Set transport to refresh initially. |
|
101
|
|
|
// Serves as a fallback in case we failt to auto-calculate js_vars. |
|
102
|
|
|
$this->transport = 'refresh'; |
|
103
|
|
|
|
|
104
|
|
|
$js_vars = array(); |
|
105
|
|
|
|
|
106
|
|
|
// Try to auto-generate js_vars. |
|
107
|
|
|
// First we need to check if js_vars are empty, and that output is not empty. |
|
108
|
|
|
if ( ! empty( $this->output ) ) { |
|
109
|
|
|
|
|
110
|
|
|
// Start going through each item in the $output array. |
|
111
|
|
|
foreach ( $this->output as $output ) { |
|
112
|
|
|
|
|
113
|
|
|
// If 'element' or 'property' are not defined, skip this. |
|
114
|
|
|
if ( ! isset( $output['element'] ) ) { |
|
115
|
|
|
continue; |
|
116
|
|
|
} |
|
117
|
|
|
if ( is_array( $output['element'] ) ) { |
|
118
|
|
|
$output['element'] = implode( ',', $output['element'] ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
// If we got this far, it's safe to add this. |
|
122
|
|
|
$js_vars[] = $output; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
// Did we manage to get all the items from 'output'? |
|
126
|
|
|
// If not, then we're missing something so don't add this. |
|
127
|
|
|
if ( count( $js_vars ) !== count( $this->output ) ) { |
|
128
|
|
|
return; |
|
129
|
|
|
} |
|
130
|
|
|
$this->js_vars = $js_vars; |
|
131
|
|
|
$this->transport = 'postMessage'; |
|
132
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Sanitizes typography controls |
|
139
|
|
|
* |
|
140
|
|
|
* @static |
|
141
|
|
|
* @since 2.2.0 |
|
142
|
|
|
* @param array $value The value. |
|
143
|
|
|
* @return array |
|
144
|
|
|
*/ |
|
145
|
|
|
public static function sanitize( $value ) { |
|
146
|
|
|
|
|
147
|
|
|
if ( ! is_array( $value ) ) { |
|
148
|
|
|
return array(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
foreach ( $value as $key => $val ) { |
|
152
|
|
|
switch ( $key ) { |
|
153
|
|
|
case 'font-family': |
|
154
|
|
|
$value['font-family'] = esc_attr( $val ); |
|
155
|
|
|
break; |
|
156
|
|
|
case 'font-weight': |
|
157
|
|
|
if ( isset( $value['variant'] ) ) { |
|
158
|
|
|
break; |
|
159
|
|
|
} |
|
160
|
|
|
$value['variant'] = $val; |
|
161
|
|
|
if ( isset( $value['font-style'] ) && 'italic' === $value['font-style'] ) { |
|
162
|
|
|
$value['variant'] = ( '400' !== $val || 400 !== $val ) ? $value['variant'] . 'italic' : 'italic'; |
|
163
|
|
|
} |
|
164
|
|
|
break; |
|
165
|
|
|
case 'variant': |
|
166
|
|
|
// Use 'regular' instead of 400 for font-variant. |
|
167
|
|
|
$value['variant'] = ( 400 === $val || '400' === $val ) ? 'regular' : $val; |
|
168
|
|
|
// Get font-weight from variant. |
|
169
|
|
|
$value['font-weight'] = filter_var( $value['variant'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION ); |
|
170
|
|
|
$value['font-weight'] = ( 'regular' === $value['variant'] || 'italic' === $value['variant'] ) ? 400 : absint( $value['font-weight'] ); |
|
171
|
|
|
// Get font-style from variant. |
|
172
|
|
|
if ( ! isset( $value['font-style'] ) ) { |
|
173
|
|
|
$value['font-style'] = ( false === strpos( $value['variant'], 'italic' ) ) ? 'normal' : 'italic'; |
|
174
|
|
|
} |
|
175
|
|
|
break; |
|
176
|
|
|
case 'subset': |
|
177
|
|
|
// Make sure the saved value is "subsets" (plural) and not "subset". |
|
178
|
|
|
// This is for compatibility with older versions. |
|
179
|
|
|
if ( ! empty( $value['subset'] ) && ! isset( $value['subsets'] ) || empty( $value['subset'] ) ) { |
|
180
|
|
|
$value['subsets'] = $value['subset']; |
|
181
|
|
|
} |
|
182
|
|
|
unset( $value['subset'] ); |
|
183
|
|
|
// Make sure we're using a valid subset. |
|
184
|
|
|
$valid_subsets = Kirki_Fonts::get_google_font_subsets(); |
|
185
|
|
|
$subsets_ok = array(); |
|
186
|
|
|
$value['subsets'] = (array) $value['subsets']; |
|
187
|
|
|
foreach ( $value['subsets'] as $subset ) { |
|
188
|
|
|
if ( array_key_exists( $subset, $valid_subsets ) ) { |
|
189
|
|
|
$subsets_ok[] = $subset; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
$value['subsets'] = $subsets_ok; |
|
193
|
|
|
break; |
|
194
|
|
|
case 'font-size': |
|
195
|
|
|
case 'letter-spacing': |
|
196
|
|
|
case 'word-spacing': |
|
197
|
|
|
case 'line-height': |
|
198
|
|
|
$value[ $key ] = Kirki_Sanitize_Values::css_dimension( $val ); |
|
199
|
|
|
break; |
|
200
|
|
|
case 'text-align': |
|
201
|
|
|
if ( ! in_array( $val, array( 'inherit', 'left', 'center', 'right', 'justify' ), true ) ) { |
|
202
|
|
|
$value['text-align'] = 'inherit'; |
|
203
|
|
|
} |
|
204
|
|
|
break; |
|
205
|
|
|
case 'text-transform': |
|
206
|
|
|
if ( ! in_array( $val, array( 'none', 'capitalize', 'uppercase', 'lowercase', 'initial', 'inherit' ), true ) ) { |
|
207
|
|
|
$value['text-transform'] = 'none'; |
|
208
|
|
|
} |
|
209
|
|
|
break; |
|
210
|
|
|
case 'text-decoration': |
|
211
|
|
|
if ( ! in_array( $val, array( 'none', 'underline', 'overline', 'line-through', 'initial', 'inherit' ), true ) ) { |
|
212
|
|
|
$value['text-transform'] = 'none'; |
|
213
|
|
|
} |
|
214
|
|
|
break; |
|
215
|
|
|
case 'color': |
|
216
|
|
|
$value['color'] = ariColor::newColor( $val )->toCSS( 'hex' ); |
|
217
|
|
|
break; |
|
218
|
|
|
} // End switch(). |
|
219
|
|
|
} // End foreach(). |
|
220
|
|
|
|
|
221
|
|
|
return $value; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Sets the $choices |
|
226
|
|
|
* |
|
227
|
|
|
* @access protected |
|
228
|
|
|
* @since 3.0.0 |
|
229
|
|
|
*/ |
|
230
|
|
|
protected function set_choices() { |
|
231
|
|
|
|
|
232
|
|
|
if ( ! is_array( $this->choices ) ) { |
|
233
|
|
|
$this->choices = array(); |
|
234
|
|
|
} |
|
235
|
|
|
$this->choices = wp_parse_args( |
|
236
|
|
|
$this->choices, array( |
|
237
|
|
|
'variant' => array(), |
|
238
|
|
|
'fonts' => array( |
|
239
|
|
|
'standard' => array(), |
|
240
|
|
|
'google' => array(), |
|
241
|
|
|
), |
|
242
|
|
|
) |
|
243
|
|
|
); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|