Completed
Push — develop ( 6bc4f7...cab9c0 )
by Aristeides
04:02
created

Kirki_Control_Code::to_json()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 4
nop 0
dl 0
loc 19
rs 9.4285
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) 2017, 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
	 * Enqueue control related scripts/styles.
51
	 *
52
	 * @access public
53
	 */
54
	public function enqueue() {
55
56
		// Register codemirror.
57
		wp_register_script( 'codemirror', trailingslashit( Kirki::$url ) . 'assets/vendor/codemirror/lib/codemirror.js', array( 'jquery' ) );
58
59
		// If we're using html mode, we'll also need to include the multiplex addon
60
		// as well as dependencies for XML, JS, CSS languages.
61
		if ( in_array( $this->choices['language'], array( 'html', 'htmlmixed' ), true ) ) {
62
			wp_enqueue_script( 'codemirror-multiplex', trailingslashit( Kirki::$url ) . 'assets/vendor/codemirror/addon/mode/multiplex.js', array( 'jquery', 'codemirror' ) );
63
			wp_enqueue_script( 'codemirror-language-xml', trailingslashit( Kirki::$url ) . 'assets/vendor/codemirror/mode/xml/xml.js', array( 'jquery', 'codemirror' ) );
64
			wp_enqueue_script( 'codemirror-language-javascript', trailingslashit( Kirki::$url ) . 'assets/vendor/codemirror/mode/javascript/javascript.js', array( 'jquery', 'codemirror' ) );
65
			wp_enqueue_script( 'codemirror-language-css', trailingslashit( Kirki::$url ) . 'assets/vendor/codemirror/mode/css/css.js', array( 'jquery', 'codemirror' ) );
66
			wp_enqueue_script( 'codemirror-language-htmlmixed', trailingslashit( Kirki::$url ) . 'assets/vendor/codemirror/mode/htmlmixed/htmlmixed.js', array( 'jquery', 'codemirror', 'codemirror-multiplex', 'codemirror-language-xml', 'codemirror-language-javascript', 'codemirror-language-css' ) );
67
		} elseif ( 'php' === $this->choices['language'] ) {
68
			wp_enqueue_script( 'codemirror-language-xml', trailingslashit( Kirki::$url ) . 'assets/vendor/codemirror/mode/xml/xml.js', array( 'jquery', 'codemirror' ) );
69
			wp_enqueue_script( 'codemirror-language-clike', trailingslashit( Kirki::$url ) . 'assets/vendor/codemirror/mode/clike/clike.js', array( 'jquery', 'codemirror' ) );
70
			wp_enqueue_script( 'codemirror-language-php', trailingslashit( Kirki::$url ) . 'assets/vendor/codemirror/mode/php/php.js', array( 'jquery', 'codemirror', 'codemirror-language-xml', 'codemirror-language-clike' ) );
71
		} else {
72
			// Add language script.
73
			wp_enqueue_script( 'codemirror-language-' . $this->choices['language'], trailingslashit( Kirki::$url ) . 'assets/vendor/codemirror/mode/' . $this->choices['language'] . '/' . $this->choices['language'] . '.js', array( 'jquery', 'codemirror' ) );
74
		}
75
76
		// Add theme styles.
77
		wp_enqueue_style( 'codemirror-theme-' . $this->choices['theme'], trailingslashit( Kirki::$url ) . 'assets/vendor/codemirror/theme/' . $this->choices['theme'] . '.css' );
78
79
		wp_enqueue_script( 'kirki-code', trailingslashit( Kirki::$url ) . 'controls/code/code.js', array( 'jquery', 'customize-base', 'codemirror' ), false, true );
80
		wp_enqueue_style( 'kirki-code-css', trailingslashit( Kirki::$url ) . 'controls/code/code.css', null );
81
82
	}
83
84
	/**
85
	 * Refresh the parameters passed to the JavaScript via JSON.
86
	 *
87
	 * @see WP_Customize_Control::to_json()
88
	 */
89
	public function to_json() {
90
		parent::to_json();
91
92
		$this->json['default'] = $this->setting->default;
93
		if ( isset( $this->default ) ) {
94
			$this->json['default'] = $this->default;
95
		}
96
		$this->json['output']  = $this->output;
97
		$this->json['value']   = $this->value();
98
		$this->json['choices'] = $this->choices;
99
		$this->json['link']    = $this->get_link();
100
		$this->json['id']      = $this->id;
101
102
		$this->json['inputAttrs'] = '';
103
		foreach ( $this->input_attrs as $attr => $value ) {
104
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
105
		}
106
107
	}
108
109
	/**
110
	 * An Underscore (JS) template for this control's content (but not its container).
111
	 *
112
	 * Class variables for this control class are available in the `data` JS object;
113
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
114
	 *
115
	 * @see WP_Customize_Control::print_template()
116
	 *
117
	 * @access protected
118
	 */
119
	protected function content_template() {
120
		?>
121
		<label>
122
			<# if ( data.label ) { #>
123
				<span class="customize-control-title">{{{ data.label }}}</span>
124
			<# } #>
125
			<# if ( data.description ) { #>
126
				<span class="description customize-control-description">{{{ data.description }}}</span>
127
			<# } #>
128
			<div class="codemirror-kirki-wrapper">
129
				<textarea {{{ data.inputAttrs }}} class="kirki-codemirror-editor">{{{ data.value }}}</textarea>
130
			</div>
131
		</label>
132
		<?php
133
	}
134
}
135