viewMarkup = $viewMarkup; $this->acfViewData = $acfViewData->getDeepClone(); } protected function cloneItemMeta(int $fromPostId, int $toPostId): void { $originAcfViewGroup = $this->acfViewData->getDeepClone(); $originAcfViewGroup->load($fromPostId); $originAcfViewGroup->setSource($toPostId); $originAcfViewGroup->save(true); } protected function updateMarkup(int $postId): void { $view = $this->acfViewData->getDeepClone(); $view->load($postId); // pageId 0, so without CSS $viewMarkup = $this->viewMarkup->getMarkup($view, 0); $view->markup = $viewMarkup; $view->save(); } public function getUniqueFieldId(AcfViewData $acfViewData, $excludeObject, string $name): string { $isUnique = true; foreach ($acfViewData->items as $item) { if ($item === $excludeObject || $item->field->id !== $name) { continue; } $isUnique = false; break; } return $isUnique ? $name : $this->getUniqueFieldId($acfViewData, $excludeObject, $name . '2'); } public function setHooks(): void { parent::setHooks(); add_action('init', [$this, 'addCPT']); add_action( 'manage_' . self::NAME . '_posts_custom_column', [ $this, 'printColumn', ], 10, 2 ); // priority is important here, should be after the acf code, so bigger than 10 add_action('acf/save_post', [$this, 'performSaveActions',], 20); add_action('admin_menu', [$this, 'removeAddNewItemLink']); add_filter('manage_' . self::NAME . '_posts_columns', [$this, 'getColumns',]); add_filter('manage_edit-' . self::NAME . '_sortable_columns', [$this, 'getSortableColumns',]); add_action('pre_get_posts', [$this, 'addSortableColumnsToRequest',]); add_filter('enter_title_here', [$this, 'getTitlePlaceholder',]); } /** * @param string|int $postId * * @return void */ public function performSaveActions($postId): void { // for 'site-settings' and similar if (! is_numeric($postId)) { return; } $post = get_post($postId); if (self::NAME !== $post->post_type || wp_is_post_revision($postId) || 'publish' !== $post->post_status) { return; } $this->updateIdentifiers($post->ID); $this->updateMarkup($post->ID); } public function addCPT(): void { $labels = [ 'name' => 'ACF Views', 'singular_name' => 'ACF View', 'menu_name' => 'ACF Views', 'parent_item_colon' => 'Parent ACF View', 'all_items' => 'ACF Views', 'view_item' => 'Browse ACF View', 'add_new_item' => 'Add New ACF View', 'add_new' => 'Add New', 'edit_item' => 'Edit ACF View', 'update_item' => 'Update ACF View', 'search_items' => 'Search ACF View', 'not_found' => 'Not Found', 'not_found_in_trash' => 'Not Found In Trash', ]; $args = [ 'label' => 'ACF Views', 'description' => 'Create ACF View item to select target ACF fields and copy the shortcode to display field values for a specific post/page/CPT item.', 'labels' => $labels, 'public' => true, 'publicly_queryable' => false, 'show_ui' => true, 'show_in_rest' => false, 'has_archive' => false, 'show_in_menu' => true, 'show_in_nav_menus' => false, 'delete_with_user' => false, 'exclude_from_search' => true, 'capability_type' => 'post', 'hierarchical' => false, 'can_export' => false, 'rewrite' => false, 'query_var' => false, 'menu_icon' => 'dashicons-format-gallery', 'supports' => ['title',], 'show_in_graphql' => false, ]; register_post_type(self::NAME, $args); } public function getColumns(array $columns): array { unset($columns['date']); return array_merge($columns, [ self::COLUMN_DESCRIPTION => 'Description', self::COLUMN_SHORTCODE => 'Shortcode', self::COLUMN_AUTHOR => 'Author', self::COLUMN_LAST_MODIFIED => 'Last modified', self::COLUMN_CREATED => 'Created', ]); } public function getSortableColumns(array $columns): array { return array_merge($columns, [ self::COLUMN_AUTHOR => self::COLUMN_AUTHOR, self::COLUMN_LAST_MODIFIED => self::COLUMN_LAST_MODIFIED, self::COLUMN_CREATED => self::COLUMN_CREATED, ]); } public function addSortableColumnsToRequest(WP_Query $query): void { if (! is_admin()) { return; } $orderBy = $query->get('orderby'); switch ($orderBy) { case self::COLUMN_AUTHOR: $query->set('orderby', 'post_author'); break; case self::COLUMN_LAST_MODIFIED: $query->set('orderby', 'post_modified'); break; case self::COLUMN_CREATED: $query->set('orderby', 'post_date'); break; } } public function printColumn(string $column, int $postId): void { switch ($column) { case self::COLUMN_DESCRIPTION: $view = $this->acfViewData->getDeepClone(); $view->load($postId); echo esc_html($view->description); break; case self::COLUMN_SHORTCODE: echo $this->html->postboxShortcodes($postId, true, Plugin::SHORTCODE, false); break; case self::COLUMN_AUTHOR: echo esc_html(get_user_by('id', get_post($postId)->post_author)->display_name ?? ''); break; case self::COLUMN_LAST_MODIFIED: echo esc_html(explode(' ', get_post($postId)->post_modified)[0]); break; case self::COLUMN_CREATED: echo esc_html(explode(' ', get_post($postId)->post_date)[0]); break; } } public function addMetaboxes(): void { parent::addMetaboxes(); add_meta_box( 'acf-views_shortcode', 'Shortcode', function ($post, $meta) { if (! $post || 'publish' !== $post->post_status) { echo 'Press the publish button to see the shortcode.'; return; } echo $this->html->postboxShortcodes($post->ID, false, Plugin::SHORTCODE, false); }, [ self::NAME, ], 'side', // right after the publish button 'core' ); } public function updateIdentifiers(int $postId): void { $acfViewData = $this->acfViewData->getDeepClone(); $acfViewData->load($postId); foreach ($acfViewData->items as $item) { $item->field->id = ($item->field->id && ! preg_match('/^[a-zA-Z0-9_\-]+$/', $item->field->id)) ? '' : $item->field->id; if ($item->field->id && $item->field->id === $this->getUniqueFieldId($acfViewData, $item, $item->field->id)) { continue; } $fieldMeta = new FieldMeta($item->field->getAcfFieldId()); if (! $fieldMeta->isFieldExist()) { continue; } $item->field->id = $this->getUniqueFieldId($acfViewData, $item, $fieldMeta->getName()); } $acfViewData->save(); } public function getTitlePlaceholder(string $title): string { if (! function_exists('get_current_screen')) { return $title; } $screen = get_current_screen()->post_type ?? ''; if (self::NAME !== $screen) { return $title; } return 'Name your view'; } public function removeAddNewItemLink(): void { $url = sprintf('edit.php?post_type=%s', self::NAME); global $submenu; if (! $submenu[$url]) { $submenu[$url] = []; } foreach ($submenu[$url] as $itemKey => $item) { if (3 !== count($item) || $item[2] !== 'post-new.php?post_type=acf_views') { continue; } unset($submenu[$url][$itemKey]); break; } } }