PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.3.0
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.3.0
2.3.0 2.2.14 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
← All changes | includes/Config/Blocks.php +206 -128 2.2.2 → 2.3.0 View file →
@@ -1,163 +1,241 @@
1 1 <?php
2 +/**
3 + * Block registration, editor/frontend asset enqueueing, and saved-markup filtering
4 + *
5 + * @package TableKit
6 + */
2 7
3 8 namespace TableBuilder\Config;
4 9
5 -defined('ABSPATH') || exit;
10 +defined( 'ABSPATH' ) || exit;
6 11
7 12 use WP_Query;
8 13 use TableBuilder\Traits\Singleton;
9 14 use TableBuilder\Helpers\Utils;
10 15
11 -class Blocks
12 -{
13 - use Singleton;
16 +/**
17 + * Registers TableKit's blocks, their assets, and post-processes their saved markup.
18 + */
19 +class Blocks {
14 20
15 - protected function __construct()
16 - {
17 - add_action('init', [$this, 'register_blocks']);
18 - add_action('block_categories_all', [$this, 'register_block_categories'], 10, 2);
19 - add_action('enqueue_block_assets', [$this, 'enqueue_block_assets']);
20 - add_action('enqueue_block_editor_assets', [$this, 'block_editor_assets'], 5);
21 - add_filter('render_block', [$this, 'save_block_element'], 10, 3);
22 - }
21 + use Singleton;
23 22
24 - /**
25 - * Register custom blocks for TableBuilder.
26 - */
27 - public function register_blocks()
28 - {
29 - $blocks_list = \TableBuilder\Config\BlockList::get_block_list();
23 + /**
24 + * Hooks block registration and asset enqueueing into WordPress.
25 + */
26 + protected function __construct() {
27 + add_action( 'init', array( $this, 'register_blocks' ) );
28 + add_action( 'block_categories_all', array( $this, 'register_block_categories' ), 10, 2 );
29 + add_action( 'enqueue_block_assets', array( $this, 'maybe_enqueue_block_assets' ) );
30 + add_action( 'enqueue_block_editor_assets', array( $this, 'block_editor_assets' ), 5 );
31 + add_filter( 'render_block', array( $this, 'save_block_element' ), 10, 3 );
32 + }
30 33
31 - if (! empty($blocks_list)) {
32 - foreach ($blocks_list as $key => $block) {
33 - $package = isset($block['package']) ? $block['package'] : '';
34 - $blocks_dir = '';
35 - $plugin_dir = '';
36 - $plugin_slug = '';
34 + /**
35 + * Registers custom blocks for TableBuilder.
36 + */
37 + public function register_blocks() {
38 + $blocks_list = \TableBuilder\Config\BlockList::get_block_list();
37 39
38 - if (!empty($package) && $package === 'free') {
39 - $plugin_dir = TABLE_BUILDER_BLOCK_PLUGIN_DIR;
40 - $blocks_dir = TABLE_BUILDER_BLOCK_DIR . $key;
41 - $plugin_slug = 'table-builder-block';
42 - }
40 + if ( ! empty( $blocks_list ) ) {
41 + foreach ( $blocks_list as $key => $block ) {
42 + $package = isset( $block['package'] ) ? $block['package'] : '';
43 + $blocks_dir = '';
44 + $plugin_dir = '';
45 + $plugin_slug = '';
43 46
44 - if (!empty($package) && $package === 'pro' && defined('TABLE_BUILDER_BLOCK_PRO_PLUGIN_DIR')) {
45 - $plugin_dir = TABLE_BUILDER_BLOCK_PRO_PLUGIN_DIR;
46 - $blocks_dir = $plugin_dir . '/build/blocks/' . $key;
47 - $plugin_slug = 'table-builder-block-pro';
48 - }
47 + if ( ! empty( $package ) && 'free' === $package ) {
48 + $plugin_dir = TABLE_BUILDER_BLOCK_PLUGIN_DIR;
49 + $blocks_dir = TABLE_BUILDER_BLOCK_DIR . $key;
50 + $plugin_slug = 'table-builder-block';
51 + }
49 52
50 - if (file_exists($blocks_dir)) {
51 - register_block_type($blocks_dir);
52 - }
53 - }
54 - }
55 - }
53 + if ( ! empty( $package ) && 'pro' === $package && defined( 'TABLE_BUILDER_BLOCK_PRO_PLUGIN_DIR' ) ) {
54 + $plugin_dir = TABLE_BUILDER_BLOCK_PRO_PLUGIN_DIR;
55 + $blocks_dir = $plugin_dir . '/build/blocks/' . $key;
56 + $plugin_slug = 'table-builder-block-pro';
57 + }
56 58
57 - /**
58 - * Register custom block categories for TableBuilder.
59 - */
60 - public function register_block_categories($categories, $post)
61 - {
62 - return array_merge([
63 - [
64 - 'slug' => 'tablebuilder',
65 - 'title' => __('Table Builder', 'tablebuilder'),
66 - ],
67 - ], $categories);
68 - }
59 + if ( file_exists( $blocks_dir ) ) {
60 + register_block_type( $blocks_dir );
61 + }
62 + }
63 + }
64 + }
69 65
66 + /**
67 + * Registers the "Table Builder" block category.
68 + *
69 + * @param array[] $categories Existing block categories.
70 + * @param \WP_Post|null $post The post being edited, if any.
71 + * @return array[] Categories with the Table Builder category prepended.
72 + */
73 + public function register_block_categories( $categories, $post ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- required by the "block_categories_all" filter signature; not needed in the body.
74 + return array_merge(
75 + array(
76 + array(
77 + 'slug' => 'tablebuilder',
78 + 'title' => __( 'Table Builder', 'table-builder-block' ),
79 + ),
80 + ),
81 + $categories
82 + );
83 + }
70 84
71 - // Enqueue block global assets
72 - public function enqueue_block_assets()
73 - {
74 - wp_enqueue_style(
75 - 'gkit-components',
76 - TABLE_BUILDER_BLOCK_PLUGIN_URL . 'build/tablebuilder/components.css',
77 - [],
78 - TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
79 - 'all'
80 - );
81 85
82 - wp_enqueue_style(
83 - 'table-builder-block-global',
84 - TABLE_BUILDER_BLOCK_PLUGIN_URL . 'build/tablebuilder/global.css',
85 - [],
86 - TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
87 - 'all'
88 - );
89 - }
86 + /**
87 + * Hooked to "enqueue_block_assets": loads the shared global/component
88 + * stylesheets only in the block editor (incl. its iframe canvas).
89 + *
90 + * These files only style editor UI (inspector controls, template library,
91 + * placeholders); the frontend table never uses them, so loading them on every
92 + * frontend page only added ~120KB of render-blocking CSS and global selectors.
93 + * Contexts that need them outside the editor (Elementor widget, [tableKit]
94 + * shortcode) enqueue/print them explicitly.
95 + *
96 + * @return void
97 + */
98 + public function maybe_enqueue_block_assets() {
99 + if ( ! is_admin() ) {
100 + return;
101 + }
90 102
91 - // Enqueue block editor assets.
92 - public function block_editor_assets()
93 - {
94 - $blocks_list = \TableBuilder\Config\BlockList::get_block_list();
103 + $this->enqueue_block_assets();
104 + }
95 105
96 - foreach ($blocks_list as $key => $block) {
97 - $block_dir = TABLE_BUILDER_BLOCK_PLUGIN_DIR . "blocks/{$key}";
98 - $plugin_url = TABLE_BUILDER_BLOCK_PLUGIN_URL . "build/blocks/{$key}";
106 + /**
107 + * Enqueues the shared global/component stylesheets. Public so other render
108 + * paths (e.g. the Elementor widget) can request them explicitly.
109 + *
110 + * @return void
111 + */
112 + public function enqueue_block_assets() {
113 + wp_enqueue_style(
114 + 'gkit-components',
115 + TABLE_BUILDER_BLOCK_PLUGIN_URL . 'build/tablebuilder/components.css',
116 + array(),
117 + TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
118 + 'all'
119 + );
99 120
100 - if (!file_exists("{$block_dir}/index.asset.php")) {
101 - continue;
102 - }
121 + wp_enqueue_style(
122 + 'table-builder-block-global',
123 + TABLE_BUILDER_BLOCK_PLUGIN_URL . 'build/tablebuilder/global.css',
124 + array(),
125 + TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
126 + 'all'
127 + );
128 + }
103 129
104 - $editor_asset = include "{$block_dir}/index.asset.php";
130 + /**
131 + * Enqueues each registered block's editor script/style, based on its
132 + * generated index.asset.php dependency manifest.
133 + *
134 + * @return void
135 + */
136 + public function block_editor_assets() {
137 + $blocks_list = \TableBuilder\Config\BlockList::get_block_list();
105 138
106 - wp_enqueue_script(
107 - "{$key}-editor",
108 - "{$plugin_url}/index.js",
109 - $editor_asset['dependencies'] ?? [],
110 - $editor_asset['version'] ?? TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
111 - true
112 - );
139 + foreach ( $blocks_list as $key => $block ) {
140 + $block_dir = TABLE_BUILDER_BLOCK_PLUGIN_DIR . "blocks/{$key}";
141 + $plugin_url = TABLE_BUILDER_BLOCK_PLUGIN_URL . "build/blocks/{$key}";
113 142
114 - wp_enqueue_style(
115 - "{$key}-editor-style",
116 - "{$plugin_url}/index.css",
117 - [],
118 - TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
119 - 'all'
120 - );
121 - }
122 - }
143 + if ( ! file_exists( "{$block_dir}/index.asset.php" ) ) {
144 + continue;
145 + }
123 146
124 - /**
125 - * Save block element markup.
126 - */
127 - public function save_block_element($block_content, $parsed_block, $instance)
128 - {
129 - if (!empty($block_content) && Utils::is_table_builder_block($block_content, $parsed_block, 'blockClass')) {
130 - $block_processor = new \WP_HTML_Tag_Processor($block_content);
131 - $block_processor->next_tag();
147 + $editor_asset = include "{$block_dir}/index.asset.php";
132 148
133 - $attributes = [
134 - 'id' => "block-" . ($parsed_block['attrs']['blockID'] ?? 'default'),
135 - 'data-block' => $parsed_block['blockName'] ?? '',
136 - ];
149 + wp_enqueue_script(
150 + "{$key}-editor",
151 + "{$plugin_url}/index.js",
152 + $editor_asset['dependencies'] ?? array(),
153 + $editor_asset['version'] ?? TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
154 + true
155 + );
137 156
138 - foreach ($attributes as $attr => $value) {
139 - if (empty($block_processor->get_attribute($attr))) {
140 - $block_processor->set_attribute($attr, $value);
141 - }
142 - }
157 + wp_enqueue_style(
158 + "{$key}-editor-style",
159 + "{$plugin_url}/index.css",
160 + array(),
161 + TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
162 + 'all'
163 + );
164 + }
165 + }
143 166
144 - if (!empty($parsed_block['attrs']['blockClass'])) {
145 - $block_processor->add_class($parsed_block['attrs']['blockClass']);
146 - }
167 + /**
168 + * Filters a table block's saved markup to add a stable id/data attributes
169 + * and the "table-builder-block" class, via WP_HTML_Tag_Processor.
170 + *
171 + * @param string $block_content Rendered block HTML.
172 + * @param array $parsed_block Parsed block data (name, attrs, etc.).
173 + * @param mixed $instance The block instance (WP_Block or similar).
174 + * @return string The filtered block HTML.
175 + */
176 + public function save_block_element( $block_content, $parsed_block, $instance ) {
177 + if ( ! empty( $block_content ) && Utils::is_table_builder_block( $block_content, $parsed_block, 'blockClass' ) ) {
178 + $block_processor = new \WP_HTML_Tag_Processor( $block_content );
179 + $block_processor->next_tag();
147 180
148 - $block_processor->add_class('table-builder-block');
181 + $attributes = array(
182 + 'id' => 'block-' . ( $parsed_block['attrs']['blockID'] ?? 'default' ),
183 + 'data-block' => $parsed_block['blockName'] ?? '',
184 + );
149 185
150 - $before_markup = apply_filters('tablebuilder/save_element_markup_before', "", $parsed_block);
151 - $after_markup = apply_filters('tablebuilder/save_element_markup_after', "", $parsed_block);
152 - $block_content = apply_filters('tablebuilder_save_element_markup', $block_processor, $parsed_block, $instance);
186 + foreach ( $attributes as $attr => $value ) {
187 + if ( empty( $block_processor->get_attribute( $attr ) ) ) {
188 + $block_processor->set_attribute( $attr, $value );
189 + }
190 + }
153 191
154 - if (method_exists($block_content, 'get_updated_html')) {
155 - $block_content = $block_content->get_updated_html();
156 - }
192 + if ( ! empty( $parsed_block['attrs']['blockClass'] ) ) {
193 + $block_processor->add_class( $parsed_block['attrs']['blockClass'] );
194 + }
157 195
158 - return $before_markup . $block_content . $after_markup;
159 - }
196 + $block_processor->add_class( 'table-builder-block' );
160 197
161 - return $block_content;
162 - }
198 + /**
199 + * Filters markup prepended to a table block's saved element, before rendering.
200 + *
201 + * @since Unknown
202 + *
203 + * @param string $before_markup Markup to prepend; empty by default.
204 + * @param array $parsed_block Parsed block data (name, attrs, etc.).
205 + */
206 + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- slash-namespaced hook names are this plugin's established public API (used by the Pro add-on); renaming would be a breaking change.
207 + $before_markup = apply_filters( 'tablebuilder/save_element_markup_before', '', $parsed_block );
208 +
209 + /**
210 + * Filters markup appended to a table block's saved element, after rendering.
211 + *
212 + * @since Unknown
213 + *
214 + * @param string $after_markup Markup to append; empty by default.
215 + * @param array $parsed_block Parsed block data (name, attrs, etc.).
216 + */
217 + // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- slash-namespaced hook names are this plugin's established public API (used by the Pro add-on); renaming would be a breaking change.
218 + $after_markup = apply_filters( 'tablebuilder/save_element_markup_after', '', $parsed_block );
219 +
220 + /**
221 + * Filters the WP_HTML_Tag_Processor instance for a table block's saved element markup,
222 + * letting callbacks make further attribute/class changes before it's serialized.
223 + *
224 + * @since Unknown
225 + *
226 + * @param \WP_HTML_Tag_Processor $block_processor Tag processor wrapping the block's markup.
227 + * @param array $parsed_block Parsed block data (name, attrs, etc.).
228 + * @param mixed $instance The block instance (WP_Block or similar).
229 + */
230 + $block_content = apply_filters( 'tablebuilder_save_element_markup', $block_processor, $parsed_block, $instance );
231 +
232 + if ( method_exists( $block_content, 'get_updated_html' ) ) {
233 + $block_content = $block_content->get_updated_html();
234 + }
235 +
236 + return $before_markup . $block_content . $after_markup;
237 + }
238 +
239 + return $block_content;
240 + }
163 241 }