PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / assets.php
secure-custom-fields / includes Last commit date
Blocks 1 year ago admin 1 year ago ajax 1 year ago api 1 year ago fields 1 year ago forms 1 year ago legacy 1 year ago locations 1 year ago post-types 1 year ago rest-api 1 year ago walkers 1 year ago acf-bidirectional-functions.php 1 year ago acf-field-functions.php 1 year ago acf-field-group-functions.php 1 year ago acf-form-functions.php 1 year ago acf-helper-functions.php 1 year ago acf-hook-functions.php 1 year ago acf-input-functions.php 1 year ago acf-internal-post-type-functions.php 1 year ago acf-meta-functions.php 1 year ago acf-post-functions.php 1 year ago acf-post-type-functions.php 1 year ago acf-taxonomy-functions.php 1 year ago acf-user-functions.php 1 year ago acf-utility-functions.php 1 year ago acf-value-functions.php 1 year ago acf-wp-functions.php 1 year ago assets.php 1 year ago class-acf-data.php 1 year ago class-acf-internal-post-type.php 1 year ago class-acf-site-health.php 1 year ago compatibility.php 1 year ago deprecated.php 1 year ago fields.php 1 year ago index.php 1 year ago l10n.php 1 year ago local-fields.php 1 year ago local-json.php 1 year ago local-meta.php 1 year ago locations.php 1 year ago loop.php 1 year ago media.php 1 year ago rest-api.php 1 year ago revisions.php 1 year ago third-party.php 1 year ago upgrades.php 1 year ago validation.php 1 year ago wpml.php 1 year ago
assets.php
619 lines
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( 'SCF_DEVELOPMENT_MODE' ) && SCF_DEVELOPMENT_MODE ? '' : '.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', 'wp-a11y' ), $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' . $suffix . '.css' ), array( 'dashicons' ), $version );
122 wp_register_style( 'acf-input', acf_get_url( 'assets/build/css/acf-input' . $suffix . '.css' ), array( 'acf-global' ), $version );
123 wp_register_style( 'acf-field-group', acf_get_url( 'assets/build/css/acf-field-group' . $suffix . '.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 // Block Validation
373 'An ACF Block on this page requires attention before you can save.' => __( 'An ACF Block on this page requires attention before you can save.', 'acf' ),
374
375 // Other
376 'Edit field group' => __( 'Edit field group', 'acf' ),
377 )
378 );
379
380 /**
381 * Fires during "admin_enqueue_scripts" when ACF scripts are enqueued.
382 *
383 * @since 5.6.9
384 *
385 * @param void
386 */
387 do_action( 'acf/input/admin_enqueue_scripts' );
388 }
389
390 /**
391 * Fires during "admin_enqueue_scripts" when ACF scripts are enqueued.
392 *
393 * @since 5.6.9
394 *
395 * @param void
396 */
397 do_action( 'acf/admin_enqueue_scripts' );
398 do_action( 'acf/enqueue_scripts' );
399
400 // Filter i18n translations that differ from English and localize script.
401 $text = array();
402 foreach ( $this->text as $k => $v ) {
403 if ( str_replace( '.verb', '', $k ) !== $v ) {
404 $text[ $k ] = $v;
405 }
406 }
407 if ( $text ) {
408 wp_localize_script( 'acf', 'acfL10n', $text );
409 }
410 }
411
412 /**
413 * Prints scripts in head.
414 *
415 * @date 27/4/20
416 * @since 5.9.0
417 *
418 * @param void
419 * @return void
420 */
421 public function print_scripts() {
422 if ( wp_script_is( 'acf-input' ) ) {
423
424 /**
425 * Fires during "admin_head" when ACF scripts are enqueued.
426 *
427 * @since 5.6.9
428 *
429 * @param void
430 */
431 do_action( 'acf/input/admin_head' );
432 do_action( 'acf/input/admin_print_scripts' );
433 }
434
435 /**
436 * Fires during "admin_head" when ACF scripts are enqueued.
437 *
438 * @since 5.6.9
439 *
440 * @param void
441 */
442 do_action( 'acf/admin_head' );
443 do_action( 'acf/admin_print_scripts' );
444 }
445
446 /**
447 * Prints scripts in footer.
448 *
449 * @date 27/4/20
450 * @since 5.9.0
451 *
452 * @param void
453 * @return void
454 */
455 public function print_footer_scripts() {
456 global $wp_version;
457
458 // Bail early if 'acf' script was never enqueued (fixes Elementor enqueue reset conflict).
459 if ( ! wp_script_is( 'acf' ) ) {
460 return;
461 }
462
463 // Localize data.
464 $data_to_localize = array(
465 'admin_url' => admin_url(),
466 'ajaxurl' => admin_url( 'admin-ajax.php' ),
467 'nonce' => wp_create_nonce( 'acf_nonce' ),
468 'acf_version' => acf_get_setting( 'version' ),
469 'wp_version' => $wp_version,
470 'browser' => acf_get_browser(),
471 'locale' => acf_get_locale(),
472 'rtl' => is_rtl(),
473 'screen' => acf_get_form_data( 'screen' ),
474 'post_id' => acf_get_form_data( 'post_id' ),
475 'validation' => acf_get_form_data( 'validation' ),
476 'editor' => acf_is_block_editor() ? 'block' : 'classic',
477 'is_pro' => true,
478 'debug' => acf_is_beta() || ( defined( 'SCF_DEVELOPMENT_MODE' ) && SCF_DEVELOPMENT_MODE ),
479 );
480
481 acf_localize_data( $data_to_localize );
482
483 // Print inline script.
484 printf( "<script>\n%s\n</script>\n", 'acf.data = ' . wp_json_encode( $this->data ) . ';' );
485
486 if ( wp_script_is( 'acf-input' ) ) {
487
488 /**
489 * Filters an empty array for compat l10n data.
490 *
491 * @since 5.0.0
492 *
493 * @param array $data An array of data to append to.
494 */
495 $compat_l10n = apply_filters( 'acf/input/admin_l10n', array() );
496 if ( $compat_l10n ) {
497 printf( "<script>\n%s\n</script>\n", 'acf.l10n = ' . wp_json_encode( $compat_l10n ) . ';' );
498 }
499
500 /**
501 * Fires during "admin_footer" when ACF scripts are enqueued.
502 *
503 * @since 5.6.9
504 */
505 do_action( 'acf/input/admin_footer' );
506 do_action( 'acf/input/admin_print_footer_scripts' );
507 }
508
509 /**
510 * Fires during "admin_footer" when ACF scripts are enqueued.
511 *
512 * @since 5.6.9
513 *
514 * @param void
515 */
516 do_action( 'acf/admin_footer' );
517 do_action( 'acf/admin_print_footer_scripts' );
518
519 // Once all data is localized, trigger acf.prepare() to execute functionality before DOM ready.
520 printf( "<script>\n%s\n</script>\n", "acf.doAction( 'prepare' );" );
521 }
522
523 /**
524 * Prints uploader scripts in footer.
525 *
526 * @date 11/06/2020
527 * @since 5.9.0
528 *
529 * @param void
530 * @return void
531 */
532 public function print_uploader_scripts() {
533 // Todo: investigate output-buffer to hide HTML.
534 ?>
535 <div id="acf-hidden-wp-editor" style="display: none;">
536 <?php wp_editor( '', 'acf_content' ); ?>
537 </div>
538 <?php
539
540 /**
541 * Fires when printing uploader scripts.
542 *
543 * @since 5.6.9
544 *
545 * @param void
546 */
547 do_action( 'acf/admin_print_uploader_scripts' );
548 }
549 }
550
551 // instantiate
552 acf_new_instance( 'ACF_Assets' );
553 endif; // class_exists check
554
555 /**
556 * Appends an array of i18n data for localization.
557 *
558 * @date 13/4/18
559 * @since 5.6.9
560 *
561 * @param array $text An array of text for i18n.
562 * @return void
563 */
564 function acf_localize_text( $text ) {
565 return acf_get_instance( 'ACF_Assets' )->add_text( $text );
566 }
567
568 /**
569 * Appends an array of l10n data for localization.
570 *
571 * @date 13/4/18
572 * @since 5.6.9
573 *
574 * @param array $data An array of data for l10n.
575 * @return void
576 */
577 function acf_localize_data( $data ) {
578 return acf_get_instance( 'ACF_Assets' )->add_data( $data );
579 }
580
581 /**
582 * Enqueues a script with support for supplemental inline scripts.
583 *
584 * @date 27/4/20
585 * @since 5.9.0
586 *
587 * @param string $name The script name.
588 * @return void
589 */
590 function acf_enqueue_script( $name ) {
591 return acf_get_instance( 'ACF_Assets' )->enqueue_script( $name );
592 }
593
594 /**
595 * Enqueues the input scripts required for fields.
596 *
597 * @date 13/4/18
598 * @since 5.6.9
599 *
600 * @param array $args See ACF_Assets::enqueue_scripts() for a list of args.
601 * @return void
602 */
603 function acf_enqueue_scripts( $args = array() ) {
604 return acf_get_instance( 'ACF_Assets' )->enqueue( $args );
605 }
606
607 /**
608 * Enqueues the WP media uploader scripts and styles.
609 *
610 * @date 27/10/2014
611 * @since 5.0.9
612 *
613 * @param void
614 * @return void
615 */
616 function acf_enqueue_uploader() {
617 return acf_get_instance( 'ACF_Assets' )->enqueue_uploader();
618 }
619