Kirki_Field_Image::set_button_labels()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 10
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 11
rs 9.9332
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       2.2.7
10
 */
11
12
/**
13
 * Field overrides.
14
 */
15
class Kirki_Field_Image extends Kirki_Field {
16
17
	/**
18
	 * Custom labels.
19
	 * This only exists here for backwards-compatibility purposes.
20
	 *
21
	 * @access public
22
	 * @since 3.0.23
23
	 * @var string
24
	 */
25
	public $button_labels = array();
26
27
	/**
28
	 * Sets the control type.
29
	 *
30
	 * @access protected
31
	 */
32
	protected function set_type() {
33
		$this->type = 'kirki-image';
34
	}
35
36
	/**
37
	 * Sets the button labels.
38
	 *
39
	 * @access protected
40
	 * @since 3.0.23
41
	 * @return void
42
	 */
43
	protected function set_button_labels() {
44
		$this->button_labels = wp_parse_args(
45
			$this->button_labels,
46
			array(
47
				'select'       => esc_html__( 'Select image', 'kirki' ),
48
				'change'       => esc_html__( 'Change image', 'kirki' ),
49
				'default'      => esc_html__( 'Default', 'kirki' ),
50
				'remove'       => esc_html__( 'Remove', 'kirki' ),
51
				'placeholder'  => esc_html__( 'No image selected', 'kirki' ),
52
				'frame_title'  => esc_html__( 'Select image', 'kirki' ),
53
				'frame_button' => esc_html__( 'Choose image', 'kirki' ),
54
			)
55
		);
56
	}
57
58
	/**
59
	 * Set the choices.
60
	 * Adds a pseudo-element "controls" that helps with the JS API.
61
	 *
62
	 * @access protected
63
	 */
64
	protected function set_choices() {
65
		if ( ! is_array( $this->choices ) ) {
0 ignored issues
show
introduced by
The condition is_array($this->choices) is always true.
Loading history...
66
			$this->choices = (array) $this->choices;
67
		}
68
		if ( ! isset( $this->choices['save_as'] ) ) {
69
			$this->choices['save_as'] = 'url';
70
		}
71
		if ( ! isset( $this->choices['labels'] ) ) {
72
			$this->choices['labels'] = array();
73
		}
74
		$this->set_button_labels();
75
		$this->choices['labels'] = wp_parse_args( $this->choices['labels'], $this->button_labels );
76
	}
77
78
	/**
79
	 * Sets the $sanitize_callback
80
	 *
81
	 * @access protected
82
	 */
83
	protected function set_sanitize_callback() {
84
85
		// If a custom sanitize_callback has been defined,
86
		// then we don't need to proceed any further.
87
		if ( ! empty( $this->sanitize_callback ) ) {
88
			return;
89
		}
90
		$this->sanitize_callback = array( $this, 'sanitize' );
91
	}
92
93
	/**
94
	 * The sanitize method that will be used as a falback
95
	 *
96
	 * @param string|array $value The control's value.
97
	 */
98
	public function sanitize( $value ) {
99
		if ( isset( $this->choices['save_as'] ) && 'array' === $this->choices['save_as'] ) {
100
			return array(
101
				'id'     => ( isset( $value['id'] ) && '' !== $value['id'] ) ? (int) $value['id'] : '',
102
				'url'    => ( isset( $value['url'] ) && '' !== $value['url'] ) ? esc_url_raw( $value['url'] ) : '',
103
				'width'  => ( isset( $value['width'] ) && '' !== $value['width'] ) ? (int) $value['width'] : '',
104
				'height' => ( isset( $value['height'] ) && '' !== $value['height'] ) ? (int) $value['height'] : '',
105
			);
106
		}
107
		if ( isset( $this->choices['save_as'] ) && 'id' === $this->choices['save_as'] ) {
108
			return absint( $value );
109
		}
110
		if ( is_string( $value ) ) {
111
			return esc_url_raw( $value );
112
		}
113
		return $value;
114
	}
115
}
116