Completed
Push — develop ( e1c74c...783e35 )
by Aristeides
02:25 queued 33s
created

Kirki_Field_Gradient::sanitize()   D

Complexity

Conditions 9
Paths 36

Size

Total Lines 44
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 20
nc 36
nop 1
dl 0
loc 44
rs 4.909
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       3.0.0
10
 */
11
12
/**
13
 * Field overrides.
14
 */
15
class Kirki_Field_Gradient 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-gradient';
25
26
	}
27
28
	/**
29
	 * Sets the $choices
30
	 *
31
	 * @access protected
32
	 */
33
	protected function set_choices() {
34
35
		if ( ! is_customize_preview() ) {
36
			return;
37
		}
38
		if ( ! is_array( $this->choices ) ) {
39
			$this->choices = array();
40
		}
41
42
		$defaults = array(
43
			'alpha'   => true,
44
			'preview' => array(
45
				'width'  => '100%',
46
				'height' => '60px',
47
			),
48
		);
49
		$this->choices = wp_parse_args( $this->choices, $defaults );
50
51
	}
52
53
	/**
54
	 * Sets the $sanitize_callback.
55
	 *
56
	 * @access protected
57
	 */
58
	protected function set_sanitize_callback() {
59
60
		$this->sanitize_callback = array( $this, 'sanitize' );
61
62
	}
63
64
	/**
65
	 * Sets the $transport.
66
	 *
67
	 * @access protected
68
	 */
69
	protected function set_transport() {
70
		$this->transport = 'refresh';
71
	}
72
73
	/**
74
	 * Sanitizes checkbox values.
75
	 *
76
	 * @access public
77
	 * @param boolean|integer|string|null $value The checkbox value.
78
	 * @return array
79
	 */
80
	public function sanitize( $value = null ) {
81
82
		// Make sure value in an array.
83
		$value = (array) $value;
84
85
		// Sanitize the type.
86
		if ( ! isset( $value['mode'] ) || 'linear' !== $value['mode'] || 'radial' !== $value['mode'] ) {
87
			$value['mode'] = 'linear';
88
		}
89
		foreach ( array( 'start', 'end' ) as $context ) {
90
91
			// Make sure value is array.
92
			if ( ! isset( $value[ $context ] ) ) {
93
				$value[ $context ] = array();
94
			}
95
96
			// Make sure color is defined.
97
			if ( ! isset( $value[ $context ]['color'] ) ) {
98
				$value[ $context ]['color'] = '';
99
			}
100
			// Sanitize colors.
101
			$color_obj = ariColor::newColor( $value[ $context ]['color'] );
102
			$value[ $context ]['color'] = $color_obj->toCSS( $color_obj->mode );
103
104
			// Make sure position is defined.
105
			if ( ! isset( $value[ $context ]['position'] ) ) {
106
				$value[ $context ]['position'] = 0;
107
			};
108
			// Sanitize positions.
109
			$value[ $context ]['position'] = (int) $value[ $context ]['position'];
110
			$value[ $context ]['position'] = max( min( $value[ $context ]['position'], 100 ), 0 );
111
		}
112
113
		// Make siure angle exists.
114
		if ( ! isset( $value['angle'] ) ) {
115
			$value['angle'] = 0;
116
		}
117
		// Sanitize angle.
118
		$value['angle'] = (int) $value['angle'];
119
		$value['angle'] = max( min( $value['angle'], 90 ), -90 );
120
121
		return $value;
122
123
	}
124
125
}
126