| 1 |
<?php |
| 2 |
|
| 3 |
namespace TableBuilder\Config\CPT; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
use TableBuilder\Traits\Singleton; |
| 8 |
use WP_Post; |
| 9 |
use WP_Query; |
| 10 |
|
| 11 |
class TableCPT |
| 12 |
{ |
| 13 |
use Singleton; |
| 14 |
|
| 15 |
const POST_TYPE = 'tablekit_table'; |
| 16 |
|
| 17 |
private const CPT_CONFIG = array( |
| 18 |
'singular' => 'Table', |
| 19 |
'plural' => 'Tables', |
| 20 |
'menu_name' => 'Tables', |
| 21 |
'slug' => 'tablekit-table', |
| 22 |
'menu_icon' => 'dashicons-table-col-after', |
| 23 |
'menu_position' => 27, |
| 24 |
'text_domain' => 'table-builder-block', |
| 25 |
'supports' => array('title', 'editor', 'revisions', 'author'), |
| 26 |
); |
| 27 |
|
| 28 |
private const TABLE_BLOCKS = array( |
| 29 |
'tablebuilder/table-builder', |
| 30 |
'tablebuilder/data-table', |
| 31 |
'tablebuilder/post-table', |
| 32 |
); |
| 33 |
|
| 34 |
private const BLOCK_LABELS = array( |
| 35 |
'tablebuilder/table-builder' => 'Table Builder block', |
| 36 |
'tablebuilder/data-table' => 'Data Table block', |
| 37 |
'tablebuilder/post-table' => 'Post Table block', |
| 38 |
); |
| 39 |
|
| 40 |
private InlineTableScanner $scanner; |
| 41 |
private VirtualPostBuilder $virtual_posts; |
| 42 |
private TableCPTAdmin $admin; |
| 43 |
|
| 44 |
public static function get_table_blocks(): array |
| 45 |
{ |
| 46 |
return self::TABLE_BLOCKS; |
| 47 |
} |
| 48 |
|
| 49 |
public static function get_block_label(string $block_name): string |
| 50 |
{ |
| 51 |
return self::BLOCK_LABELS[$block_name] ?? $block_name; |
| 52 |
} |
| 53 |
|
| 54 |
protected function __construct() |
| 55 |
{ |
| 56 |
$this->scanner = new InlineTableScanner(); |
| 57 |
$this->virtual_posts = new VirtualPostBuilder($this->scanner); |
| 58 |
$this->admin = new TableCPTAdmin($this->scanner, $this->virtual_posts); |
| 59 |
|
| 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); |
| 64 |
|
| 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); |
| 72 |
} |
| 73 |
|
| 74 |
public function register_post_type(): void |
| 75 |
{ |
| 76 |
$config = self::CPT_CONFIG; |
| 77 |
$labels = $this->build_labels( |
| 78 |
$config['singular'], |
| 79 |
$config['plural'], |
| 80 |
$config['menu_name'], |
| 81 |
$config['text_domain'] |
| 82 |
); |
| 83 |
|
| 84 |
register_post_type( |
| 85 |
self::POST_TYPE, |
| 86 |
array( |
| 87 |
'labels' => $labels, |
| 88 |
'public' => false, |
| 89 |
'show_ui' => true, |
| 90 |
'show_in_menu' => 'tablebuilder', |
| 91 |
'show_in_rest' => true, |
| 92 |
'menu_position' => $config['menu_position'], |
| 93 |
'menu_icon' => $config['menu_icon'], |
| 94 |
'capability_type' => 'post', |
| 95 |
'map_meta_cap' => true, |
| 96 |
'hierarchical' => false, |
| 97 |
'supports' => $config['supports'], |
| 98 |
'has_archive' => false, |
| 99 |
'publicly_queryable' => false, |
| 100 |
'exclude_from_search' => true, |
| 101 |
'rewrite' => array('slug' => $config['slug']), |
| 102 |
'show_in_admin_bar' => false, |
| 103 |
) |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 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); |
| 110 |
} |
| 111 |
|
| 112 |
public function get_inline_table_source_options(): array |
| 113 |
{ |
| 114 |
return $this->scanner->get_inline_table_source_options(); |
| 115 |
} |
| 116 |
|
| 117 |
public function maybe_invalidate_cache(int $post_id, ?WP_Post $post = null): void |
| 118 |
{ |
| 119 |
$this->scanner->maybe_invalidate_cache($post_id, $post); |
| 120 |
} |
| 121 |
|
| 122 |
public function invalidate_cache(): void |
| 123 |
{ |
| 124 |
$this->scanner->invalidate_cache(); |
| 125 |
} |
| 126 |
|
| 127 |
private function build_labels(string $singular, string $plural, string $menu_name, string $domain): array |
| 128 |
{ |
| 129 |
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), |
| 146 |
); |
| 147 |
} |
| 148 |
} |
| 149 |
|