Completed
Push — develop ( edbe40...81fa35 )
by Aristeides
02:58
created

Kirki_Control_Image   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 136
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A enqueue() 0 9 2
A to_json() 0 20 3
A render_content() 0 1 1
B content_template() 0 34 1
A l10n() 0 8 1
1
<?php
2
/**
3
 * Customizer Control: image.
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
 * Adds the image control.
14
 */
15
class Kirki_Control_Image extends WP_Customize_Control {
16
17
	/**
18
	 * The control type.
19
	 *
20
	 * @access public
21
	 * @var string
22
	 */
23
	public $type = 'kirki-image';
24
25
	/**
26
	 * Used to automatically generate all CSS output.
27
	 *
28
	 * @access public
29
	 * @var array
30
	 */
31
	public $output = array();
32
33
	/**
34
	 * Data type
35
	 *
36
	 * @access public
37
	 * @var string
38
	 */
39
	public $option_type = 'theme_mod';
40
41
	/**
42
	 * Enqueue control related scripts/styles.
43
	 *
44
	 * @access public
45
	 */
46
	public function enqueue() {
47
48
		Kirki_Custom_Build::register_dependency( 'jquery' );
49
50
		if ( ! Kirki_Custom_Build::is_custom_build() ) {
51
			wp_enqueue_script( 'kirki-image', trailingslashit( Kirki::$url ) . 'controls/image/image.js', array( 'jquery', 'customize-base' ) );
52
			wp_enqueue_style( 'kirki-image', trailingslashit( Kirki::$url ) . 'controls/image/image.css', null );
53
		}
54
	}
55
56
	/**
57
	 * Refresh the parameters passed to the JavaScript via JSON.
58
	 *
59
	 * @access public
60
	 */
61
	public function to_json() {
62
		parent::to_json();
63
64
		$this->json['default'] = $this->setting->default;
65
		if ( isset( $this->default ) ) {
66
			$this->json['default'] = $this->default;
67
		}
68
		$this->json['output']  = $this->output;
69
		$this->json['value']   = $this->value();
70
		$this->json['choices'] = $this->choices;
71
		$this->json['link']    = $this->get_link();
72
		$this->json['id']      = $this->id;
73
74
		$this->json['inputAttrs'] = '';
75
		foreach ( $this->input_attrs as $attr => $value ) {
76
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
77
		}
78
		$config_id = 'global';
79
		$this->json['l10n'] = $this->l10n( $config_id );
80
	}
81
82
	/**
83
	 * Render the control's content.
84
	 *
85
	 * @see WP_Customize_Control::render_content()
86
	 */
87
	protected function render_content() {}
88
89
	/**
90
	 * An Underscore (JS) template for this control's content (but not its container).
91
	 *
92
	 * Class variables for this control class are available in the `data` JS object;
93
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
94
	 *
95
	 * @see WP_Customize_Control::print_template()
96
	 *
97
	 * @access protected
98
	 */
99
	protected function content_template() {
100
		?>
101
		<label>
102
			<span class="customize-control-title">
103
				{{{ data.label }}}
104
			</span>
105
			<# if ( data.description ) { #>
106
				<span class="description customize-control-description">{{{ data.description }}}</span>
107
			<# } #>
108
		</label>
109
		<div class="image-wrapper attachment-media-view image-upload">
110
			<# if ( data.value['url'] ) { #>
111
				<div class="thumbnail thumbnail-image">
112
					<img src="{{ data.value['url'] }}" alt="" />
113
				</div>
114
			<# } else { #>
115
				<div class="placeholder">
116
					{{ data.l10n['no-file-selected'] }}
117
				</div>
118
			<# } #>
119
			<div class="actions">
120
				<button class="button image-upload-remove-button<# if ( ! data.value['url'] ) { #> hidden <# } #>">
121
					{{ data.l10n['remove'] }}
122
				</button>
123
				<button type="button" class="button image-upload-button">
124
					{{ data.l10n['select-file'] }}
125
				</button>
126
			</div>
127
		</div>
128
		<# value = ( ! _.isUndefined( data.choices ) && ! _.isUndefined( data.choices.save_as ) && 'array' === data.choices.save_as ) ? JSON.stringify( data.value ) : data.value; #>
129
130
		<input class="image-hidden-value" type="hidden" value='{{{ value }}}' {{{ data.link }}}>
131
		<?php
132
	}
133
134
	/**
135
	 * Returns an array of translation strings.
136
	 *
137
	 * @access protected
138
	 * @since 3.0.0
139
	 * @param string|false $config_id The string-ID.
140
	 * @return array
141
	 */
142
	protected function l10n( $config_id ) {
143
		$translation_strings = array(
144
			'no-file-selected'      => esc_attr__( 'No File Selected', 'kirki' ),
145
			'remove'                => esc_attr__( 'Remove', 'kirki' ),
146
			'select-file'           => esc_attr__( 'Select File', 'kirki' ),
147
		);
148
		return apply_filters( "kirki/{$config_id}/l10n", $translation_strings );
149
	}
150
}
151