Completed
Branch develop (98485c)
by Aristeides
12:56 queued 06:30
created

Kirki_Output_Field_Typography   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 77
rs 10
c 2
b 0
f 0
wmc 22

1 Method

Rating   Name   Duplication   Size   Complexity  
F process_output() 0 68 22
1
<?php
2
/**
3
 * Handles CSS output for typography fields.
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.0
10
 */
11
12
/**
13
 * Output overrides.
14
 */
15
class Kirki_Output_Field_Typography extends Kirki_Output {
16
17
	/**
18
	 * Processes a single item from the `output` array.
19
	 *
20
	 * @access protected
21
	 * @param array $output The `output` item.
22
	 * @param array $value  The field's value.
23
	 */
24
	protected function process_output( $output, $value ) {
25
26
		$output['media_query'] = ( isset( $output['media_query'] ) ) ? $output['media_query'] : 'global';
27
		$output['element']     = ( isset( $output['element'] ) ) ? $output['element'] : 'body';
28
		$output['prefix']      = ( isset( $output['prefix'] ) ) ? $output['prefix'] : '';
29
		$output['suffix']      = ( isset( $output['suffix'] ) ) ? $output['suffix'] : '';
30
31
		$value = Kirki_Field_Typography::sanitize( $value );
32
33
		$properties = array(
34
			'font-family',
35
			'font-size',
36
			'variant',
37
			'font-weight',
38
			'font-style',
39
			'letter-spacing',
40
			'word-spacing',
41
			'line-height',
42
			'text-align',
43
			'text-transform',
44
			'text-decoration',
45
			'color',
46
		);
47
48
		foreach ( $properties as $property ) {
49
50
			// Early exit if the value is not in the defaults.
51
			if ( ! isset( $this->field['default'][ $property ] ) ) {
52
				continue;
53
			}
54
55
			// Early exit if the value is not saved in the values.
56
			if ( ! isset( $value[ $property ] ) || ! $value[ $property ] ) {
57
				continue;
58
			}
59
60
			// Early exit if we use "choice" but not for this property.
61
			if ( isset( $output['choice'] ) && $output['choice'] !== $property ) {
62
				continue;
63
			}
64
65
			// Take care of variants.
66
			if ( 'variant' === $property && isset( $value['variant'] ) && ! empty( $value['variant'] ) ) {
67
68
				// Get the font_weight.
69
				$font_weight = str_replace( 'italic', '', $value['variant'] );
70
				$font_weight = ( in_array( $font_weight, array( '', 'regular' ), true ) ) ? '400' : $font_weight;
71
72
				// Is this italic?
73
				$is_italic = ( false !== strpos( $value['variant'], 'italic' ) );
74
				$this->styles[ $output['media_query'] ][ $output['element'] ]['font-weight'] = $font_weight;
75
				if ( $is_italic ) {
76
					$this->styles[ $output['media_query'] ][ $output['element'] ]['font-style'] = 'italic';
77
				}
78
				continue;
79
			}
80
81
			$property_value = $this->process_property_value( $property, $value[ $property ] );
82
			if ( 'font-family' === $property ) {
83
				$value['font-backup'] = ( isset( $value['font-backup'] ) ) ? $value['font-backup'] : '';
84
				$property_value       = $this->process_property_value( $property, array(
85
					$value['font-family'],
86
					$value['font-backup'],
87
				) );
88
			}
89
			$property       = ( isset( $output['choice'] ) && isset( $output['property'] ) ) ? $output['property'] : $property;
90
			$property_value = ( is_array( $property_value ) && isset( $property_value[0] ) ) ? $property_value[0] : $property_value;
91
			$this->styles[ $output['media_query'] ][ $output['element'] ][ $property ] = $output['prefix'] . $property_value . $output['suffix'];
92
		}
93
	}
94
}
95