Completed
Push — develop ( a7b7e8...718dc0 )
by Aristeides
03:14
created

Kirki_Control_Image::l10n()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
rs 9.4285
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
		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
	}
80
81
	/**
82
	 * Render the control's content.
83
	 *
84
	 * @see WP_Customize_Control::render_content()
85
	 */
86
	protected function render_content() {}
87
88
	/**
89
	 * An Underscore (JS) template for this control's content (but not its container).
90
	 *
91
	 * Class variables for this control class are available in the `data` JS object;
92
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
93
	 *
94
	 * @see WP_Customize_Control::print_template()
95
	 *
96
	 * @access protected
97
	 */
98
	protected function content_template() {
99
		?>
100
		<div class="kirki-controls-loading-spinner"><div class="bounce1"></div><div class="bounce2"></div><div class="bounce3"></div></div>
101
		<# saveAs = ( ! _.isUndefined( data.choices ) && ! _.isUndefined( data.choices.save_as ) && 'array' === data.choices.save_as ) ? 'array' : 'url'; #>
102
		<# url = ( 'array' === saveAs && data.value['url'] ) ? data.value['url'] : data.value; #>
103
		<label>
104
			<span class="customize-control-title">
105
				{{{ data.label }}}
106
			</span>
107
			<# if ( data.description ) { #>
108
				<span class="description customize-control-description">{{{ data.description }}}</span>
109
			<# } #>
110
		</label>
111
		<div class="image-wrapper attachment-media-view image-upload">
112
			<# if ( data.value['url'] ) { #>
113
				<div class="thumbnail thumbnail-image">
114
					<img src="{{ url }}" alt="" />
115
				</div>
116
			<# } else { #>
117
				<div class="placeholder">
118
					<?php esc_attr_e( 'No File Selected', 'kirki' ); ?>
119
				</div>
120
			<# } #>
121
			<div class="actions">
122
				<button class="button image-upload-remove-button<# if ( '' === url ) { #> hidden <# } #>">
123
					<?php esc_attr_e( 'Remove', 'kirki' ); ?>
124
				</button>
125
				<button type="button" class="button image-upload-button">
126
					<?php esc_attr_e( 'Select File', 'kirki' ); ?>
127
				</button>
128
			</div>
129
		</div>
130
		<# value = ( 'array' === saveAs ) ? JSON.stringify( data.value ) : data.value; #>
131
		<input class="image-hidden-value" type="hidden" value='{{{ value }}}' {{{ data.link }}}>
132
		<?php
133
	}
134
}
135