Completed
Push — develop ( 067e83...a90151 )
by Aristeides
04:18 queued 37s
created

Kirki_Field_Gradient::set_choices()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Override field methods
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2016, 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
			'direction' => 'top-to-bottom',
42
		);
43
		$this->choices = wp_parse_args( $this->choices, $defaults );
44
45
	}
46
47
	/**
48
	 * Sets the $sanitize_callback.
49
	 *
50
	 * @access protected
51
	 */
52
	protected function set_sanitize_callback() {
53
54
		$this->sanitize_callback = array( $this, 'sanitize' );
55
56
	}
57
58
	/**
59
	 * Sanitizes checkbox values.
60
	 *
61
	 * @access public
62
	 * @param boolean|integer|string|null $value The checkbox value.
63
	 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
64
	 */
65
	public function sanitize( $value = null ) {
66
67
		// Make sure value in an array.
68
		$value = ( ! is_array( $value ) ) ? array() : $value;
69
70
		// Make sure start & end values are arrays.
71
		$value['start'] = ( ! isset( $value['start'] ) ) ? array() : $value['start'];
72
		$value['end']   = ( ! isset( $value['end'] ) )   ? array() : $value['end'];
73
74
		foreach ( array( 'start', 'end' ) as $context ) {
75
76
			// Sanitize colors.
77
			if ( ! isset( $value[ $context ]['color'] ) ) {
78
				$value[ $context ]['color'] = '';
79
			}
80
			$value[ $context ]['color'] = esc_attr( $value[ $context ]['color'] );
81
82
			// Sanitize positions.
83
			if ( ! isset( $value[ $context ]['position'] ) ) {
84
				$value[ $context ]['position'] = 0;
85
			};
86
			$value[ $context ]['position'] = (int) $value[ $context ]['position'];
87
			$value[ $context ]['position'] = max( min( $value[ $context ]['position'], 100 ), 0 );
88
		}
89
90
		// Sanitize angle.
91
		if ( ! isset( $value['angle'] ) ) {
92
			$value['angle'] = 0;
93
		}
94
		$value['angle'] = (int) $value['angle'];
95
		$value['angle'] = max( min( $value['angle'], 90 ), -90 );
96
97
		// Sanitize the type.
98
		$value['type'] = ( ! isset( $value['type'] ) || 'linear' !== $value['type'] || 'radial' !== $value['type'] ) ? 'linear' : $value['type'];
99
100
		return $value;
101
102
	}
103
104
}
105