Completed
Push — develop ( 8a2f67...52f1e8 )
by Aristeides
02:16
created

Kirki_Control_Sortable::render_content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Control: sortable.
4
 *
5
 * @package     Kirki
6
 * @subpackage  Controls
7
 * @copyright   Copyright (c) 2016, 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
 * Sortable control (uses checkboxes).
19
 */
20
class Kirki_Control_Sortable extends WP_Customize_Control {
21
22
	/**
23
	 * The control type.
24
	 *
25
	 * @access public
26
	 * @var string
27
	 */
28
	public $type = 'kirki-sortable';
29
30
	/**
31
	 * Used to automatically generate all CSS output.
32
	 *
33
	 * @access public
34
	 * @var array
35
	 */
36
	public $output = array();
37
38
	/**
39
	 * Data type
40
	 *
41
	 * @access public
42
	 * @var string
43
	 */
44
	public $option_type = 'theme_mod';
45
46
	/**
47
	 * The kirki_config we're using for this control
48
	 *
49
	 * @access public
50
	 * @var string
51
	 */
52
	public $kirki_config = 'global';
53
54
	/**
55
	 * Enqueue control related scripts/styles.
56
	 *
57
	 * @access public
58
	 */
59
	public function enqueue() {
60
61
		wp_enqueue_script( 'kirki-sortable', trailingslashit( Kirki::$url ) . 'controls/sortable/sortable.js', array( 'jquery', 'customize-base', 'jquery-ui-core', 'jquery-ui-sortable' ), false, true );
62
		wp_enqueue_style( 'kirki-sortable-css', trailingslashit( Kirki::$url ) . 'controls/sortable/sortable.css', null );
63
64
	}
65
66
	/**
67
	 * Refresh the parameters passed to the JavaScript via JSON.
68
	 *
69
	 * @see WP_Customize_Control::to_json()
70
	 */
71
	public function to_json() {
72
		parent::to_json();
73
74
		$this->json['default'] = $this->setting->default;
75
		if ( isset( $this->default ) ) {
76
			$this->json['default'] = $this->default;
77
		}
78
		$this->json['output']      = $this->output;
79
		$this->json['value']       = maybe_unserialize( $this->value() );
80
		$this->json['choices']     = $this->choices;
81
		$this->json['link']        = $this->get_link();
82
		$this->json['id']          = $this->id;
83
		$this->json['kirkiConfig'] = $this->kirki_config;
84
85
		if ( 'user_meta' === $this->option_type ) {
86
			$this->json['value'] = get_user_meta( get_current_user_id(), $this->id, true );
87
		}
88
89
		$this->json['inputAttrs'] = '';
90
		foreach ( $this->input_attrs as $attr => $value ) {
91
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
92
		}
93
		$this->json['inputAttrs'] = maybe_serialize( $this->input_attrs() );
94
95
	}
96
97
	/**
98
	 * An Underscore (JS) template for this control's content (but not its container).
99
	 *
100
	 * Class variables for this control class are available in the `data` JS object;
101
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
102
	 *
103
	 * @see WP_Customize_Control::print_template()
104
	 *
105
	 * @access protected
106
	 */
107
	protected function content_template() {
108
		?>
109
		<label class='kirki-sortable'>
110
			<span class="customize-control-title">
111
				{{{ data.label }}}
112
			</span>
113
			<# if ( data.description ) { #>
114
				<span class="description customize-control-description">{{{ data.description }}}</span>
115
			<# } #>
116
117
			<ul class="sortable">
118
				<# _.each( data.value, function( choiceID ) { #>
119
					<li {{{ data.inputAttrs }}} class='kirki-sortable-item' data-value='{{ choiceID }}'>
120
						<i class='dashicons dashicons-menu'></i>
121
						<i class="dashicons dashicons-visibility visibility"></i>
122
						{{{ data.choices[ choiceID ] }}}
123
					</li>
124
				<# }); #>
125
				<# _.each( data.choices, function( choiceLabel, choiceID ) { #>
126
					<# if ( -1 === data.value.indexOf( choiceID ) ) { #>
127
						<li {{{ data.inputAttrs }}} class='kirki-sortable-item invisible' data-value='{{ choiceID }}'>
128
							<i class='dashicons dashicons-menu'></i>
129
							<i class="dashicons dashicons-visibility visibility"></i>
130
							{{{ data.choices[ choiceID ] }}}
131
						</li>
132
					<# } #>
133
				<# }); #>
134
			</ul>
135
		</label>
136
137
		<?php
138
	}
139
140
	/**
141
	 * Render the control's content.
142
	 *
143
	 * @see WP_Customize_Control::render_content()
144
	 */
145
	protected function render_content() {}
146
}
147