Completed
Push — develop ( c69b96...434ccf )
by Aristeides
02:58
created

Kirki_Control_Image::to_json()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 4
nop 0
dl 0
loc 18
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
		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
		    url    = '';
98
99
		if ( ! _.isUndefined( data.choices ) && ! _.isUndefined( data.choices.save_as ) ) {
100
			saveAs = data.choices.save_as;
101
		}
102
		if ( 'url' === saveAs ) {
103
			url = data.value;
104
		}
105
		if ( 'array' === saveAs && data.value['url'] ) {
106
			url = data.value['url'];
107
		}
108
		if ( 'id' === saveAs ) {
109
			url = <?php echo ( is_int( $this->value() ) ) ? wp_get_attachment_url( $this->value() ) : "''"; ?>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '('
Loading history...
110
		}
111
		#>
112
		<label>
113
			<span class="customize-control-title">
114
				{{{ data.label }}}
115
			</span>
116
			<# if ( data.description ) { #>
117
				<span class="description customize-control-description">{{{ data.description }}}</span>
118
			<# } #>
119
		</label>
120
		<div class="image-wrapper attachment-media-view image-upload">
121
			<# if ( data.value['url'] || '' !== url ) { #>
122
				<div class="thumbnail thumbnail-image">
123
					<img src="{{ url }}" alt="" />
124
				</div>
125
			<# } else { #>
126
				<div class="placeholder">
127
					<?php esc_attr_e( 'No File Selected', 'kirki' ); ?>
128
				</div>
129
			<# } #>
130
			<div class="actions">
131
				<button class="button image-upload-remove-button<# if ( '' === url ) { #> hidden <# } #>">
132
					<?php esc_attr_e( 'Remove', 'kirki' ); ?>
133
				</button>
134
				<# if ( data.default && '' !== data.default ) { #>
135
					<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;"<# } #>>
136
						<?php esc_attr_e( 'Default', 'kirki' ); ?>
137
					</button>
138
				<# } #>
139
				<button type="button" class="button image-upload-button">
140
					<?php esc_attr_e( 'Select File', 'kirki' ); ?>
141
				</button>
142
			</div>
143
		</div>
144
		<# value = ( 'array' === saveAs ) ? JSON.stringify( data.value ) : data.value; #>
145
		<input class="image-hidden-value" type="hidden" value='{{{ value }}}' {{{ data.link }}}>
146
		<?php
147
	}
148
}
149