123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735 |
- <?php
- if ( ! class_exists( 'acf_admin_field_group' ) ) {
-
- class acf_admin_field_group {
-
- public function __construct() {
-
- add_action( 'current_screen', array( $this, 'current_screen' ) );
- add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
-
- add_action( 'wp_ajax_acf/field_group/render_field_settings', array( $this, 'ajax_render_field_settings' ) );
- add_action( 'wp_ajax_acf/field_group/render_location_rule', array( $this, 'ajax_render_location_rule' ) );
- add_action( 'wp_ajax_acf/field_group/move_field', array( $this, 'ajax_move_field' ) );
-
- add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
- add_filter( 'use_block_editor_for_post_type', array( $this, 'use_block_editor_for_post_type' ), 10, 2 );
- }
-
- public function use_block_editor_for_post_type( $use_block_editor, $post_type ) {
- if ( $post_type === 'acf-field-group' ) {
- return false;
- }
- return $use_block_editor;
- }
-
- public function post_updated_messages( $messages ) {
-
- $messages['acf-field-group'] = array(
- 0 => '',
- 1 => __( 'Field group updated.', 'acf' ),
- 2 => __( 'Field group updated.', 'acf' ),
- 3 => __( 'Field group deleted.', 'acf' ),
- 4 => __( 'Field group updated.', 'acf' ),
- 5 => false,
- 6 => __( 'Field group published.', 'acf' ),
- 7 => __( 'Field group saved.', 'acf' ),
- 8 => __( 'Field group submitted.', 'acf' ),
- 9 => __( 'Field group scheduled for.', 'acf' ),
- 10 => __( 'Field group draft updated.', 'acf' ),
- );
-
- return $messages;
- }
-
- public function current_screen() {
-
- if ( ! acf_is_screen( 'acf-field-group' ) ) {
- return;
- }
-
- acf_disable_filters();
-
- acf_enqueue_scripts();
-
- add_action( 'admin_body_class', array( $this, 'admin_body_class' ) );
- add_action( 'acf/input/admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
- add_action( 'acf/input/admin_head', array( $this, 'admin_head' ) );
- add_action( 'acf/input/form_data', array( $this, 'form_data' ) );
- add_action( 'acf/input/admin_footer', array( $this, 'admin_footer' ) );
-
- add_filter( 'acf/input/admin_l10n', array( $this, 'admin_l10n' ) );
- }
-
- public function admin_body_class( $classes ) {
- $classes .= ' acf-admin-single-field-group';
- return $classes;
- }
-
- public function admin_enqueue_scripts() {
-
- wp_dequeue_script( 'autosave' );
-
- wp_enqueue_style( 'acf-field-group' );
- wp_enqueue_script( 'acf-field-group' );
-
- acf_localize_text(
- array(
- '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' ),
- 'This field cannot be moved until its changes have been saved' => __( 'This field cannot be moved until its changes have been saved', 'acf' ),
- 'Field group title is required' => __( 'Field group title is required', 'acf' ),
- 'Move field group to trash?' => __( 'Move field group to trash?', 'acf' ),
- 'No toggle fields available' => __( 'No toggle fields available', 'acf' ),
- 'Move Custom Field' => __( 'Move Custom Field', 'acf' ),
- 'Close modal' => __( 'Close modal', 'acf' ),
- 'Field moved to other group' => __( 'Field moved to other group', 'acf' ),
- 'Checked' => __( 'Checked', 'acf' ),
- '(no label)' => __( '(no label)', 'acf' ),
- '(this field)' => __( '(this field)', 'acf' ),
- 'copy' => __( 'copy', 'acf' ),
- 'or' => __( 'or', 'acf' ),
- 'Show this field group if' => __( 'Show this field group if', 'acf' ),
- 'Null' => __( 'Null', 'acf' ),
-
- 'Has any value' => __( 'Has any value', 'acf' ),
- 'Has no value' => __( 'Has no value', 'acf' ),
- 'Value is equal to' => __( 'Value is equal to', 'acf' ),
- 'Value is not equal to' => __( 'Value is not equal to', 'acf' ),
- 'Value matches pattern' => __( 'Value matches pattern', 'acf' ),
- 'Value contains' => __( 'Value contains', 'acf' ),
- 'Value is greater than' => __( 'Value is greater than', 'acf' ),
- 'Value is less than' => __( 'Value is less than', 'acf' ),
- 'Selection is greater than' => __( 'Selection is greater than', 'acf' ),
- 'Selection is less than' => __( 'Selection is less than', 'acf' ),
-
- 'Repeater (Pro only)' => __( 'Repeater (Pro only)', 'acf' ),
- 'Flexibly Content (Pro only)' => __( 'Flexible Content (Pro only)', 'acf' ),
- 'Clone (Pro only)' => __( 'Clone (Pro only)', 'acf' ),
- 'Gallery (Pro only)' => __( 'Gallery (Pro only)', 'acf' ),
- )
- );
-
- acf_localize_data(
- array(
- 'fieldTypes' => acf_get_field_types_info(),
- )
- );
-
- do_action( 'acf/field_group/admin_enqueue_scripts' );
- }
-
- public function admin_head() {
-
- global $post, $field_group;
-
- $field_group = acf_get_field_group( $post->ID );
-
- add_meta_box( 'acf-field-group-fields', __( 'Fields', 'acf' ), array( $this, 'mb_fields' ), 'acf-field-group', 'normal', 'high' );
- if ( ! defined( 'ACF_PRO' ) || ! ACF_PRO ) {
- add_meta_box( 'acf-field-group-pro-features', 'ACF PRO', array( $this, 'mb_pro_features' ), 'acf-field-group', 'normal', 'high' );
- }
- add_meta_box( 'acf-field-group-options', __( 'Settings', 'acf' ), array( $this, 'mb_options' ), 'acf-field-group', 'normal', 'high' );
-
- add_action( 'post_submitbox_misc_actions', array( $this, 'post_submitbox_misc_actions' ), 10, 0 );
- add_action( 'edit_form_after_title', array( $this, 'edit_form_after_title' ), 10, 0 );
-
- add_filter( 'screen_settings', array( $this, 'screen_settings' ), 10, 1 );
- add_filter( 'get_user_option_screen_layout_acf-field-group', array( $this, 'screen_layout' ), 10, 1 );
-
- do_action( 'acf/field_group/admin_head' );
- }
-
- public function edit_form_after_title() {
-
- global $post;
-
- acf_form_data(
- array(
- 'screen' => 'field_group',
- 'post_id' => $post->ID,
- 'delete_fields' => 0,
- 'validation' => 0,
- )
- );
- }
-
- public function form_data( $args ) {
-
- do_action( 'acf/field_group/form_data', $args );
- }
-
- public function admin_l10n( $l10n ) {
- return apply_filters( 'acf/field_group/admin_l10n', $l10n );
- }
-
- public function admin_footer() {
-
- do_action( 'acf/field_group/admin_footer' );
- }
-
- public function screen_settings( $html ) {
- $show_field_keys = acf_get_user_setting( 'show_field_keys' ) ? 'checked="checked"' : '';
- $show_field_settings_tabs = acf_get_user_setting( 'show_field_settings_tabs', true ) ? 'checked="checked"' : '';
- $hide_field_settings_tabs = apply_filters( 'acf/field_group/disable_field_settings_tabs', false );
- $html .= '<div id="acf-append-show-on-screen" class="acf-hidden">';
- $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>';
- if ( ! $hide_field_settings_tabs ) {
- $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>';
- }
- $html .= '</div>';
- return $html;
- }
-
- public function screen_layout( $columns = 0 ) {
- return 1;
- }
-
- public function post_submitbox_misc_actions() {
- global $field_group;
- $status_label = $field_group['active'] ? _x( 'Active', 'post status', 'acf' ) : _x( 'Inactive', 'post status', 'acf' );
- ?>
- <script type="text/javascript">
- (function($) {
- $('#post-status-display').html( '<?php echo esc_html( $status_label ); ?>' );
- })(jQuery);
- </script>
- <?php
- }
-
- public function save_post( $post_id, $post ) {
-
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
- return $post_id;
- }
-
- if ( $post->post_type !== 'acf-field-group' ) {
- return $post_id;
- }
-
- if ( wp_is_post_revision( $post_id ) ) {
- return $post_id;
- }
-
- if ( ! acf_verify_nonce( 'field_group' ) ) {
- return $post_id;
- }
-
- if ( ! current_user_can( acf_get_setting( 'capability' ) ) ) {
- return $post_id;
- }
-
- acf_disable_filters();
-
- if ( ! empty( $_POST['acf_fields'] ) ) {
-
- foreach ( $_POST['acf_fields'] as $field ) {
- if ( ! isset( $field['key'] ) ) {
- continue;
- }
-
- $specific = false;
- $save = acf_extract_var( $field, 'save' );
-
- if ( $save == 'meta' ) {
- $specific = array(
- 'menu_order',
- 'post_parent',
- );
- }
-
- if ( ! $field['parent'] ) {
- $field['parent'] = $post_id;
- }
-
- acf_update_field( $field, $specific );
- }
- }
-
- if ( $_POST['_acf_delete_fields'] ) {
-
- $ids = explode( '|', $_POST['_acf_delete_fields'] );
- $ids = array_map( 'intval', $ids );
-
- foreach ( $ids as $id ) {
-
- if ( ! $id ) {
- continue;
- }
-
- acf_delete_field( $id );
- }
- }
-
- $_POST['acf_field_group']['ID'] = $post_id;
-
- $_POST['acf_field_group']['title'] = $_POST['post_title'];
-
- acf_update_field_group( $_POST['acf_field_group'] );
-
- return $post_id;
- }
-
- public function mb_fields() {
-
- global $field_group;
-
- $view = array(
- 'fields' => acf_get_fields( $field_group ),
- 'parent' => 0,
- );
-
- acf_get_view( 'field-group-fields', $view );
- }
-
- public function mb_pro_features() {
-
- acf_get_view( 'field-group-pro-features' );
- }
-
- public function mb_options() {
-
- global $field_group;
-
- if ( ! acf_is_field_group_key( $field_group['key'] ) ) {
- $field_group['key'] = uniqid( 'group_' );
- }
-
- acf_get_view( 'field-group-options' );
- }
-
- public function ajax_render_location_rule() {
-
- if ( ! acf_verify_ajax() ) {
- die();
- }
-
- if ( ! acf_current_user_can_admin() ) {
- die();
- }
-
- $rule = acf_validate_location_rule( acf_sanitize_request_args( $_POST['rule'] ) );
-
- acf_get_view(
- 'html-location-rule',
- array(
- 'rule' => $rule,
- )
- );
-
- die();
- }
-
- public function ajax_render_field_settings() {
-
- if ( ! acf_verify_ajax() || ! acf_current_user_can_admin() ) {
- wp_send_json_error();
- }
-
- $field = acf_maybe_get_POST( 'field' );
- if ( ! $field ) {
- wp_send_json_error();
- }
- $field['prefix'] = acf_maybe_get_POST( 'prefix' );
- $field = acf_get_valid_field( $field );
- $tabs = array(
- 'general' => '',
- 'validation' => '',
- 'presentation' => '',
- 'conditional_logic' => '',
- );
- foreach ( $tabs as $tab => $content ) {
- ob_start();
- if ( 'general' === $tab ) {
-
- do_action( "acf/render_field_settings/type={$field['type']}", $field );
- }
- do_action( "acf/render_field_{$tab}_settings/type={$field['type']}", $field );
- $sections[ $tab ] = ob_get_clean();
- }
- wp_send_json_success( $sections );
- }
-
- public function ajax_move_field() {
-
- acf_disable_filters();
- $args = acf_parse_args(
-
- $_POST,
- array(
- 'nonce' => '',
- 'post_id' => 0,
- 'field_id' => 0,
- 'field_group_id' => 0,
- )
- );
-
- if ( ! wp_verify_nonce( $args['nonce'], 'acf_nonce' ) ) {
- die();
- }
-
- if ( ! acf_current_user_can_admin() ) {
- die();
- }
-
- if ( $args['field_id'] && $args['field_group_id'] ) {
-
- $field = acf_get_field( $args['field_id'] );
- $field_group = acf_get_field_group( $args['field_group_id'] );
-
- $field['parent'] = $field_group['ID'];
-
- $field['conditional_logic'] = 0;
-
- acf_update_field( $field );
-
- $link = '<a href="' . admin_url( 'post.php?post=' . $field_group['ID'] . '&action=edit' ) . '" target="_blank">' . esc_html( $field_group['title'] ) . '</a>';
- echo '' .
- '<p><strong>' . __( 'Move Complete.', 'acf' ) . '</strong></p>' .
- '<p>' . sprintf(
-
- acf_punctify( __( 'The %1$s field can now be found in the %2$s field group', 'acf' ) ),
- esc_html( $field['label'] ),
- $link
- ) . '</p>' .
- '<a href="#" class="button button-primary acf-close-popup">' . __( 'Close Modal', 'acf' ) . '</a>';
- die();
- }
-
- $field_groups = acf_get_field_groups();
- $choices = array();
-
- if ( ! empty( $field_groups ) ) {
-
- foreach ( $field_groups as $field_group ) {
-
- if ( ! $field_group['ID'] ) {
- continue;
- }
-
- if ( $field_group['ID'] == $args['post_id'] ) {
- continue;
- }
- $choices[ $field_group['ID'] ] = $field_group['title'];
- }
- }
-
- $field = acf_get_valid_field(
- array(
- 'type' => 'select',
- 'name' => 'acf_field_group',
- 'choices' => $choices,
- 'aria-label' => __( 'Please select the destination for this field', 'acf' ),
- )
- );
- echo '<p>' . __( 'Please select the destination for this field', 'acf' ) . '</p>';
- echo '<form id="acf-move-field-form">';
-
- acf_render_field_wrap( $field );
- echo '<button type="submit" class="acf-btn">' . __( 'Move Field', 'acf' ) . '</button>';
- echo '</form>';
-
- die();
- }
- }
-
- new acf_admin_field_group();
- }
- ?>
|