| 1 |
<?php |
| 2 |
/** |
| 3 |
* Register custom blocks |
| 4 |
* |
| 5 |
* @package BoldBlocks |
| 6 |
* @author Phi Phan <mrphipv@gmail.com> |
| 7 |
* @copyright Copyright (c) 2021, Phi Phan |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace BoldBlocks; |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
if ( ! class_exists( 'CustomBlocks' ) ) : |
| 16 |
/** |
| 17 |
* Create/edit custom content blocks. |
| 18 |
*/ |
| 19 |
class CustomBlocks { |
| 20 |
/** |
| 21 |
* Store all custom blocks |
| 22 |
* |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
protected $all_custom_blocks = []; |
| 26 |
|
| 27 |
/** |
| 28 |
* Store all names of custom blocks |
| 29 |
* |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
protected $all_custom_block_names = []; |
| 33 |
|
| 34 |
/** |
| 35 |
* Store all repeater blocks |
| 36 |
* |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
protected $repeater_blocks = []; |
| 40 |
|
| 41 |
/** |
| 42 |
* A dummy constructor |
| 43 |
*/ |
| 44 |
public function __construct() {} |
| 45 |
|
| 46 |
/** |
| 47 |
* Run main hooks |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function run() { |
| 52 |
// Register custom post type. |
| 53 |
add_action( 'init', [ $this, 'register_custom_post_type' ], 5 ); |
| 54 |
|
| 55 |
// Load custom blocks. |
| 56 |
add_action( 'init', [ $this, 'load_custom_blocks' ], 5 ); |
| 57 |
|
| 58 |
// Load data for js. |
| 59 |
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Register custom post type |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
public function register_custom_post_type() { |
| 68 |
// Custom block args. |
| 69 |
$labels = array( |
| 70 |
'name' => _x( 'Custom Blocks', 'post type general name', 'boldblocks' ), |
| 71 |
'singular_name' => _x( 'Custom Block', 'post type singular name', 'boldblocks' ), |
| 72 |
'menu_name' => _x( 'Custom Blocks', 'admin menu', 'boldblocks' ), |
| 73 |
'name_admin_bar' => _x( 'Custom Blocks', 'add new on admin bar', 'boldblocks' ), |
| 74 |
'add_new' => _x( 'Add New', 'item', 'boldblocks' ), |
| 75 |
'add_new_item' => __( 'Add New Custom Block', 'boldblocks' ), |
| 76 |
'new_item' => __( 'New Custom Block', 'boldblocks' ), |
| 77 |
'edit_item' => __( 'Edit Custom Block', 'boldblocks' ), |
| 78 |
'view_item' => __( 'View Custom Block', 'boldblocks' ), |
| 79 |
'all_items' => __( 'All Custom Blocks', 'boldblocks' ), |
| 80 |
'search_items' => __( 'Search Custom Blocks', 'boldblocks' ), |
| 81 |
'parent_item_colon' => __( 'Parent Custom Block:', 'boldblocks' ), |
| 82 |
'not_found' => __( 'No items found.', 'boldblocks' ), |
| 83 |
'not_found_in_trash' => __( 'No items found in trash.', 'boldblocks' ), |
| 84 |
); |
| 85 |
|
| 86 |
$args = array( |
| 87 |
'labels' => $labels, |
| 88 |
'description' => __( 'All custom blocks', 'boldblocks' ), |
| 89 |
'public' => true, |
| 90 |
'publicly_queryable' => false, |
| 91 |
'exclude_from_search' => true, |
| 92 |
'show_ui' => true, |
| 93 |
'show_in_menu' => true, |
| 94 |
'show_in_nav_menus' => false, |
| 95 |
'show_in_admin_bar' => true, |
| 96 |
'show_in_rest' => true, |
| 97 |
'query_var' => false, |
| 98 |
'rewrite' => false, |
| 99 |
'capability_type' => 'post', |
| 100 |
'has_archive' => false, |
| 101 |
'hierarchical' => false, |
| 102 |
'menu_position' => 5, |
| 103 |
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'custom-fields', 'revisions' ), |
| 104 |
'can_export' => true, |
| 105 |
'template_lock' => 'insert', |
| 106 |
); |
| 107 |
|
| 108 |
// Register post type. |
| 109 |
register_post_type( 'boldblocks_block', $args ); |
| 110 |
|
| 111 |
// Meta fields. |
| 112 |
register_meta( |
| 113 |
'post', |
| 114 |
'boldblocks_block_blocks', |
| 115 |
array( |
| 116 |
'object_subtype' => 'boldblocks_block', |
| 117 |
'show_in_rest' => true, |
| 118 |
'type' => 'string', |
| 119 |
'default' => '{}', |
| 120 |
'single' => true, |
| 121 |
) |
| 122 |
); |
| 123 |
|
| 124 |
register_meta( |
| 125 |
'post', |
| 126 |
'boldblocks_block_class', |
| 127 |
array( |
| 128 |
'object_subtype' => 'boldblocks_block', |
| 129 |
'show_in_rest' => true, |
| 130 |
'type' => 'string', |
| 131 |
'default' => '', |
| 132 |
'single' => true, |
| 133 |
) |
| 134 |
); |
| 135 |
|
| 136 |
register_meta( |
| 137 |
'post', |
| 138 |
'boldblocks_template_lock', |
| 139 |
array( |
| 140 |
'object_subtype' => 'boldblocks_block', |
| 141 |
'show_in_rest' => true, |
| 142 |
'type' => 'string', |
| 143 |
'default' => 'all', |
| 144 |
'single' => true, |
| 145 |
) |
| 146 |
); |
| 147 |
|
| 148 |
register_meta( |
| 149 |
'post', |
| 150 |
'boldblocks_enable_repeater', |
| 151 |
array( |
| 152 |
'object_subtype' => 'boldblocks_block', |
| 153 |
'show_in_rest' => true, |
| 154 |
'type' => 'boolean', |
| 155 |
'default' => false, |
| 156 |
'single' => true, |
| 157 |
) |
| 158 |
); |
| 159 |
|
| 160 |
register_meta( |
| 161 |
'post', |
| 162 |
'boldblocks_disable_standalone', |
| 163 |
array( |
| 164 |
'object_subtype' => 'boldblocks_block', |
| 165 |
'show_in_rest' => true, |
| 166 |
'type' => 'boolean', |
| 167 |
'default' => false, |
| 168 |
'single' => true, |
| 169 |
) |
| 170 |
); |
| 171 |
|
| 172 |
register_meta( |
| 173 |
'post', |
| 174 |
'boldblocks_is_fixed_nested_item_count', |
| 175 |
array( |
| 176 |
'object_subtype' => 'boldblocks_block', |
| 177 |
'show_in_rest' => true, |
| 178 |
'type' => 'boolean', |
| 179 |
'default' => false, |
| 180 |
'single' => true, |
| 181 |
) |
| 182 |
); |
| 183 |
|
| 184 |
register_meta( |
| 185 |
'post', |
| 186 |
'boldblocks_nested_item_count', |
| 187 |
array( |
| 188 |
'object_subtype' => 'boldblocks_block', |
| 189 |
'show_in_rest' => true, |
| 190 |
'type' => 'integer', |
| 191 |
'default' => 1, |
| 192 |
'single' => true, |
| 193 |
) |
| 194 |
); |
| 195 |
|
| 196 |
register_meta( |
| 197 |
'post', |
| 198 |
'boldblocks_parent_block_name', |
| 199 |
array( |
| 200 |
'object_subtype' => 'boldblocks_block', |
| 201 |
'show_in_rest' => true, |
| 202 |
'type' => 'string', |
| 203 |
'default' => '', |
| 204 |
'single' => true, |
| 205 |
) |
| 206 |
); |
| 207 |
|
| 208 |
register_meta( |
| 209 |
'post', |
| 210 |
'boldblocks_parent_block_title', |
| 211 |
array( |
| 212 |
'object_subtype' => 'boldblocks_block', |
| 213 |
'show_in_rest' => true, |
| 214 |
'type' => 'string', |
| 215 |
'default' => '', |
| 216 |
'single' => true, |
| 217 |
) |
| 218 |
); |
| 219 |
|
| 220 |
register_meta( |
| 221 |
'post', |
| 222 |
'boldblocks_parent_block_description', |
| 223 |
array( |
| 224 |
'object_subtype' => 'boldblocks_block', |
| 225 |
'show_in_rest' => true, |
| 226 |
'type' => 'string', |
| 227 |
'default' => '', |
| 228 |
'single' => true, |
| 229 |
) |
| 230 |
); |
| 231 |
|
| 232 |
register_meta( |
| 233 |
'post', |
| 234 |
'boldblocks_parent_block_class', |
| 235 |
array( |
| 236 |
'object_subtype' => 'boldblocks_block', |
| 237 |
'show_in_rest' => true, |
| 238 |
'type' => 'string', |
| 239 |
'default' => '', |
| 240 |
'single' => true, |
| 241 |
) |
| 242 |
); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Load all custom blocks |
| 247 |
* |
| 248 |
* @return void |
| 249 |
*/ |
| 250 |
public function load_custom_blocks() { |
| 251 |
// Load all custom blocks. |
| 252 |
$this->all_custom_blocks = $this->get_all_custom_blocks(); |
| 253 |
|
| 254 |
// Build an array of custom block name. |
| 255 |
$this->all_custom_block_names = array_map( |
| 256 |
function ( $block_args ) { |
| 257 |
return [ 'name' => $block_args['name'] ]; |
| 258 |
}, |
| 259 |
$this->all_custom_blocks |
| 260 |
); |
| 261 |
|
| 262 |
// Retrieve all repeater blocks. |
| 263 |
$this->repeater_blocks = array_filter( |
| 264 |
array_map( |
| 265 |
function ( $block_arg ) { |
| 266 |
return isset( $block_arg['parent'] ) ? [ 'name' => $block_arg['parent']['name'] ] : false; |
| 267 |
}, |
| 268 |
$this->all_custom_blocks |
| 269 |
) |
| 270 |
); |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Enqueue editor assets |
| 275 |
* |
| 276 |
* @return void |
| 277 |
*/ |
| 278 |
public function enqueue_block_editor_assets() { |
| 279 |
global $boldblocks_cbb; |
| 280 |
|
| 281 |
// Custom blocks access file. |
| 282 |
$custom_blocks_asset = $boldblocks_cbb->include_file( 'build/custom-blocks.asset.php' ); |
| 283 |
|
| 284 |
// Scripts. |
| 285 |
wp_enqueue_script( |
| 286 |
'boldblocks-custom-blocks', |
| 287 |
BOLDBLOCKS_CBB_URL . '/build/custom-blocks.js', |
| 288 |
$custom_blocks_asset['dependencies'], |
| 289 |
$custom_blocks_asset['version'], |
| 290 |
true |
| 291 |
); |
| 292 |
|
| 293 |
// Load all custom blocks for registration. |
| 294 |
wp_localize_script( |
| 295 |
'boldblocks-custom-blocks', |
| 296 |
'BoldblocksBlocks', |
| 297 |
[ |
| 298 |
'blocks' => $this->all_custom_blocks, |
| 299 |
'allowedBlocks' => $this->get_custom_blocks_allowed_blocks(), |
| 300 |
] |
| 301 |
); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Get all custom blocks |
| 306 |
* |
| 307 |
* @return array |
| 308 |
*/ |
| 309 |
public function get_all_custom_blocks() { |
| 310 |
$block_posts = get_posts( |
| 311 |
[ |
| 312 |
'post_type' => 'boldblocks_block', |
| 313 |
'posts_per_page' => -1, |
| 314 |
'post_status' => 'publish', |
| 315 |
] |
| 316 |
); |
| 317 |
|
| 318 |
return array_map( |
| 319 |
function( $item ) { |
| 320 |
$template_lock = get_post_meta( $item->ID, 'boldblocks_template_lock', true ); |
| 321 |
// Convert boolean string to boolean. |
| 322 |
$template_lock = 'false' === $template_lock ? false : $template_lock; |
| 323 |
$block_args = [ |
| 324 |
'name' => sprintf( 'boldblocks/%s', sanitize_key( $item->post_name ) ), |
| 325 |
'title' => wp_strip_all_tags( $item->post_title ), |
| 326 |
'postContent' => $item->post_content, |
| 327 |
'blocks' => get_post_meta( $item->ID, 'boldblocks_block_blocks', true ), |
| 328 |
'description' => $item->post_excerpt, |
| 329 |
'templateLock' => $template_lock, |
| 330 |
'className' => get_post_meta( $item->ID, 'boldblocks_block_class', true ), |
| 331 |
]; |
| 332 |
|
| 333 |
// Get parent block parameters. |
| 334 |
$enable_repeater = get_post_meta( $item->ID, 'boldblocks_enable_repeater', true ); |
| 335 |
if ( $enable_repeater ) { |
| 336 |
$parent_name = get_post_meta( $item->ID, 'boldblocks_parent_block_name', true ); |
| 337 |
if ( ! $parent_name ) { |
| 338 |
$parent_name = $item->post_name; |
| 339 |
} |
| 340 |
|
| 341 |
$parent_title = get_post_meta( $item->ID, 'boldblocks_parent_block_title', true ); |
| 342 |
if ( ! $parent_title ) { |
| 343 |
// translators: Repeater block title. |
| 344 |
$parent_title = sprintf( __( 'Repeater: %s', 'boldblocks' ), $item->post_title ); |
| 345 |
} |
| 346 |
|
| 347 |
$parent_description = get_post_meta( $item->ID, 'boldblocks_parent_block_description', true ); |
| 348 |
if ( ! $parent_description ) { |
| 349 |
$parent_description = $item->post_excerpt; |
| 350 |
} |
| 351 |
|
| 352 |
$disable_standalone = get_post_meta( $item->ID, 'boldblocks_disable_standalone', true ); |
| 353 |
|
| 354 |
$parent_args = [ |
| 355 |
'name' => sprintf( 'boldblocks/%s-repeater', sanitize_key( $parent_name ) ), |
| 356 |
'title' => wp_strip_all_tags( $parent_title ), |
| 357 |
'description' => $parent_description, |
| 358 |
'className' => get_post_meta( $item->ID, 'boldblocks_parent_block_class', true ) ?? '', |
| 359 |
]; |
| 360 |
|
| 361 |
$is_fixed_nested_item_count = get_post_meta( $item->ID, 'boldblocks_is_fixed_nested_item_count', true ); |
| 362 |
if ( $is_fixed_nested_item_count ) { |
| 363 |
$nested_item_count = get_post_meta( $item->ID, 'boldblocks_nested_item_count', true ); |
| 364 |
$nested_item_count = $nested_item_count ? absint( $nested_item_count ) : 1; |
| 365 |
$parent_args['nestedItemCount'] = $nested_item_count ? $nested_item_count : 1; |
| 366 |
} |
| 367 |
|
| 368 |
$block_args['standalone'] = ! $disable_standalone; |
| 369 |
$block_args['parent'] = $parent_args; |
| 370 |
} |
| 371 |
|
| 372 |
return $block_args; |
| 373 |
}, |
| 374 |
$block_posts |
| 375 |
); |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Get list of blocks that allow creating custom blocks from toolbar menu. |
| 380 |
*/ |
| 381 |
public function get_custom_blocks_allowed_blocks() { |
| 382 |
return apply_filters( |
| 383 |
'boldblocks/get_custom_block_allowed_blocks', |
| 384 |
[ |
| 385 |
'core/group', |
| 386 |
'core/cover', |
| 387 |
'core/columns', |
| 388 |
] |
| 389 |
); |
| 390 |
} |
| 391 |
} |
| 392 |
endif; |
| 393 |
|