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 * * @param $columns * * @return array */ function table_columns($columns){ switch($this->view){ // sync case('sync'): { acfe_unset($columns, 'acf-field-group-category'); break; } // 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'), ); }else{ $columns = array( 'cb' => '', 'title' => __('Title', 'acf'), 'acfe-source' => __('Source', 'acf'), 'acf-count' => __('Fields', 'acf'), 'acf-location' => __('Location', 'acf'), ); } if(acf_get_setting('acfe/php') || acf_get_setting('acfe/json')){ $columns['acfe-load'] = __('Load', 'acf'); } if(acf_get_setting('acfe/php')){ $columns['acfe-autosync-php'] = __('PHP Sync'); } if(acf_get_setting('acfe/json')){ $columns['acfe-autosync-json'] = __('Json Sync', 'acf'); } break; } // active/trash default: { // acf < 5.9 if($this->old_version){ acfe_unset($columns, 'acf-fg-status'); $columns['acfe-location'] = __('Location', 'acf'); if(acf_get_setting('acfe/php') || acf_get_setting('acfe/json')){ $columns['acfe-load'] = __('Load', 'acf'); } if(acf_get_setting('acfe/php')){ $columns['acfe-autosync-php'] = __('PHP'); } if(acf_get_setting('acfe/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'); if(acf_get_setting('acfe/php') || acf_get_setting('acfe/json')){ $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'); } } // 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 * * @param $column * @param $post_id */ 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); } /** * render_table_column * * @param $column * @param $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; } // autosync php case 'acfe-autosync-php': { $this->render_admin_table_column_php_html($field_group); break; } // autosync json case 'acfe-autosync-json': { $this->render_admin_table_column_json_html($field_group); break; } } } /** * render_admin_table_column_source_html * * @param $field_group */ 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(acfe_get($field_group, 'acfe_local_source')){ $file = acfe_get($field_group, 'acfe_local_source'); $file_readable = $this->get_human_readable_file_location($file); $source = '' . __('AutoSync', 'acfe') . ''; } $source = apply_filters('acfe/field_groups_third_party/source', $source, $field_group['key'], $field_group); echo $source; } /** * render_admin_table_column_load_html * * @param $field_group */ 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']]) || acfe_get($local_field_group, 'local') === 'php'){ echo 'php'; // Json }elseif(acfe_get($local_field_group, 'local') === 'json'){ echo 'Json'; // DB }else{ echo 'DB'; } } /** * render_admin_table_column_php_html * * @param $field_group */ 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'] .= ' acfe-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(acfe_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 = acfe_get_php_save_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; } /** * render_admin_table_column_json_html * * @param $field_group */ 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'] .= ' acfe-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'); if($field_group['ID']){ $wrapper_start = ''; }else{ $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'] = __('Import', 'acf') . '. ' . $file_readable; $return['file'] = $file_readable; $return['icon'] = 'download'; $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 = acfe_get_json_save_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; } /** * default_hidden_columns * * @param $hidden * @param $screen * * @return mixed */ function default_hidden_columns($hidden, $screen){ $hidden[] = 'acf-description'; return $hidden; } /** * posts_per_page * * @return int */ function posts_per_page(){ return 999; } /** * row_actions * * @param $actions * @param $post * * @return mixed */ 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 * * @param $states * @param $post * * @return mixed */ 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'] = ''; } } // display title $display_title = acfe_get($field_group, 'acfe_display_title'); $tooltip_title = __('Display Title', 'acfe'); // ACF 6.6+: native display title if(acfe_is_acf('6.6')){ $display_title = acfe_get($field_group, 'display_title'); $tooltip_title = __('Display Title', 'acfe'); } 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 * * @param $post_type * @param $post * @param $field_groups */ 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; }); } } /** * get_sync_compatibility * * @return array */ 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 = acfe_get($field_group, 'local'); $modified = acfe_get($field_group, 'modified'); $private = acfe_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;