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