Passed
Push — develop ( fe986b...95ca01 )
by Aristeides
04:19
created

Kirki_Field_Typography::set_js_vars()   B

Complexity

Conditions 8
Paths 20

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 20
nop 0
dl 0
loc 44
rs 7.9715
c 0
b 0
f 0
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
62
	/**
63
	 * Sets the $sanitize_callback
64
	 *
65
	 * @access protected
66
	 */
67
	protected function set_sanitize_callback() {
68
69
		// If a custom sanitize_callback has been defined,
70
		// then we don't need to proceed any further.
71
		if ( ! empty( $this->sanitize_callback ) ) {
72
			return;
73
		}
74
		$this->sanitize_callback = array( __CLASS__, 'sanitize' );
75
76
	}
77
78
	/**
79
	 * Sets the $js_vars
80
	 *
81
	 * @access protected
82
	 */
83
	protected function set_js_vars() {
84
85
		if ( ! is_array( $this->js_vars ) ) {
0 ignored issues
show
introduced by
The condition is_array($this->js_vars) is always true.
Loading history...
86
			$this->js_vars = array();
87
		}
88
89
		// Check if transport is set to auto.
90
		// If not, then skip the auto-calculations and exit early.
91
		if ( 'auto' !== $this->transport ) {
92
			return;
93
		}
94
95
		// Set transport to refresh initially.
96
		// Serves as a fallback in case we failt to auto-calculate js_vars.
97
		$this->transport = 'refresh';
98
99
		$js_vars = array();
100
101
		// Try to auto-generate js_vars.
102
		// First we need to check if js_vars are empty, and that output is not empty.
103
		if ( ! empty( $this->output ) ) {
104
105
			// Start going through each item in the $output array.
106
			foreach ( $this->output as $output ) {
107
108
				// If 'element' or 'property' are not defined, skip this.
109
				if ( ! isset( $output['element'] ) ) {
110
					continue;
111
				}
112
				if ( is_array( $output['element'] ) ) {
113
					$output['element'] = implode( ',', $output['element'] );
114
				}
115
116
				// If we got this far, it's safe to add this.
117
				$js_vars[] = $output;
118
			}
119
120
			// Did we manage to get all the items from 'output'?
121
			// If not, then we're missing something so don't add this.
122
			if ( count( $js_vars ) !== count( $this->output ) ) {
123
				return;
124
			}
125
			$this->js_vars   = $js_vars;
126
			$this->transport = 'postMessage';
127
128
		}
129
130
	}
131
132
	/**
133
	 * Sanitizes typography controls
134
	 *
135
	 * @static
136
	 * @since 2.2.0
137
	 * @param array $value The value.
138
	 * @return array
139
	 */
140
	public static function sanitize( $value ) {
141
142
		if ( ! is_array( $value ) ) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
143
			return array();
144
		}
145
146
		foreach ( $value as $key => $val ) {
147
			switch ( $key ) {
148
				case 'font-family':
149
					$value['font-family'] = esc_attr( $val );
150
					break;
151
				case 'font-weight':
152
					if ( isset( $value['variant'] ) ) {
153
						break;
154
					}
155
					$value['variant'] = $val;
156
					if ( isset( $value['font-style'] ) && 'italic' === $value['font-style'] ) {
157
						$value['variant'] = ( '400' !== $val || 400 !== $val ) ? $value['variant'] . 'italic' : 'italic';
158
					}
159
					break;
160
				case 'variant':
161
					// Use 'regular' instead of 400 for font-variant.
162
					$value['variant'] = ( 400 === $val || '400' === $val ) ? 'regular' : $val;
163
					// Get font-weight from variant.
164
					$value['font-weight'] = filter_var( $value['variant'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION );
165
					$value['font-weight'] = ( 'regular' === $value['variant'] || 'italic' === $value['variant'] ) ? 400 : absint( $value['font-weight'] );
166
					// Get font-style from variant.
167
					if ( ! isset( $value['font-style'] ) ) {
168
						$value['font-style'] = ( false === strpos( $value['variant'], 'italic' ) ) ? 'normal' : 'italic';
169
					}
170
					break;
171
				case 'font-size':
172
				case 'letter-spacing':
173
				case 'word-spacing':
174
				case 'line-height':
175
					$value[ $key ] = '' === trim( $value[ $key ] ) ? '' : sanitize_text_field( $val );
176
					break;
177
				case 'text-align':
178
					if ( ! in_array( $val, array( '', 'inherit', 'left', 'center', 'right', 'justify' ), true ) ) {
179
						$value['text-align'] = '';
180
					}
181
					break;
182
				case 'text-transform':
183
					if ( ! in_array( $val, array( '', 'none', 'capitalize', 'uppercase', 'lowercase', 'initial', 'inherit' ), true ) ) {
184
						$value['text-transform'] = '';
185
					}
186
					break;
187
				case 'text-decoration':
188
					if ( ! in_array( $val, array( ''. 'none', 'underline', 'overline', 'line-through', 'initial', 'inherit' ), true ) ) {
189
						$value['text-transform'] = '';
190
					}
191
					break;
192
				case 'color':
193
					$value['color'] = '' === $value['color'] ? '' : ariColor::newColor( $val )->toCSS( 'hex' );
194
					break;
195
			} // End switch().
196
		} // End foreach().
197
198
		return $value;
199
	}
200
201
	/**
202
	 * Sets the $choices
203
	 *
204
	 * @access protected
205
	 * @since 3.0.0
206
	 */
207
	protected function set_choices() {
208
209
		if ( ! is_array( $this->choices ) ) {
0 ignored issues
show
introduced by
The condition is_array($this->choices) is always true.
Loading history...
210
			$this->choices = array();
211
		}
212
		$this->choices = wp_parse_args(
213
			$this->choices, array(
214
				'variant' => array(),
215
				'fonts'   => array(
216
					'standard' => array(),
217
					'google'   => array(),
218
				),
219
			)
220
		);
221
	}
222
}
223