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