PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.2.13
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.2.13
2.2.13 2.2.12 2.2.11 2.2.10 2.2.9 2.2.8 2.2.7 2.2.6 2.2.5 2.2.4 2.2.3 trunk 1.0.0 1.0.1 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2
table-builder-block / includes / Config / CPT / TableCPT.php

TableCPT.php in TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables 2.2.13, at includes/Config/CPT/TableCPT.php

240 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The "tablekit_table" custom post type and its inline-table integration
4 *
5 * @package TableKit
6 */
7
8 namespace TableBuilder\Config\CPT;
9
10 defined( 'ABSPATH' ) || exit;
11
12 use TableBuilder\Traits\Singleton;
13 use WP_Post;
14 use WP_Query;
15
16 /**
17 * Registers the "tablekit_table" post type and wires together the scanner,
18 * virtual-post builder, and admin-list helpers that support inline table blocks.
19 */
20 class TableCPT {
21
22 use Singleton;
23
24 const POST_TYPE = 'tablekit_table';
25
26 private const CPT_CONFIG = array(
27 'singular' => 'Table',
28 'plural' => 'Tables',
29 'menu_name' => 'Tables',
30 'slug' => 'tablekit-table',
31 'menu_icon' => 'dashicons-table-col-after',
32 'menu_position' => 27,
33 'text_domain' => 'table-builder-block',
34 'supports' => array( 'title', 'editor', 'revisions', 'author' ),
35 );
36
37 private const TABLE_BLOCKS = array(
38 'tablebuilder/table-builder',
39 'tablebuilder/data-table',
40 'tablebuilder/post-table',
41 );
42
43 private const BLOCK_LABELS = array(
44 'tablebuilder/table-builder' => 'Table Builder block',
45 'tablebuilder/data-table' => 'Data Table block',
46 'tablebuilder/post-table' => 'Post Table block',
47 );
48
49 /**
50 * Scans post content for inline table blocks.
51 *
52 * @var InlineTableScanner
53 */
54 private InlineTableScanner $scanner;
55
56 /**
57 * Builds virtual "tablekit_table" posts for inline-only tables.
58 *
59 * @var VirtualPostBuilder
60 */
61 private VirtualPostBuilder $virtual_posts;
62
63 /**
64 * Admin list-table integration for this post type.
65 *
66 * @var TableCPTAdmin
67 */
68 private TableCPTAdmin $admin;
69
70 /**
71 * Gets the block names treated as "table blocks" throughout the plugin
72 * (used to find/inject table blocks inside arbitrary post content).
73 *
74 * @return string[] Block names, e.g. "tablebuilder/table-builder".
75 */
76 public static function get_table_blocks(): array {
77 return self::TABLE_BLOCKS;
78 }
79
80 /**
81 * Gets a human-readable label for one of the table block names.
82 *
83 * @param string $block_name Block name, e.g. "tablebuilder/table-builder".
84 * @return string The block's display label, or the block name if unknown.
85 */
86 public static function get_block_label( string $block_name ): string {
87 return self::BLOCK_LABELS[ $block_name ] ?? $block_name;
88 }
89
90 /**
91 * Instantiates collaborators and hooks post-type registration, inline-table
92 * injection, cache invalidation, and admin-list integration into WordPress.
93 */
94 protected function __construct() {
95 $this->scanner = new InlineTableScanner();
96 $this->virtual_posts = new VirtualPostBuilder( $this->scanner );
97 $this->admin = new TableCPTAdmin( $this->scanner, $this->virtual_posts );
98
99 add_action( 'init', array( $this, 'register_post_type' ) );
100 add_filter( 'the_posts', array( $this, 'inject_inline_block_tables' ), 10, 2 );
101 add_action( 'save_post', array( $this, 'maybe_invalidate_cache' ), 10, 2 );
102 add_action( 'delete_post', array( $this, 'maybe_invalidate_cache' ), 10, 2 );
103
104 add_action( 'load-edit.php', array( $this->admin, 'prepare_bulk_post_ids' ) );
105 add_action( 'admin_notices', array( $this->admin, 'print_recommendation_notice' ) );
106 add_action( 'admin_enqueue_scripts', array( $this->admin, 'enqueue_admin_assets' ) );
107 add_filter( 'manage_' . self::POST_TYPE . '_posts_columns', array( $this->admin, 'add_list_columns' ) );
108 add_action( 'manage_' . self::POST_TYPE . '_posts_custom_column', array( $this->admin, 'render_list_column' ), 10, 2 );
109 add_filter( 'wp_list_table_show_post_checkbox', array( $this->admin, 'show_list_table_checkbox' ), 10, 2 );
110 add_filter( 'post_row_actions', array( $this->admin, 'filter_row_actions' ), 10, 2 );
111 }
112
113 /**
114 * Registers the "tablekit_table" custom post type, hooked to "init".
115 *
116 * @return void
117 */
118 public function register_post_type(): void {
119 $config = self::CPT_CONFIG;
120 $labels = $this->build_labels();
121
122 register_post_type(
123 self::POST_TYPE,
124 array(
125 'labels' => $labels,
126 'public' => false,
127 'show_ui' => true,
128 'show_in_menu' => 'tablebuilder',
129 'show_in_rest' => true,
130 'menu_position' => $config['menu_position'],
131 'menu_icon' => $config['menu_icon'],
132 'capability_type' => 'post',
133 'map_meta_cap' => true,
134 'hierarchical' => false,
135 'supports' => $config['supports'],
136 'has_archive' => false,
137 'publicly_queryable' => false,
138 'exclude_from_search' => true,
139 'rewrite' => array( 'slug' => $config['slug'] ),
140 'show_in_admin_bar' => false,
141 )
142 );
143 }
144
145 /**
146 * Injects virtual "tablekit_table" posts for inline-only table blocks into
147 * a query's results, hooked to "the_posts".
148 *
149 * @param array $posts Posts returned by the query.
150 * @param WP_Query $query The query that was executed.
151 * @return array Posts, with virtual inline-table posts merged in where applicable.
152 */
153 public function inject_inline_block_tables( array $posts, WP_Query $query ): array {
154 return $this->virtual_posts->inject_inline_block_tables( $posts, $query );
155 }
156
157 /**
158 * Gets the list of posts that may contain inline table blocks, for use as
159 * a source-selection dropdown/options list.
160 *
161 * @return array Options describing candidate posts.
162 */
163 public function get_inline_table_source_options(): array {
164 return $this->scanner->get_inline_table_source_options();
165 }
166
167 /**
168 * Invalidates the inline-table scan cache when a post is saved/deleted, if
169 * that post could plausibly contain a table block. Hooked to "save_post"
170 * and "delete_post".
171 *
172 * @param int $post_id Post ID being saved/deleted.
173 * @param WP_Post|null $post The post object, if available.
174 * @return void
175 */
176 public function maybe_invalidate_cache( int $post_id, ?WP_Post $post = null ): void {
177 $this->scanner->maybe_invalidate_cache( $post_id, $post );
178 }
179
180 /**
181 * Unconditionally clear the inline-table scan cache.
182 *
183 * @return void
184 */
185 public function invalidate_cache(): void {
186 $this->scanner->invalidate_cache();
187 }
188
189 /**
190 * Builds the post type labels array.
191 *
192 * Strings are hardcoded (rather than built from self::CPT_CONFIG) because
193 * __()/sprintf(__()) require literal string and text-domain arguments to
194 * be picked up by WordPress.org's translation string extraction tooling.
195 * The values below must stay in sync with self::CPT_CONFIG['singular'],
196 * ['plural'], and ['menu_name'].
197 *
198 * @return array Post type labels, keyed per register_post_type()'s "labels" arg.
199 */
200 private function build_labels(): array {
201 /* translators: %s: Singular label for the "Table" post type. */
202 $add_new_item = sprintf( __( 'Add New %s', 'table-builder-block' ), __( 'Table', 'table-builder-block' ) );
203 /* translators: %s: Singular label for the "Table" post type. */
204 $new_item = sprintf( __( 'New %s', 'table-builder-block' ), __( 'Table', 'table-builder-block' ) );
205 /* translators: %s: Singular label for the "Table" post type. */
206 $edit_item = sprintf( __( 'Edit %s', 'table-builder-block' ), __( 'Table', 'table-builder-block' ) );
207 /* translators: %s: Singular label for the "Table" post type. */
208 $view_item = sprintf( __( 'View %s', 'table-builder-block' ), __( 'Table', 'table-builder-block' ) );
209 /* translators: %s: Plural label for the "Tables" post type. */
210 $all_items = sprintf( __( 'All %s', 'table-builder-block' ), __( 'Tables', 'table-builder-block' ) );
211 /* translators: %s: Plural label for the "Tables" post type. */
212 $search_items = sprintf( __( 'Search %s', 'table-builder-block' ), __( 'Tables', 'table-builder-block' ) );
213 /* translators: %s: Lowercase plural label for the "Tables" post type. */
214 $filter_items_list = sprintf( __( 'Filter %s list', 'table-builder-block' ), strtolower( __( 'Tables', 'table-builder-block' ) ) );
215 /* translators: %s: Plural label for the "Tables" post type. */
216 $items_list_navigation = sprintf( __( '%s list navigation', 'table-builder-block' ), __( 'Tables', 'table-builder-block' ) );
217 /* translators: %s: Plural label for the "Tables" post type. */
218 $items_list = sprintf( __( '%s list', 'table-builder-block' ), __( 'Tables', 'table-builder-block' ) );
219
220 return array(
221 'name' => __( 'Tables', 'table-builder-block' ),
222 'singular_name' => __( 'Table', 'table-builder-block' ),
223 'menu_name' => __( 'Tables', 'table-builder-block' ),
224 'name_admin_bar' => __( 'Table', 'table-builder-block' ),
225 'add_new' => __( 'Add New', 'table-builder-block' ),
226 'add_new_item' => $add_new_item,
227 'new_item' => $new_item,
228 'edit_item' => $edit_item,
229 'view_item' => $view_item,
230 'all_items' => $all_items,
231 'search_items' => $search_items,
232 'not_found' => __( 'No tables found.', 'table-builder-block' ),
233 'not_found_in_trash' => __( 'No tables found in Trash.', 'table-builder-block' ),
234 'filter_items_list' => $filter_items_list,
235 'items_list_navigation' => $items_list_navigation,
236 'items_list' => $items_list,
237 );
238 }
239 }
240