PluginProbe
Advanced Custom Fields (ACF®) / 5.8.7
Advanced Custom Fields (ACF®) v5.8.7
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / assets.php
assets.php
482 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
5 if( ! class_exists('ACF_Assets') ) :
6
7 class ACF_Assets {
8
9 /** @var array Storage for translations */
10 var $text = array();
11
12 /** @var array Storage for data */
13 var $data = array();
14
15
16 /**
17 * __construct
18 *
19 * description
20 *
21 * @date 10/4/18
22 * @since 5.6.9
23 *
24 * @param void
25 * @return void
26 */
27
28 function __construct() {
29
30 // actions
31 add_action('init', array($this, 'register_scripts'));
32 }
33
34
35 /**
36 * add_text
37 *
38 * description
39 *
40 * @date 13/4/18
41 * @since 5.6.9
42 *
43 * @param type $var Description. Default.
44 * @return type Description.
45 */
46
47 function add_text( $text ) {
48 foreach( (array) $text as $k => $v ) {
49 $this->text[ $k ] = $v;
50 }
51 }
52
53
54 /**
55 * add_data
56 *
57 * description
58 *
59 * @date 13/4/18
60 * @since 5.6.9
61 *
62 * @param type $var Description. Default.
63 * @return type Description.
64 */
65
66 function add_data( $data ) {
67 foreach( (array) $data as $k => $v ) {
68 $this->data[ $k ] = $v;
69 }
70 }
71
72
73 /**
74 * register_scripts
75 *
76 * description
77 *
78 * @date 13/4/18
79 * @since 5.6.9
80 *
81 * @param type $var Description. Default.
82 * @return type Description.
83 */
84
85 function register_scripts() {
86
87 // vars
88 $version = acf_get_setting('version');
89 $min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
90
91 // scripts
92 wp_register_script('acf-input', acf_get_url("assets/js/acf-input{$min}.js"), array('jquery', 'jquery-ui-sortable', 'jquery-ui-resizable'), $version );
93 wp_register_script('acf-field-group', acf_get_url("assets/js/acf-field-group{$min}.js"), array('acf-input'), $version );
94
95 // styles
96 wp_register_style('acf-global', acf_get_url('assets/css/acf-global.css'), array(), $version );
97 wp_register_style('acf-input', acf_get_url('assets/css/acf-input.css'), array('acf-global'), $version );
98 wp_register_style('acf-field-group', acf_get_url('assets/css/acf-field-group.css'), array('acf-input'), $version );
99
100 // action
101 do_action('acf/register_scripts', $version, $min);
102 }
103
104
105 /**
106 * enqueue_scripts
107 *
108 * Enqueue scripts for input
109 *
110 * @date 13/4/18
111 * @since 5.6.9
112 *
113 * @param type $var Description. Default.
114 * @return type Description.
115 */
116
117 function enqueue_scripts( $args = array() ) {
118
119 // run only once
120 if( acf_has_done('enqueue_scripts') ) {
121 return;
122 }
123
124 // defaults
125 $args = wp_parse_args($args, array(
126
127 // force tinymce editor to be enqueued
128 'uploader' => false,
129
130 // priority used for action callbacks, defaults to 20 which runs after defaults
131 'priority' => 20,
132
133 // action prefix
134 'context' => is_admin() ? 'admin' : 'wp'
135 ));
136
137 // define actions
138 $actions = array(
139 'admin_enqueue_scripts' => $args['context'] . '_enqueue_scripts',
140 'admin_print_scripts' => $args['context'] . '_print_scripts',
141 'admin_head' => $args['context'] . '_head',
142 'admin_footer' => $args['context'] . '_footer',
143 'admin_print_footer_scripts' => $args['context'] . '_print_footer_scripts',
144 );
145
146 // fix customizer actions where head and footer are not available
147 if( $args['context'] == 'customize_controls' ) {
148 $actions['admin_head'] = $actions['admin_print_scripts'];
149 $actions['admin_footer'] = $actions['admin_print_footer_scripts'];
150 }
151
152 // add actions
153 foreach( $actions as $function => $action ) {
154 acf_maybe_add_action( $action, array($this, $function), $args['priority'] );
155 }
156
157 // enqueue uploader
158 // WP requires a lot of JS + inline scripes to create the media modal and should be avoioded when possible.
159 // - priority must be less than 10 to allow WP to enqueue
160 if( $args['uploader'] ) {
161 add_action($actions['admin_footer'], 'acf_enqueue_uploader', 5);
162 }
163 }
164
165
166 /**
167 * admin_enqueue_scripts
168 *
169 * description
170 *
171 * @date 16/4/18
172 * @since 5.6.9
173 *
174 * @param type $var Description. Default.
175 * @return type Description.
176 */
177
178 function admin_enqueue_scripts() {
179
180 // Localize text.
181 acf_localize_text(array(
182
183 // unload
184 'The changes you made will be lost if you navigate away from this page' => __('The changes you made will be lost if you navigate away from this page', 'acf'),
185
186 // media
187 'Select.verb' => _x('Select', 'verb', 'acf'),
188 'Edit.verb' => _x('Edit', 'verb', 'acf'),
189 'Update.verb' => _x('Update', 'verb', 'acf'),
190 'Uploaded to this post' => __('Uploaded to this post', 'acf'),
191 'Expand Details' => __('Expand Details', 'acf'),
192 'Collapse Details' => __('Collapse Details', 'acf'),
193 'Restricted' => __('Restricted', 'acf'),
194 'All images' => __('All images', 'acf'),
195
196 // validation
197 'Validation successful' => __('Validation successful', 'acf'),
198 'Validation failed' => __('Validation failed', 'acf'),
199 '1 field requires attention' => __('1 field requires attention', 'acf'),
200 '%d fields require attention' => __('%d fields require attention', 'acf'),
201
202 // tooltip
203 'Are you sure?' => __('Are you sure?','acf'),
204 'Yes' => __('Yes','acf'),
205 'No' => __('No','acf'),
206 'Remove' => __('Remove','acf'),
207 'Cancel' => __('Cancel','acf'),
208
209 // conditions
210 'Has any value' => __('Has any value', 'acf'),
211 'Has no value' => __('Has no value', 'acf'),
212 'Value is equal to' => __('Value is equal to', 'acf'),
213 'Value is not equal to' => __('Value is not equal to', 'acf'),
214 'Value matches pattern' => __('Value matches pattern', 'acf'),
215 'Value contains' => __('Value contains', 'acf'),
216 'Value is greater than' => __('Value is greater than', 'acf'),
217 'Value is less than' => __('Value is less than', 'acf'),
218 'Selection is greater than' => __('Selection is greater than', 'acf'),
219 'Selection is less than' => __('Selection is less than', 'acf'),
220
221 // misc
222 'Edit field group' => __('Edit field group', 'acf'),
223 ));
224
225 // enqueue
226 wp_enqueue_script('acf-input');
227 wp_enqueue_style('acf-input');
228
229 // vars
230 $text = array();
231
232 // actions
233 do_action('acf/enqueue_scripts');
234 do_action('acf/admin_enqueue_scripts');
235 do_action('acf/input/admin_enqueue_scripts');
236
237 // only include translated strings
238 foreach( $this->text as $k => $v ) {
239 if( str_replace('.verb', '', $k) !== $v ) {
240 $text[ $k ] = $v;
241 }
242 }
243
244 // localize text
245 if( $text ) {
246 wp_localize_script( 'acf-input', 'acfL10n', $text );
247 }
248 }
249
250
251 /**
252 * admin_print_scripts
253 *
254 * description
255 *
256 * @date 18/4/18
257 * @since 5.6.9
258 *
259 * @param type $var Description. Default.
260 * @return type Description.
261 */
262
263 function admin_print_scripts() {
264 do_action('acf/admin_print_scripts');
265 }
266
267
268 /**
269 * admin_head
270 *
271 * description
272 *
273 * @date 16/4/18
274 * @since 5.6.9
275 *
276 * @param type $var Description. Default.
277 * @return type Description.
278 */
279
280 function admin_head() {
281
282 // actions
283 do_action('acf/admin_head');
284 do_action('acf/input/admin_head');
285 }
286
287
288 /**
289 * admin_footer
290 *
291 * description
292 *
293 * @date 16/4/18
294 * @since 5.6.9
295 *
296 * @param type $var Description. Default.
297 * @return type Description.
298 */
299
300 function admin_footer() {
301
302 // global
303 global $wp_version;
304
305 // get data
306 $data = wp_parse_args($this->data, array(
307 'screen' => acf_get_form_data('screen'),
308 'post_id' => acf_get_form_data('post_id'),
309 'nonce' => wp_create_nonce( 'acf_nonce' ),
310 'admin_url' => admin_url(),
311 'ajaxurl' => admin_url( 'admin-ajax.php' ),
312 'validation' => acf_get_form_data('validation'),
313 'wp_version' => $wp_version,
314 'acf_version' => acf_get_setting('version'),
315 'browser' => acf_get_browser(),
316 'locale' => acf_get_locale(),
317 'rtl' => is_rtl(),
318 'editor' => acf_is_block_editor() ? 'block' : 'classic'
319 ));
320
321 // get l10n (old)
322 $l10n = apply_filters( 'acf/input/admin_l10n', array() );
323
324 // todo: force 'acf-input' script enqueue if not yet included
325 // - fixes potential timing issue if acf_enqueue_assest() was called during body
326
327 // localize data
328 ?>
329 <script type="text/javascript">
330 acf.data = <?php echo wp_json_encode($data); ?>;
331 acf.l10n = <?php echo wp_json_encode($l10n); ?>;
332 </script>
333 <?php
334
335 // actions
336 do_action('acf/admin_footer');
337 do_action('acf/input/admin_footer');
338
339 // trigger prepare
340 ?>
341 <script type="text/javascript">
342 acf.doAction('prepare');
343 </script>
344 <?php
345
346 }
347
348
349 /**
350 * admin_print_footer_scripts
351 *
352 * description
353 *
354 * @date 18/4/18
355 * @since 5.6.9
356 *
357 * @param type $var Description. Default.
358 * @return type Description.
359 */
360
361 function admin_print_footer_scripts() {
362 do_action('acf/admin_print_footer_scripts');
363 }
364
365 /*
366 * enqueue_uploader
367 *
368 * This function will render a WP WYSIWYG and enqueue media
369 *
370 * @type function
371 * @date 27/10/2014
372 * @since 5.0.9
373 *
374 * @param n/a
375 * @return n/a
376 */
377
378 function enqueue_uploader() {
379
380 // run only once
381 if( acf_has_done('enqueue_uploader') ) {
382 return;
383 }
384
385 // bail early if doing ajax
386 if( acf_is_ajax() ) {
387 return;
388 }
389
390 // enqueue media if user can upload
391 if( current_user_can('upload_files') ) {
392 wp_enqueue_media();
393 }
394
395 // create dummy editor
396 ?>
397 <div id="acf-hidden-wp-editor" class="acf-hidden">
398 <?php wp_editor( '', 'acf_content' ); ?>
399 </div>
400 <?php
401
402 // action
403 do_action('acf/enqueue_uploader');
404 }
405 }
406
407 // instantiate
408 acf_new_instance('ACF_Assets');
409
410 endif; // class_exists check
411
412
413 /**
414 * acf_localize_text
415 *
416 * description
417 *
418 * @date 13/4/18
419 * @since 5.6.9
420 *
421 * @param type $var Description. Default.
422 * @return type Description.
423 */
424
425 function acf_localize_text( $text ) {
426 return acf_get_instance('ACF_Assets')->add_text( $text );
427 }
428
429
430 /**
431 * acf_localize_data
432 *
433 * description
434 *
435 * @date 13/4/18
436 * @since 5.6.9
437 *
438 * @param type $var Description. Default.
439 * @return type Description.
440 */
441
442 function acf_localize_data( $data ) {
443 return acf_get_instance('ACF_Assets')->add_data( $data );
444 }
445
446
447 /*
448 * acf_enqueue_scripts
449 *
450 *
451 *
452 * @type function
453 * @date 6/10/13
454 * @since 5.0.0
455 *
456 * @param n/a
457 * @return n/a
458 */
459
460 function acf_enqueue_scripts( $args = array() ) {
461 return acf_get_instance('ACF_Assets')->enqueue_scripts( $args );
462 }
463
464
465 /*
466 * acf_enqueue_uploader
467 *
468 * This function will render a WP WYSIWYG and enqueue media
469 *
470 * @type function
471 * @date 27/10/2014
472 * @since 5.0.9
473 *
474 * @param n/a
475 * @return n/a
476 */
477
478 function acf_enqueue_uploader() {
479 return acf_get_instance('ACF_Assets')->enqueue_uploader();
480 }
481
482 ?>