Issues (377)

field/class-kirki-field-typography.php (2 issues)

1
<?php
2
/**
3
 * Override field methods
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
8
 * @license    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
		$this->type = 'kirki-typography';
24
	}
25
26
	/**
27
	 * The class constructor.
28
	 * Parses and sanitizes all field arguments.
29
	 * Then it adds the field to Kirki::$fields.
30
	 *
31
	 * @access public
32
	 * @param string $config_id    The ID of the config we want to use.
33
	 *                             Defaults to "global".
34
	 *                             Configs are handled by the Kirki_Config class.
35
	 * @param array  $args         The arguments of the field.
36
	 */
37
	public function __construct( $config_id = 'global', $args = array() ) {
38
		parent::__construct( $config_id, $args );
39
		$this->set_default();
40
	}
41
42
	/**
43
	 * Sets the default value.
44
	 *
45
	 * @access protected
46
	 */
47
	protected function set_default() {
48
49
		// Accomodate the use of font-weight and convert to variant.
50
		if ( isset( $this->default['font-weight'] ) ) {
51
			$this->default['variant'] = ( 'regular' === $this->default['font-weight'] ) ? 400 : (string) intval( $this->default['font-weight'] );
52
		}
53
54
		// Make sure letter-spacing has units.
55
		if ( isset( $this->default['letter-spacing'] ) && is_numeric( $this->default['letter-spacing'] ) && $this->default['letter-spacing'] ) {
56
			$this->default['letter-spacing'] .= 'px';
57
		}
58
	}
59
60
	/**
61
	 * Sets the $sanitize_callback
62
	 *
63
	 * @access protected
64
	 */
65
	protected function set_sanitize_callback() {
66
67
		// If a custom sanitize_callback has been defined,
68
		// then we don't need to proceed any further.
69
		if ( ! empty( $this->sanitize_callback ) ) {
70
			return;
71
		}
72
		$this->sanitize_callback = array( __CLASS__, 'sanitize' );
73
	}
74
75
	/**
76
	 * Sets the $js_vars
77
	 *
78
	 * @access protected
79
	 */
80
	protected function set_js_vars() {
81
		if ( ! is_array( $this->js_vars ) ) {
82
			$this->js_vars = array();
83
		}
84
85
		// Check if transport is set to auto.
86
		// If not, then skip the auto-calculations and exit early.
87
		if ( 'auto' !== $this->transport ) {
88
			return;
89
		}
90
91
		// Set transport to refresh initially.
92
		// Serves as a fallback in case we failt to auto-calculate js_vars.
93
		$this->transport = 'refresh';
94
95
		$js_vars = array();
96
97
		// Try to auto-generate js_vars.
98
		// First we need to check if js_vars are empty, and that output is not empty.
99
		if ( ! empty( $this->output ) ) {
100
101
			// Start going through each item in the $output array.
102
			foreach ( $this->output as $output ) {
103
104
				// If 'element' or 'property' are not defined, skip this.
105
				if ( ! isset( $output['element'] ) ) {
106
					continue;
107
				}
108
				if ( is_array( $output['element'] ) ) {
109
					$output['element'] = implode( ',', $output['element'] );
110
				}
111
112
				// If we got this far, it's safe to add this.
113
				$js_vars[] = $output;
114
			}
115
116
			// Did we manage to get all the items from 'output'?
117
			// If not, then we're missing something so don't add this.
118
			if ( count( $js_vars ) !== count( $this->output ) ) {
119
				return;
120
			}
121
			$this->js_vars   = $js_vars;
122
			$this->transport = 'postMessage';
123
		}
124
	}
125
126
	/**
127
	 * Sanitizes typography controls
128
	 *
129
	 * @static
130
	 * @since 2.2.0
131
	 * @param array $value The value.
132
	 * @return array
133
	 */
134
	public static function sanitize( $value ) {
135
		if ( ! is_array( $value ) ) {
136
			return array();
137
		}
138
139
		foreach ( $value as $key => $val ) {
140
			switch ( $key ) {
141
				case 'font-family':
142
					$value['font-family'] = sanitize_text_field( $val );
143
					break;
144
				case 'font-weight':
145
					if ( isset( $value['variant'] ) ) {
146
						break;
147
					}
148
					$value['variant'] = $val;
149
					if ( isset( $value['font-style'] ) && 'italic' === $value['font-style'] ) {
150
						$value['variant'] = ( '400' !== $val || 400 !== $val ) ? $value['variant'] . 'italic' : 'italic';
151
					}
152
					break;
153
				case 'variant':
0 ignored issues
show
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
154
155
					// Use 'regular' instead of 400 for font-variant.
156
					$value['variant'] = ( 400 === $val || '400' === $val ) ? 'regular' : $val;
157
158
					// Get font-weight from variant.
159
					$value['font-weight'] = filter_var( $value['variant'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION );
160
					$value['font-weight'] = ( 'regular' === $value['variant'] || 'italic' === $value['variant'] ) ? 400 : absint( $value['font-weight'] );
161
162
					// Get font-style from variant.
163
					if ( ! isset( $value['font-style'] ) ) {
164
						$value['font-style'] = ( false === strpos( $value['variant'], 'italic' ) ) ? 'normal' : 'italic';
165
					}
166
					break;
167
				case 'font-size':
168
				case 'letter-spacing':
169
				case 'word-spacing':
170
				case 'line-height':
171
					$value[ $key ] = '' === trim( $value[ $key ] ) ? '' : sanitize_text_field( $val );
172
					break;
173
				case 'text-align':
174
					if ( ! in_array( $val, array( '', 'inherit', 'left', 'center', 'right', 'justify' ), true ) ) {
175
						$value['text-align'] = '';
176
					}
177
					break;
178
				case 'text-transform':
179
					if ( ! in_array( $val, array( '', 'none', 'capitalize', 'uppercase', 'lowercase', 'initial', 'inherit' ), true ) ) {
180
						$value['text-transform'] = '';
181
					}
182
					break;
183
				case 'text-decoration':
184
					if ( ! in_array( $val, array( '', 'none', 'underline', 'overline', 'line-through', 'initial', 'inherit' ), true ) ) {
185
						$value['text-transform'] = '';
186
					}
187
					break;
188
				case 'color':
189
					$value['color'] = '' === $value['color'] ? '' : ariColor::newColor( $val )->toCSS( 'hex' );
190
					break;
191
			}
192
		}
193
194
		return $value;
195
	}
196
197
	/**
198
	 * Sets the $choices
199
	 *
200
	 * @access protected
201
	 * @since 3.0.0
202
	 */
203
	protected function set_choices() {
204
		if ( ! is_array( $this->choices ) ) {
205
			$this->choices = array();
206
		}
207
		$this->choices = wp_parse_args(
208
			$this->choices, array(
0 ignored issues
show
For multi-line function calls, each argument should be on a separate line.

For a function calls that spawns multiple lines, the coding style suggests to split arguments to separate lines like this:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
);
Loading history...
209
				'variant' => array(),
210
				'fonts'   => array(
211
					'standard' => array(),
212
					'google'   => array(),
213
				),
214
			)
215
		);
216
	}
217
}
218