Passed
Push — develop ( fe986b...95ca01 )
by Aristeides
04:19
created

Kirki_Field_Background::set_js_vars()   B

Complexity

Conditions 10
Paths 14

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 14
nop 0
dl 0
loc 48
rs 7.2678
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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_Background 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-background';
25
26
	}
27
28
	/**
29
	 * Sets the $sanitize_callback
30
	 *
31
	 * @access protected
32
	 */
33
	protected function set_sanitize_callback() {
34
35
		// If a custom sanitize_callback has been defined,
36
		// then we don't need to proceed any further.
37
		if ( ! empty( $this->sanitize_callback ) ) {
38
			return;
39
		}
40
		$this->sanitize_callback = array( $this, 'sanitize' );
41
42
	}
43
44
	/**
45
	 * Sanitizes typography controls
46
	 *
47
	 * @since 2.2.0
48
	 * @param array $value The value.
49
	 * @return array
50
	 */
51
	public function sanitize( $value ) {
52
53
		if ( ! is_array( $value ) ) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
54
			return array();
55
		}
56
		return array(
57
			'background-color'      => ( isset( $value['background-color'] ) ) ? esc_attr( $value['background-color'] ) : '',
58
			'background-image'      => ( isset( $value['background-image'] ) ) ? esc_url_raw( $value['background-image'] ) : '',
59
			'background-repeat'     => ( isset( $value['background-repeat'] ) ) ? esc_attr( $value['background-repeat'] ) : '',
60
			'background-position'   => ( isset( $value['background-position'] ) ) ? esc_attr( $value['background-position'] ) : '',
61
			'background-size'       => ( isset( $value['background-size'] ) ) ? esc_attr( $value['background-size'] ) : '',
62
			'background-attachment' => ( isset( $value['background-attachment'] ) ) ? esc_attr( $value['background-attachment'] ) : '',
63
		);
64
	}
65
66
	/**
67
	 * Sets the $js_vars
68
	 *
69
	 * @access protected
70
	 */
71
	protected function set_js_vars() {
72
73
		// Typecast to array.
74
		$this->js_vars = (array) $this->js_vars;
75
76
		// Check if transport is set to auto.
77
		// If not, then skip the auto-calculations and exit early.
78
		if ( 'auto' !== $this->transport ) {
79
			return;
80
		}
81
82
		// Set transport to refresh initially.
83
		// Serves as a fallback in case we failt to auto-calculate js_vars.
84
		$this->transport = 'refresh';
85
86
		$js_vars = array();
87
88
		// Try to auto-generate js_vars.
89
		// First we need to check if js_vars are empty, and that output is not empty.
90
		if ( empty( $this->js_vars ) && ! empty( $this->output ) ) {
91
92
			// Start going through each item in the $output array.
93
			foreach ( $this->output as $output ) {
94
95
				// If 'element' is not defined, skip this.
96
				if ( ! isset( $output['element'] ) ) {
97
					continue;
98
				}
99
				if ( is_array( $output['element'] ) ) {
100
					$output['element'] = implode( ',', $output['element'] );
101
				}
102
103
				// If there's a sanitize_callback defined, skip this.
104
				if ( isset( $output['sanitize_callback'] ) && ! empty( $output['sanitize_callback'] ) ) {
105
					continue;
106
				}
107
108
				// If we got this far, it's safe to add this.
109
				$js_vars[] = $output;
110
			}
111
112
			// Did we manage to get all the items from 'output'?
113
			// If not, then we're missing something so don't add this.
114
			if ( count( $js_vars ) !== count( $this->output ) ) {
115
				return;
116
			}
117
			$this->js_vars   = $js_vars;
118
			$this->transport = 'postMessage';
119
120
		}
121
	}
122
}
123