PluginProbe
Advanced Custom Fields (ACF®) / 6.1.6
Advanced Custom Fields (ACF®) v6.1.6
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 in Advanced Custom Fields (ACF®) 6.1.6, at includes/assets.php

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