| 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 |
use Singleton; |
| 13 |
|
| 14 |
protected function __construct() { |
| 15 |
add_action('init', [$this, 'register_blocks']); |
| 16 |
add_action('block_categories_all', [$this, 'register_block_categories'], 10, 2); |
| 17 |
add_action('enqueue_block_assets', [$this, 'enqueue_block_assets']); |
| 18 |
add_action('enqueue_block_editor_assets', [$this, 'block_editor_assets'], 5); |
| 19 |
add_filter('render_block', [$this, 'save_block_element'], 10, 3); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Register custom blocks for TableBuilder. |
| 24 |
*/ |
| 25 |
public function register_blocks() { |
| 26 |
$blocks = [ |
| 27 |
'table-builder', |
| 28 |
'table-builder-row', |
| 29 |
'table-builder-item', |
| 30 |
]; |
| 31 |
|
| 32 |
foreach ($blocks as $block) { |
| 33 |
$block_path = TABLE_BUILDER_BLOCK_PLUGIN_DIR . "build/blocks/{$block}"; |
| 34 |
if (file_exists($block_path)) { |
| 35 |
register_block_type($block_path); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Register custom block categories for TableBuilder. |
| 42 |
*/ |
| 43 |
public function register_block_categories($categories, $post) { |
| 44 |
return array_merge([ |
| 45 |
[ |
| 46 |
'slug' => 'tablebuilder', |
| 47 |
'title' => __('Table Builder', 'tablebuilder'), |
| 48 |
], |
| 49 |
], $categories); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Enqueue global assets for both block editor and frontend. |
| 54 |
*/ |
| 55 |
public function enqueue_block_assets() { |
| 56 |
wp_enqueue_style( |
| 57 |
'table-builder-block-components', |
| 58 |
TABLE_BUILDER_BLOCK_PLUGIN_URL . 'build/tablebuilder/components.css', |
| 59 |
[], |
| 60 |
TABLE_BUILDER_BLOCK_PLUGIN_VERSION, |
| 61 |
'all' |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Enqueue block editor assets. |
| 67 |
*/ |
| 68 |
public function block_editor_assets() { |
| 69 |
$blocks_list = \TableBuilder\Config\BlockList::get_block_list(); |
| 70 |
|
| 71 |
foreach ($blocks_list as $key => $block) { |
| 72 |
$block_dir = TABLE_BUILDER_BLOCK_PLUGIN_DIR . "blocks/{$key}"; |
| 73 |
$plugin_url = TABLE_BUILDER_BLOCK_PLUGIN_URL . "build/blocks/{$key}"; |
| 74 |
|
| 75 |
if (!file_exists("{$block_dir}/index.asset.php")) { |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
$editor_asset = include "{$block_dir}/index.asset.php"; |
| 80 |
|
| 81 |
wp_enqueue_script( |
| 82 |
"{$key}-editor", |
| 83 |
"{$plugin_url}/index.js", |
| 84 |
$editor_asset['dependencies'] ?? [], |
| 85 |
$editor_asset['version'] ?? TABLE_BUILDER_BLOCK_PLUGIN_VERSION, |
| 86 |
true |
| 87 |
); |
| 88 |
|
| 89 |
wp_enqueue_style( |
| 90 |
"{$key}-editor-style", |
| 91 |
"{$plugin_url}/index.css", |
| 92 |
[], |
| 93 |
TABLE_BUILDER_BLOCK_PLUGIN_VERSION, |
| 94 |
'all' |
| 95 |
); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Save block element markup. |
| 101 |
*/ |
| 102 |
public function save_block_element($block_content, $parsed_block, $instance) { |
| 103 |
if (!empty($block_content) && Utils::is_table_builder_block($block_content, $parsed_block, 'blockClass')) { |
| 104 |
$block_processor = new \WP_HTML_Tag_Processor($block_content); |
| 105 |
$block_processor->next_tag(); |
| 106 |
|
| 107 |
$attributes = [ |
| 108 |
'id' => "block-" . ($parsed_block['attrs']['blockID'] ?? 'default'), |
| 109 |
'data-block' => $parsed_block['blockName'] ?? '', |
| 110 |
]; |
| 111 |
|
| 112 |
foreach ($attributes as $attr => $value) { |
| 113 |
if (empty($block_processor->get_attribute($attr))) { |
| 114 |
$block_processor->set_attribute($attr, $value); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
if (!empty($parsed_block['attrs']['blockClass'])) { |
| 119 |
$block_processor->add_class($parsed_block['attrs']['blockClass']); |
| 120 |
} |
| 121 |
|
| 122 |
$block_processor->add_class('table-builder-block'); |
| 123 |
|
| 124 |
$before_markup = apply_filters('tablebuilder/save_element_markup_before', "", $parsed_block); |
| 125 |
$after_markup = apply_filters('tablebuilder/save_element_markup_after', "", $parsed_block); |
| 126 |
$block_content = apply_filters('tablebuilder_save_element_markup', $block_processor, $parsed_block, $instance); |
| 127 |
|
| 128 |
if (method_exists($block_content, 'get_updated_html')) { |
| 129 |
$block_content = $block_content->get_updated_html(); |
| 130 |
} |
| 131 |
|
| 132 |
return $before_markup . $block_content . $after_markup; |
| 133 |
} |
| 134 |
|
| 135 |
return $block_content; |
| 136 |
} |
| 137 |
} |
| 138 |
|