Completed
Push — develop ( d8be36...adf00f )
by Aristeides
02:34
created

Kirki_Control_Editor::to_json()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 8
nop 0
dl 0
loc 25
rs 8.5806
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) 2016, 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', 'kirki-l10n' ), false, true );
72
		wp_localize_script( 'kirki-editor', 'kirkiL10n', $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
		$this->json['kirkiConfig'] = $this->kirki_config;
95
96
		if ( 'user_meta' === $this->option_type ) {
97
			$this->json['value'] = get_user_meta( get_current_user_id(), $this->id, true );
98
		}
99
100
		$this->json['inputAttrs'] = '';
101
		foreach ( $this->input_attrs as $attr => $value ) {
102
			$this->json['inputAttrs'] .= $attr . '="' . esc_attr( $value ) . '" ';
103
		}
104
105
	}
106
107
	/**
108
	 * An Underscore (JS) template for this control's content (but not its container).
109
	 *
110
	 * Class variables for this control class are available in the `data` JS object;
111
	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
112
	 *
113
	 * The actual editor is added from the Kirki_Field_Editor class.
114
	 * All this template contains is a button that triggers the global editor on/off
115
	 * and a hidden textarea element that is used to mirror save the options.
116
	 *
117
	 * @see WP_Customize_Control::print_template()
118
	 *
119
	 * @access protected
120
	 */
121
	protected function content_template() {
122
		?>
123
		<label>
124
			<# if ( data.label ) { #>
125
				<span class="customize-control-title">{{{ data.label }}}</span>
126
			<# } #>
127
			<# if ( data.description ) { #>
128
				<span class="description customize-control-description">{{{ data.description }}}</span>
129
			<# } #>
130
			<div class="customize-control-content">
131
				<a href="#" class="button button-primary toggle-editor"></a>
132
				<textarea {{{ data.inputAttrs }}} class="hidden" {{{ data.link }}}>{{ data.value }}</textarea>
133
			</div>
134
		</label>
135
		<?php
136
	}
137
138
	/**
139
	 * Returns an array of translation strings.
140
	 *
141
	 * @access protected
142
	 * @since 2.4.0
143
	 * @param string|false $id The string-ID.
144
	 * @return string
145
	 */
146
	protected function l10n( $id = false ) {
147
		$translation_strings = array(
148
			'open-editor'   => esc_attr__( 'Open Editor', 'kirki' ),
149
			'close-editor'  => esc_attr__( 'Close Editor', 'kirki' ),
150
			'switch-editor' => esc_attr__( 'Switch Editor', 'kirki' ),
151
		);
152
		return apply_filters( 'kirki/' . $this->kirki_config . '/l10n', $translation_strings );
153
	}
154
}
155