Kirki_Control_Radio_Image   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A content_template() 0 3 1
A to_json() 0 8 3
1
<?php
2
/**
3
 * Customizer Control: radio-image.
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
8
 * @license    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
		foreach ( $this->input_attrs as $attr => $value ) {
38
			if ( 'style' !== $attr ) {
39
				$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
40
				continue;
41
			}
42
			$this->json['labelStyle'] = 'style="' . esc_attr( $value ) . '" ';
43
		}
44
45
	}
46
47
	/**
48
	 * An Underscore (JS) template for this control's content (but not its container).
49
	 *
50
	 * Class variables for this control class are available in the `data` JS object;
51
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
52
	 *
53
	 * @see WP_Customize_Control::print_template()
54
	 *
55
	 * @access protected
56
	 */
57
	protected function content_template() {
58
		?>
59
		<label class="customizer-text">
60
			<# if ( data.label ) { #><span class="customize-control-title">{{{ data.label }}}</span><# } #>
61
			<# if ( data.description ) { #><span class="description customize-control-description">{{{ data.description }}}</span><# } #>
62
		</label>
63
		<div id="input_{{ data.id }}" class="image">
64
			<# for ( key in data.choices ) { #>
65
				<# dataAlt = ( _.isObject( data.choices[ key ] ) && ! _.isUndefined( data.choices[ key ].alt ) ) ? data.choices[ key ].alt : '' #>
66
				<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 }}">
67
					<label for="{{ data.id }}{{ key }}" {{{ data.labelStyle }}} class="{{{ data.id + key }}}">
68
						<# if ( _.isObject( data.choices[ key ] ) ) { #>
69
							<img src="{{ data.choices[ key ].src }}" alt="{{ data.choices[ key ].alt }}">
70
							<span class="image-label"><span class="inner">{{ data.choices[ key ].alt }}</span></span>
71
						<# } else { #>
72
							<img src="{{ data.choices[ key ] }}">
73
						<# } #>
74
						<span class="image-clickable"></span>
75
					</label>
76
				</input>
77
			<# } #>
78
		</div>
79
		<?php
80
	}
81
}
82