Issues (377)

field/class-kirki-field-background.php (1 issue)

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