PluginProbe
Advanced Custom Fields (ACF®) / 5.9.8
Advanced Custom Fields (ACF®) v5.9.8
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
604 lines 14.0 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 /**
10 * Storage for i18n data.
11 *
12 * @since 5.6.9
13 * @var array
14 */
15 public $text = array();
16
17 /**
18 * Storage for l10n data.
19 *
20 * @since 5.6.9
21 * @var array
22 */
23 public $data = array();
24
25 /**
26 * List of enqueue flags.
27 *
28 * @since 5.9.0
29 * @var bool
30 */
31 private $enqueue = array();
32
33 /**
34 * Constructor.
35 *
36 * @date 10/4/18
37 * @since 5.6.9
38 *
39 * @param void
40 * @return void
41 */
42 public function __construct() {
43 add_action( 'init', array( $this, 'register_scripts' ) );
44 }
45
46 /**
47 * Magic __call method for backwards compatibility.
48 *
49 * @date 10/4/20
50 * @since 5.9.0
51 *
52 * @param string $name The method name.
53 * @param array $arguments The array of arguments.
54 * @return mixed
55 */
56 public function __call( $name, $arguments ) {
57 switch ( $name ) {
58 case 'admin_enqueue_scripts':
59 case 'admin_print_scripts':
60 case 'admin_head':
61 case 'admin_footer':
62 case 'admin_print_footer_scripts':
63 _doing_it_wrong( __FUNCTION__, 'The ACF_Assets class should not be accessed directly.', '5.9.0' );
64 }
65 }
66
67 /**
68 * Appends an array of i18n data.
69 *
70 * @date 13/4/18
71 * @since 5.6.9
72 *
73 * @param array $text An array of text for i18n.
74 * @return void
75 */
76 public function add_text( $text ) {
77 foreach( (array) $text as $k => $v ) {
78 $this->text[ $k ] = $v;
79 }
80 }
81
82 /**
83 * Appends an array of l10n data.
84 *
85 * @date 13/4/18
86 * @since 5.6.9
87 *
88 * @param array $data An array of data for l10n.
89 * @return void
90 */
91 public function add_data( $data ) {
92 foreach( (array) $data as $k => $v ) {
93 $this->data[ $k ] = $v;
94 }
95 }
96
97 /**
98 * Registers the ACF scripts and styles.
99 *
100 * @date 10/4/18
101 * @since 5.6.9
102 *
103 * @param void
104 * @return void
105 */
106 public function register_scripts() {
107 // Extract vars.
108 $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
109 $version = acf_get_setting('version');
110
111 // Register scripts.
112 wp_register_script( 'acf', acf_get_url( 'assets/build/js/acf' . $suffix . '.js' ), array( 'jquery' ), $version );
113 wp_register_script( 'acf-input', acf_get_url( 'assets/build/js/acf-input' . $suffix . '.js' ), array( 'jquery', 'jquery-ui-sortable', 'jquery-ui-resizable', 'acf' ), $version );
114 wp_register_script( 'acf-field-group', acf_get_url( 'assets/build/js/acf-field-group' . $suffix . '.js' ), array( 'acf-input' ), $version );
115
116 // Register styles.
117 wp_register_style( 'acf-global', acf_get_url( 'assets/build/css/acf-global.css' ), array( 'dashicons' ), $version );
118 wp_register_style( 'acf-input', acf_get_url( 'assets/build/css/acf-input.css' ), array( 'acf-global' ), $version );
119 wp_register_style( 'acf-field-group', acf_get_url( 'assets/build/css/acf-field-group.css' ), array( 'acf-input' ), $version );
120
121 /**
122 * Fires after core scripts and styles have been registered.
123 *
124 * @since 5.6.9
125 *
126 * @param string $version The ACF version.
127 * @param string $suffix The potential ".min" filename suffix.
128 */
129 do_action( 'acf/register_scripts', $version, $suffix );
130 }
131
132 /**
133 * Enqueues a script and sets up actions for priting supplemental scripts.
134 *
135 * @date 27/4/20
136 * @since 5.9.0
137 *
138 * @param string $name The script name.
139 * @return void
140 */
141 public function enqueue_script( $name ) {
142 wp_enqueue_script( $name );
143 $this->add_actions();
144 }
145
146 /**
147 * Enqueues a style.
148 *
149 * @date 27/4/20
150 * @since 5.9.0
151 *
152 * @param string $name The style name.
153 * @return void
154 */
155 public function enqueue_style( $name ) {
156 wp_enqueue_style( $name );
157 }
158
159 /**
160 * Adds the actions needed to print supporting inline scripts.
161 *
162 * @date 27/4/20
163 * @since 5.9.0
164 *
165 * @param void
166 * @return void
167 */
168 private function add_actions() {
169
170 // Only run once.
171 if( acf_has_done('ACF_Assets::add_actions') ) {
172 return;
173 }
174
175 // Add actions.
176 $this->add_action( 'admin_enqueue_scripts', 'enqueue_scripts' , 20 );
177 $this->add_action( 'admin_print_scripts', 'print_scripts', 20 );
178 $this->add_action( 'admin_print_footer_scripts', 'print_footer_scripts', 20 );
179 }
180
181 /**
182 * Extends the add_action() function with two additional features:
183 * 1. Renames $action depending on the current page (customizer, login, front-end).
184 * 2. Alters the priotiry or calls the method directly if the action has already passed.
185 *
186 * @date 28/4/20
187 * @since 5.9.0
188 *
189 * @param string $action The action name.
190 * @param string $method The method name.
191 * @param int $priority See add_action().
192 * @param int $accepted_args See add_action().
193 * @return void
194 */
195 public function add_action( $action, $method, $priority = 10, $accepted_args = 1 ) {
196
197 // Generate an array of action replacements.
198 $replacements = array(
199 'customizer' => array(
200 'admin_enqueue_scripts' => 'admin_enqueue_scripts',
201 'admin_print_scripts' => 'customize_controls_print_scripts',
202 'admin_head' => 'customize_controls_print_scripts',
203 'admin_footer' => 'customize_controls_print_footer_scripts',
204 'admin_print_footer_scripts' => 'customize_controls_print_footer_scripts'
205 ),
206 'login' => array(
207 'admin_enqueue_scripts' => 'login_enqueue_scripts',
208 'admin_print_scripts' => 'login_head',
209 'admin_head' => 'login_head',
210 'admin_footer' => 'login_footer',
211 'admin_print_footer_scripts' => 'login_footer'
212 ),
213 'wp' => array(
214 'admin_enqueue_scripts' => 'wp_enqueue_scripts',
215 'admin_print_scripts' => 'wp_print_scripts',
216 'admin_head' => 'wp_head',
217 'admin_footer' => 'wp_footer',
218 'admin_print_footer_scripts' => 'wp_print_footer_scripts'
219 )
220 );
221
222 // Determine the current context.
223 if( did_action('customize_controls_init') ) {
224 $context = 'customizer';
225 } elseif( did_action('login_form_register') ) {
226 $context = 'login';
227 } elseif( is_admin() ) {
228 $context = 'admin';
229 } else {
230 $context = 'wp';
231 }
232
233 // Replace action if possible.
234 if( isset( $replacements[ $context ][ $action ] ) ) {
235 $action = $replacements[ $context ][ $action ];
236 }
237
238 // Check if action is currently being or has already been run.
239 if( did_action($action) ) {
240 $doing = acf_doing_action( $action );
241 if( $doing && $doing < $priority ) {
242 // Allow action to be added as per usual.
243 } else {
244 // Call method directly.
245 return call_user_func( array( $this, $method ) );
246 }
247 }
248
249 // Add action.
250 add_action( $action, array( $this, $method ), $priority, $accepted_args );
251 }
252
253 /**
254 * Generic controller for enqueuing scripts and styles.
255 *
256 * @date 28/4/20
257 * @since 5.9.0
258 *
259 * @param array $args {
260 * @type bool $uploader Whether or not to enqueue uploader scripts.
261 * }
262 * @return void
263 */
264 public function enqueue( $args = array() ) {
265
266 // Apply defaults.
267 $args = wp_parse_args($args, array(
268 'input' => true,
269 'uploader' => false
270 ));
271
272 // Set enqueue flags and add actions.
273 if( $args['input'] ) {
274 $this->enqueue[] = 'input';
275 }
276 if( $args['uploader'] ) {
277 $this->enqueue[] = 'uploader';
278 }
279 $this->add_actions();
280 }
281
282 /**
283 * Enqueues the scripts and styles needed for the WP media uploader.
284 *
285 * @date 27/10/2014
286 * @since 5.0.9
287 *
288 * @param void
289 * @return void
290 */
291 public function enqueue_uploader() {
292
293 // Only run once.
294 if( acf_has_done('ACF_Assets::enqueue_uploader') ) {
295 return;
296 }
297
298 // Enqueue media assets.
299 if( current_user_can('upload_files') ) {
300 wp_enqueue_media();
301 }
302
303 // Add actions.
304 $this->add_action( 'admin_footer', 'print_uploader_scripts', 1 );
305
306 /**
307 * Fires when enqueuing the uploader.
308 *
309 * @since 5.6.9
310 *
311 * @param void
312 */
313 do_action( 'acf/enqueue_uploader' );
314 }
315
316 /**
317 * Enqueues and localizes scripts.
318 *
319 * @date 27/4/20
320 * @since 5.9.0
321 *
322 * @param void
323 * @return void
324 */
325 public function enqueue_scripts() {
326
327 // Enqueue input scripts.
328 if( in_array('input', $this->enqueue) ) {
329 wp_enqueue_script( 'acf-input' );
330 wp_enqueue_style( 'acf-input' );
331 }
332
333 // Enqueue media scripts.
334 if( in_array('uploader', $this->enqueue) ) {
335 $this->enqueue_uploader();
336 }
337
338 // Localize text.
339 acf_localize_text(array(
340
341 // Tooltip
342 'Are you sure?' => __('Are you sure?','acf'),
343 'Yes' => __('Yes','acf'),
344 'No' => __('No','acf'),
345 'Remove' => __('Remove','acf'),
346 'Cancel' => __('Cancel','acf'),
347 ));
348
349 // Localize "input" text.
350 if( wp_script_is('acf-input') ) {
351 acf_localize_text(array(
352
353 // Unload
354 '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'),
355
356 // Validation
357 'Validation successful' => __('Validation successful', 'acf'),
358 'Validation failed' => __('Validation failed', 'acf'),
359 '1 field requires attention' => __('1 field requires attention', 'acf'),
360 '%d fields require attention' => __('%d fields require attention', 'acf'),
361
362 // Other
363 'Edit field group' => __('Edit field group', 'acf'),
364 ));
365
366 /**
367 * Fires during "admin_enqueue_scripts" when ACF scripts are enqueued.
368 *
369 * @since 5.6.9
370 *
371 * @param void
372 */
373 do_action( 'acf/input/admin_enqueue_scripts' );
374 }
375
376 /**
377 * Fires during "admin_enqueue_scripts" when ACF scripts are enqueued.
378 *
379 * @since 5.6.9
380 *
381 * @param void
382 */
383 do_action( 'acf/admin_enqueue_scripts' );
384 do_action( 'acf/enqueue_scripts' );
385
386 // Filter i18n translations that differ from English and localize script.
387 $text = array();
388 foreach( $this->text as $k => $v ) {
389 if( str_replace('.verb', '', $k) !== $v ) {
390 $text[ $k ] = $v;
391 }
392 }
393 if( $text ) {
394 wp_localize_script( 'acf', 'acfL10n', $text );
395 }
396 }
397
398 /**
399 * Prints scripts in head.
400 *
401 * @date 27/4/20
402 * @since 5.9.0
403 *
404 * @param void
405 * @return void
406 */
407 public function print_scripts() {
408 if( wp_script_is('acf-input') ) {
409
410 /**
411 * Fires during "admin_head" when ACF scripts are enqueued.
412 *
413 * @since 5.6.9
414 *
415 * @param void
416 */
417 do_action( 'acf/input/admin_head' );
418 do_action( 'acf/input/admin_print_scripts' );
419 }
420
421 /**
422 * Fires during "admin_head" when ACF scripts are enqueued.
423 *
424 * @since 5.6.9
425 *
426 * @param void
427 */
428 do_action( 'acf/admin_head' );
429 do_action( 'acf/admin_print_scripts' );
430 }
431
432 /**
433 * Prints scripts in footer.
434 *
435 * @date 27/4/20
436 * @since 5.9.0
437 *
438 * @param void
439 * @return void
440 */
441 public function print_footer_scripts() {
442 global $wp_version;
443
444 // Bail early if 'acf' script was never enqueued (fixes Elementor enqueue reset conflict).
445 if( !wp_script_is('acf') ) {
446 return;
447 }
448
449 // Localize data.
450 acf_localize_data(array(
451 'admin_url' => admin_url(),
452 'ajaxurl' => admin_url( 'admin-ajax.php' ),
453 'nonce' => wp_create_nonce( 'acf_nonce' ),
454 'acf_version' => acf_get_setting('version'),
455 'wp_version' => $wp_version,
456 'browser' => acf_get_browser(),
457 'locale' => acf_get_locale(),
458 'rtl' => is_rtl(),
459 'screen' => acf_get_form_data('screen'),
460 'post_id' => acf_get_form_data('post_id'),
461 'validation' => acf_get_form_data('validation'),
462 'editor' => acf_is_block_editor() ? 'block' : 'classic'
463 ));
464
465 // Print inline script.
466 printf( "<script>\n%s\n</script>\n", 'acf.data = ' . wp_json_encode( $this->data ) . ';' );
467
468 if( wp_script_is('acf-input') ) {
469
470 /**
471 * Filters an empty array for compat l10n data.
472 *
473 * @since 5.0.0
474 *
475 * @param array $data An array of data to append to.
476 */
477 $compat_l10n = apply_filters( 'acf/input/admin_l10n', array() );
478 if( $compat_l10n ) {
479 printf( "<script>\n%s\n</script>\n", 'acf.l10n = ' . wp_json_encode( $compat_l10n ) . ';' );
480 }
481
482 /**
483 * Fires during "admin_footer" when ACF scripts are enqueued.
484 *
485 * @since 5.6.9
486 *
487 * @param void
488 */
489 do_action( 'acf/input/admin_footer' );
490 do_action( 'acf/input/admin_print_footer_scripts' );
491 }
492
493 /**
494 * Fires during "admin_footer" when ACF scripts are enqueued.
495 *
496 * @since 5.6.9
497 *
498 * @param void
499 */
500 do_action( 'acf/admin_footer' );
501 do_action( 'acf/admin_print_footer_scripts' );
502
503 // Once all data is localized, trigger acf.prepare() to execute functionality before DOM ready.
504 printf( "<script>\n%s\n</script>\n", "acf.doAction( 'prepare' );" );
505 }
506
507 /**
508 * Prints uploader scripts in footer.
509 *
510 * @date 11/06/2020
511 * @since 5.9.0
512 *
513 * @param void
514 * @return void
515 */
516 public function print_uploader_scripts() {
517 // Todo: investigate output-buffer to hide HTML.
518 ?>
519 <div id="acf-hidden-wp-editor" style="display: none;">
520 <?php wp_editor( '', 'acf_content' ); ?>
521 </div>
522 <?php
523
524 /**
525 * Fires when printing uploader scripts.
526 *
527 * @since 5.6.9
528 *
529 * @param void
530 */
531 do_action( 'acf/admin_print_uploader_scripts' );
532 }
533 }
534
535 // instantiate
536 acf_new_instance('ACF_Assets');
537
538 endif; // class_exists check
539
540 /**
541 * Appends an array of i18n data for localization.
542 *
543 * @date 13/4/18
544 * @since 5.6.9
545 *
546 * @param array $text An array of text for i18n.
547 * @return void
548 */
549 function acf_localize_text( $text ) {
550 return acf_get_instance('ACF_Assets')->add_text( $text );
551 }
552
553 /**
554 * Appends an array of l10n data for localization.
555 *
556 * @date 13/4/18
557 * @since 5.6.9
558 *
559 * @param array $data An array of data for l10n.
560 * @return void
561 */
562 function acf_localize_data( $data ) {
563 return acf_get_instance('ACF_Assets')->add_data( $data );
564 }
565
566 /**
567 * Enqueues a script with support for supplemental inline scripts.
568 *
569 * @date 27/4/20
570 * @since 5.9.0
571 *
572 * @param string $name The script name.
573 * @return void
574 */
575 function acf_enqueue_script( $name ) {
576 return acf_get_instance('ACF_Assets')->enqueue_script( $name );
577 }
578
579 /**
580 * Enqueues the input scripts required for fields.
581 *
582 * @date 13/4/18
583 * @since 5.6.9
584 *
585 * @param array $args See ACF_Assets::enqueue_scripts() for a list of args.
586 * @return void
587 */
588 function acf_enqueue_scripts( $args = array() ) {
589 return acf_get_instance('ACF_Assets')->enqueue( $args );
590 }
591
592 /**
593 * Enqueues the WP media uploader scripts and styles.
594 *
595 * @date 27/10/2014
596 * @since 5.0.9
597 *
598 * @param void
599 * @return void
600 */
601 function acf_enqueue_uploader() {
602 return acf_get_instance('ACF_Assets')->enqueue_uploader();
603 }
604