forked from taylormsj/acf-code_area-field
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacf_code_area-v5.php
More file actions
224 lines (197 loc) · 7.16 KB
/
acf_code_area-v5.php
File metadata and controls
224 lines (197 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
class acf_field_code_area extends acf_field {
public $settings = array();
/*
* __construct
*
* This function will setup the field type data
*
* @type function
* @date 5/03/2014
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
function __construct() {
$this->name = 'code_area';
$this->label = __( 'Code Area' );
$this->category = __( "Content", 'acf' );
$this->settings = array(
'path' => apply_filters( 'acf/helpers/get_path', __FILE__ ),
'dir' => apply_filters( 'acf/helpers/get_dir', __FILE__ ),
'version' => '1.0.0'
);
$this->defaults = array();
parent::__construct();
}
/*
* render_field_settings()
*
* Create extra settings for your field. These are visible when editing a field
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param $field (array) the $field being edited
* @return n/a
*/
function render_field_settings( $field ) {
acf_render_field_setting( $field, array(
'label' => __( 'Language', 'acf' ),
'instructions' => '',
'type' => 'radio',
'name' => 'language',
'choices' => array(
'Plain' => __( 'Plain', 'acf' ),
'css' => __( 'CSS', 'acf' ),
'javascript' => __( 'Javascript', 'acf' ),
'htmlmixed' => __( 'HTML', 'acf' ),
'php' => __( 'PHP', 'acf' ),
)
) );
acf_render_field_setting( $field, array(
'label' => __( 'Theme', 'acf' ),
'instructions' => __( 'Set a theme for the editor (<a href="http://codemirror.net/demo/theme.html" target="_blank">Preview Here</a>) ', 'acf' ),
'type' => 'select',
'name' => 'theme',
'choices' => array(
'default' => __( "Default", 'acf' ),
'ambiance' => __( "Ambiance", 'acf' ),
'blackboard' => __( "Blackboard", 'acf' ),
'cobalt' => __( "Cobalt", 'acf' ),
'eclipse' => __( "Eclipse", 'acf' ),
'elegant' => __( "Elegant", 'acf' ),
'erlang-dark' => __( "Erlang Dark", 'acf' ),
'lesser-dark' => __( "Lesser Dark", 'acf' ),
'material' => __( 'Material', 'acf' ),
'midnight' => __( "Midnight", 'acf' ),
'monokai' => __( "Monokai", 'acf' ),
'neat' => __( "Neat", 'acf' ),
'night' => __( "Night", 'acf' ),
'rubyblue' => __( "Rubyblue", 'acf' ),
'solarized' => __( "Solarized", 'acf' ),
'twilight' => __( "Twilight", 'acf' ),
'vibrant-ink' => __( "Vibrant Ink", 'acf' ),
'xq-dark' => __( "XQ Dark", 'acf' ),
'xq-light' => __( "XQ Light", 'acf' ),
)
) );
acf_render_field_setting( $field, array(
'label' => __( 'Show language info', 'acf' ),
'instructions' => '',
'type' => 'radio',
'name' => 'show_info',
'choices' => array(
'yes' => __( 'Yes', 'acf' ),
'no' => __( 'No', 'acf' ),
)
) );
}
/*
* render_field()
*
* Create the HTML interface for your field
*
* @param $field (array) the $field being rendered
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param $field (array) the $field being edited
* @return n/a
*/
function render_field( $field ) {
$field['value'] = esc_textarea( $field['value'] );
$language = '';
switch ( $field['language'] ) {
case 'plain':
$language = 'Plain';
break;
case 'css':
$language = 'CSS';
break;
case 'javascript':
$language = 'Javascript';
break;
case 'htmlmixed':
$language = 'HTML';
break;
case 'php':
$language = 'PHP';
break;
default:
$language = 'Plain';
}
echo '<textarea id="' . $field['id'] . '" rows="4" class="' . $field['class'] . '" name="' . $field['name'] .
'" data-language="' . $field['language'] . '" data-theme="' . $field['theme'] . '">' . $field['value'] . '</textarea>';
if ( $field['show_info'] === 'yes' ) {
echo '<p style="margin-bottom:0;"><small>You are writing ' . $language . ' code.</small></p>';
}
if ( $field['theme'] !== 'default' ): ?>
<link rel="stylesheet"
href="<?php echo plugin_dir_url( __FILE__ ) ?>/css/theme/<?php echo $field["theme"] ?>.css">
<?php endif;
}
/**
* input_admin_enqueue_scripts
*
* Enqueue admin scripts
*/
function input_admin_enqueue_scripts() {
$dir = plugin_dir_url( __FILE__ );
if ( SCRIPT_DEBUG ) {
$this->enqueue_full_scripts( $dir );
} else {
$this->enqueue_prod_scripts( $dir );
}
}
/**
* Enqueue not minified scripts
*
* @param $dir
*/
private function enqueue_full_scripts( $dir ) {
wp_register_script( 'acf-input-code_area-code_mirror_js', $dir . 'js/codemirror.js', array( 'acf-input' ), acf_field_code_area_plugin::$version );
wp_register_script( 'acf-input-code_area-code_mirror_mode_js', $dir . 'js/mode/javascript.js', array( 'acf-input' ), acf_field_code_area_plugin::$version );
wp_register_script( 'acf-input-code_area-code_mirror_mode_css', $dir . 'js/mode/css.js', array( 'acf-input' ), acf_field_code_area_plugin::$version );
wp_register_style( 'acf-input-code_area-code_mirror_css', $dir . 'css/codemirror.css', array( 'acf-input' ), acf_field_code_area_plugin::$version );
wp_register_script( 'acf-input-code_area-code_mirror_mode_html', $dir . 'js/mode/htmlmixed.js', array( 'acf-input' ), acf_field_code_area_plugin::$version );
wp_register_script( 'acf-input-code_area-code_mirror_mode_xml', $dir . 'js/mode/xml.js', array( 'acf-input' ), acf_field_code_area_plugin::$version );
wp_register_script( 'acf-input-code_area-code_mirror_mode_php', $dir . 'js/mode/php.js', array( 'acf-input' ), acf_field_code_area_plugin::$version );
wp_register_script( 'acf-input-code_area-code_mirror_mode_clike', $dir . 'js/mode/clike.js', array( 'acf-input' ), acf_field_code_area_plugin::$version );
wp_register_script( 'acf-input-code_area-code_xml-fold', $dir . 'js/addon/xml-fold.js', array( 'acf-input' ), acf_field_code_area_plugin::$version );
wp_register_script( 'acf-input-code_area-code_mirror_addon_matchtags', $dir . 'js/addon/matchtags.js', array( 'acf-input' ), acf_field_code_area_plugin::$version );
wp_register_script( 'acf-input-code_area-field', $dir . 'js/acf-code_area.js', array( 'acf-input' ), acf_field_code_area_plugin::$version );
// scripts
wp_enqueue_script( array(
'acf-input-code_area-code_mirror_js',
'acf-input-code_area-code_mirror_mode_js',
'acf-input-code_area-code_mirror_mode_css',
'acf-input-code_area-code_mirror_mode_html',
'acf-input-code_area-code_mirror_mode_xml',
'acf-input-code_area-code_mirror_mode_php',
'acf-input-code_area-code_mirror_mode_clike',
'acf-input-code_area-code_xml-fold',
'acf-input-code_area-code_mirror_addon_matchtags',
'acf-input-code_area-field'
) );
// styles
wp_enqueue_style( array(
'acf-input-code_area-code_mirror_css',
) );
}
/**
* Enqueue minified scripts
*
* @param $dir
*/
private function enqueue_prod_scripts( $dir ) {
wp_register_script( 'acf-input-code_area-field', $dir . 'js/acf-code_area.min.js', array(), acf_field_code_area_plugin::$version );
wp_register_style( 'acf-input-code_area-code_mirror_css', $dir . 'css/codemirror.min.css', array(), acf_field_code_area_plugin::$version );
wp_enqueue_script( 'acf-input-code_area-field' );
wp_enqueue_style( 'acf-input-code_area-code_mirror_css' );
}
}