| @@ -1,16 +1,25 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * The "tablekit_table" custom post type and its inline-table integration | |
| 4 | + * | |
| 5 | + * @package TableKit | |
| 6 | + */ | |
| 2 | 7 | |
| 3 | 8 | namespace TableBuilder\Config\CPT; |
| 4 | 9 | |
| 5 | -defined('ABSPATH') || exit; | |
| 10 | +defined( 'ABSPATH' ) || exit; | |
| 6 | 11 | |
| 7 | 12 | use TableBuilder\Traits\Singleton; |
| 8 | 13 | use WP_Post; |
| 9 | 14 | use WP_Query; |
| 10 | 15 | |
| 11 | -class TableCPT | |
| 12 | -{ | |
| 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 | + | |
| 13 | 22 | use Singleton; |
| 14 | 23 | |
| 15 | 24 | const POST_TYPE = 'tablekit_table'; |
| 16 | 25 | |
| @@ -21,9 +30,9 @@ | ||
| 21 | 30 | 'slug' => 'tablekit-table', |
| 22 | 31 | 'menu_icon' => 'dashicons-table-col-after', |
| 23 | 32 | 'menu_position' => 27, |
| 24 | 33 | 'text_domain' => 'table-builder-block', |
| 25 | - 'supports' => array('title', 'editor', 'revisions', 'author'), | |
| 34 | + 'supports' => array( 'title', 'editor', 'revisions', 'author' ), | |
| 26 | 35 | ); |
| 27 | 36 | |
| 28 | 37 | private const TABLE_BLOCKS = array( |
| 29 | 38 | 'tablebuilder/table-builder', |
| @@ -36,51 +45,80 @@ | ||
| 36 | 45 | 'tablebuilder/data-table' => 'Data Table block', |
| 37 | 46 | 'tablebuilder/post-table' => 'Post Table block', |
| 38 | 47 | ); |
| 39 | 48 | |
| 49 | + /** | |
| 50 | + * Scans post content for inline table blocks. | |
| 51 | + * | |
| 52 | + * @var InlineTableScanner | |
| 53 | + */ | |
| 40 | 54 | private InlineTableScanner $scanner; |
| 55 | + | |
| 56 | + /** | |
| 57 | + * Builds virtual "tablekit_table" posts for inline-only tables. | |
| 58 | + * | |
| 59 | + * @var VirtualPostBuilder | |
| 60 | + */ | |
| 41 | 61 | private VirtualPostBuilder $virtual_posts; |
| 62 | + | |
| 63 | + /** | |
| 64 | + * Admin list-table integration for this post type. | |
| 65 | + * | |
| 66 | + * @var TableCPTAdmin | |
| 67 | + */ | |
| 42 | 68 | private TableCPTAdmin $admin; |
| 43 | 69 | |
| 44 | - public static function get_table_blocks(): array | |
| 45 | - { | |
| 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 { | |
| 46 | 77 | return self::TABLE_BLOCKS; |
| 47 | 78 | } |
| 48 | 79 | |
| 49 | - public static function get_block_label(string $block_name): string | |
| 50 | - { | |
| 51 | - return self::BLOCK_LABELS[$block_name] ?? $block_name; | |
| 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; | |
| 52 | 88 | } |
| 53 | 89 | |
| 54 | - protected function __construct() | |
| 55 | - { | |
| 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() { | |
| 56 | 95 | $this->scanner = new InlineTableScanner(); |
| 57 | - $this->virtual_posts = new VirtualPostBuilder($this->scanner); | |
| 58 | - $this->admin = new TableCPTAdmin($this->scanner, $this->virtual_posts); | |
| 96 | + $this->virtual_posts = new VirtualPostBuilder( $this->scanner ); | |
| 97 | + $this->admin = new TableCPTAdmin( $this->scanner, $this->virtual_posts ); | |
| 59 | 98 | |
| 60 | - add_action('init', array($this, 'register_post_type')); | |
| 61 | - add_filter('the_posts', array($this, 'inject_inline_block_tables'), 10, 2); | |
| 62 | - add_action('save_post', array($this, 'maybe_invalidate_cache'), 10, 2); | |
| 63 | - add_action('delete_post', array($this, 'maybe_invalidate_cache'), 10, 2); | |
| 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 ); | |
| 64 | 103 | |
| 65 | - add_action('load-edit.php', array($this->admin, 'prepare_bulk_post_ids')); | |
| 66 | - add_action('admin_notices', array($this->admin, 'print_recommendation_notice')); | |
| 67 | - add_action('admin_enqueue_scripts', array($this->admin, 'enqueue_admin_assets')); | |
| 68 | - add_filter('manage_' . self::POST_TYPE . '_posts_columns', array($this->admin, 'add_list_columns')); | |
| 69 | - add_action('manage_' . self::POST_TYPE . '_posts_custom_column', array($this->admin, 'render_list_column'), 10, 2); | |
| 70 | - add_filter('wp_list_table_show_post_checkbox', array($this->admin, 'show_list_table_checkbox'), 10, 2); | |
| 71 | - add_filter('post_row_actions', array($this->admin, 'filter_row_actions'), 10, 2); | |
| 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 ); | |
| 72 | 111 | } |
| 73 | 112 | |
| 74 | - public function register_post_type(): void | |
| 75 | - { | |
| 113 | + /** | |
| 114 | + * Registers the "tablekit_table" custom post type, hooked to "init". | |
| 115 | + * | |
| 116 | + * @return void | |
| 117 | + */ | |
| 118 | + public function register_post_type(): void { | |
| 76 | 119 | $config = self::CPT_CONFIG; |
| 77 | - $labels = $this->build_labels( | |
| 78 | - $config['singular'], | |
| 79 | - $config['plural'], | |
| 80 | - $config['menu_name'], | |
| 81 | - $config['text_domain'] | |
| 82 | - ); | |
| 120 | + $labels = $this->build_labels(); | |
| 83 | 121 | |
| 84 | 122 | register_post_type( |
| 85 | 123 | self::POST_TYPE, |
| 86 | 124 | array( |
| @@ -97,52 +135,105 @@ | ||
| 97 | 135 | 'supports' => $config['supports'], |
| 98 | 136 | 'has_archive' => false, |
| 99 | 137 | 'publicly_queryable' => false, |
| 100 | 138 | 'exclude_from_search' => true, |
| 101 | - 'rewrite' => array('slug' => $config['slug']), | |
| 139 | + 'rewrite' => array( 'slug' => $config['slug'] ), | |
| 102 | 140 | 'show_in_admin_bar' => false, |
| 103 | 141 | ) |
| 104 | 142 | ); |
| 105 | 143 | } |
| 106 | 144 | |
| 107 | - public function inject_inline_block_tables(array $posts, WP_Query $query): array | |
| 108 | - { | |
| 109 | - return $this->virtual_posts->inject_inline_block_tables($posts, $query); | |
| 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 ); | |
| 110 | 155 | } |
| 111 | 156 | |
| 112 | - public function get_inline_table_source_options(): array | |
| 113 | - { | |
| 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 { | |
| 114 | 164 | return $this->scanner->get_inline_table_source_options(); |
| 115 | 165 | } |
| 116 | 166 | |
| 117 | - public function maybe_invalidate_cache(int $post_id, ?WP_Post $post = null): void | |
| 118 | - { | |
| 119 | - $this->scanner->maybe_invalidate_cache($post_id, $post); | |
| 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 ); | |
| 120 | 178 | } |
| 121 | 179 | |
| 122 | - public function invalidate_cache(): void | |
| 123 | - { | |
| 180 | + /** | |
| 181 | + * Unconditionally clear the inline-table scan cache. | |
| 182 | + * | |
| 183 | + * @return void | |
| 184 | + */ | |
| 185 | + public function invalidate_cache(): void { | |
| 124 | 186 | $this->scanner->invalidate_cache(); |
| 125 | 187 | } |
| 126 | 188 | |
| 127 | - private function build_labels(string $singular, string $plural, string $menu_name, string $domain): array | |
| 128 | - { | |
| 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 | + | |
| 129 | 220 | return array( |
| 130 | - 'name' => __($plural, $domain), | |
| 131 | - 'singular_name' => __($singular, $domain), | |
| 132 | - 'menu_name' => __($menu_name, $domain), | |
| 133 | - 'name_admin_bar' => __($singular, $domain), | |
| 134 | - 'add_new' => __('Add New', $domain), | |
| 135 | - 'add_new_item' => sprintf(__('Add New %s', $domain), $singular), | |
| 136 | - 'new_item' => sprintf(__('New %s', $domain), $singular), | |
| 137 | - 'edit_item' => sprintf(__('Edit %s', $domain), $singular), | |
| 138 | - 'view_item' => sprintf(__('View %s', $domain), $singular), | |
| 139 | - 'all_items' => sprintf(__('All %s', $domain), $plural), | |
| 140 | - 'search_items' => sprintf(__('Search %s', $domain), $plural), | |
| 141 | - 'not_found' => __('No tables found.', $domain), | |
| 142 | - 'not_found_in_trash' => __('No tables found in Trash.', $domain), | |
| 143 | - 'filter_items_list' => sprintf(__('Filter %s list', $domain), strtolower($plural)), | |
| 144 | - 'items_list_navigation' => sprintf(__('%s list navigation', $domain), $plural), | |
| 145 | - 'items_list' => sprintf(__('%s list', $domain), $plural), | |
| 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, | |
| 146 | 237 | ); |
| 147 | 238 | } |
| 148 | 239 | } |