| 1 |
<?php |
| 2 |
/** |
| 3 |
* Register custom patterns |
| 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( 'Patterns' ) ) : |
| 16 |
/** |
| 17 |
* Manage custom patterns. |
| 18 |
*/ |
| 19 |
class Patterns { |
| 20 |
/** |
| 21 |
* A dummy constructor |
| 22 |
*/ |
| 23 |
public function __construct() {} |
| 24 |
|
| 25 |
/** |
| 26 |
* Run main hooks |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function run() { |
| 31 |
// Register custom post type. |
| 32 |
add_action( 'init', [ $this, 'register_custom_post_type' ], 5 ); |
| 33 |
|
| 34 |
// Register custom taxonomy. |
| 35 |
add_action( 'init', [ $this, 'register_custom_taxonomy' ], 5 ); |
| 36 |
|
| 37 |
// Load data for js. |
| 38 |
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] ); |
| 39 |
|
| 40 |
// Register patterns. |
| 41 |
add_action( 'load-post.php', [ $this, 'register_patterns' ] ); |
| 42 |
add_action( 'load-post-new.php', [ $this, 'register_patterns' ] ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Register custom post type |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function register_custom_post_type() { |
| 51 |
// Block pattern args. |
| 52 |
$pattern_labels = array( |
| 53 |
'name' => _x( 'Block Patterns', 'post type general name', 'boldblocks' ), |
| 54 |
'singular_name' => _x( 'Block Pattern', 'post type singular name', 'boldblocks' ), |
| 55 |
'menu_name' => _x( 'Block Patterns', 'admin menu', 'boldblocks' ), |
| 56 |
'name_admin_bar' => _x( 'Block Patterns', 'add new on admin bar', 'boldblocks' ), |
| 57 |
'add_new' => _x( 'Add New', 'item', 'boldblocks' ), |
| 58 |
'add_new_item' => __( 'Add New Block Pattern', 'boldblocks' ), |
| 59 |
'new_item' => __( 'New Block Pattern', 'boldblocks' ), |
| 60 |
'edit_item' => __( 'Edit Block Pattern', 'boldblocks' ), |
| 61 |
'view_item' => __( 'View Block Pattern', 'boldblocks' ), |
| 62 |
'all_items' => __( 'All Block Patterns', 'boldblocks' ), |
| 63 |
'search_items' => __( 'Search Block Patterns', 'boldblocks' ), |
| 64 |
'parent_item_colon' => __( 'Parent Block Pattern:', 'boldblocks' ), |
| 65 |
'not_found' => __( 'No items found.', 'boldblocks' ), |
| 66 |
'not_found_in_trash' => __( 'No items found in trash.', 'boldblocks' ), |
| 67 |
); |
| 68 |
|
| 69 |
$pattern_args = array( |
| 70 |
'labels' => $pattern_labels, |
| 71 |
'description' => __( 'All custom block patterns', 'boldblocks' ), |
| 72 |
'public' => true, |
| 73 |
'publicly_queryable' => false, |
| 74 |
'exclude_from_search' => true, |
| 75 |
'show_ui' => true, |
| 76 |
'show_in_menu' => true, |
| 77 |
'show_in_nav_menus' => false, |
| 78 |
'show_in_admin_bar' => true, |
| 79 |
'show_in_rest' => true, |
| 80 |
'query_var' => false, |
| 81 |
'rewrite' => false, |
| 82 |
'capability_type' => 'post', |
| 83 |
'has_archive' => false, |
| 84 |
'hierarchical' => false, |
| 85 |
'menu_position' => 5, |
| 86 |
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'custom-fields', 'revisions' ), |
| 87 |
'can_export' => true, |
| 88 |
); |
| 89 |
|
| 90 |
// Register post type. |
| 91 |
register_post_type( 'boldblocks_pattern', $pattern_args ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Register custom taxonomy |
| 96 |
* |
| 97 |
* @return void |
| 98 |
*/ |
| 99 |
public function register_custom_taxonomy() { |
| 100 |
// Pattern category. |
| 101 |
$labels = array( |
| 102 |
'name' => _x( 'Pattern Categories', 'Taxonomy General Name', 'boldblocks' ), |
| 103 |
'singular_name' => _x( 'Pattern Category', 'Taxonomy Singular Name', 'boldblocks' ), |
| 104 |
'menu_name' => __( 'Pattern Categories', 'boldblocks' ), |
| 105 |
'all_items' => __( 'All Items', 'boldblocks' ), |
| 106 |
'parent_item' => __( 'Parent Item', 'boldblocks' ), |
| 107 |
'parent_item_colon' => __( 'Parent Item:', 'boldblocks' ), |
| 108 |
'new_item_name' => __( 'New Item Name', 'boldblocks' ), |
| 109 |
'add_new_item' => __( 'Add New Item', 'boldblocks' ), |
| 110 |
'edit_item' => __( 'Edit Item', 'boldblocks' ), |
| 111 |
'update_item' => __( 'Update Item', 'boldblocks' ), |
| 112 |
'view_item' => __( 'View Item', 'boldblocks' ), |
| 113 |
'separate_items_with_commas' => __( 'Separate items with commas', 'boldblocks' ), |
| 114 |
'add_or_remove_items' => __( 'Add or remove items', 'boldblocks' ), |
| 115 |
'choose_from_most_used' => __( 'Choose from the most used', 'boldblocks' ), |
| 116 |
'popular_items' => __( 'Popular Items', 'boldblocks' ), |
| 117 |
'search_items' => __( 'Search Items', 'boldblocks' ), |
| 118 |
'not_found' => __( 'Not Found', 'boldblocks' ), |
| 119 |
); |
| 120 |
|
| 121 |
$args = array( |
| 122 |
'labels' => $labels, |
| 123 |
'hierarchical' => true, |
| 124 |
'public' => true, |
| 125 |
'show_ui' => true, |
| 126 |
'show_admin_column' => true, |
| 127 |
'show_in_nav_menus' => false, |
| 128 |
'show_tagcloud' => false, |
| 129 |
'query_var' => 'pattern-category', |
| 130 |
'rewrite' => array( |
| 131 |
'slug' => 'pattern-category', |
| 132 |
), |
| 133 |
'show_in_rest' => true, |
| 134 |
); |
| 135 |
|
| 136 |
register_taxonomy( 'boldblocks_pattern_category', array( 'boldblocks_pattern' ), $args ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Enqueue editor assets |
| 141 |
* |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
public function enqueue_block_editor_assets() { |
| 145 |
global $boldblocks_cbb; |
| 146 |
|
| 147 |
// Custom blocks access file. |
| 148 |
$patterns_asset = $boldblocks_cbb->include_file( 'build/patterns.asset.php' ); |
| 149 |
|
| 150 |
// Scripts. |
| 151 |
wp_enqueue_script( |
| 152 |
'boldblocks-patterns', |
| 153 |
BOLDBLOCKS_CBB_URL . '/build/patterns.js', |
| 154 |
$patterns_asset['dependencies'], |
| 155 |
BOLDBLOCKS_CBB_VERSION, |
| 156 |
true |
| 157 |
); |
| 158 |
|
| 159 |
// Load all patterns for registration. |
| 160 |
wp_localize_script( |
| 161 |
'boldblocks-patterns', |
| 162 |
'BoldblocksPatterns', |
| 163 |
[ 'allowedBlocks' => $this->get_pattern_allowed_blocks() ] |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Get the list of blocks that allow creating patterns from toolbar menu. |
| 169 |
*/ |
| 170 |
public function get_pattern_allowed_blocks() { |
| 171 |
return apply_filters( |
| 172 |
'boldblocks_get_pattern_allowed_blocks', |
| 173 |
[ |
| 174 |
'core/group', |
| 175 |
'core/cover', |
| 176 |
'core/columns', |
| 177 |
] |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Register all custom patterns |
| 183 |
* |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
public function register_patterns() { |
| 187 |
// Register a custom category for patterns made by BoldBlocks. |
| 188 |
register_block_pattern_category( 'boldblocks', array( 'label' => _x( 'All patterns by BoldBlocks', 'Block pattern category' ) ) ); |
| 189 |
|
| 190 |
// Query all custom taxonomies. |
| 191 |
$all_categories = get_terms( [ 'taxonomy' => 'boldblocks_pattern_category' ] ); |
| 192 |
foreach ( $all_categories as $term ) { |
| 193 |
register_block_pattern_category( $term->slug, array( 'label' => $term->name ) ); |
| 194 |
} |
| 195 |
|
| 196 |
// Query all patterns. |
| 197 |
$pattern_posts = get_posts( |
| 198 |
[ |
| 199 |
'post_type' => 'boldblocks_pattern', |
| 200 |
'posts_per_page' => -1, |
| 201 |
] |
| 202 |
); |
| 203 |
|
| 204 |
foreach ( $pattern_posts as $pattern_post ) { |
| 205 |
$blocks = parse_blocks( $pattern_post->post_content ); |
| 206 |
if ( count( $blocks ) === 0 ) { |
| 207 |
continue; |
| 208 |
} |
| 209 |
|
| 210 |
$categories = [ 'boldblocks' ]; |
| 211 |
$pattern_categories = get_the_terms( $pattern_post->ID, 'boldblocks_pattern_category' ); |
| 212 |
if ( is_array( $pattern_categories ) && count( $pattern_categories ) > 0 ) { |
| 213 |
foreach ( $pattern_categories as $term ) { |
| 214 |
$categories[] = $term->slug; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
$pattern_settings = [ |
| 219 |
'title' => wp_strip_all_tags( $pattern_post->post_title ), |
| 220 |
'content' => $pattern_post->post_content, |
| 221 |
'categories' => $categories, |
| 222 |
]; |
| 223 |
|
| 224 |
if ( count( $blocks ) === 1 ) { |
| 225 |
$pattern_settings['blockTypes'] = [ $blocks[0]['blockName'] ]; |
| 226 |
} |
| 227 |
|
| 228 |
register_block_pattern( |
| 229 |
sprintf( 'boldblocks/%s', sanitize_key( $pattern_post->post_name ) ), |
| 230 |
$pattern_settings |
| 231 |
); |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
endif; |
| 236 |
|