PluginProbe
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables / 2.2.14
TableKit – WordPress Table Builder for Data Tables, WooCommerce Product Tables & Post Tables v2.2.14
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 +220 -163 2.2.32.2.14 View file →
@@ -1,163 +1,220 @@
1 -<?php
2 -
3 -namespace TableBuilder\Config;
4 -
5 -defined('ABSPATH') || exit;
6 -
7 -use WP_Query;
8 -use TableBuilder\Traits\Singleton;
9 -use TableBuilder\Helpers\Utils;
10 -
11 -class Blocks
12 -{
13 - use Singleton;
14 -
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 - }
23 -
24 - /**
25 - * Register custom blocks for TableBuilder.
26 - */
27 - public function register_blocks()
28 - {
29 - $blocks_list = \TableBuilder\Config\BlockList::get_block_list();
30 -
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 = '';
37 -
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 - }
43 -
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 - }
49 -
50 - if (file_exists($blocks_dir)) {
51 - register_block_type($blocks_dir);
52 - }
53 - }
54 - }
55 - }
56 -
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 - }
69 -
70 -
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 -
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 - }
90 -
91 - // Enqueue block editor assets.
92 - public function block_editor_assets()
93 - {
94 - $blocks_list = \TableBuilder\Config\BlockList::get_block_list();
95 -
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}";
99 -
100 - if (!file_exists("{$block_dir}/index.asset.php")) {
101 - continue;
102 - }
103 -
104 - $editor_asset = include "{$block_dir}/index.asset.php";
105 -
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 - );
113 -
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 - }
123 -
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();
132 -
133 - $attributes = [
134 - 'id' => "block-" . ($parsed_block['attrs']['blockID'] ?? 'default'),
135 - 'data-block' => $parsed_block['blockName'] ?? '',
136 - ];
137 -
138 - foreach ($attributes as $attr => $value) {
139 - if (empty($block_processor->get_attribute($attr))) {
140 - $block_processor->set_attribute($attr, $value);
141 - }
142 - }
143 -
144 - if (!empty($parsed_block['attrs']['blockClass'])) {
145 - $block_processor->add_class($parsed_block['attrs']['blockClass']);
146 - }
147 -
148 - $block_processor->add_class('table-builder-block');
149 -
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);
153 -
154 - if (method_exists($block_content, 'get_updated_html')) {
155 - $block_content = $block_content->get_updated_html();
156 - }
157 -
158 - return $before_markup . $block_content . $after_markup;
159 - }
160 -
161 - return $block_content;
162 - }
163 -}
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, '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 + * Enqueues the shared global/component stylesheets on both the editor and frontend.
88 + *
89 + * @return void
90 + */
91 + public function enqueue_block_assets() {
92 + wp_enqueue_style(
93 + 'gkit-components',
94 + TABLE_BUILDER_BLOCK_PLUGIN_URL . 'build/tablebuilder/components.css',
95 + array(),
96 + TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
97 + 'all'
98 + );
99 +
100 + wp_enqueue_style(
101 + 'table-builder-block-global',
102 + TABLE_BUILDER_BLOCK_PLUGIN_URL . 'build/tablebuilder/global.css',
103 + array(),
104 + TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
105 + 'all'
106 + );
107 + }
108 +
109 + /**
110 + * Enqueues each registered block's editor script/style, based on its
111 + * generated index.asset.php dependency manifest.
112 + *
113 + * @return void
114 + */
115 + public function block_editor_assets() {
116 + $blocks_list = \TableBuilder\Config\BlockList::get_block_list();
117 +
118 + foreach ( $blocks_list as $key => $block ) {
119 + $block_dir = TABLE_BUILDER_BLOCK_PLUGIN_DIR . "blocks/{$key}";
120 + $plugin_url = TABLE_BUILDER_BLOCK_PLUGIN_URL . "build/blocks/{$key}";
121 +
122 + if ( ! file_exists( "{$block_dir}/index.asset.php" ) ) {
123 + continue;
124 + }
125 +
126 + $editor_asset = include "{$block_dir}/index.asset.php";
127 +
128 + wp_enqueue_script(
129 + "{$key}-editor",
130 + "{$plugin_url}/index.js",
131 + $editor_asset['dependencies'] ?? array(),
132 + $editor_asset['version'] ?? TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
133 + true
134 + );
135 +
136 + wp_enqueue_style(
137 + "{$key}-editor-style",
138 + "{$plugin_url}/index.css",
139 + array(),
140 + TABLE_BUILDER_BLOCK_PLUGIN_VERSION,
141 + 'all'
142 + );
143 + }
144 + }
145 +
146 + /**
147 + * Filters a table block's saved markup to add a stable id/data attributes
148 + * and the "table-builder-block" class, via WP_HTML_Tag_Processor.
149 + *
150 + * @param string $block_content Rendered block HTML.
151 + * @param array $parsed_block Parsed block data (name, attrs, etc.).
152 + * @param mixed $instance The block instance (WP_Block or similar).
153 + * @return string The filtered block HTML.
154 + */
155 + public function save_block_element( $block_content, $parsed_block, $instance ) {
156 + if ( ! empty( $block_content ) && Utils::is_table_builder_block( $block_content, $parsed_block, 'blockClass' ) ) {
157 + $block_processor = new \WP_HTML_Tag_Processor( $block_content );
158 + $block_processor->next_tag();
159 +
160 + $attributes = array(
161 + 'id' => 'block-' . ( $parsed_block['attrs']['blockID'] ?? 'default' ),
162 + 'data-block' => $parsed_block['blockName'] ?? '',
163 + );
164 +
165 + foreach ( $attributes as $attr => $value ) {
166 + if ( empty( $block_processor->get_attribute( $attr ) ) ) {
167 + $block_processor->set_attribute( $attr, $value );
168 + }
169 + }
170 +
171 + if ( ! empty( $parsed_block['attrs']['blockClass'] ) ) {
172 + $block_processor->add_class( $parsed_block['attrs']['blockClass'] );
173 + }
174 +
175 + $block_processor->add_class( 'table-builder-block' );
176 +
177 + /**
178 + * Filters markup prepended to a table block's saved element, before rendering.
179 + *
180 + * @since Unknown
181 + *
182 + * @param string $before_markup Markup to prepend; empty by default.
183 + * @param array $parsed_block Parsed block data (name, attrs, etc.).
184 + */
185 + // 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.
186 + $before_markup = apply_filters( 'tablebuilder/save_element_markup_before', '', $parsed_block );
187 +
188 + /**
189 + * Filters markup appended to a table block's saved element, after rendering.
190 + *
191 + * @since Unknown
192 + *
193 + * @param string $after_markup Markup to append; empty by default.
194 + * @param array $parsed_block Parsed block data (name, attrs, etc.).
195 + */
196 + // 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.
197 + $after_markup = apply_filters( 'tablebuilder/save_element_markup_after', '', $parsed_block );
198 +
199 + /**
200 + * Filters the WP_HTML_Tag_Processor instance for a table block's saved element markup,
201 + * letting callbacks make further attribute/class changes before it's serialized.
202 + *
203 + * @since Unknown
204 + *
205 + * @param \WP_HTML_Tag_Processor $block_processor Tag processor wrapping the block's markup.
206 + * @param array $parsed_block Parsed block data (name, attrs, etc.).
207 + * @param mixed $instance The block instance (WP_Block or similar).
208 + */
209 + $block_content = apply_filters( 'tablebuilder_save_element_markup', $block_processor, $parsed_block, $instance );
210 +
211 + if ( method_exists( $block_content, 'get_updated_html' ) ) {
212 + $block_content = $block_content->get_updated_html();
213 + }
214 +
215 + return $before_markup . $block_content . $after_markup;
216 + }
217 +
218 + return $block_content;
219 + }
220 +}