Completed
Push — develop ( fe9689...674c42 )
by Aristeides
02:41
created

Kirki_Field_Gradient   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 14
lcom 4
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A set_type() 0 5 1
A set_choices() 0 16 2
A set_sanitize_callback() 0 5 1
A set_transport() 0 3 1
D sanitize() 0 44 9
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
	 * Sets the $transport.
63
	 *
64
	 * @access protected
65
	 */
66
	protected function set_transport() {
67
		$this->transport = 'refresh';
68
	}
69
70
	/**
71
	 * Sanitizes checkbox values.
72
	 *
73
	 * @access public
74
	 * @param boolean|integer|string|null $value The checkbox value.
75
	 * @return array
76
	 */
77
	public function sanitize( $value = null ) {
78
79
		// Make sure value in an array.
80
		$value = (array) $value;
81
82
		// Sanitize the type.
83
		if ( ! isset( $value['mode'] ) || 'linear' !== $value['mode'] || 'radial' !== $value['mode'] ) {
84
			$value['mode'] = 'linear';
85
		}
86
		foreach ( array( 'start', 'end' ) as $context ) {
87
88
			// Make sure value is array.
89
			if ( ! isset( $value[ $context ] ) ) {
90
				$value[ $context ] = array();
91
			}
92
93
			// Make sure color is defined.
94
			if ( ! isset( $value[ $context ]['color'] ) ) {
95
				$value[ $context ]['color'] = '';
96
			}
97
			// Sanitize colors.
98
			$color_obj = ariColor::newColor( $value[ $context ]['color'] );
99
			$value[ $context ]['color'] = $color_obj->toCSS( $color_obj->mode );
100
101
			// Make sure position is defined.
102
			if ( ! isset( $value[ $context ]['position'] ) ) {
103
				$value[ $context ]['position'] = 0;
104
			};
105
			// Sanitize positions.
106
			$value[ $context ]['position'] = (int) $value[ $context ]['position'];
107
			$value[ $context ]['position'] = max( min( $value[ $context ]['position'], 100 ), 0 );
108
		}
109
110
		// Make siure angle exists.
111
		if ( ! isset( $value['angle'] ) ) {
112
			$value['angle'] = 0;
113
		}
114
		// Sanitize angle.
115
		$value['angle'] = (int) $value['angle'];
116
		$value['angle'] = max( min( $value['angle'], 90 ), -90 );
117
118
		return $value;
119
120
	}
121
122
}
123