|
1 <?php |
|
2 /** |
|
3 * Customize API: WP_Customize_Code_Editor_Control class |
|
4 * |
|
5 * @package WordPress |
|
6 * @subpackage Customize |
|
7 * @since 4.9.0 |
|
8 */ |
|
9 |
|
10 /** |
|
11 * Customize Code Editor Control class. |
|
12 * |
|
13 * @since 4.9.0 |
|
14 * |
|
15 * @see WP_Customize_Control |
|
16 */ |
|
17 class WP_Customize_Code_Editor_Control extends WP_Customize_Control { |
|
18 |
|
19 /** |
|
20 * Customize control type. |
|
21 * |
|
22 * @since 4.9.0 |
|
23 * @var string |
|
24 */ |
|
25 public $type = 'code_editor'; |
|
26 |
|
27 /** |
|
28 * Type of code that is being edited. |
|
29 * |
|
30 * @since 4.9.0 |
|
31 * @var string |
|
32 */ |
|
33 public $code_type = ''; |
|
34 |
|
35 /** |
|
36 * Code editor settings. |
|
37 * |
|
38 * @see wp_enqueue_code_editor() |
|
39 * @since 4.9.0 |
|
40 * @var array|false |
|
41 */ |
|
42 public $editor_settings = array(); |
|
43 |
|
44 /** |
|
45 * Enqueue control related scripts/styles. |
|
46 * |
|
47 * @since 4.9.0 |
|
48 */ |
|
49 public function enqueue() { |
|
50 $this->editor_settings = wp_enqueue_code_editor( array_merge( |
|
51 array( |
|
52 'type' => $this->code_type, |
|
53 'codemirror' => array( |
|
54 'indentUnit' => 2, |
|
55 'tabSize' => 2, |
|
56 ), |
|
57 ), |
|
58 $this->editor_settings |
|
59 ) ); |
|
60 } |
|
61 |
|
62 /** |
|
63 * Refresh the parameters passed to the JavaScript via JSON. |
|
64 * |
|
65 * @since 4.9.0 |
|
66 * @see WP_Customize_Control::json() |
|
67 * |
|
68 * @return array Array of parameters passed to the JavaScript. |
|
69 */ |
|
70 public function json() { |
|
71 $json = parent::json(); |
|
72 $json['editor_settings'] = $this->editor_settings; |
|
73 $json['input_attrs'] = $this->input_attrs; |
|
74 return $json; |
|
75 } |
|
76 |
|
77 /** |
|
78 * Don't render the control content from PHP, as it's rendered via JS on load. |
|
79 * |
|
80 * @since 4.9.0 |
|
81 */ |
|
82 public function render_content() {} |
|
83 |
|
84 /** |
|
85 * Render a JS template for control display. |
|
86 * |
|
87 * @since 4.9.0 |
|
88 */ |
|
89 public function content_template() { |
|
90 ?> |
|
91 <# var elementIdPrefix = 'el' + String( Math.random() ); #> |
|
92 <# if ( data.label ) { #> |
|
93 <label for="{{ elementIdPrefix }}_editor" class="customize-control-title"> |
|
94 {{ data.label }} |
|
95 </label> |
|
96 <# } #> |
|
97 <# if ( data.description ) { #> |
|
98 <span class="description customize-control-description">{{{ data.description }}}</span> |
|
99 <# } #> |
|
100 <div class="customize-control-notifications-container"></div> |
|
101 <textarea id="{{ elementIdPrefix }}_editor" |
|
102 <# _.each( _.extend( { 'class': 'code' }, data.input_attrs ), function( value, key ) { #> |
|
103 {{{ key }}}="{{ value }}" |
|
104 <# }); #> |
|
105 ></textarea> |
|
106 <?php |
|
107 } |
|
108 } |