Completed
Push — develop ( 6534e5...217322 )
by Aristeides
02:54
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_array( $this->choices ) ) {
36
			$this->choices = array();
37
		}
38
39
		$defaults = array(
40
			'alpha'   => true,
41
			'preview' => array(
42
				'width'  => '100%',
43
				'height' => '60px',
44
			),
45
		);
46
		$this->choices = wp_parse_args( $this->choices, $defaults );
47
48
	}
49
50
	/**
51
	 * Sets the $sanitize_callback.
52
	 *
53
	 * @access protected
54
	 */
55
	protected function set_sanitize_callback() {
56
57
		$this->sanitize_callback = array( $this, 'sanitize' );
58
59
	}
60
61
	/**
62
	 * Sanitizes checkbox values.
63
	 *
64
	 * @access public
65
	 * @param boolean|integer|string|null $value The checkbox value.
66
	 * @return array
67
	 */
68
	public function sanitize( $value = null ) {
69
70
		// Make sure value in an array.
71
		$value = (array) $value;
72
73
		// Sanitize the type.
74
		if ( ! isset( $value['mode'] ) || 'linear' !== $value['mode'] || 'radial' !== $value['mode'] ) {
75
			$value['mode'] = 'linear';
76
		}
77
		foreach ( array( 'start', 'end' ) as $context ) {
78
79
			// Make sure value is array.
80
			if ( ! isset( $value[ $context ] ) ) {
81
				$value[ $context ] = array();
82
			}
83
84
			// Make sure color is defined.
85
			if ( ! isset( $value[ $context ]['color'] ) ) {
86
				$value[ $context ]['color'] = '';
87
			}
88
			// Sanitize colors.
89
			$color_obj = ariColor::newColor( $value[ $context ]['color'] );
90
			$value[ $context ]['color'] = $color_obj->toCSS( $color_obj->mode );
91
92
			// Make sure position is defined.
93
			if ( ! isset( $value[ $context ]['position'] ) ) {
94
				$value[ $context ]['position'] = 0;
95
			};
96
			// Sanitize positions.
97
			$value[ $context ]['position'] = (int) $value[ $context ]['position'];
98
			$value[ $context ]['position'] = max( min( $value[ $context ]['position'], 100 ), 0 );
99
		}
100
101
		// Make siure angle exists.
102
		if ( ! isset( $value['angle'] ) ) {
103
			$value['angle'] = 0;
104
		}
105
		// Sanitize angle.
106
		$value['angle'] = (int) $value['angle'];
107
		$value['angle'] = max( min( $value['angle'], 90 ), -90 );
108
109
		return $value;
110
111
	}
112
113
}
114