__('Advanced settings'), 'name' => 'acfe_form', 'prefix' => 'acf_field_group', 'type' => 'true_false', 'ui' => 1, 'instructions' => __('Enable advanced fields settings & validation'), 'value' => (isset($field_group['acfe_form'])) ? $field_group['acfe_form'] : '', 'required' => false, 'wrapper' => array( 'data-after' => 'active' ) )); } /** * Field Group Options: Data */ add_action('acf/field_group/admin_head', 'acfe_render_field_group_settings'); function acfe_render_field_group_settings(){ add_meta_box('acf-field-group-acfe', __('Field group', 'acf'), function(){ global $field_group; // Meta acf_render_field_wrap(array( 'label' => __('Custom meta data'), 'name' => 'acfe_meta', 'key' => 'acfe_meta', 'instructions' => __('Add custom meta data to the field group. Can be retrived using acf_get_field_group()'), 'prefix' => 'acf_field_group', 'type' => 'repeater', 'button_label' => __('+ Meta'), 'required' => false, 'layout' => 'table', 'value' => (isset($field_group['acfe_meta'])) ? $field_group['acfe_meta'] : array(), 'sub_fields' => array( array( 'ID' => false, 'label' => __('Key'), 'name' => 'acfe_meta_key', 'key' => 'acfe_meta_key', 'prefix' => '', '_name' => '', '_prepare' => '', 'type' => 'text', 'instructions' => false, 'required' => false, 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), ), array( 'ID' => false, 'label' => __('Value'), 'name' => 'acfe_meta_value', 'key' => 'acfe_meta_value', 'prefix' => '', '_name' => '', '_prepare' => '', 'type' => 'text', 'instructions' => false, 'required' => false, 'wrapper' => array( 'width' => '', 'class' => '', 'id' => '', ), ), ) )); // Data acf_render_field_wrap(array( 'label' => __('Field group data'), 'instructions' => __('View raw field group data, for development use'), 'type' => 'acfe_dynamic_message', 'name' => 'acfe_data', 'prefix' => 'acf_field_group', 'value' => $field_group['key'], )); // Note acf_render_field_wrap(array( 'label' => __('Note'), 'name' => 'acfe_note', 'prefix' => 'acf_field_group', 'type' => 'textarea', 'instructions' => __('Add personal note. Only visible to administrators'), 'value' => (isset($field_group['acfe_note'])) ? $field_group['acfe_note'] : '', 'required' => false, )); ?> post_type !== 'acf-field-group') return; global $field_group; ?>
Export: PHP Json
__('Display title', 'acfe'), 'instructions' => __('Render this title on edit post screen', 'acfe'), 'type' => 'text', 'name' => 'acfe_display_title', 'prefix' => 'acf_field_group', 'value' => (isset($field_group['acfe_display_title'])) ? $field_group['acfe_display_title'] : '', 'placeholder' => '', 'prepend' => '', 'append' => '' )); if(acfe_is_field_group_json_desync($field_group)){ acf_render_field_wrap(array( 'label' => __('Json Desync'), 'instructions' => __('Local json file is different from this version. If you manually synchronize it, you will lose your current field group settings'), 'type' => 'acfe_dynamic_message', 'name' => 'acfe_sync_available', 'prefix' => 'acf_field_group', 'value' => $field_group['key'], )); } $force_json_sync = false; $json_text = 'Json'; if(acfe_has_field_group_autosync_file($_field_group, 'json')){ if(isset($_field_group['acfe_autosync']) && is_array($_field_group['acfe_autosync'])) $_field_group['acfe_autosync'][] = 'json'; else $_field_group['acfe_autosync'] = array('json'); $json_text = 'Json'; $force_json_sync = true; } $force_php_sync = false; $php_text = 'PHP'; acf_enable_filter('local'); if(acfe_has_field_group_autosync_file($_field_group, 'php')){ acf_disable_filter('local'); if(isset($_field_group['acfe_autosync']) && is_array($_field_group['acfe_autosync'])) $_field_group['acfe_autosync'][] = 'php'; else $_field_group['acfe_autosync'] = array('php'); $php_text = 'PHP'; $force_php_sync = true; } acf_render_field_wrap(array( 'label' => __('Auto Sync'), 'instructions' => '', 'type' => 'checkbox', 'name' => 'acfe_autosync', 'prefix' => 'acf_field_group', 'value' => (isset($_field_group['acfe_autosync']) && !empty($_field_group['acfe_autosync'])) ? $_field_group['acfe_autosync'] : array(), 'choices' => array( 'php' => $php_text, 'json' => $json_text, ) )); acf_render_field_wrap(array( 'label' => __('Permissions'), 'name' => 'acfe_permissions', 'prefix' => 'acf_field_group', 'type' => 'checkbox', 'instructions' => __('Select user roles that are allowed to view and edit this field group in post edition'), 'required' => false, 'default_value' => false, 'choices' => acfe_get_roles(), 'value' => (isset($field_group['acfe_permissions'])) ? $field_group['acfe_permissions'] : array(), 'layout' => 'vertical' )); ?> Synchronize'; } /** * Render: Sync Warnings */ add_action('acf/render_field', 'acfe_render_field_acfe_sync_warnings', 5); function acfe_render_field_acfe_sync_warnings($field){ if($field['_name'] !== 'acfe_autosync') return; global $field_group; // PHP // Fix to load local field groups acf_enable_filters(); if(acfe_has_field_group_autosync($field_group, 'php') && !acf_get_setting('acfe/php_found')){ echo '

Folder /acfe-php was not found in your theme. You must create it to activate PHP Sync

'; } elseif(!acfe_has_field_group_autosync($field_group, 'php') && acfe_has_field_group_autosync_file($field_group, 'php')){ echo '

This field group is registered via a third-party PHP code

'; } elseif(acfe_has_field_group_autosync($field_group, 'php') && !acfe_has_field_group_autosync_file($field_group, 'php')){ echo '

' . $field_group['key'] . '.php will be created upon update

'; } // Re-disable filters, as natively acf_disable_filters(); // Json if(acfe_has_field_group_autosync($field_group, 'json') && !acfe_has_field_group_autosync_file($field_group, 'json')){ if(!acf_get_setting('acfe/json_found')){ echo '

Folder /acf-json was not found in your theme. You must create it to activate Json Sync.

'; } else{ echo '

' . $field_group['key'] . '.json will be created upon update.

'; } } } /** * Render: Data button */ add_action('acf/render_field/name=acfe_data', 'acfe_render_field_group_data'); function acfe_render_field_group_data($field){ $field_group = acf_get_field_group($field['value']); $field_group_raw = get_post($field_group['ID']); if(!$field_group){ echo '' . __('Data') . ''; return; } echo '' . __('Data') . ''; echo '
' . print_r($field_group, true) . '
' . print_r($field_group_raw, true) . '
'; } /** * Hooks: Display title (post edit) */ add_filter('acf/load_field_groups', 'acfe_render_field_groups', 999); function acfe_render_field_groups($field_groups){ if(!is_admin()) return $field_groups; if(acfe_is_admin_screen()) return $field_groups; foreach($field_groups as &$field_group){ if(!acf_maybe_get($field_group, 'acfe_display_title')) continue; $field_group['title'] = $field_group['acfe_display_title']; } return $field_groups; } /** * Hooks: Permissions (post edit) */ add_filter('acf/load_field_groups', 'acfe_permissions_field_groups', 999); function acfe_permissions_field_groups($field_groups){ if(!is_admin()) return $field_groups; if(acfe_is_admin_screen()) return $field_groups; $current_user_roles = acfe_get_current_user_roles(); foreach($field_groups as $key => $field_group){ if(!acf_maybe_get($field_group, 'acfe_permissions')) continue; $render_field_group = false; foreach($current_user_roles as $current_user_role){ foreach($field_group['acfe_permissions'] as $field_group_role){ if($current_user_role !== $field_group_role) continue; $render_field_group = true; break; } if($render_field_group) break; } if(!$render_field_group) unset($field_groups[$key]); } return $field_groups; } add_filter('acf/prepare_field/name=instruction_placement', 'acfe_field_group_instruction_placement'); function acfe_field_group_instruction_placement($field){ $field['choices'] = array_merge($field['choices'], array('acfe_instructions_tooltip' => 'Tooltip')); return $field; } /** * Validate field group */ add_filter('acf/validate_field_group', 'acfc_field_group_default_options'); function acfc_field_group_default_options($field_group){ // Only new field groups if(!acf_maybe_get($field_group, 'location')){ // Default label placement: Left $field_group['label_placement'] = 'left'; // AutoSync $acfe_autosync = array(); if(acf_get_setting('acfe/json_found', false)){ $acfe_autosync[] = 'json'; } if(acf_get_setting('acfe/php_found', false)){ $acfe_autosync[] = 'php'; } if(!empty($acfe_autosync)){ $field_group['acfe_autosync'] = $acfe_autosync; } } return $field_group; } /** * Field Group: Hide on screen */ add_filter('acf/prepare_field/name=hide_on_screen', 'acfc_field_group_hide_on_screen'); function acfc_field_group_hide_on_screen($field){ $choices = array(); foreach($field['choices'] as $key => $value){ if($key == 'the_content'){ $choices['block_editor'] = __('Block Editor'); } $choices[$key] = $value; } $field['choices'] = $choices; return $field; } add_filter('acf/prepare_field_group_for_export', 'acfc_field_group_export_categories'); function acfc_field_group_export_categories($field_group){ $_field_group = acf_get_field_group($field_group['key']); if(empty($_field_group)) return $field_group; if(!acf_maybe_get($_field_group, 'ID')) return $field_group; $categories = get_the_terms($_field_group['ID'], 'acf-field-group-category'); if(empty($categories) || is_wp_error($categories)) return $field_group; $field_group['acfe_categories'] = array(); foreach($categories as $term){ $field_group['acfe_categories'][$term->slug] = $term->name; } return $field_group; } add_action('acf/import_field_group', 'acfc_field_group_import_categories'); function acfc_field_group_import_categories($field_group){ if(!$categories = acf_maybe_get($field_group, 'acfe_categories')) return; foreach($categories as $term_slug => $term_name){ $new_term_id = false; $get_term = get_term_by('slug', $term_slug, 'acf-field-group-category'); // Term doesn't exists if(empty($get_term)){ $new_term = wp_insert_term($term_name, 'acf-field-group-category', array( 'slug' => $term_slug )); if(!is_wp_error($new_term)){ $new_term_id = $new_term['term_id']; } } // Term already exists else{ $new_term_id = $get_term->term_id; } if($new_term_id){ wp_set_post_terms($field_group['ID'], array($new_term_id), 'acf-field-group-category', true); } } } add_action('load-post.php', 'acfe_field_group_disable_editor'); add_action('load-post-new.php', 'acfe_field_group_disable_editor'); function acfe_field_group_disable_editor(){ // globals global $typenow; // restrict specific post types $restricted = array('acf-field-group', 'attachment'); if( in_array($typenow, $restricted) ) { return; } $post_type = $typenow; $post_id = 0; if ( isset( $_GET['post'] ) ) { $post_id = (int) $_GET['post']; } elseif ( isset( $_POST['post_ID'] ) ) { $post_id = (int) $_POST['post_ID']; } $field_groups = acf_get_field_groups(array( 'post_id' => $post_id, 'post_type' => $post_type )); $hide_block_editor = false; foreach($field_groups as $field_group){ $hide_on_screen = acf_get_array($field_group['hide_on_screen']); if(!in_array('block_editor', $hide_on_screen)) continue; $hide_block_editor = true; break; } if($hide_block_editor){ add_filter('use_block_editor_for_post_type', '__return_false'); } }