Completed
Pull Request — master (#1454)
by Aristeides
05:20 queued 02:36
created

Kirki_Control_Image::content_template()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 48
rs 9.125
c 0
b 0
f 0
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
		wp_enqueue_script( 'kirki-image', trailingslashit( Kirki::$url ) . 'controls/image/image.js', array( 'jquery', 'customize-base' ) );
49
		wp_enqueue_style( 'kirki-image', trailingslashit( Kirki::$url ) . 'controls/image/image.css', null );
50
	}
51
52
	/**
53
	 * Refresh the parameters passed to the JavaScript via JSON.
54
	 *
55
	 * @access public
56
	 */
57
	public function to_json() {
58
		parent::to_json();
59
60
		$this->json['default'] = $this->setting->default;
61
		if ( isset( $this->default ) ) {
62
			$this->json['default'] = $this->default;
63
		}
64
		$this->json['output']  = $this->output;
65
		$this->json['value']   = $this->value();
66
		$this->json['choices'] = $this->choices;
67
		$this->json['link']    = $this->get_link();
68
		$this->json['id']      = $this->id;
69
70
		$this->json['inputAttrs'] = '';
71
		foreach ( $this->input_attrs as $attr => $value ) {
72
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
73
		}
74
	}
75
76
	/**
77
	 * Render the control's content.
78
	 *
79
	 * @see WP_Customize_Control::render_content()
80
	 */
81
	protected function render_content() {}
82
83
	/**
84
	 * An Underscore (JS) template for this control's content (but not its container).
85
	 *
86
	 * Class variables for this control class are available in the `data` JS object;
87
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
88
	 *
89
	 * @see WP_Customize_Control::print_template()
90
	 *
91
	 * @access protected
92
	 */
93
	protected function content_template() {
94
		?>
95
		<#
96
		var saveAs = 'url';
97
		if ( ! _.isUndefined( data.choices ) && ! _.isUndefined( data.choices.save_as ) ) {
98
			saveAs = data.choices.save_as;
99
		}
100
		url = data.value;
101
		if ( _.isObject( data.value ) && ! _.isUndefined( data.value.url ) ) {
102
			url = data.value.url;
103
		}
104
		#>
105
		<label>
106
			<span class="customize-control-title">
107
				{{{ data.label }}}
108
			</span>
109
			<# if ( data.description ) { #>
110
				<span class="description customize-control-description">{{{ data.description }}}</span>
111
			<# } #>
112
		</label>
113
		<div class="image-wrapper attachment-media-view image-upload">
114
			<# if ( data.value['url'] || '' !== url ) { #>
115
				<div class="thumbnail thumbnail-image">
116
					<img src="{{ url }}" alt="" />
117
				</div>
118
			<# } else { #>
119
				<div class="placeholder">
120
					<?php esc_attr_e( 'No File Selected', 'kirki' ); ?>
121
				</div>
122
			<# } #>
123
			<div class="actions">
124
				<button class="button image-upload-remove-button<# if ( '' === url ) { #> hidden <# } #>">
125
					<?php esc_attr_e( 'Remove', 'kirki' ); ?>
126
				</button>
127
				<# if ( data.default && '' !== data.default ) { #>
128
					<button type="button" class="button image-default-button"<# if ( data.default === data.value || ( ! _.isUndefined( data.value.url ) && data.default === data.value.url ) ) { #> style="display:none;"<# } #>>
129
						<?php esc_attr_e( 'Default', 'kirki' ); ?>
130
					</button>
131
				<# } #>
132
				<button type="button" class="button image-upload-button">
133
					<?php esc_attr_e( 'Select File', 'kirki' ); ?>
134
				</button>
135
			</div>
136
		</div>
137
		<# value = ( 'array' === saveAs ) ? JSON.stringify( data.value ) : data.value; #>
138
		<input class="image-hidden-value" type="hidden" value='{{{ value }}}' {{{ data.link }}}>
139
		<?php
140
	}
141
}
142