blocks.php
5.12 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* Blocks Loader
*
* Enqueue CSS/JS of all the blocks.
*
* @since 2.17.2
* @package Stackable
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! function_exists( 'stackable_get_metadata_by_folders' ) ) {
/**
* Function for getting the block.json metadata
* based on folder names array.
*
* @array array folders
* @array string handle
* @return array metadata
*/
function stackable_get_metadata_by_folders( $block_folders, $handle = 'metadata' ) {
$blocks = array();
$blocks_dir = dirname( __FILE__ ) . '/block';
if ( ! file_exists( $blocks_dir ) ) {
return $blocks;
}
foreach ( $block_folders as $folder_name ) {
$block_json_file = $blocks_dir . '/' . $folder_name . '/block.json';
if ( ! file_exists( $block_json_file ) ) {
continue;
}
$metadata = json_decode( file_get_contents( $block_json_file ), true );
array_push( $blocks, array_merge( $metadata, array( 'block_json_file' => $block_json_file ) ) );
}
return $blocks;
}
}
if ( ! function_exists( 'stackable_get_stk_block_folders_metadata' ) ) {
function stackable_get_stk_block_folders_metadata() {
/**
* folders containing stackable blocks without inner blocks.
*/
$stk_block_folders = array(
'button',
'count-up',
'countdown',
'divider',
'heading',
'icon-button',
'icon-list',
'icon',
'image',
'number-box',
'map',
'progress-bar',
'progress-circle',
'separator',
'spacer',
'subtitle',
'table-of-contents',
'tab-labels',
'text',
);
return stackable_get_metadata_by_folders( $stk_block_folders, 'stk-block-folders' );
}
}
if ( ! function_exists( 'stackable_get_stk_wrapper_block_folders_metadata' ) ) {
function stackable_get_stk_wrapper_block_folders_metadata() {
/**
* folders containing stackable blocks with inner blocks.
*/
$stk_wrapper_block_folders = array(
'accordion',
'blockquote',
'button-group',
'call-to-action',
'card',
'column',
'columns',
'expand',
'feature-grid',
'feature',
'hero',
'icon-box',
'icon-label',
'image-box',
'notification',
'posts',
'price',
'tabs',
'tab-content',
'pricing-box',
'team-member',
'testimonial',
'timeline',
'video-popup',
'horizontal-scroller',
);
return stackable_get_metadata_by_folders( $stk_wrapper_block_folders, 'stk-wrapper-block-folders' );
}
}
if ( ! function_exists( 'stackable_register_blocks' ) ) {
function stackable_register_blocks() {
// Blocks directory may not exist if working from a fresh clone.
$blocks_dir = dirname( __FILE__ ) . '/block';
if ( ! file_exists( $blocks_dir ) ) {
return;
}
$blocks_metadata = array_merge(
stackable_get_stk_block_folders_metadata(),
stackable_get_stk_wrapper_block_folders_metadata()
);
foreach ( $blocks_metadata as $metadata ) {
$registry = WP_Block_Type_Registry::get_instance();
if ( $registry->is_registered( $metadata['name'] ) ) {
$registry->unregister( $metadata['name'] );
}
$register_options = apply_filters( 'stackable.register-blocks.options',
// This automatically enqueues all our styles and scripts.
array(
'style' => 'ugb-style-css', // Frontend styles.
// 'script' => 'ugb-block-frontend-js', // Frontend scripts.
'editor_script' => 'ugb-block-js', // Editor scripts.
'editor_style' => 'ugb-block-editor-css', // Editor styles.
),
$metadata['name'],
$metadata
);
register_block_type_from_metadata( $metadata['block_json_file'], $register_options );
}
}
add_action( 'init', 'stackable_register_blocks' );
}
/**
* Allow our blocks to display post excerpts
* when calling `get_the_excerpt` function.
*
* @see https://developer.wordpress.org/reference/hooks/excerpt_allowed_blocks/
*/
if ( ! function_exists( 'stackable_add_excerpt_wrapper_blocks' ) ) {
/**
* Register stackable blocks with inner blocks.
*/
function stackable_add_excerpt_wrapper_blocks( $allowed_wrapper_blocks ) {
$blocks_dir = dirname( __FILE__ ) . '/block';
if ( ! file_exists( $blocks_dir ) ) {
return $allowed_wrapper_blocks;
}
$allowed_stackable_wrapper_blocks = array();
$blocks_metadata = stackable_get_stk_wrapper_block_folders_metadata();
foreach ( $blocks_metadata as $metadata ) {
array_push( $allowed_stackable_wrapper_blocks, $metadata['name'] );
}
return array_merge( $allowed_stackable_wrapper_blocks, $allowed_wrapper_blocks );
}
add_filter( 'excerpt_allowed_wrapper_blocks', 'stackable_add_excerpt_wrapper_blocks' );
}
if ( ! function_exists( 'stackable_add_excerpt_blocks' ) ) {
/**
* Register "unit" stackable blocks (blocks without inner blocks)
*/
function stackable_add_excerpt_blocks( $allowed_blocks ) {
$blocks_dir = dirname( __FILE__ ) . '/block';
if ( ! file_exists( $blocks_dir ) ) {
return $allowed_blocks;
}
$allowed_stackable_blocks = array();
$blocks_metadata = stackable_get_stk_block_folders_metadata();
foreach ( $blocks_metadata as $metadata ) {
array_push( $allowed_stackable_blocks, $metadata['name'] );
}
return array_merge( $allowed_stackable_blocks, $allowed_blocks );
}
add_filter( 'excerpt_allowed_blocks', 'stackable_add_excerpt_blocks' );
}