-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfunctions-patterns.php
More file actions
75 lines (61 loc) · 1.88 KB
/
functions-patterns.php
File metadata and controls
75 lines (61 loc) · 1.88 KB
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
<?php
/**
* Register block patterns.
*
* @package BlockPatternBuilder
* @author Justin Tadlock <justintadlock@gmail.com>
* @copyright Copyright (c) 2020, Justin Tadlock
* @link https://github.com/justintadlock/block-pattern-builder
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
namespace BlockPatternBuilder;
use WP_Query;
# Don't execute code if file is accessed directly.
defined( 'ABSPATH' ) || exit;
// For now, we're just going to load these up on the post editing screen so that
// we're not running a query on every page of the site. In the future, we should
// implement some caching for the patterns. There may be a more suitable hook
// to only load when the block editor is present.
add_action( 'load-post.php', __NAMESPACE__ . '\\register_patterns' );
add_action( 'load-post-new.php', __NAMESPACE__ . '\\register_patterns' );
/**
* Registers block patterns.
*
* @access public
* @since 1.0.0
* @return void
*/
function register_patterns() {
$register = false;
// Assign pattern registration function for Gutenberg 8.1.0+.
if ( function_exists( 'register_block_pattern' ) ) {
$register = 'register_block_pattern';
// Old registration function pre-8.1.0.
} elseif ( function_exists( 'register_pattern' ) ) {
$register = 'register_pattern';
}
// Bail if Block Patterns API doesn't exist.
if ( ! $register ) {
return;
}
// Query all published patterns.
$patterns = new WP_Query( [
'post_type' => 'bpb_pattern',
'number_posts' => -1
] );
if ( $patterns->have_posts() ) {
while ( $patterns->have_posts() ) {
$patterns->the_post();
global $post;
$register(
sprintf( 'bpb/%s', sanitize_key( $post->post_name ) ),
[
'title' => wp_strip_all_tags( $post->post_title ),
'content' => $post->post_content,
'description' => $post->post_excerpt
]
);
}
}
wp_reset_postdata();
}