-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtk-block-pattern-ui.php
130 lines (116 loc) · 3.32 KB
/
tk-block-pattern-ui.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Plugin Name: TK Block Pattern UI
* Plugin URI: https://trewknowledge.com
* Description: Provides a UI for creating block patterns.
* Author: Trew Knowledge
* Author URI: https://trewknowledge.com
* Version: 0.0.1
*
* @package TK_BlockPatternUI
*/
/**
* Registers the block patterns post type.
*/
function tk_block_pattern_register_post_type() {
$args = [
'label' => __( 'Block Patterns', 'tk_block_patterns' ),
'public' => false,
'publicly_queryable' => false,
'show_in_rest' => true,
'show_in_nav_menus' => false,
'show_in_admin_bar' => true,
'exclude_from_search' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => null,
'menu_icon' => 'dashicons-screenoptions',
'can_export' => true,
'delete_with_user' => false,
'hierarchical' => false,
'has_archive' => false,
'rewrite' => false,
'supports' => [ 'title', 'editor', 'revisions' ],
];
register_post_type( 'tk_block_patterns', $args );
}
add_action( 'init', 'tk_block_pattern_register_post_type' );
/**
* Registers the block patterns taxonomy.
*/
function tk_block_pattern_register_taxonomy() {
$labels = [
'name' => __( 'Category', 'tk_block_patterns' ),
'singular_name' => __( 'Category', 'tk_block_patterns' ),
'menu_name' => __( 'Category', 'tk_block_patterns' ),
];
$args = [
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_in_nav_menus' => false,
'show_admin_column' => true,
'query_var' => true,
];
register_taxonomy( 'tk_block_pattern_category', 'tk_block_patterns', $args );
}
add_action( 'init', 'tk_block_pattern_register_taxonomy' );
/**
* Creates the block Patterns.
*/
function tk_register_block_patterns() {
/**
* Creates the block pattern categories from the Block Pattern taxonomy.
*/
$terms = get_terms(
[
'taxonomy' => 'tk_block_pattern_category',
'hide_empty' => true,
]
);
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
// phpcs:disable WordPress.WP.I18n.NonSingularStringLiteralText
// The Term name needs to be dynamic and not a hard coded string.
register_block_pattern_category(
'tk_' . $term->slug,
[ 'label' => __( $term->name, 'tk_block_patterns' ) ]
);
//phpcs:enable
}
}
/**
* Creates the block patterns from the Block Patterns post type
*/
$patterns = new WP_Query(
[
'post_type' => 'tk_block_patterns',
'posts_per_page' => -1,
]
);
if ( $patterns->have_posts() ) {
while ( $patterns->have_posts() ) {
$patterns->the_post();
global $post;
$term_obj_list = get_the_terms( $post->ID, 'tk_block_pattern_category' );
if ( is_array( $term_obj_list ) ) {
register_block_pattern(
'tk/' . sanitize_key( $post->post_name ),
[
'title' => wp_strip_all_tags( $post->post_title ),
'content' => $post->post_content,
'categories' => array_map(
function( $value ) {
return 'tk_' . $value;
},
wp_list_pluck( $term_obj_list, 'slug' )
),
]
);
}
}
}
wp_reset_postdata();
}
add_action( 'init', 'tk_register_block_patterns' );