Completed
Pull Request — develop (#1603)
by Aristeides
02:18
created

Kirki_Control_Radio_Image   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A to_json() 0 12 3
B content_template() 0 24 1
1
<?php
2
/**
3
 * Customizer Control: radio-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       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Radio Image control (modified radio).
19
 */
20
class Kirki_Control_Radio_Image extends Kirki_Control_Base {
21
22
	/**
23
	 * The control type.
24
	 *
25
	 * @access public
26
	 * @var string
27
	 */
28
	public $type = 'kirki-radio-image';
29
30
	/**
31
	 * Refresh the parameters passed to the JavaScript via JSON.
32
	 *
33
	 * @see WP_Customize_Control::to_json()
34
	 */
35
	public function to_json() {
36
		parent::to_json();
37
38
		foreach ( $this->input_attrs as $attr => $value ) {
39
			if ( 'style' !== $attr ) {
40
				$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
41
				continue;
42
			}
43
			$this->json['labelStyle'] = 'style="' . esc_attr( $value ) . '" ';
44
		}
45
46
	}
47
48
	/**
49
	 * An Underscore (JS) template for this control's content (but not its container).
50
	 *
51
	 * Class variables for this control class are available in the `data` JS object;
52
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
53
	 *
54
	 * @see WP_Customize_Control::print_template()
55
	 *
56
	 * @access protected
57
	 */
58
	protected function content_template() {
59
		?>
60
		<label class="customizer-text">
61
			<# if ( data.label ) { #><span class="customize-control-title">{{{ data.label }}}</span><# } #>
62
			<# if ( data.description ) { #><span class="description customize-control-description">{{{ data.description }}}</span><# } #>
63
		</label>
64
		<div id="input_{{ data.id }}" class="image">
65
			<# for ( key in data.choices ) { #>
66
				<# dataAlt = ( _.isObject( data.choices[ key ] ) && ! _.isUndefined( data.choices[ key ].alt ) ) ? data.choices[ key ].alt : '' #>
67
				<input {{{ data.inputAttrs }}} class="image-select" type="radio" value="{{ key }}" name="_customize-radio-{{ data.id }}" id="{{ data.id }}{{ key }}" {{{ data.link }}}<# if ( data.value === key ) { #> checked="checked"<# } #> data-alt="{{ dataAlt }}">
68
					<label for="{{ data.id }}{{ key }}" {{{ data.labelStyle }}} class="{{{ data.id + key }}}">
69
						<# if ( _.isObject( data.choices[ key ] ) ) { #>
70
							<img src="{{ data.choices[ key ].src }}" alt="{{ data.choices[ key ].alt }}">
71
							<span class="image-label"><span class="inner">{{ data.choices[ key ].alt }}</span></span>
72
						<# } else { #>
73
							<img src="{{ data.choices[ key ] }}">
74
						<# } #>
75
						<span class="image-clickable"></span>
76
					</label>
77
				</input>
78
			<# } #>
79
		</div>
80
		<?php
81
	}
82
}
83