Completed
Push — develop ( 81fa35...152d4f )
by Aristeides
02:48
created

Kirki_Field_Image::set_sanitize_callback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 10
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) 2017, Aristeides Stathopoulos
8
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
9
 * @since       2.2.7
10
 */
11
12
/**
13
 * Field overrides.
14
 */
15
class Kirki_Field_Image 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-image';
25
26
	}
27
28
	/**
29
	 * Set the choices.
30
	 * Adds a pseudo-element "controls" that helps with the JS API.
31
	 *
32
	 * @access protected
33
	 */
34
	protected function set_choices() {
35
36
		if ( ! is_array( $this->choices ) ) {
37
			$this->choices = (array) $this->choices;
38
		}
39
		if ( ! isset( $this->choices['save_as'] ) ) {
40
			$this->choices['save_as'] = 'url';
41
		}
42
	}
43
44
	/**
45
	 * Sets the $sanitize_callback
46
	 *
47
	 * @access protected
48
	 */
49
	protected function set_sanitize_callback() {
50
51
		// If a custom sanitize_callback has been defined,
52
		// then we don't need to proceed any further.
53
		if ( ! empty( $this->sanitize_callback ) ) {
54
			return;
55
		}
56
		$this->sanitize_callback = array( $this, 'sanitize' );
57
58
	}
59
60
	/**
61
	 * The sanitize method that will be used as a falback
62
	 *
63
	 * @param string|array $value The control's value.
64
	 */
65
	public function sanitize( $value ) {
66
67
		if ( isset( $this->choices['save_as'] ) && 'array' === $this->choices['save_as'] ) {
68
			return array(
69
				'id'     => ( isset( $value['id'] ) && '' !== $value['id'] ) ? (int) $value['id'] : '',
70
				'url'    => ( isset( $value['url'] ) && '' !== $value['url'] ) ? esc_url_raw( $value['url'] ) : '',
71
				'width'  => ( isset( $value['width'] ) && '' !== $value['width'] ) ? (int) $value['width'] : '',
72
				'height' => ( isset( $value['height'] ) && '' !== $value['height'] ) ? (int) $value['height'] : '',
73
			);
74
		}
75
		if ( isset( $this->choices['save_as'] ) && 'id' === $this->choices['save_as'] ) {
76
			return absint( $value );
77
		}
78
		if ( is_string( $value ) ) {
79
			return esc_url_raw( $value );
80
		}
81
		return $value;
82
	}
83
}
84