# content-blocks-builder/1.0.1/includes/patterns.php

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

- Page: https://pluginprobe.com/plugins/content-blocks-builder/1.0.1/code/includes/patterns.php
- Raw: https://pluginprobe.com/plugins/content-blocks-builder/1.0.1/raw/includes/patterns.php
- Modified: 2021-09-27T03:10:58+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.1/code/includes/patterns.php#L10-L20`.

```php
<?php
/**
 * Register custom pattern
 *
 * @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( 'Patterns' ) ) :
	/**
	 * Manage custom patterns.
	 */
	class Patterns {
		/**
		 * 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 );

			// Register custom taxonomy.
			add_action( 'init', [ $this, 'register_custom_taxonomy' ], 5 );

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

			// Register patterns.
			add_action( 'load-post.php', [ $this, 'register_patterns' ] );
			add_action( 'load-post-new.php', [ $this, 'register_patterns' ] );
		}

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

			$pattern_args = array(
				'labels'              => $pattern_labels,
				'description'         => __( 'All custom block patterns', '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,
			);

			// Register post type.
			register_post_type( 'boldblocks_pattern', $pattern_args );
		}

		/**
		 * Register custom taxonomy
		 *
		 * @return void
		 */
		public function register_custom_taxonomy() {
			// Pattern category.
			$labels = array(
				'name'                       => _x( 'Pattern Categories', 'Taxonomy General Name', 'boldblocks' ),
				'singular_name'              => _x( 'Pattern Category', 'Taxonomy Singular Name', 'boldblocks' ),
				'menu_name'                  => __( 'Pattern Categories', 'boldblocks' ),
				'all_items'                  => __( 'All Items', 'boldblocks' ),
				'parent_item'                => __( 'Parent Item', 'boldblocks' ),
				'parent_item_colon'          => __( 'Parent Item:', 'boldblocks' ),
				'new_item_name'              => __( 'New Item Name', 'boldblocks' ),
				'add_new_item'               => __( 'Add New Item', 'boldblocks' ),
				'edit_item'                  => __( 'Edit Item', 'boldblocks' ),
				'update_item'                => __( 'Update Item', 'boldblocks' ),
				'view_item'                  => __( 'View Item', 'boldblocks' ),
				'separate_items_with_commas' => __( 'Separate items with commas', 'boldblocks' ),
				'add_or_remove_items'        => __( 'Add or remove items', 'boldblocks' ),
				'choose_from_most_used'      => __( 'Choose from the most used', 'boldblocks' ),
				'popular_items'              => __( 'Popular Items', 'boldblocks' ),
				'search_items'               => __( 'Search Items', 'boldblocks' ),
				'not_found'                  => __( 'Not Found', 'boldblocks' ),
			);

			$args = array(
				'labels'            => $labels,
				'hierarchical'      => true,
				'public'            => true,
				'show_ui'           => true,
				'show_admin_column' => true,
				'show_in_nav_menus' => false,
				'show_tagcloud'     => false,
				'query_var'         => 'pattern-category',
				'rewrite'           => array(
					'slug' => 'pattern-category',
				),
				'show_in_rest'      => true,
			);

			register_taxonomy( 'boldblocks_pattern_category', array( 'boldblocks_pattern' ), $args );
		}

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

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

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

			// Load all patterns for registration.
			wp_localize_script(
				'boldblocks-patterns',
				'BoldblocksPatterns',
				[ 'allowedBlocks' => $this->get_pattern_allowed_blocks() ]
			);
		}

		/**
		 * Get the list of blocks that allow creating patterns from toolbar menu.
		 */
		public function get_pattern_allowed_blocks() {
			return apply_filters(
				'boldblocks_get_pattern_allowed_blocks',
				[
					'core/group',
					'core/cover',
					'core/columns',
				]
			);
		}

		/**
		 * Register all custom patterns
		 *
		 * @return void
		 */
		public function register_patterns() {
			// Register a custom category for patterns made by BoldBlocks.
			register_block_pattern_category( 'boldblocks', array( 'label' => _x( 'All patterns by BoldBlocks', 'Block pattern category' ) ) );

			// Query all custom taxonomies.
			$all_categories = get_terms( [ 'taxonomy' => 'boldblocks_pattern_category' ] );
			foreach ( $all_categories as $term ) {
				register_block_pattern_category( $term->slug, array( 'label' => $term->name ) );
			}

			// Query all patterns.
			$pattern_posts = get_posts(
				[
					'post_type'      => 'boldblocks_pattern',
					'posts_per_page' => -1,
				]
			);

			foreach ( $pattern_posts as $pattern_post ) {
				$blocks = parse_blocks( $pattern_post->post_content );
				if ( count( $blocks ) === 0 ) {
					continue;
				}

				$categories         = [ 'boldblocks' ];
				$pattern_categories = get_the_terms( $pattern_post->ID, 'boldblocks_pattern_category' );
				if ( is_array( $pattern_categories ) && count( $pattern_categories ) > 0 ) {
					foreach ( $pattern_categories as $term ) {
						$categories[] = $term->slug;
					}
				}

				$pattern_settings = [
					'title'      => wp_strip_all_tags( $pattern_post->post_title ),
					'content'    => $pattern_post->post_content,
					'categories' => $categories,
				];

				if ( count( $blocks ) === 1 ) {
					$pattern_settings['blockTypes'] = [ $blocks[0]['blockName'] ];
				}

				register_block_pattern(
					sprintf( 'boldblocks/%s', sanitize_key( $pattern_post->post_name ) ),
					$pattern_settings
				);
			}
		}
	}
endif;

```
