| 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 |
} |
| 221 |
|