acfCardData = $acfCardData->getDeepClone(); } protected function cloneItemMeta(int $fromPostId, int $toPostId) { $originGroup = $this->acfCardData->getDeepClone(); $originGroup->load($fromPostId); $originGroup->setSource($toPostId); $originGroup->save(true); } public function addCPT(): void { $labels = [ 'name' => 'ACF Cards', 'singular_name' => 'ACF Card', 'menu_name' => 'ACF Cards', 'parent_item_colon' => 'Parent ACF Card', 'all_items' => 'ACF Cards', 'view_item' => 'Browse ACF Card', 'add_new_item' => 'Add New ACF Card', 'add_new' => 'Add New', 'edit_item' => 'Edit ACF Card', 'update_item' => 'Update ACF Card', 'search_items' => 'Search ACF Card', 'not_found' => 'Not Found', 'not_found_in_trash' => 'Not Found In Trash', ]; $args = [ 'label' => 'ACF Cards', 'description' => 'Create ACF Card item to choose a set of posts (or CPT items) and paste the shortcode in a target place to display the posts with their ACF fields (which fields are printed depending on a selected ACF View in the Card settings).', 'labels' => $labels, 'public' => true, 'publicly_queryable' => false, 'show_ui' => true, 'show_in_rest' => false, 'has_archive' => false, 'show_in_menu' => false, '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 setHooks(): void { parent::setHooks(); add_action('init', [$this, 'addCPT']); add_action('admin_menu', [$this, 'addPage']); add_action( 'manage_' . self::NAME . '_posts_custom_column', [ $this, 'printColumn', ], 10, 2 ); add_action('pre_get_posts', [$this, 'addSortableColumnsToRequest',]); add_filter('manage_' . self::NAME . '_posts_columns', [$this, 'getColumns',]); add_filter('manage_edit-' . self::NAME . '_sortable_columns', [$this, 'getSortableColumns',]); add_filter('enter_title_here', [$this, 'getTitlePlaceholder',]); } public function addPage(): void { $url = sprintf('edit.php?post_type=%s', AcfViews::NAME); global $submenu; if (! $submenu[$url]) { $submenu[$url] = []; } // 'ACF Views' has 5, so 6 is right after $submenu[$url][6] = [ 'ACF Cards', 'manage_options', sprintf( 'edit.php?post_type=%s', self::NAME ) ]; ksort($submenu[$url]); } 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 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->acfCardData->getDeepClone(); $view->load($postId); echo esc_html($view->description); break; case self::COLUMN_SHORTCODE: echo $this->html->postboxShortcodes($postId, true, Plugin::SHORTCODE_CARDS, true); 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_cpt', '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_CARDS, true); }, [ self::NAME, ], 'side', // right after the publish button 'core' ); } }