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

Kirki_Control_Editor::to_json()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 4
nop 0
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Customizer Control: editor.
4
 *
5
 * Creates a TinyMCE textarea.
6
 *
7
 * @package     Kirki
8
 * @subpackage  Controls
9
 * @copyright   Copyright (c) 2017, Aristeides Stathopoulos
10
 * @license     http://opensource.org/licenses/https://opensource.org/licenses/MIT
11
 * @since       1.0
12
 */
13
14
// Exit if accessed directly.
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
/**
20
 * A TinyMCE control.
21
 */
22
class Kirki_Control_Editor extends WP_Customize_Control {
23
24
	/**
25
	 * The control type.
26
	 *
27
	 * @access public
28
	 * @var string
29
	 */
30
	public $type = 'kirki-editor';
31
32
	/**
33
	 * Used to automatically generate all CSS output.
34
	 *
35
	 * @access public
36
	 * @var array
37
	 */
38
	public $output = array();
39
40
	/**
41
	 * Data type
42
	 *
43
	 * @access public
44
	 * @var string
45
	 */
46
	public $option_type = 'theme_mod';
47
48
	/**
49
	 * The kirki_config we're using for this control
50
	 *
51
	 * @access public
52
	 * @var string
53
	 */
54
	public $kirki_config = 'global';
55
56
	/**
57
	 * The translation strings.
58
	 *
59
	 * @access protected
60
	 * @since 2.3.5
61
	 * @var array
62
	 */
63
	protected $l10n = array();
64
65
	/**
66
	 * Enqueue control related scripts/styles.
67
	 *
68
	 * @access public
69
	 */
70
	public function enqueue() {
71
		wp_enqueue_script( 'kirki-editor', trailingslashit( Kirki::$url ) . 'controls/editor/editor.js', array( 'jquery', 'customize-base' ), false, true );
72
		wp_localize_script( 'kirki-editor', 'editorKirkiL10n', $this->l10n() );
73
		wp_enqueue_style( 'kirki-editor-css', trailingslashit( Kirki::$url ) . 'controls/editor/editor.css', null );
74
	}
75
76
	/**
77
	 * Refresh the parameters passed to the JavaScript via JSON.
78
	 *
79
	 * @see WP_Customize_Control::to_json()
80
	 */
81
	public function to_json() {
82
		parent::to_json();
83
84
		$this->json['default'] = $this->setting->default;
85
		if ( isset( $this->default ) ) {
86
			$this->json['default'] = $this->default;
87
		}
88
		$this->json['output']  = $this->output;
89
		$this->json['value']   = $this->value();
90
		$this->json['choices'] = $this->choices;
91
		$this->json['link']    = $this->get_link();
92
		$this->json['id']      = $this->id;
93
		$this->json['l10n']    = $this->l10n();
94
95
		$this->json['inputAttrs'] = '';
96
		foreach ( $this->input_attrs as $attr => $value ) {
97
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
98
		}
99
100
	}
101
102
	/**
103
	 * An Underscore (JS) template for this control's content (but not its container).
104
	 *
105
	 * Class variables for this control class are available in the `data` JS object;
106
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
107
	 *
108
	 * The actual editor is added from the Kirki_Field_Editor class.
109
	 * All this template contains is a button that triggers the global editor on/off
110
	 * and a hidden textarea element that is used to mirror save the options.
111
	 *
112
	 * @see WP_Customize_Control::print_template()
113
	 *
114
	 * @access protected
115
	 */
116
	protected function content_template() {
117
		?>
118
		<label>
119
			<# if ( data.label ) { #>
120
				<span class="customize-control-title">{{{ data.label }}}</span>
121
			<# } #>
122
			<# if ( data.description ) { #>
123
				<span class="description customize-control-description">{{{ data.description }}}</span>
124
			<# } #>
125
			<div class="customize-control-content">
126
				<a href="#" class="button button-primary toggle-editor"></a>
127
				<textarea {{{ data.inputAttrs }}} class="hidden" {{{ data.link }}}>{{ data.value }}</textarea>
128
			</div>
129
		</label>
130
		<?php
131
	}
132
133
	/**
134
	 * Returns an array of translation strings.
135
	 *
136
	 * @access protected
137
	 * @since 3.0.0
138
	 * @return string
139
	 */
140
	protected function l10n() {
141
		$translation_strings = array(
142
			'open-editor'   => esc_attr__( 'Open Editor', 'kirki' ),
143
			'close-editor'  => esc_attr__( 'Close Editor', 'kirki' ),
144
			'switch-editor' => esc_attr__( 'Switch Editor', 'kirki' ),
145
		);
146
		return apply_filters( "kirki/{$this->kirki_config}/l10n", $translation_strings );
147
	}
148
}
149