old_version = true; // ACF < 5.9 if($this->old_version){ $this->view = isset($_GET['post_status']) ? sanitize_text_field($_GET['post_status']) : ''; $this->sync = $this->get_sync_compatibility(); // ACF >= 5.9 }else{ $this->view = acf_get_instance('ACF_Admin_Field_Groups')->view; $this->sync = acf_get_instance('ACF_Admin_Field_Groups')->sync; } // Hooks add_filter('manage_edit-acf-field-group_columns', array($this, 'table_columns'), 20); add_action('manage_acf-field-group_posts_custom_column', array($this, 'table_columns_html'), 10, 2); add_filter('default_hidden_columns', array($this, 'default_hidden_columns'), 10, 2); add_filter('edit_acf-field-group_per_page', array($this, 'posts_per_page')); add_filter('page_row_actions', array($this, 'row_actions'), 20, 2); add_filter('display_post_states', array($this, 'post_states'), 20, 2); add_action('admin_footer', array($this, 'admin_footer')); if($this->view !== 'sync'){ add_filter('bulk_actions-edit-acf-field-group', array($this, 'bulk_actions')); add_filter('handle_bulk_actions-edit-acf-field-group', array($this, 'handle_bulk_actions'), 10, 3); } } /* * Table Columns */ function table_columns($columns){ switch($this->view){ // View: Sync case('sync'): acfe_unset($columns, 'acf-field-group-category'); break; // View: Local case('acfe-local'): if($this->old_version){ $columns = array( 'cb' => '', 'title' => __('Title', 'acf'), 'acfe-source' => __('Source', 'acf'), 'acf-count' => __('Fields', 'acf'), 'acfe-location' => __('Location', 'acf'), 'acfe-load' => __('Load', 'acf'), ); }else{ $columns = array( 'cb' => '', 'title' => __('Title', 'acf'), 'acfe-source' => __('Source', 'acf'), 'acf-count' => __('Fields', 'acf'), 'acf-location' => __('Location', 'acf'), 'acfe-load' => __('Load', 'acf'), ); } if(acf_get_setting('acfe/php')) $columns['acfe-autosync-php'] = __('PHP Sync'); if(acf_get_setting('json')) $columns['acfe-autosync-json'] = __('Json Sync', 'acf'); break; // View: Active/Trash default: // ACF < 5.9 if($this->old_version){ acfe_unset($columns, 'acf-fg-status'); $columns['acfe-location'] = __('Location', 'acf'); $columns['acfe-load'] = __('Load', 'acf'); if(acf_get_setting('acfe/php')) $columns['acfe-autosync-php'] = __('PHP'); if(acf_get_setting('json')) $columns['acfe-autosync-json'] = __('Json'); // ACF >= 5.9 }else{ // Re-order acfe_unset($columns, 'acf-key'); acfe_unset($columns, 'acf-location'); acfe_unset($columns, 'acf-count'); acfe_unset($columns, 'acf-json'); $columns['acf-count'] = __('Fields', 'acf'); $columns['acf-location'] = __('Location', 'acf'); $columns['acfe-load'] = __('Load', 'acf'); if(acf_get_setting('acfe/php')) $columns['acfe-autosync-php'] = __('PHP'); if(acf_get_setting('json')) $columns['acfe-autosync-json'] = __('Json', 'acf'); } // Remove Category column if empty if(isset($columns['acf-field-group-category'])){ $categories_count = get_terms(array( 'taxonomy' => 'acf-field-group-category', 'hide_empty' => false, 'fields' => 'count' )); if(empty($categories_count)) unset($columns['acf-field-group-category']); } break; } return $columns; } /* * Table Columns HTML */ function table_columns_html($column, $post_id){ $field_group = acf_get_field_group($post_id); if(!$field_group) return; $this->render_table_column($column, $field_group); } function render_table_column($column, $field_group){ switch($column){ // Source case 'acfe-source': $this->render_admin_table_column_source_html($field_group); break; // Count case 'acfe-count': echo esc_html( acf_get_field_count($field_group)); break; // Location case 'acfe-location': acfe_render_field_group_locations_html($field_group); break; // Load case 'acfe-load': $this->render_admin_table_column_load_html($field_group); break; // PHP Sync case 'acfe-autosync-php': $this->render_admin_table_column_php_html($field_group); break; // New Json Sync case 'acfe-autosync-json': $this->render_admin_table_column_json_html($field_group); break; } } /* * Column: Source */ function render_admin_table_column_source_html($field_group){ $source = __('Theme/Plugin', 'acf'); // ACF Extended if(in_array($field_group['key'], acfe_get_setting('reserved_field_groups', array()))){ $source = 'ACF Extended'; // Advanced Forms }elseif($field_group['key'] === 'group_form_settings' || $field_group['key'] === 'group_entry_data'){ $source = 'Advanced Forms'; }elseif(acf_maybe_get($field_group, 'acfe_local_source')){ $file = acf_maybe_get($field_group, 'acfe_local_source'); $file_readable = $this->get_human_readable_file_location($file); $source = 'AutoSync'; } $source = apply_filters('acfe/field_groups_third_party/source', $source, $field_group['key'], $field_group); echo $source; } /* * Column: Load */ function render_admin_table_column_load_html($field_group){ $php = acfe_get_local_php_files(); $local_field_group = acf_get_local_field_group($field_group['key']); // PHP if(isset($php[$field_group['key']]) || acf_maybe_get($local_field_group, 'local') === 'php'){ echo 'php'; // Json }elseif(acf_maybe_get($local_field_group, 'local') === 'json'){ echo 'Json'; // DB }else{ echo 'DB'; } } /* * Column: PHP Sync HTML */ function render_admin_table_column_php_html($field_group){ $return = $this->get_php_data($field_group); $wrapper = array( 'class' => '' ); if($return['class']) $wrapper['class'] = $return['class']; if($return['message']){ $wrapper['class'] .= ' acf-js-tooltip'; $wrapper['title'] = $return['message']; } $icons = array(); if($return['icon']) $icons[] = ''; if($return['warning']) $icons[] = ''; ?> > false, 'file' => false, 'wrapper_start' => '', 'wrapper_end' => '', 'class' => false, 'warning' => false, 'icon' => false, ); $php = acfe_get_local_php_files(); if(isset($php[$field_group['key']])){ $file = $php[$field_group['key']]; $file_readable = $this->get_human_readable_file_location($file); $local_field_group = acf_get_local_field_group($field_group['key']); if(acf_maybe_get($local_field_group, 'local') === 'php'){ $return['message'] = __('Synchronized', 'acf') . '. ' . $file_readable . '

