Completed
Push — develop ( 25b4bc...d8be36 )
by Aristeides
02:18
created

Kirki_Control_Code::enqueue()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 0
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Control: code.
4
 *
5
 * Creates a new custom control.
6
 * Custom controls accept raw HTML/JS.
7
 *
8
 * @package     Kirki
9
 * @subpackage  Controls
10
 * @copyright   Copyright (c) 2016, Aristeides Stathopoulos
11
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
12
 * @since       1.0
13
 */
14
15
// Exit if accessed directly.
16
if ( ! defined( 'ABSPATH' ) ) {
17
	exit;
18
}
19
20
/**
21
 * Adds a "code" control, using CodeMirror.
22
 */
23
class Kirki_Control_Code extends WP_Customize_Control {
24
25
	/**
26
	 * The control type.
27
	 *
28
	 * @access public
29
	 * @var string
30
	 */
31
	public $type = 'kirki-code';
32
33
	/**
34
	 * Used to automatically generate all CSS output.
35
	 *
36
	 * @access public
37
	 * @var array
38
	 */
39
	public $output = array();
40
41
	/**
42
	 * Data type
43
	 *
44
	 * @access public
45
	 * @var string
46
	 */
47
	public $option_type = 'theme_mod';
48
49
	/**
50
	 * The kirki_config we're using for this control
51
	 *
52
	 * @access public
53
	 * @var string
54
	 */
55
	public $kirki_config = 'global';
56
57
	/**
58
	 * The translation strings.
59
	 *
60
	 * @access protected
61
	 * @since 2.3.5
62
	 * @var array
63
	 */
64
	protected $l10n = array();
65
66
	/**
67
	 * Enqueue control related scripts/styles.
68
	 *
69
	 * @access public
70
	 */
71
	public function enqueue() {
72
73
		// Register codemirror.
74
		wp_register_script( 'codemirror', trailingslashit( Kirki::$url ) . 'controls/code/codemirror/lib/codemirror.js', array( 'jquery' ) );
75
76
		// If we're using html mode, we'll also need to include the multiplex addon
77
		// as well as dependencies for XML, JS, CSS languages.
78
		if ( in_array( $this->choices['language'], array( 'html', 'htmlmixed' ), true ) ) {
79
			wp_enqueue_script( 'codemirror-multiplex', trailingslashit( Kirki::$url ) . 'controls/code/codemirror/addon/mode/multiplex.js', array( 'jquery', 'codemirror' ) );
80
			wp_enqueue_script( 'codemirror-language-xml', trailingslashit( Kirki::$url ) . 'controls/code/codemirror/mode/xml/xml.js', array( 'jquery', 'codemirror' ) );
81
			wp_enqueue_script( 'codemirror-language-javascript', trailingslashit( Kirki::$url ) . 'controls/code/codemirror/mode/javascript/javascript.js', array( 'jquery', 'codemirror' ) );
82
			wp_enqueue_script( 'codemirror-language-css', trailingslashit( Kirki::$url ) . 'controls/code/codemirror/mode/css/css.js', array( 'jquery', 'codemirror' ) );
83
			wp_enqueue_script( 'codemirror-language-htmlmixed', trailingslashit( Kirki::$url ) . 'controls/code/codemirror/mode/htmlmixed/htmlmixed.js', array( 'jquery', 'codemirror', 'codemirror-multiplex', 'codemirror-language-xml', 'codemirror-language-javascript', 'codemirror-language-css' ) );
84
		} elseif ( 'php' === $this->choices['language'] ) {
85
			wp_enqueue_script( 'codemirror-language-xml', trailingslashit( Kirki::$url ) . 'controls/code/codemirror/mode/xml/xml.js', array( 'jquery', 'codemirror' ) );
86
			wp_enqueue_script( 'codemirror-language-clike', trailingslashit( Kirki::$url ) . 'controls/code/codemirror/mode/clike/clike.js', array( 'jquery', 'codemirror' ) );
87
			wp_enqueue_script( 'codemirror-language-php', trailingslashit( Kirki::$url ) . 'controls/code/codemirror/mode/php/php.js', array( 'jquery', 'codemirror', 'codemirror-language-xml', 'codemirror-language-clike' ) );
88
		} else {
89
			// Add language script.
90
			wp_enqueue_script( 'codemirror-language-' . $this->choices['language'], trailingslashit( Kirki::$url ) . 'controls/code/codemirror/mode/' . $this->choices['language'] . '/' . $this->choices['language'] . '.js', array( 'jquery', 'codemirror' ) );
91
		}
92
93
		// Add theme styles.
94
		wp_enqueue_style( 'codemirror-theme-' . $this->choices['theme'], trailingslashit( Kirki::$url ) . 'controls/code/codemirror/theme/' . $this->choices['theme'] . '.css' );
95
96
		wp_enqueue_script( 'kirki-code', trailingslashit( Kirki::$url ) . 'controls/code/code.js', array( 'jquery', 'customize-base', 'codemirror' ), false, true );
97
		wp_enqueue_style( 'kirki-code-css', trailingslashit( Kirki::$url ) . 'controls/code/code.css', null );
98
99
	}
100
101
	/**
102
	 * Refresh the parameters passed to the JavaScript via JSON.
103
	 *
104
	 * @see WP_Customize_Control::to_json()
105
	 */
106
	public function to_json() {
107
		parent::to_json();
108
109
		$this->json['default'] = $this->setting->default;
110
		if ( isset( $this->default ) ) {
111
			$this->json['default'] = $this->default;
112
		}
113
		$this->json['output']      = $this->output;
114
		$this->json['value']       = $this->value();
115
		$this->json['choices']     = $this->choices;
116
		$this->json['link']        = $this->get_link();
117
		$this->json['id']          = $this->id;
118
		$this->json['l10n']        = Kirki_l10n::get_strings( $this->kirki_config );
119
		$this->json['kirkiConfig'] = $this->kirki_config;
120
121
		if ( 'user_meta' === $this->option_type ) {
122
			$this->json['value'] = get_user_meta( get_current_user_id(), $this->id, true );
123
		}
124
125
		$this->json['inputAttrs'] = '';
126
		foreach ( $this->input_attrs as $attr => $value ) {
127
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
128
		}
129
130
	}
131
132
	/**
133
	 * An Underscore (JS) template for this control's content (but not its container).
134
	 *
135
	 * Class variables for this control class are available in the `data` JS object;
136
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
137
	 *
138
	 * @see WP_Customize_Control::print_template()
139
	 *
140
	 * @access protected
141
	 */
142
	protected function content_template() {
143
		?>
144
		<label>
145
			<# if ( data.label ) { #>
146
				<span class="customize-control-title">{{{ data.label }}}</span>
147
			<# } #>
148
			<# if ( data.description ) { #>
149
				<span class="description customize-control-description">{{{ data.description }}}</span>
150
			<# } #>
151
			<a href="#" class="button edit button-primary">{{ data.choices.label }}</a>
152
			<textarea {{{ data.inputAttrs }}} class="kirki-codemirror-editor collapsed">{{{ data.value }}}</textarea>
153
			<a href="#" class="close">
154
				<span class="dashicons dashicons-no"></span>
155
				<span class="screen-reader-text">{{ data.l10n['close-editor'] }}</span>
156
			</a>
157
		</label>
158
		<?php
159
	}
160
161
	/**
162
	 * Returns an array of translation strings.
163
	 *
164
	 * @access protected
165
	 * @since 2.4.0
166
	 * @param string|false $id The string-ID.
167
	 * @return string
168
	 */
169
	protected function l10n( $id = false ) {
170
		$translation_strings = array(
171
			'close-editor' => esc_attr__( 'Close Editor', 'kirki' ),
172
		);
173
		return apply_filters( 'kirki/' . $this->kirki_config . '/l10n', $translation_strings );
174
	}
175
}
176