Completed
Push — develop ( e1fcbb...c5616a )
by Aristeides
02:10
created

Kirki_Control_Code   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A enqueue() 0 11 2
A to_json() 0 19 3
A content_template() 0 16 2
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
		wp_enqueue_script( 'kirki-dynamic-control', trailingslashit( Kirki::$url ) . 'assets/js/dynamic-control.js', array( 'jquery', 'customize-base' ), false, true );
57
58
		// Make sure $this->choices['language'] is defined.
59
		$this->choices['language'] = ( ! isset( $this->choices['language'] ) ) ? 'css' : $this->choices['language'];
60
61
		wp_enqueue_script( 'kirki-code', trailingslashit( Kirki::$url ) . 'controls/code/code.js', array( 'jquery', 'customize-base', 'kirki-dynamic-control' ), false, true );
62
		wp_enqueue_style( 'kirki-code-css', trailingslashit( Kirki::$url ) . 'controls/code/code.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']   = $this->value();
80
		$this->json['choices'] = $this->choices;
81
		$this->json['link']    = $this->get_link();
82
		$this->json['id']      = $this->id;
83
84
		$this->json['inputAttrs'] = '';
85
		foreach ( $this->input_attrs as $attr => $value ) {
86
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
87
		}
88
89
	}
90
91
	/**
92
	 * An Underscore (JS) template for this control's content (but not its container).
93
	 *
94
	 * Class variables for this control class are available in the `data` JS object;
95
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
96
	 *
97
	 * @see WP_Customize_Control::print_template()
98
	 *
99
	 * @access protected
100
	 */
101
	protected function content_template() {
102
		global $wp_version;
103
		// If we're on WP 4.9+, then we don't need to add anything here.
104
		if ( version_compare( $wp_version, '4.9-beta' ) >= 0 ) {
105
			return;
106
		}
107
		?>
108
		<label>
109
			<# if ( data.label ) { #><span class="customize-control-title">{{{ data.label }}}</span><# } #>
110
			<# if ( data.description ) { #><span class="description customize-control-description">{{{ data.description }}}</span><# } #>
111
			<div class="codemirror-kirki-wrapper">
112
				<textarea {{{ data.inputAttrs }}} data-language="{{ data.choices.language }}" class="kirki-codemirror-editor" {{{ data.link }}}>{{{ data.value }}}</textarea>
113
			</div>
114
		</label>
115
		<?php
116
	}
117
}
118