# content-blocks-builder/1.0.2/includes/custom-blocks.php

Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts, version 1.0.2. 393 lines.

- Page: https://pluginprobe.com/plugins/content-blocks-builder/1.0.2/code/includes/custom-blocks.php
- Raw: https://pluginprobe.com/plugins/content-blocks-builder/1.0.2/raw/includes/custom-blocks.php
- Modified: 2021-10-13T00:15:00+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/content-blocks-builder/1.0.2/code/includes/custom-blocks.php#L10-L20`.

```php
<?php
/**
 * Register custom blocks
 *
 * @package    BoldBlocks
 * @author     Phi Phan <mrphipv@gmail.com>
 * @copyright  Copyright (c) 2021, Phi Phan
 */

namespace BoldBlocks;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'CustomBlocks' ) ) :
	/**
	 * Create/edit custom content blocks.
	 */
	class CustomBlocks {
		/**
		 * Store all custom blocks
		 *
		 * @var array
		 */
		protected $all_custom_blocks = [];

		/**
		 * Store all names of custom blocks
		 *
		 * @var array
		 */
		protected $all_custom_block_names = [];

		/**
		 * Store all repeater blocks
		 *
		 * @var array
		 */
		protected $repeater_blocks = [];

		/**
		 * A dummy constructor
		 */
		public function __construct() {}

		/**
		 * Run main hooks
		 *
		 * @return void
		 */
		public function run() {
			// Register custom post type.
			add_action( 'init', [ $this, 'register_custom_post_type' ], 5 );

			// Load custom blocks.
			add_action( 'init', [ $this, 'load_custom_blocks' ], 5 );

			// Load data for js.
			add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] );
		}

		/**
		 * Register custom post type
		 *
		 * @return void
		 */
		public function register_custom_post_type() {
			// Custom block args.
			$labels = array(
				'name'               => _x( 'Custom Blocks', 'post type general name', 'boldblocks' ),
				'singular_name'      => _x( 'Custom Block', 'post type singular name', 'boldblocks' ),
				'menu_name'          => _x( 'Custom Blocks', 'admin menu', 'boldblocks' ),
				'name_admin_bar'     => _x( 'Custom Blocks', 'add new on admin bar', 'boldblocks' ),
				'add_new'            => _x( 'Add New', 'item', 'boldblocks' ),
				'add_new_item'       => __( 'Add New Custom Block', 'boldblocks' ),
				'new_item'           => __( 'New Custom Block', 'boldblocks' ),
				'edit_item'          => __( 'Edit Custom Block', 'boldblocks' ),
				'view_item'          => __( 'View Custom Block', 'boldblocks' ),
				'all_items'          => __( 'All Custom Blocks', 'boldblocks' ),
				'search_items'       => __( 'Search Custom Blocks', 'boldblocks' ),
				'parent_item_colon'  => __( 'Parent Custom Block:', 'boldblocks' ),
				'not_found'          => __( 'No items found.', 'boldblocks' ),
				'not_found_in_trash' => __( 'No items found in trash.', 'boldblocks' ),
			);

			$args = array(
				'labels'              => $labels,
				'description'         => __( 'All custom blocks', 'boldblocks' ),
				'public'              => true,
				'publicly_queryable'  => false,
				'exclude_from_search' => true,
				'show_ui'             => true,
				'show_in_menu'        => true,
				'show_in_nav_menus'   => false,
				'show_in_admin_bar'   => true,
				'show_in_rest'        => true,
				'query_var'           => false,
				'rewrite'             => false,
				'capability_type'     => 'post',
				'has_archive'         => false,
				'hierarchical'        => false,
				'menu_position'       => 5,
				'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'custom-fields', 'revisions' ),
				'can_export'          => true,
				'template_lock'       => 'insert',
			);

			// Register post type.
			register_post_type( 'boldblocks_block', $args );

			// Meta fields.
			register_meta(
				'post',
				'boldblocks_block_blocks',
				array(
					'object_subtype' => 'boldblocks_block',
					'show_in_rest'   => true,
					'type'           => 'string',
					'default'        => '{}',
					'single'         => true,
				)
			);

			register_meta(
				'post',
				'boldblocks_block_class',
				array(
					'object_subtype' => 'boldblocks_block',
					'show_in_rest'   => true,
					'type'           => 'string',
					'default'        => '',
					'single'         => true,
				)
			);

			register_meta(
				'post',
				'boldblocks_template_lock',
				array(
					'object_subtype' => 'boldblocks_block',
					'show_in_rest'   => true,
					'type'           => 'string',
					'default'        => 'all',
					'single'         => true,
				)
			);

			register_meta(
				'post',
				'boldblocks_enable_repeater',
				array(
					'object_subtype' => 'boldblocks_block',
					'show_in_rest'   => true,
					'type'           => 'boolean',
					'default'        => false,
					'single'         => true,
				)
			);

			register_meta(
				'post',
				'boldblocks_disable_standalone',
				array(
					'object_subtype' => 'boldblocks_block',
					'show_in_rest'   => true,
					'type'           => 'boolean',
					'default'        => false,
					'single'         => true,
				)
			);

			register_meta(
				'post',
				'boldblocks_is_fixed_nested_item_count',
				array(
					'object_subtype' => 'boldblocks_block',
					'show_in_rest'   => true,
					'type'           => 'boolean',
					'default'        => false,
					'single'         => true,
				)
			);

			register_meta(
				'post',
				'boldblocks_nested_item_count',
				array(
					'object_subtype' => 'boldblocks_block',
					'show_in_rest'   => true,
					'type'           => 'integer',
					'default'        => 1,
					'single'         => true,
				)
			);

			register_meta(
				'post',
				'boldblocks_parent_block_name',
				array(
					'object_subtype' => 'boldblocks_block',
					'show_in_rest'   => true,
					'type'           => 'string',
					'default'        => '',
					'single'         => true,
				)
			);

			register_meta(
				'post',
				'boldblocks_parent_block_title',
				array(
					'object_subtype' => 'boldblocks_block',
					'show_in_rest'   => true,
					'type'           => 'string',
					'default'        => '',
					'single'         => true,
				)
			);

			register_meta(
				'post',
				'boldblocks_parent_block_description',
				array(
					'object_subtype' => 'boldblocks_block',
					'show_in_rest'   => true,
					'type'           => 'string',
					'default'        => '',
					'single'         => true,
				)
			);

			register_meta(
				'post',
				'boldblocks_parent_block_class',
				array(
					'object_subtype' => 'boldblocks_block',
					'show_in_rest'   => true,
					'type'           => 'string',
					'default'        => '',
					'single'         => true,
				)
			);
		}

		/**
		 * Load all custom blocks
		 *
		 * @return void
		 */
		public function load_custom_blocks() {
			// Load all custom blocks.
			$this->all_custom_blocks = $this->get_all_custom_blocks();

			// Build an array of custom block name.
			$this->all_custom_block_names = array_map(
				function ( $block_args ) {
					return [ 'name' => $block_args['name'] ];
				},
				$this->all_custom_blocks
			);

			// Retrieve all repeater blocks.
			$this->repeater_blocks = array_filter(
				array_map(
					function ( $block_arg ) {
						return isset( $block_arg['parent'] ) ? [ 'name' => $block_arg['parent']['name'] ] : false;
					},
					$this->all_custom_blocks
				)
			);
		}

		/**
		 * Enqueue editor assets
		 *
		 * @return void
		 */
		public function enqueue_block_editor_assets() {
			global $boldblocks_cbb;

			// Custom blocks access file.
			$custom_blocks_asset = $boldblocks_cbb->include_file( 'build/custom-blocks.asset.php' );

			// Scripts.
			wp_enqueue_script(
				'boldblocks-custom-blocks',
				BOLDBLOCKS_CBB_URL . '/build/custom-blocks.js',
				$custom_blocks_asset['dependencies'],
				$custom_blocks_asset['version'],
				true
			);

			// Load all custom blocks for registration.
			wp_localize_script(
				'boldblocks-custom-blocks',
				'BoldblocksBlocks',
				[
					'blocks'        => $this->all_custom_blocks,
					'allowedBlocks' => $this->get_custom_blocks_allowed_blocks(),
				]
			);
		}

		/**
		 * Get all custom blocks
		 *
		 * @return array
		 */
		public function get_all_custom_blocks() {
			$block_posts = get_posts(
				[
					'post_type'      => 'boldblocks_block',
					'posts_per_page' => -1,
					'post_status'    => 'publish',
				]
			);

			return array_map(
				function( $item ) {
					$template_lock = get_post_meta( $item->ID, 'boldblocks_template_lock', true );
					// Convert boolean string to boolean.
					$template_lock = 'false' === $template_lock ? false : $template_lock;
					$block_args    = [
						'name'         => sprintf( 'boldblocks/%s', sanitize_key( $item->post_name ) ),
						'title'        => wp_strip_all_tags( $item->post_title ),
						'postContent'  => $item->post_content,
						'blocks'       => get_post_meta( $item->ID, 'boldblocks_block_blocks', true ),
						'description'  => $item->post_excerpt,
						'templateLock' => $template_lock,
						'className'    => get_post_meta( $item->ID, 'boldblocks_block_class', true ),
					];

					// Get parent block parameters.
					$enable_repeater = get_post_meta( $item->ID, 'boldblocks_enable_repeater', true );
					if ( $enable_repeater ) {
						$parent_name = get_post_meta( $item->ID, 'boldblocks_parent_block_name', true );
						if ( ! $parent_name ) {
							$parent_name = $item->post_name;
						}

						$parent_title = get_post_meta( $item->ID, 'boldblocks_parent_block_title', true );
						if ( ! $parent_title ) {
							// translators: Repeater block title.
							$parent_title = sprintf( __( 'Repeater: %s', 'boldblocks' ), $item->post_title );
						}

						$parent_description = get_post_meta( $item->ID, 'boldblocks_parent_block_description', true );
						if ( ! $parent_description ) {
							$parent_description = $item->post_excerpt;
						}

						$disable_standalone = get_post_meta( $item->ID, 'boldblocks_disable_standalone', true );

						$parent_args = [
							'name'        => sprintf( 'boldblocks/%s-repeater', sanitize_key( $parent_name ) ),
							'title'       => wp_strip_all_tags( $parent_title ),
							'description' => $parent_description,
							'className'   => get_post_meta( $item->ID, 'boldblocks_parent_block_class', true ) ?? '',
						];

						$is_fixed_nested_item_count = get_post_meta( $item->ID, 'boldblocks_is_fixed_nested_item_count', true );
						if ( $is_fixed_nested_item_count ) {
							$nested_item_count              = get_post_meta( $item->ID, 'boldblocks_nested_item_count', true );
							$nested_item_count              = $nested_item_count ? absint( $nested_item_count ) : 1;
							$parent_args['nestedItemCount'] = $nested_item_count ? $nested_item_count : 1;
						}

						$block_args['standalone'] = ! $disable_standalone;
						$block_args['parent']     = $parent_args;
					}

					return $block_args;
				},
				$block_posts
			);
		}

		/**
		 * Get list of blocks that allow creating custom blocks from toolbar menu.
		 */
		public function get_custom_blocks_allowed_blocks() {
			return apply_filters(
				'boldblocks/get_custom_block_allowed_blocks',
				[
					'core/group',
					'core/cover',
					'core/columns',
				]
			);
		}
	}
endif;

```
