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