| @@ -1,163 +1,220 @@ | ||
| 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, '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 | + * 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 | + ); | |
| 90 | 99 | |
| 91 | - // Enqueue block editor assets. | |
| 92 | - public function block_editor_assets() | |
| 93 | - { | |
| 94 | - $blocks_list = \TableBuilder\Config\BlockList::get_block_list(); | |
| 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 | + } | |
| 95 | 108 | |
| 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}"; | |
| 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(); | |
| 99 | 117 | |
| 100 | - if (!file_exists("{$block_dir}/index.asset.php")) { | |
| 101 | - continue; | |
| 102 | - } | |
| 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}"; | |
| 103 | 121 | |
| 104 | - $editor_asset = include "{$block_dir}/index.asset.php"; | |
| 122 | + if ( ! file_exists( "{$block_dir}/index.asset.php" ) ) { | |
| 123 | + continue; | |
| 124 | + } | |
| 105 | 125 | |
| 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 | - ); | |
| 126 | + $editor_asset = include "{$block_dir}/index.asset.php"; | |
| 113 | 127 | |
| 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 | - } | |
| 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 | + ); | |
| 123 | 135 | |
| 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(); | |
| 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 | + } | |
| 132 | 145 | |
| 133 | - $attributes = [ | |
| 134 | - 'id' => "block-" . ($parsed_block['attrs']['blockID'] ?? 'default'), | |
| 135 | - 'data-block' => $parsed_block['blockName'] ?? '', | |
| 136 | - ]; | |
| 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(); | |
| 137 | 159 | |
| 138 | - foreach ($attributes as $attr => $value) { | |
| 139 | - if (empty($block_processor->get_attribute($attr))) { | |
| 140 | - $block_processor->set_attribute($attr, $value); | |
| 141 | - } | |
| 142 | - } | |
| 160 | + $attributes = array( | |
| 161 | + 'id' => 'block-' . ( $parsed_block['attrs']['blockID'] ?? 'default' ), | |
| 162 | + 'data-block' => $parsed_block['blockName'] ?? '', | |
| 163 | + ); | |
| 143 | 164 | |
| 144 | - if (!empty($parsed_block['attrs']['blockClass'])) { | |
| 145 | - $block_processor->add_class($parsed_block['attrs']['blockClass']); | |
| 146 | - } | |
| 165 | + foreach ( $attributes as $attr => $value ) { | |
| 166 | + if ( empty( $block_processor->get_attribute( $attr ) ) ) { | |
| 167 | + $block_processor->set_attribute( $attr, $value ); | |
| 168 | + } | |
| 169 | + } | |
| 147 | 170 | |
| 148 | - $block_processor->add_class('table-builder-block'); | |
| 171 | + if ( ! empty( $parsed_block['attrs']['blockClass'] ) ) { | |
| 172 | + $block_processor->add_class( $parsed_block['attrs']['blockClass'] ); | |
| 173 | + } | |
| 149 | 174 | |
| 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); | |
| 175 | + $block_processor->add_class( 'table-builder-block' ); | |
| 153 | 176 | |
| 154 | - if (method_exists($block_content, 'get_updated_html')) { | |
| 155 | - $block_content = $block_content->get_updated_html(); | |
| 156 | - } | |
| 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 ); | |
| 157 | 187 | |
| 158 | - return $before_markup . $block_content . $after_markup; | |
| 159 | - } | |
| 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 ); | |
| 160 | 198 | |
| 161 | - return $block_content; | |
| 162 | - } | |
| 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 | + } | |
| 163 | 220 | } |