' . __('Warning: Duplicated PHP code found in theme/plugin.', 'acf'); $return['file'] = $file_readable . '

' . __('Warning: Duplicated PHP code found in theme/plugin.', 'acf'); $return['icon'] = 'yes'; $return['warning'] = true; }else{ $return['message'] = __('Synchronized', 'acf') . '. ' . $file_readable; $return['file'] = $file_readable; $return['icon'] = 'yes'; } }else{ $path = untrailingslashit(acf_get_setting('acfe/php_save')); $path = apply_filters("acfe/settings/php_save/all", $path, $field_group); $path = apply_filters("acfe/settings/php_save/ID={$field_group['ID']}", $path, $field_group); $path = apply_filters("acfe/settings/php_save/key={$field_group['key']}", $path, $field_group); $found = (bool) is_dir($path) && wp_is_writable($path); $folder = $this->get_human_readable_file_location($path, $found, false); if(acfe_has_php_sync($field_group)){ $return['message'] = __('Awaiting save', 'acf') . '.
' . __('Save path', 'acf') . ' ' . lcfirst($folder); $return['file'] = __('Save path', 'acf') . ' ' . lcfirst($folder); $return['class'] = 'secondary'; $return['icon'] = 'yes'; $return['warning'] = true; }else{ $return['file'] = __('Save path', 'acf') . ' ' . lcfirst($folder); $return['class'] = 'secondary'; $return['icon'] = 'no-alt'; } } if($this->view === 'acfe-local'){ $return['message'] = false; $return['warning'] = false; $return['class'] = false; if($return['icon'] !== 'yes') $return['class'] = 'secondary'; } return $return; } /* * Column: Json Sync HTML */ function render_admin_table_column_json_html($field_group){ $return = $this->get_json_data($field_group); $wrapper = array( 'class' => '' ); if($return['class']) $wrapper['class'] = $return['class']; if($return['message']){ $wrapper['class'] .= ' acf-js-tooltip'; $wrapper['title'] = $return['message']; } $icons = array(); if($return['icon']) $icons[] = ''; if($return['warning']) $icons[] = ''; ?> > false, 'file' => false, 'wrapper_start' => false, 'wrapper_end' => false, 'class' => false, 'warning' => false, 'icon' => false, ); $json = acf_get_local_json_files(); if(isset($json[$field_group['key']])){ $file = $json[$field_group['key']]; $file_readable = $this->get_human_readable_file_location($file); if(isset($this->sync[$field_group['key']])){ // vars $nonce = wp_create_nonce('bulk-posts'); if($this->old_version){ $url = admin_url('edit.php?post_type=acf-field-group&post_status=sync&acfsync=' . $field_group['key'] . '&_wpnonce=' . $nonce ); $text = $field_group['ID'] ? __('Sync', 'acf') : __('Import', 'acf'); $wrapper_start = ''; $wrapper_end = '
' . $text . '
'; }else{ $url = admin_url('edit.php?post_type=acf-field-group&acfsync=' . $field_group['key'] . '&_wpnonce=' . $nonce); $text = $field_group['ID'] ? __('Review', 'acf') : __('Import', 'acf'); $wrapper_start = ''; $wrapper_end = '
' . $text . '
'; } if($field_group['ID']){ $return['message'] = __('Sync available', 'acf') . '. ' . $file_readable; $return['file'] = $file_readable; $return['icon'] = 'update'; $return['wrapper_start'] = $wrapper_start; $return['wrapper_end'] = $wrapper_end; }else{ $return['message'] = __('Sync available', 'acf') . '. ' . $file_readable; $return['file'] = $file_readable; $return['icon'] = 'update'; $return['wrapper_start'] = $wrapper_start; $return['wrapper_end'] = $wrapper_end; } }else{ $return['message'] = __('Synchronized', 'acf') . '. ' . $file_readable; $return['file'] = $file_readable; $return['icon'] = 'yes'; } }else{ $path = untrailingslashit(acf_get_setting('save_json')); $path = apply_filters("acfe/settings/json_save/all", $path, $field_group); $path = apply_filters("acfe/settings/json_save/ID={$field_group['ID']}", $path, $field_group); $path = apply_filters("acfe/settings/json_save/key={$field_group['key']}", $path, $field_group); $found = (bool) is_dir($path) && wp_is_writable($path); $folder = $this->get_human_readable_file_location($path, $found, false); if(acfe_has_json_sync($field_group)){ $return['message'] = __('Awaiting save', 'acf') . '.
' . __('Save path', 'acf') . ' ' . lcfirst($folder); $return['file'] = __('Save path', 'acf') . ' ' . lcfirst($folder); $return['class'] = 'secondary'; $return['icon'] = 'yes'; $return['warning'] = 'true'; }else{ $return['file'] = __('Save path', 'acf') . ' ' . lcfirst($folder); $return['class'] = 'secondary'; $return['icon'] = 'no-alt'; } } if($this->view === 'acfe-local'){ $return['message'] = false; $return['warning'] = false; $return['class'] = false; if($return['icon'] !== 'yes') $return['class'] = 'secondary'; } return $return; } /* * Hide Default Columns */ function default_hidden_columns($hidden, $screen){ $hidden[] = 'acf-description'; return $hidden; } /* * Posts Per Page */ function posts_per_page(){ return 999; } /* * Row Actions */ function row_actions($actions, $post){ // bail early if($post->post_type !== 'acf-field-group'){ return $actions; } $field_group = acf_get_field_group($post->ID); $actions['acfe-export-php'] = 'PHP'; $actions['acfe-export-json'] = 'Json'; $actions['acfe-key'] = '' . $field_group['key'] . ''; //$actions['acfe-id'] = 'ID: ' . $field_group['ID'] . ''; return $actions; } /* * Post States */ function post_states($states, $post){ // Vars $field_group = acf_get_field_group($post->ID); // ACF < 5.9 if($this->old_version){ // Disabled if($post->post_status === 'acf-disabled'){ $states['acf-disabled'] = ''; } // ACF > 5.9 }else{ if(isset($states['acf-disabled'])){ unset($states['acf-disabled']); $states['acf-disabled'] = ''; } } // Alternative Title $display_title = acf_maybe_get($field_group, 'acfe_display_title'); if(!empty($display_title)){ $states['acfe-title'] = '' . $display_title . ''; } return $states; } /* * Admin Footer */ function admin_footer(){ ?> " : ' '); $located .= $rel_file; } elseif( strpos($file, WP_PLUGIN_DIR) !== false ) { $rel_file = str_replace( WP_PLUGIN_DIR, '', $file ); if($prefix){ $located .= __($found ? 'Located' : 'Not found', 'acf'); } $located .= ' ' . __('in plugin:', 'acf') . (($br) ? "
" : ' '); $located .= $rel_file; } else { $rel_file = str_replace( ABSPATH, '', $file ); if($prefix){ $located .= __($found ? 'Located' : 'Not found', 'acf'); } $located .= ' ' . __('in:', 'acf') . (($br) ? "
" : ' '); $located .= $rel_file; } return $located; } /* * Seamless Metabox */ function seamless_metabox($post_type, $post, $field_groups){ // check gutenberg $is_gutenberg = acfe_is_block_editor(); foreach($field_groups as $field_group){ add_filter("postbox_classes_{$post_type}_acf-{$field_group['key']}", function($classes) use($field_group, $is_gutenberg){ // default $classes[] = 'acf-postbox'; // seamless if(!$is_gutenberg && $field_group['style'] === 'seamless'){ $classes[] = 'seamless'; } // left if($field_group['label_placement'] === 'left'){ $classes[] = 'acfe-postbox-left'; } // top if($field_group['label_placement'] === 'top'){ $classes[] = 'acfe-postbox-top'; } // return return $classes; }); } } function get_sync_compatibility(){ $sync = array(); if(!acf_get_local_json_files()) return $sync; $field_groups = acf_get_field_groups(); foreach($field_groups as $field_group){ // Vars $local = acf_maybe_get($field_group, 'local'); $modified = acf_maybe_get($field_group, 'modified'); $private = acf_maybe_get($field_group, 'private'); // Bail early if($private || $local !== 'json') continue; // If field group doesn't exists in DB or modified file date more recent than DB if(!$field_group['ID'] || ($modified && $modified > get_post_modified_time('U', true, $field_group['ID']))){ $sync[$field_group['key']] = $field_group; } } return $sync; } } acf_new_instance('ACFE_Field_Groups'); endif;