Completed
Push — develop ( 4235a8...0751fb )
by Aristeides
02:33
created

Kirki_Field_Gradient   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 17
lcom 3
cbo 1

4 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
F sanitize() 0 42 13
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'
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
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 = ( ! is_array( $value ) ) ? array() : $value;
72
73
		if ( ! isset( $value['mode'] ) || 'linear' !== $value['mode'] || 'radial' !== $value['mode'] ) {
74
			$value['mode'] = 'linear';
75
		}
76
		foreach ( array( 'start', 'end' ) as $context ) {
77
78
			// Make sure value is array.
79
			if ( ! isset( $value[ $context ] ) ) {
80
				$value[ $context ] = array();
81
			}
82
83
			// Sanitize colors.
84
			if ( ! isset( $value[ $context ]['color'] ) ) {
85
				$value[ $context ]['color'] = '';
86
			}
87
			$value[ $context ]['color'] = esc_attr( $value[ $context ]['color'] );
88
89
			// Sanitize positions.
90
			if ( ! isset( $value[ $context ]['position'] ) ) {
91
				$value[ $context ]['position'] = 0;
92
			};
93
			$value[ $context ]['position'] = (int) $value[ $context ]['position'];
94
			$value[ $context ]['position'] = max( min( $value[ $context ]['position'], 100 ), 0 );
95
		}
96
97
		// Sanitize angle.
98
		if ( ! isset( $value['angle'] ) ) {
99
			$value['angle'] = 0;
100
		}
101
		$value['angle'] = (int) $value['angle'];
102
		$value['angle'] = max( min( $value['angle'], 90 ), -90 );
103
104
		// Sanitize the type.
105
		$value['type'] = ( ! isset( $value['type'] ) || 'linear' !== $value['type'] || 'radial' !== $value['type'] ) ? 'linear' : $value['type'];
106
107
		return $value;
108
109
	}
110
111
}
112