| 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 $custom_block_names = []; |
| 33 |
|
| 34 |
/** |
| 35 |
* Store all names of repeater blocks |
| 36 |
* |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
protected $repeater_block_names = []; |
| 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 |
// Custom admin columns. |
| 56 |
add_filter( 'manage_edit-boldblocks_block_columns', [ $this, 'add_boldblocks_block_custom_columns' ] ); |
| 57 |
add_action( 'manage_boldblocks_block_posts_custom_column', [ $this, 'manage_boldblocks_block_columns' ], 10, 2 ); |
| 58 |
|
| 59 |
// Load custom blocks. |
| 60 |
add_action( 'init', [ $this, 'load_custom_blocks' ], 5 ); |
| 61 |
|
| 62 |
// Register scritps. |
| 63 |
add_action( 'init', [ $this, 'register_scripts' ] ); |
| 64 |
|
| 65 |
// Allow creating patterns from custom blocks. |
| 66 |
add_filter( 'boldblocks_get_pattern_allowed_blocks', [ $this, 'allow_creating_patterns' ] ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Register custom post type |
| 71 |
* |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function register_custom_post_type() { |
| 75 |
// Custom block args. |
| 76 |
$labels = array( |
| 77 |
'name' => _x( 'Custom Blocks', 'post type general name', 'boldblocks' ), |
| 78 |
'singular_name' => _x( 'Custom Block', 'post type singular name', 'boldblocks' ), |
| 79 |
'menu_name' => _x( 'Custom Blocks', 'admin menu', 'boldblocks' ), |
| 80 |
'name_admin_bar' => _x( 'Custom Blocks', 'add new on admin bar', 'boldblocks' ), |
| 81 |
'add_new' => _x( 'Add New', 'item', 'boldblocks' ), |
| 82 |
'add_new_item' => __( 'Add New Custom Block', 'boldblocks' ), |
| 83 |
'new_item' => __( 'New Custom Block', 'boldblocks' ), |
| 84 |
'edit_item' => __( 'Edit Custom Block', 'boldblocks' ), |
| 85 |
'view_item' => __( 'View Custom Block', 'boldblocks' ), |
| 86 |
'all_items' => __( 'All Custom Blocks', 'boldblocks' ), |
| 87 |
'search_items' => __( 'Search Custom Blocks', 'boldblocks' ), |
| 88 |
'parent_item_colon' => __( 'Parent Custom Block:', 'boldblocks' ), |
| 89 |
'not_found' => __( 'No items found.', 'boldblocks' ), |
| 90 |
'not_found_in_trash' => __( 'No items found in trash.', 'boldblocks' ), |
| 91 |
); |
| 92 |
|
| 93 |
$args = array( |
| 94 |
'labels' => $labels, |
| 95 |
'description' => __( 'All custom blocks', 'boldblocks' ), |
| 96 |
'public' => true, |
| 97 |
'publicly_queryable' => false, |
| 98 |
'exclude_from_search' => true, |
| 99 |
'show_ui' => true, |
| 100 |
'show_in_menu' => true, |
| 101 |
'show_in_nav_menus' => false, |
| 102 |
'show_in_admin_bar' => true, |
| 103 |
'show_in_rest' => true, |
| 104 |
'query_var' => false, |
| 105 |
'rewrite' => false, |
| 106 |
'capability_type' => 'post', |
| 107 |
'has_archive' => false, |
| 108 |
'hierarchical' => false, |
| 109 |
'menu_position' => 5, |
| 110 |
'supports' => array( 'title', 'editor', 'author', 'custom-fields', 'revisions' ), |
| 111 |
'can_export' => true, |
| 112 |
'template_lock' => 'insert', |
| 113 |
); |
| 114 |
|
| 115 |
// Register post type. |
| 116 |
register_post_type( 'boldblocks_block', $args ); |
| 117 |
|
| 118 |
// Meta fields. |
| 119 |
register_meta( |
| 120 |
'post', |
| 121 |
'boldblocks_block_blocks', |
| 122 |
array( |
| 123 |
'object_subtype' => 'boldblocks_block', |
| 124 |
'show_in_rest' => true, |
| 125 |
'type' => 'string', |
| 126 |
'default' => '{}', |
| 127 |
'single' => true, |
| 128 |
) |
| 129 |
); |
| 130 |
|
| 131 |
register_meta( |
| 132 |
'post', |
| 133 |
'boldblocks_block_class', |
| 134 |
array( |
| 135 |
'object_subtype' => 'boldblocks_block', |
| 136 |
'show_in_rest' => true, |
| 137 |
'type' => 'string', |
| 138 |
'default' => '', |
| 139 |
'single' => true, |
| 140 |
) |
| 141 |
); |
| 142 |
|
| 143 |
register_meta( |
| 144 |
'post', |
| 145 |
'boldblocks_block_description', |
| 146 |
array( |
| 147 |
'object_subtype' => 'boldblocks_block', |
| 148 |
'show_in_rest' => true, |
| 149 |
'type' => 'string', |
| 150 |
'default' => '', |
| 151 |
'single' => true, |
| 152 |
) |
| 153 |
); |
| 154 |
|
| 155 |
register_meta( |
| 156 |
'post', |
| 157 |
'boldblocks_block_icon', |
| 158 |
array( |
| 159 |
'object_subtype' => 'boldblocks_block', |
| 160 |
'show_in_rest' => true, |
| 161 |
'type' => 'string', |
| 162 |
'default' => '', |
| 163 |
'single' => true, |
| 164 |
) |
| 165 |
); |
| 166 |
|
| 167 |
register_meta( |
| 168 |
'post', |
| 169 |
'boldblocks_template_lock', |
| 170 |
array( |
| 171 |
'object_subtype' => 'boldblocks_block', |
| 172 |
'show_in_rest' => true, |
| 173 |
'type' => 'string', |
| 174 |
'default' => 'all', |
| 175 |
'single' => true, |
| 176 |
) |
| 177 |
); |
| 178 |
|
| 179 |
register_meta( |
| 180 |
'post', |
| 181 |
'boldblocks_enable_variation_picker', |
| 182 |
array( |
| 183 |
'object_subtype' => 'boldblocks_block', |
| 184 |
'show_in_rest' => true, |
| 185 |
'type' => 'boolean', |
| 186 |
'default' => false, |
| 187 |
'single' => true, |
| 188 |
) |
| 189 |
); |
| 190 |
|
| 191 |
register_meta( |
| 192 |
'post', |
| 193 |
'boldblocks_enable_repeater', |
| 194 |
array( |
| 195 |
'object_subtype' => 'boldblocks_block', |
| 196 |
'show_in_rest' => true, |
| 197 |
'type' => 'boolean', |
| 198 |
'default' => false, |
| 199 |
'single' => true, |
| 200 |
) |
| 201 |
); |
| 202 |
|
| 203 |
register_meta( |
| 204 |
'post', |
| 205 |
'boldblocks_parent_layout_type', |
| 206 |
array( |
| 207 |
'object_subtype' => 'boldblocks_block', |
| 208 |
'show_in_rest' => true, |
| 209 |
'type' => 'string', |
| 210 |
'default' => 'vstack', |
| 211 |
'single' => true, |
| 212 |
) |
| 213 |
); |
| 214 |
|
| 215 |
register_meta( |
| 216 |
'post', |
| 217 |
'boldblocks_parent_block_icon', |
| 218 |
array( |
| 219 |
'objeparentpe' => 'boldblocks_block', |
| 220 |
'show_in_rest' => true, |
| 221 |
'type' => 'string', |
| 222 |
'default' => '', |
| 223 |
'single' => true, |
| 224 |
) |
| 225 |
); |
| 226 |
|
| 227 |
register_meta( |
| 228 |
'post', |
| 229 |
'boldblocks_parent_enable_variation_picker', |
| 230 |
array( |
| 231 |
'object_subtype' => 'boldblocks_block', |
| 232 |
'show_in_rest' => true, |
| 233 |
'type' => 'boolean', |
| 234 |
'default' => false, |
| 235 |
'single' => true, |
| 236 |
) |
| 237 |
); |
| 238 |
|
| 239 |
register_meta( |
| 240 |
'post', |
| 241 |
'boldblocks_disable_standalone', |
| 242 |
array( |
| 243 |
'object_subtype' => 'boldblocks_block', |
| 244 |
'show_in_rest' => true, |
| 245 |
'type' => 'boolean', |
| 246 |
'default' => false, |
| 247 |
'single' => true, |
| 248 |
) |
| 249 |
); |
| 250 |
|
| 251 |
register_meta( |
| 252 |
'post', |
| 253 |
'boldblocks_is_fixed_nested_item_count', |
| 254 |
array( |
| 255 |
'object_subtype' => 'boldblocks_block', |
| 256 |
'show_in_rest' => true, |
| 257 |
'type' => 'boolean', |
| 258 |
'default' => false, |
| 259 |
'single' => true, |
| 260 |
) |
| 261 |
); |
| 262 |
|
| 263 |
register_meta( |
| 264 |
'post', |
| 265 |
'boldblocks_nested_item_count', |
| 266 |
array( |
| 267 |
'object_subtype' => 'boldblocks_block', |
| 268 |
'show_in_rest' => true, |
| 269 |
'type' => 'integer', |
| 270 |
'default' => 1, |
| 271 |
'single' => true, |
| 272 |
) |
| 273 |
); |
| 274 |
|
| 275 |
register_meta( |
| 276 |
'post', |
| 277 |
'boldblocks_parent_block_name', |
| 278 |
array( |
| 279 |
'object_subtype' => 'boldblocks_block', |
| 280 |
'show_in_rest' => true, |
| 281 |
'type' => 'string', |
| 282 |
'default' => '', |
| 283 |
'single' => true, |
| 284 |
) |
| 285 |
); |
| 286 |
|
| 287 |
register_meta( |
| 288 |
'post', |
| 289 |
'boldblocks_parent_block_title', |
| 290 |
array( |
| 291 |
'object_subtype' => 'boldblocks_block', |
| 292 |
'show_in_rest' => true, |
| 293 |
'type' => 'string', |
| 294 |
'default' => '', |
| 295 |
'single' => true, |
| 296 |
) |
| 297 |
); |
| 298 |
|
| 299 |
register_meta( |
| 300 |
'post', |
| 301 |
'boldblocks_parent_block_description', |
| 302 |
array( |
| 303 |
'object_subtype' => 'boldblocks_block', |
| 304 |
'show_in_rest' => true, |
| 305 |
'type' => 'string', |
| 306 |
'default' => '', |
| 307 |
'single' => true, |
| 308 |
) |
| 309 |
); |
| 310 |
|
| 311 |
register_meta( |
| 312 |
'post', |
| 313 |
'boldblocks_parent_block_class', |
| 314 |
array( |
| 315 |
'object_subtype' => 'boldblocks_block', |
| 316 |
'show_in_rest' => true, |
| 317 |
'type' => 'string', |
| 318 |
'default' => '', |
| 319 |
'single' => true, |
| 320 |
) |
| 321 |
); |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Load all custom blocks |
| 326 |
* |
| 327 |
* @return void |
| 328 |
*/ |
| 329 |
public function load_custom_blocks() { |
| 330 |
// Load all custom blocks. |
| 331 |
$this->all_custom_blocks = $this->get_all_custom_blocks(); |
| 332 |
|
| 333 |
// Build an array of custom block name. |
| 334 |
$this->custom_block_names = array_map( |
| 335 |
function ( $block_args ) { |
| 336 |
return $block_args['name']; |
| 337 |
}, |
| 338 |
$this->all_custom_blocks |
| 339 |
); |
| 340 |
|
| 341 |
// Build an array of repeater block name. |
| 342 |
$this->repeater_block_names = array_filter( |
| 343 |
array_map( |
| 344 |
function ( $block_arg ) { |
| 345 |
return isset( $block_arg['parent'] ) ? $block_arg['parent']['name'] : false; |
| 346 |
}, |
| 347 |
$this->all_custom_blocks |
| 348 |
) |
| 349 |
); |
| 350 |
|
| 351 |
// Register all blocks. |
| 352 |
foreach ( array_merge( $this->custom_block_names, $this->repeater_block_names ) as $name ) { |
| 353 |
register_block_type( |
| 354 |
$name, |
| 355 |
[ |
| 356 |
'editor_script' => 'boldblocks-custom-blocks', |
| 357 |
'style' => 'boldblocks-custom-blocks', |
| 358 |
'editor_style' => 'boldblocks-custom-blocks', |
| 359 |
] |
| 360 |
); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Register scripts |
| 366 |
* |
| 367 |
* @return void |
| 368 |
*/ |
| 369 |
public function register_scripts() { |
| 370 |
// Get the plugin instance. |
| 371 |
$boldblocks_cbb = boldblocks_cbb_get_instance(); |
| 372 |
|
| 373 |
// Custom blocks access file. |
| 374 |
$custom_blocks_asset = $boldblocks_cbb->include_file( 'build/custom-blocks.asset.php' ); |
| 375 |
|
| 376 |
// Scripts. |
| 377 |
wp_register_script( |
| 378 |
'boldblocks-custom-blocks', |
| 379 |
$boldblocks_cbb->get_file_uri( 'build/custom-blocks.js' ), |
| 380 |
$custom_blocks_asset['dependencies'] ?? [], |
| 381 |
BOLDBLOCKS_CBB_VERSION, |
| 382 |
true |
| 383 |
); |
| 384 |
|
| 385 |
// Define localize sripts. |
| 386 |
$blocks_scripts = [ |
| 387 |
'blocks' => $this->all_custom_blocks, |
| 388 |
'allowedBlocks' => $this->get_custom_blocks_allowed_blocks(), |
| 389 |
]; |
| 390 |
|
| 391 |
// Load all custom blocks for registration. |
| 392 |
wp_localize_script( |
| 393 |
'boldblocks-custom-blocks', |
| 394 |
'BoldblocksBlocks', |
| 395 |
$blocks_scripts |
| 396 |
); |
| 397 |
|
| 398 |
// Styles. |
| 399 |
wp_register_style( |
| 400 |
'boldblocks-custom-blocks', |
| 401 |
$boldblocks_cbb->get_file_uri( 'build/custom-blocks.css' ), |
| 402 |
[], |
| 403 |
BOLDBLOCKS_CBB_VERSION |
| 404 |
); |
| 405 |
|
| 406 |
wp_set_script_translations( 'boldblocks-custom-blocks', 'boldblocks' ); |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* Get all custom blocks. |
| 411 |
* |
| 412 |
* @return array |
| 413 |
*/ |
| 414 |
public function get_all_custom_blocks() { |
| 415 |
$block_posts = get_posts( |
| 416 |
[ |
| 417 |
'post_type' => 'boldblocks_block', |
| 418 |
'posts_per_page' => -1, |
| 419 |
'post_status' => 'publish', |
| 420 |
] |
| 421 |
); |
| 422 |
|
| 423 |
return array_map( |
| 424 |
function( $item ) { |
| 425 |
// Template lock. |
| 426 |
$template_lock = get_post_meta( $item->ID, 'boldblocks_template_lock', true ); |
| 427 |
// Convert boolean string to boolean. |
| 428 |
$template_lock = 'false' === $template_lock ? false : $template_lock; |
| 429 |
|
| 430 |
$block_args = [ |
| 431 |
'name' => sprintf( 'boldblocks/%s', sanitize_key( $item->post_name ) ), |
| 432 |
'title' => wp_strip_all_tags( $item->post_title ), |
| 433 |
'postContent' => $item->post_content, |
| 434 |
'blocks' => get_post_meta( $item->ID, 'boldblocks_block_blocks', true ), |
| 435 |
'description' => get_post_meta( $item->ID, 'boldblocks_block_description', true ), |
| 436 |
'templateLock' => $template_lock, |
| 437 |
'className' => get_post_meta( $item->ID, 'boldblocks_block_class', true ), |
| 438 |
'blockIcon' => get_post_meta( $item->ID, 'boldblocks_block_icon', true ), |
| 439 |
'enableVariationPicker' => ! ! get_post_meta( $item->ID, 'boldblocks_enable_variation_picker', true ), |
| 440 |
]; |
| 441 |
|
| 442 |
// Get parent block parameters. |
| 443 |
$enable_repeater = get_post_meta( $item->ID, 'boldblocks_enable_repeater', true ); |
| 444 |
if ( $enable_repeater ) { |
| 445 |
$parent_name = get_post_meta( $item->ID, 'boldblocks_parent_block_name', true ); |
| 446 |
if ( ! $parent_name ) { |
| 447 |
$parent_name = $item->post_name; |
| 448 |
} |
| 449 |
|
| 450 |
$parent_layout_type = get_post_meta( $item->ID, 'boldblocks_parent_layout_type', true ); |
| 451 |
|
| 452 |
$parent_title = get_post_meta( $item->ID, 'boldblocks_parent_block_title', true ); |
| 453 |
if ( ! $parent_title ) { |
| 454 |
// translators: Repeater block title. |
| 455 |
$parent_title = sprintf( __( 'Repeater: %s', 'boldblocks' ), $item->post_title ); |
| 456 |
} |
| 457 |
|
| 458 |
$disable_standalone = get_post_meta( $item->ID, 'boldblocks_disable_standalone', true ); |
| 459 |
|
| 460 |
$parent_args = [ |
| 461 |
'name' => sprintf( 'boldblocks/%s-repeater', sanitize_key( $parent_name ) ), |
| 462 |
'title' => wp_strip_all_tags( $parent_title ), |
| 463 |
'description' => get_post_meta( $item->ID, 'boldblocks_parent_block_description', true ), |
| 464 |
'className' => get_post_meta( $item->ID, 'boldblocks_parent_block_class', true ) ?? '', |
| 465 |
'blockIcon' => get_post_meta( $item->ID, 'boldblocks_parent_block_icon', true ), |
| 466 |
'enableVariationPicker' => ! ! get_post_meta( $item->ID, 'boldblocks_parent_enable_variation_picker', true ), |
| 467 |
]; |
| 468 |
|
| 469 |
$is_fixed_nested_item_count = get_post_meta( $item->ID, 'boldblocks_is_fixed_nested_item_count', true ); |
| 470 |
if ( $is_fixed_nested_item_count ) { |
| 471 |
$nested_item_count = get_post_meta( $item->ID, 'boldblocks_nested_item_count', true ); |
| 472 |
$nested_item_count = $nested_item_count ? absint( $nested_item_count ) : 1; |
| 473 |
$parent_args['nestedItemCount'] = $nested_item_count ? $nested_item_count : 1; |
| 474 |
} |
| 475 |
|
| 476 |
$parent_args['layoutType'] = $parent_layout_type; |
| 477 |
|
| 478 |
$block_args['standalone'] = ! $disable_standalone; |
| 479 |
$block_args['parent'] = $parent_args; |
| 480 |
} |
| 481 |
|
| 482 |
return $block_args; |
| 483 |
}, |
| 484 |
$block_posts |
| 485 |
); |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* Add custom blocks to the list of blocks that can create patterns from. |
| 490 |
* |
| 491 |
* @param array $allowed_blocks |
| 492 |
* @return array |
| 493 |
*/ |
| 494 |
public function allow_creating_patterns( $allowed_blocks ) { |
| 495 |
return array_merge( $allowed_blocks, $this->custom_block_names, $this->repeater_block_names ); |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
* Get list of blocks that allow creating custom blocks from toolbar menu. |
| 500 |
*/ |
| 501 |
public function get_custom_blocks_allowed_blocks() { |
| 502 |
return apply_filters( |
| 503 |
'boldblocks/get_custom_block_allowed_blocks', |
| 504 |
[ |
| 505 |
'core/group', |
| 506 |
'core/cover', |
| 507 |
'core/columns', |
| 508 |
] |
| 509 |
); |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Add custom columns to custom post type |
| 514 |
* |
| 515 |
* @param array $columns The array of columns. |
| 516 |
* @return array |
| 517 |
*/ |
| 518 |
public function add_boldblocks_block_custom_columns( $columns ) { |
| 519 |
$custom_columns = array( |
| 520 |
'block_name' => esc_html__( 'Block name', 'boldblocks' ), |
| 521 |
'parent_block_name' => esc_html__( 'Parent block name', 'boldblocks' ), |
| 522 |
); |
| 523 |
|
| 524 |
// Add after title column. |
| 525 |
$return = array_slice( $columns, 0, 2, true ) + $custom_columns + array_slice( $columns, 2, count( $columns ) - 2, true ); |
| 526 |
|
| 527 |
return $return; |
| 528 |
} |
| 529 |
|
| 530 |
/** |
| 531 |
* Manage custom columns. |
| 532 |
* |
| 533 |
* @param string $column the column name. |
| 534 |
* @param int $post_id the post id. |
| 535 |
*/ |
| 536 |
public function manage_boldblocks_block_columns( $column, $post_id ) { |
| 537 |
switch ( $column ) { |
| 538 |
case 'block_name': |
| 539 |
echo 'boldblocks/' . get_post_field( 'post_name', $post_id ); |
| 540 |
break; |
| 541 |
|
| 542 |
case 'parent_block_name': |
| 543 |
$enable_repeater = get_post_meta( $post_id, 'boldblocks_enable_repeater', true ); |
| 544 |
if ( $enable_repeater ) { |
| 545 |
echo 'boldblocks/' . get_post_field( 'post_name', $post_id ) . '-repeater'; |
| 546 |
} else { |
| 547 |
echo 'NA'; |
| 548 |
} |
| 549 |
break; |
| 550 |
|
| 551 |
// Just break out of the switch statement for everything else. |
| 552 |
default: |
| 553 |
break; |
| 554 |
} |
| 555 |
} |
| 556 |
} |
| 557 |
endif; |
| 558 |
|