admin-field-group.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. <?php
  2. /**
  3. * ACF Admin Field Group Class
  4. *
  5. * @class acf_admin_field_group
  6. *
  7. * @package ACF
  8. * @subpackage Admin
  9. */
  10. if ( ! class_exists( 'acf_admin_field_group' ) ) {
  11. /**
  12. * ACF Admin Field Group Class
  13. *
  14. * All the logic for editing a field group
  15. */
  16. class acf_admin_field_group {
  17. /**
  18. * This function will setup the class functionality
  19. *
  20. * @since 5.0.0
  21. *
  22. * @return void
  23. */
  24. public function __construct() {
  25. // actions.
  26. add_action( 'current_screen', array( $this, 'current_screen' ) );
  27. add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
  28. // ajax.
  29. add_action( 'wp_ajax_acf/field_group/render_field_settings', array( $this, 'ajax_render_field_settings' ) );
  30. add_action( 'wp_ajax_acf/field_group/render_location_rule', array( $this, 'ajax_render_location_rule' ) );
  31. add_action( 'wp_ajax_acf/field_group/move_field', array( $this, 'ajax_move_field' ) );
  32. // filters.
  33. add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
  34. add_filter( 'use_block_editor_for_post_type', array( $this, 'use_block_editor_for_post_type' ), 10, 2 );
  35. }
  36. /**
  37. * Prevents the block editor from loading when editing an ACF field group.
  38. *
  39. * @since 5.8.0
  40. *
  41. * @param bool $use_block_editor Whether the post type can be edited or not. Default true.
  42. * @param string $post_type The post type being checked.
  43. * @return bool
  44. */
  45. public function use_block_editor_for_post_type( $use_block_editor, $post_type ) {
  46. if ( $post_type === 'acf-field-group' ) {
  47. return false;
  48. }
  49. return $use_block_editor;
  50. }
  51. /**
  52. * This function will customize the message shown when editing a field group
  53. *
  54. * @since 5.0.0
  55. *
  56. * @param array $messages Post type messages.
  57. * @return $messages
  58. */
  59. public function post_updated_messages( $messages ) {
  60. // append to messages.
  61. $messages['acf-field-group'] = array(
  62. 0 => '', // Unused. Messages start at index 1.
  63. 1 => __( 'Field group updated.', 'acf' ),
  64. 2 => __( 'Field group updated.', 'acf' ),
  65. 3 => __( 'Field group deleted.', 'acf' ),
  66. 4 => __( 'Field group updated.', 'acf' ),
  67. 5 => false, // field group does not support revisions.
  68. 6 => __( 'Field group published.', 'acf' ),
  69. 7 => __( 'Field group saved.', 'acf' ),
  70. 8 => __( 'Field group submitted.', 'acf' ),
  71. 9 => __( 'Field group scheduled for.', 'acf' ),
  72. 10 => __( 'Field group draft updated.', 'acf' ),
  73. );
  74. // return.
  75. return $messages;
  76. }
  77. /**
  78. * This function is fired when loading the admin page before HTML has been rendered.
  79. *
  80. * @since 5.0.0
  81. *
  82. * @return void
  83. */
  84. public function current_screen() {
  85. // validate screen.
  86. if ( ! acf_is_screen( 'acf-field-group' ) ) {
  87. return;
  88. }
  89. // disable filters to ensure ACF loads raw data from DB.
  90. acf_disable_filters();
  91. // enqueue scripts.
  92. acf_enqueue_scripts();
  93. // actions.
  94. add_action( 'admin_body_class', array( $this, 'admin_body_class' ) );
  95. add_action( 'acf/input/admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
  96. add_action( 'acf/input/admin_head', array( $this, 'admin_head' ) );
  97. add_action( 'acf/input/form_data', array( $this, 'form_data' ) );
  98. add_action( 'acf/input/admin_footer', array( $this, 'admin_footer' ) );
  99. // filters.
  100. add_filter( 'acf/input/admin_l10n', array( $this, 'admin_l10n' ) );
  101. }
  102. /**
  103. * Modifies the admin body class.
  104. *
  105. * @since 6.0.0
  106. *
  107. * @param string $classes Space-separated list of CSS classes.
  108. * @return string
  109. */
  110. public function admin_body_class( $classes ) {
  111. $classes .= ' acf-admin-single-field-group';
  112. return $classes;
  113. }
  114. /**
  115. * This action is run after post query but before any admin script / head actions.
  116. * It is a good place to register all actions.
  117. *
  118. * @since 5.0.0
  119. *
  120. * @return void
  121. */
  122. public function admin_enqueue_scripts() {
  123. // no autosave.
  124. wp_dequeue_script( 'autosave' );
  125. // custom scripts.
  126. wp_enqueue_style( 'acf-field-group' );
  127. wp_enqueue_script( 'acf-field-group' );
  128. // localize text.
  129. acf_localize_text(
  130. array(
  131. 'The string "field_" may not be used at the start of a field name' => __( 'The string "field_" may not be used at the start of a field name', 'acf' ),
  132. 'This field cannot be moved until its changes have been saved' => __( 'This field cannot be moved until its changes have been saved', 'acf' ),
  133. 'Field group title is required' => __( 'Field group title is required', 'acf' ),
  134. 'Move field group to trash?' => __( 'Move field group to trash?', 'acf' ),
  135. 'No toggle fields available' => __( 'No toggle fields available', 'acf' ),
  136. 'Move Custom Field' => __( 'Move Custom Field', 'acf' ),
  137. 'Close modal' => __( 'Close modal', 'acf' ),
  138. 'Field moved to other group' => __( 'Field moved to other group', 'acf' ),
  139. 'Checked' => __( 'Checked', 'acf' ),
  140. '(no label)' => __( '(no label)', 'acf' ),
  141. '(this field)' => __( '(this field)', 'acf' ),
  142. 'copy' => __( 'copy', 'acf' ),
  143. 'or' => __( 'or', 'acf' ),
  144. 'Show this field group if' => __( 'Show this field group if', 'acf' ),
  145. 'Null' => __( 'Null', 'acf' ),
  146. // Conditions.
  147. 'Has any value' => __( 'Has any value', 'acf' ),
  148. 'Has no value' => __( 'Has no value', 'acf' ),
  149. 'Value is equal to' => __( 'Value is equal to', 'acf' ),
  150. 'Value is not equal to' => __( 'Value is not equal to', 'acf' ),
  151. 'Value matches pattern' => __( 'Value matches pattern', 'acf' ),
  152. 'Value contains' => __( 'Value contains', 'acf' ),
  153. 'Value is greater than' => __( 'Value is greater than', 'acf' ),
  154. 'Value is less than' => __( 'Value is less than', 'acf' ),
  155. 'Selection is greater than' => __( 'Selection is greater than', 'acf' ),
  156. 'Selection is less than' => __( 'Selection is less than', 'acf' ),
  157. // Pro-only fields.
  158. 'Repeater (Pro only)' => __( 'Repeater (Pro only)', 'acf' ),
  159. 'Flexibly Content (Pro only)' => __( 'Flexible Content (Pro only)', 'acf' ),
  160. 'Clone (Pro only)' => __( 'Clone (Pro only)', 'acf' ),
  161. 'Gallery (Pro only)' => __( 'Gallery (Pro only)', 'acf' ),
  162. )
  163. );
  164. // localize data.
  165. acf_localize_data(
  166. array(
  167. 'fieldTypes' => acf_get_field_types_info(),
  168. )
  169. );
  170. // 3rd party hook.
  171. do_action( 'acf/field_group/admin_enqueue_scripts' );
  172. }
  173. /**
  174. * This function will setup all functionality for the field group edit page to work
  175. *
  176. * @since 3.1.8
  177. *
  178. * @return void
  179. */
  180. public function admin_head() {
  181. // global.
  182. global $post, $field_group;
  183. // set global var.
  184. $field_group = acf_get_field_group( $post->ID );
  185. // metaboxes.
  186. add_meta_box( 'acf-field-group-fields', __( 'Fields', 'acf' ), array( $this, 'mb_fields' ), 'acf-field-group', 'normal', 'high' );
  187. if ( ! defined( 'ACF_PRO' ) || ! ACF_PRO ) {
  188. add_meta_box( 'acf-field-group-pro-features', 'ACF PRO', array( $this, 'mb_pro_features' ), 'acf-field-group', 'normal', 'high' );
  189. }
  190. add_meta_box( 'acf-field-group-options', __( 'Settings', 'acf' ), array( $this, 'mb_options' ), 'acf-field-group', 'normal', 'high' );
  191. // actions.
  192. add_action( 'post_submitbox_misc_actions', array( $this, 'post_submitbox_misc_actions' ), 10, 0 );
  193. add_action( 'edit_form_after_title', array( $this, 'edit_form_after_title' ), 10, 0 );
  194. // filters.
  195. add_filter( 'screen_settings', array( $this, 'screen_settings' ), 10, 1 );
  196. add_filter( 'get_user_option_screen_layout_acf-field-group', array( $this, 'screen_layout' ), 10, 1 );
  197. // 3rd party hook.
  198. do_action( 'acf/field_group/admin_head' );
  199. }
  200. /**
  201. * This action will allow ACF to render metaboxes after the title
  202. *
  203. * @date 17/08/13
  204. *
  205. * @return void
  206. */
  207. public function edit_form_after_title() {
  208. // globals.
  209. global $post;
  210. // render post data.
  211. acf_form_data(
  212. array(
  213. 'screen' => 'field_group',
  214. 'post_id' => $post->ID,
  215. 'delete_fields' => 0,
  216. 'validation' => 0,
  217. )
  218. );
  219. }
  220. /**
  221. * This function will add extra HTML to the acf form data element
  222. *
  223. * @since 5.3.8
  224. *
  225. * @param array $args Arguments array to pass through to action.
  226. * @return void
  227. */
  228. public function form_data( $args ) {
  229. // do action.
  230. do_action( 'acf/field_group/form_data', $args );
  231. }
  232. /**
  233. * This function will append extra l10n strings to the acf JS object
  234. *
  235. * @since 5.3.8
  236. *
  237. * @param array $l10n The array of translated strings.
  238. * @return $l10n
  239. */
  240. public function admin_l10n( $l10n ) {
  241. return apply_filters( 'acf/field_group/admin_l10n', $l10n );
  242. }
  243. /**
  244. * Admin footer third party hook support
  245. *
  246. * @since 5.3.2
  247. *
  248. * @return void
  249. */
  250. public function admin_footer() {
  251. // 3rd party hook
  252. do_action( 'acf/field_group/admin_footer' );
  253. }
  254. /**
  255. * Screen settings html output
  256. *
  257. * @since 3.6.0
  258. *
  259. * @param string $html Current screen settings HTML.
  260. * @return string $html
  261. */
  262. public function screen_settings( $html ) {
  263. $show_field_keys = acf_get_user_setting( 'show_field_keys' ) ? 'checked="checked"' : '';
  264. $show_field_settings_tabs = acf_get_user_setting( 'show_field_settings_tabs', true ) ? 'checked="checked"' : '';
  265. $hide_field_settings_tabs = apply_filters( 'acf/field_group/disable_field_settings_tabs', false );
  266. $html .= '<div id="acf-append-show-on-screen" class="acf-hidden">';
  267. $html .= '<label for="acf-field-key-hide"><input id="acf-field-key-hide" type="checkbox" value="1" name="show_field_keys" ' . $show_field_keys . ' /> ' . __( 'Field Keys', 'acf' ) . '</label>';
  268. if ( ! $hide_field_settings_tabs ) {
  269. $html .= '<label for="acf-field-settings-tabs"><input id="acf-field-settings-tabs" type="checkbox" value="1" name="show_field_settings_tabs" ' . $show_field_settings_tabs . ' />' . __( 'Field Settings Tabs', 'acf' ) . '</label>';
  270. }
  271. $html .= '</div>';
  272. return $html;
  273. }
  274. /**
  275. * Sets the "Edit Field Group" screen to use a one-column layout.
  276. *
  277. * @param int $columns Number of columns for layout.
  278. *
  279. * @return int
  280. */
  281. public function screen_layout( $columns = 0 ) {
  282. return 1;
  283. }
  284. /**
  285. * This function will customize the publish metabox
  286. *
  287. * @since 5.2.9
  288. *
  289. * @return void
  290. */
  291. public function post_submitbox_misc_actions() {
  292. global $field_group;
  293. $status_label = $field_group['active'] ? _x( 'Active', 'post status', 'acf' ) : _x( 'Inactive', 'post status', 'acf' );
  294. ?>
  295. <script type="text/javascript">
  296. (function($) {
  297. $('#post-status-display').html( '<?php echo esc_html( $status_label ); ?>' );
  298. })(jQuery);
  299. </script>
  300. <?php
  301. }
  302. /**
  303. * This function will save all the field group data
  304. *
  305. * @since 1.0.0
  306. *
  307. * @param int $post_id The post ID.
  308. * @param WP_Post $post The post object.
  309. *
  310. * @return $post_id (int)
  311. */
  312. public function save_post( $post_id, $post ) {
  313. // do not save if this is an auto save routine.
  314. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  315. return $post_id;
  316. }
  317. // bail early if not acf-field-group.
  318. if ( $post->post_type !== 'acf-field-group' ) {
  319. return $post_id;
  320. }
  321. // only save once! WordPress save's a revision as well.
  322. if ( wp_is_post_revision( $post_id ) ) {
  323. return $post_id;
  324. }
  325. // verify nonce.
  326. if ( ! acf_verify_nonce( 'field_group' ) ) {
  327. return $post_id;
  328. }
  329. // Bail early if request came from an unauthorised user.
  330. if ( ! current_user_can( acf_get_setting( 'capability' ) ) ) {
  331. return $post_id;
  332. }
  333. // disable filters to ensure ACF loads raw data from DB.
  334. acf_disable_filters();
  335. // save fields.
  336. if ( ! empty( $_POST['acf_fields'] ) ) {
  337. // loop.
  338. foreach ( $_POST['acf_fields'] as $field ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized when saved.
  339. if ( ! isset( $field['key'] ) ) {
  340. continue;
  341. }
  342. // vars.
  343. $specific = false;
  344. $save = acf_extract_var( $field, 'save' );
  345. // only saved field if has changed.
  346. if ( $save == 'meta' ) {
  347. $specific = array(
  348. 'menu_order',
  349. 'post_parent',
  350. );
  351. }
  352. // set parent.
  353. if ( ! $field['parent'] ) {
  354. $field['parent'] = $post_id;
  355. }
  356. // save field.
  357. acf_update_field( $field, $specific );
  358. }
  359. }
  360. // delete fields.
  361. if ( $_POST['_acf_delete_fields'] ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized below.
  362. // clean.
  363. $ids = explode( '|', $_POST['_acf_delete_fields'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized below.
  364. $ids = array_map( 'intval', $ids );
  365. // loop.
  366. foreach ( $ids as $id ) {
  367. // bai early if no id.
  368. if ( ! $id ) {
  369. continue;
  370. }
  371. // delete.
  372. acf_delete_field( $id );
  373. }
  374. }
  375. // add args.
  376. $_POST['acf_field_group']['ID'] = $post_id;
  377. // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized when saved.
  378. $_POST['acf_field_group']['title'] = $_POST['post_title'];
  379. // save field group.
  380. acf_update_field_group( $_POST['acf_field_group'] );
  381. // phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
  382. return $post_id;
  383. }
  384. /**
  385. * This function will render the HTML for the medtabox 'acf-field-group-fields'
  386. *
  387. * @since 5.0.0
  388. *
  389. * @return void
  390. */
  391. public function mb_fields() {
  392. // global.
  393. global $field_group;
  394. // get fields.
  395. $view = array(
  396. 'fields' => acf_get_fields( $field_group ),
  397. 'parent' => 0,
  398. );
  399. // load view.
  400. acf_get_view( 'field-group-fields', $view );
  401. }
  402. /**
  403. * This function will render the HTML for the metabox 'acf-field-group-pro-features'
  404. *
  405. * @since 6.0.0
  406. *
  407. * @return void
  408. */
  409. public function mb_pro_features() {
  410. // load view.
  411. acf_get_view( 'field-group-pro-features' );
  412. }
  413. /**
  414. * This function will render the HTML for the metabox 'acf-field-group-options'
  415. *
  416. * @since 5.0.0
  417. *
  418. * @return void
  419. */
  420. public function mb_options() {
  421. // global.
  422. global $field_group;
  423. // field key (leave in for compatibility).
  424. if ( ! acf_is_field_group_key( $field_group['key'] ) ) {
  425. $field_group['key'] = uniqid( 'group_' );
  426. }
  427. // view.
  428. acf_get_view( 'field-group-options' );
  429. }
  430. /**
  431. * This function can be accessed via an AJAX action and will return the result from the render_location_value function
  432. *
  433. * @since 5.0.0
  434. *
  435. * @return void
  436. */
  437. public function ajax_render_location_rule() {
  438. // validate.
  439. if ( ! acf_verify_ajax() ) {
  440. die();
  441. }
  442. // verify user capability.
  443. if ( ! acf_current_user_can_admin() ) {
  444. die();
  445. }
  446. // validate rule.
  447. $rule = acf_validate_location_rule( acf_sanitize_request_args( $_POST['rule'] ) );
  448. // view.
  449. acf_get_view(
  450. 'html-location-rule',
  451. array(
  452. 'rule' => $rule,
  453. )
  454. );
  455. // die.
  456. die();
  457. }
  458. /**
  459. * This function will return HTML containing the field's settings based on it's new type
  460. *
  461. * @since 5.0.0
  462. *
  463. * @return void
  464. */
  465. public function ajax_render_field_settings() {
  466. // Verify the current request.
  467. if ( ! acf_verify_ajax() || ! acf_current_user_can_admin() ) {
  468. wp_send_json_error();
  469. }
  470. // Make sure we have a field.
  471. $field = acf_maybe_get_POST( 'field' );
  472. if ( ! $field ) {
  473. wp_send_json_error();
  474. }
  475. $field['prefix'] = acf_maybe_get_POST( 'prefix' );
  476. $field = acf_get_valid_field( $field );
  477. $tabs = array(
  478. 'general' => '',
  479. 'validation' => '',
  480. 'presentation' => '',
  481. 'conditional_logic' => '',
  482. );
  483. foreach ( $tabs as $tab => $content ) {
  484. ob_start();
  485. if ( 'general' === $tab ) {
  486. // Back-compat for fields not using tab-specific hooks.
  487. do_action( "acf/render_field_settings/type={$field['type']}", $field );
  488. }
  489. do_action( "acf/render_field_{$tab}_settings/type={$field['type']}", $field );
  490. $sections[ $tab ] = ob_get_clean();
  491. }
  492. wp_send_json_success( $sections );
  493. }
  494. /**
  495. * Move field AJAX function
  496. *
  497. * @since 5.0.0
  498. *
  499. * @return void No return, HTML output for AJAX consumption.
  500. */
  501. public function ajax_move_field() {
  502. // disable filters to ensure ACF loads raw data from DB.
  503. acf_disable_filters();
  504. $args = acf_parse_args(
  505. //phpcs:ignore WordPress.Security.NonceVerification.Missing
  506. $_POST,
  507. array(
  508. 'nonce' => '',
  509. 'post_id' => 0,
  510. 'field_id' => 0,
  511. 'field_group_id' => 0,
  512. )
  513. );
  514. // verify nonce.
  515. if ( ! wp_verify_nonce( $args['nonce'], 'acf_nonce' ) ) {
  516. die();
  517. }
  518. // verify user capability.
  519. if ( ! acf_current_user_can_admin() ) {
  520. die();
  521. }
  522. // confirm?
  523. if ( $args['field_id'] && $args['field_group_id'] ) {
  524. // vars.
  525. $field = acf_get_field( $args['field_id'] );
  526. $field_group = acf_get_field_group( $args['field_group_id'] );
  527. // update parent.
  528. $field['parent'] = $field_group['ID'];
  529. // remove conditional logic.
  530. $field['conditional_logic'] = 0;
  531. // update field.
  532. acf_update_field( $field );
  533. // Output HTML.
  534. $link = '<a href="' . admin_url( 'post.php?post=' . $field_group['ID'] . '&action=edit' ) . '" target="_blank">' . esc_html( $field_group['title'] ) . '</a>';
  535. echo '' .
  536. '<p><strong>' . __( 'Move Complete.', 'acf' ) . '</strong></p>' .
  537. '<p>' . sprintf(
  538. /* translators: Confirmation message once a field has been moved to a different field group. */
  539. acf_punctify( __( 'The %1$s field can now be found in the %2$s field group', 'acf' ) ),
  540. esc_html( $field['label'] ),
  541. $link //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  542. ) . '</p>' .
  543. '<a href="#" class="button button-primary acf-close-popup">' . __( 'Close Modal', 'acf' ) . '</a>';
  544. die();
  545. }
  546. // get all field groups.
  547. $field_groups = acf_get_field_groups();
  548. $choices = array();
  549. // check.
  550. if ( ! empty( $field_groups ) ) {
  551. // loop.
  552. foreach ( $field_groups as $field_group ) {
  553. // bail early if no ID.
  554. if ( ! $field_group['ID'] ) {
  555. continue;
  556. }
  557. // bail early if is current.
  558. if ( $field_group['ID'] == $args['post_id'] ) {
  559. continue;
  560. }
  561. $choices[ $field_group['ID'] ] = $field_group['title'];
  562. }
  563. }
  564. // render options.
  565. $field = acf_get_valid_field(
  566. array(
  567. 'type' => 'select',
  568. 'name' => 'acf_field_group',
  569. 'choices' => $choices,
  570. 'aria-label' => __( 'Please select the destination for this field', 'acf' ),
  571. )
  572. );
  573. echo '<p>' . __( 'Please select the destination for this field', 'acf' ) . '</p>';
  574. echo '<form id="acf-move-field-form">';
  575. // render.
  576. acf_render_field_wrap( $field );
  577. echo '<button type="submit" class="acf-btn">' . __( 'Move Field', 'acf' ) . '</button>';
  578. echo '</form>';
  579. // die.
  580. die();
  581. }
  582. }
  583. // initialize.
  584. new acf_admin_field_group();
  585. }
  586. ?>