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