Нет описания

admin-field-group.php 18KB

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