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