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
table-builder-block / includes / Config / Blocks.php

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

242 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Block registration, editor/frontend asset enqueueing, and saved-markup filtering
4 *
5 * @package TableKit
6 */
7
8 namespace TableBuilder\Config;
9
10 defined( 'ABSPATH' ) || exit;
11
12 use WP_Query;
13 use TableBuilder\Traits\Singleton;
14 use TableBuilder\Helpers\Utils;
15
16 /**
17 * Registers TableKit's blocks, their assets, and post-processes their saved markup.
18 */
19 class Blocks {
20
21 use Singleton;
22
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 }
33
34 /**
35 * Registers custom blocks for TableBuilder.
36 */
37 public function register_blocks() {
38 $blocks_list = \TableBuilder\Config\BlockList::get_block_list();
39
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 = '';
46
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 }
52
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 }
58
59 if ( file_exists( $blocks_dir ) ) {
60 register_block_type( $blocks_dir );
61 }
62 }
63 }
64 }
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 }
84
85
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 }
102
103 $this->enqueue_block_assets();
104 }
105
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 );
120
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 }
129
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();
138
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}";
142
143 if ( ! file_exists( "{$block_dir}/index.asset.php" ) ) {
144 continue;
145 }
146
147 $editor_asset = include "{$block_dir}/index.asset.php";
148
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 );
156
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 }
166
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();
180
181 $attributes = array(
182 'id' => 'block-' . ( $parsed_block['attrs']['blockID'] ?? 'default' ),
183 'data-block' => $parsed_block['blockName'] ?? '',
184 );
185
186 foreach ( $attributes as $attr => $value ) {
187 if ( empty( $block_processor->get_attribute( $attr ) ) ) {
188 $block_processor->set_attribute( $attr, $value );
189 }
190 }
191
192 if ( ! empty( $parsed_block['attrs']['blockClass'] ) ) {
193 $block_processor->add_class( $parsed_block['attrs']['blockClass'] );
194 }
195
196 $block_processor->add_class( 'table-builder-block' );
197
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 }
241 }
242