assets.php 15 KB

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