PluginProbe
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts / 1.0.1
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts v1.0.1
2.8.14 2.8.13 2.8.12 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.10 2.8.11 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 trunk 1.0.1 1.0.2 1.1.0 1.1.1 All 147 releases
content-blocks-builder / includes / custom-blocks.php

custom-blocks.php in Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts 1.0.1, at includes/custom-blocks.php

390 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Register custom blocks
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( 'CustomBlocks' ) ) :
16 /**
17 * Create/edit custom content blocks.
18 */
19 class CustomBlocks {
20 /**
21 * Store all custom blocks
22 *
23 * @var array
24 */
25 protected $all_custom_blocks = [];
26
27 /**
28 * Store all names of custom blocks
29 *
30 * @var array
31 */
32 protected $all_custom_block_names = [];
33
34 /**
35 * Store all repeater blocks
36 *
37 * @var array
38 */
39 protected $repeater_blocks = [];
40
41 /**
42 * A dummy constructor
43 */
44 public function __construct() {}
45
46 /**
47 * Run main hooks
48 *
49 * @return void
50 */
51 public function run() {
52 // Register custom post type.
53 add_action( 'init', [ $this, 'register_custom_post_type' ], 5 );
54
55 // Load custom blocks.
56 add_action( 'init', [ $this, 'load_custom_blocks' ], 5 );
57
58 // Load data for js.
59 add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] );
60 }
61
62 /**
63 * Register custom post type
64 *
65 * @return void
66 */
67 public function register_custom_post_type() {
68 // Custom block args.
69 $labels = array(
70 'name' => _x( 'Custom Blocks', 'post type general name', 'boldblocks' ),
71 'singular_name' => _x( 'Custom Block', 'post type singular name', 'boldblocks' ),
72 'menu_name' => _x( 'Custom Blocks', 'admin menu', 'boldblocks' ),
73 'name_admin_bar' => _x( 'Custom Blocks', 'add new on admin bar', 'boldblocks' ),
74 'add_new' => _x( 'Add New', 'item', 'boldblocks' ),
75 'add_new_item' => __( 'Add New Custom Block', 'boldblocks' ),
76 'new_item' => __( 'New Custom Block', 'boldblocks' ),
77 'edit_item' => __( 'Edit Custom Block', 'boldblocks' ),
78 'view_item' => __( 'View Custom Block', 'boldblocks' ),
79 'all_items' => __( 'All Custom Blocks', 'boldblocks' ),
80 'search_items' => __( 'Search Custom Blocks', 'boldblocks' ),
81 'parent_item_colon' => __( 'Parent Custom Block:', 'boldblocks' ),
82 'not_found' => __( 'No items found.', 'boldblocks' ),
83 'not_found_in_trash' => __( 'No items found in trash.', 'boldblocks' ),
84 );
85
86 $args = array(
87 'labels' => $labels,
88 'description' => __( 'All custom blocks', 'boldblocks' ),
89 'public' => true,
90 'publicly_queryable' => false,
91 'exclude_from_search' => true,
92 'show_ui' => true,
93 'show_in_menu' => true,
94 'show_in_nav_menus' => false,
95 'show_in_admin_bar' => true,
96 'show_in_rest' => true,
97 'query_var' => false,
98 'rewrite' => false,
99 'capability_type' => 'post',
100 'has_archive' => false,
101 'hierarchical' => false,
102 'menu_position' => 5,
103 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'custom-fields', 'revisions' ),
104 'can_export' => true,
105 'template_lock' => 'insert',
106 );
107
108 // Register post type.
109 register_post_type( 'boldblocks_block', $args );
110
111 // Meta fields.
112 register_meta(
113 'post',
114 'boldblocks_block_blocks',
115 array(
116 'object_subtype' => 'boldblocks_block',
117 'show_in_rest' => true,
118 'type' => 'string',
119 'default' => '{}',
120 'single' => true,
121 )
122 );
123
124 register_meta(
125 'post',
126 'boldblocks_block_class',
127 array(
128 'object_subtype' => 'boldblocks_block',
129 'show_in_rest' => true,
130 'type' => 'string',
131 'default' => '',
132 'single' => true,
133 )
134 );
135
136 register_meta(
137 'post',
138 'boldblocks_template_lock',
139 array(
140 'object_subtype' => 'boldblocks_block',
141 'show_in_rest' => true,
142 'type' => 'string',
143 'default' => 'all',
144 'single' => true,
145 )
146 );
147
148 register_meta(
149 'post',
150 'boldblocks_enable_repeater',
151 array(
152 'object_subtype' => 'boldblocks_block',
153 'show_in_rest' => true,
154 'type' => 'boolean',
155 'default' => false,
156 'single' => true,
157 )
158 );
159
160 register_meta(
161 'post',
162 'boldblocks_disable_standalone',
163 array(
164 'object_subtype' => 'boldblocks_block',
165 'show_in_rest' => true,
166 'type' => 'boolean',
167 'default' => false,
168 'single' => true,
169 )
170 );
171
172 register_meta(
173 'post',
174 'boldblocks_is_fixed_nested_item_count',
175 array(
176 'object_subtype' => 'boldblocks_block',
177 'show_in_rest' => true,
178 'type' => 'boolean',
179 'default' => false,
180 'single' => true,
181 )
182 );
183
184 register_meta(
185 'post',
186 'boldblocks_nested_item_count',
187 array(
188 'object_subtype' => 'boldblocks_block',
189 'show_in_rest' => true,
190 'type' => 'integer',
191 'default' => 1,
192 'single' => true,
193 )
194 );
195
196 register_meta(
197 'post',
198 'boldblocks_parent_block_name',
199 array(
200 'object_subtype' => 'boldblocks_block',
201 'show_in_rest' => true,
202 'type' => 'string',
203 'default' => '',
204 'single' => true,
205 )
206 );
207
208 register_meta(
209 'post',
210 'boldblocks_parent_block_title',
211 array(
212 'object_subtype' => 'boldblocks_block',
213 'show_in_rest' => true,
214 'type' => 'string',
215 'default' => '',
216 'single' => true,
217 )
218 );
219
220 register_meta(
221 'post',
222 'boldblocks_parent_block_description',
223 array(
224 'object_subtype' => 'boldblocks_block',
225 'show_in_rest' => true,
226 'type' => 'string',
227 'default' => '',
228 'single' => true,
229 )
230 );
231
232 register_meta(
233 'post',
234 'boldblocks_parent_block_class',
235 array(
236 'object_subtype' => 'boldblocks_block',
237 'show_in_rest' => true,
238 'type' => 'string',
239 'default' => '',
240 'single' => true,
241 )
242 );
243 }
244
245 /**
246 * Load all custom blocks
247 *
248 * @return void
249 */
250 public function load_custom_blocks() {
251 // Load all custom blocks.
252 $this->all_custom_blocks = $this->get_all_custom_blocks();
253
254 // Build an array of custom block name.
255 $this->all_custom_block_names = array_map(
256 function ( $block_args ) {
257 return [ 'name' => $block_args['name'] ];
258 },
259 $this->all_custom_blocks
260 );
261
262 // Retrieve all repeater blocks.
263 $this->repeater_blocks = array_filter(
264 array_map(
265 function ( $block_arg ) {
266 return isset( $block_arg['parent'] ) ? [ 'name' => $block_arg['parent']['name'] ] : false;
267 },
268 $this->all_custom_blocks
269 )
270 );
271 }
272
273 /**
274 * Enqueue editor assets
275 *
276 * @return void
277 */
278 public function enqueue_block_editor_assets() {
279 global $boldblocks_cbb;
280
281 // Custom blocks access file.
282 $custom_blocks_asset = $boldblocks_cbb->include_file( 'build/custom-blocks.asset.php' );
283
284 // Scripts.
285 wp_enqueue_script(
286 'boldblocks-custom-blocks',
287 BOLDBLOCKS_CBB_URL . '/build/custom-blocks.js',
288 $custom_blocks_asset['dependencies'],
289 $custom_blocks_asset['version'],
290 true
291 );
292
293 // Load all custom blocks for registration.
294 wp_localize_script(
295 'boldblocks-custom-blocks',
296 'BoldblocksBlocks',
297 [
298 'blocks' => $this->all_custom_blocks,
299 'allowedBlocks' => $this->get_custom_blocks_allowed_blocks(),
300 ]
301 );
302 }
303
304 /**
305 * Get all custom blocks
306 *
307 * @return array
308 */
309 public function get_all_custom_blocks() {
310 $block_posts = get_posts(
311 [
312 'post_type' => 'boldblocks_block',
313 'posts_per_page' => -1,
314 'post_status' => 'publish',
315 ]
316 );
317
318 return array_map(
319 function( $item ) {
320 $block_args = [
321 'name' => sprintf( 'boldblocks/%s', sanitize_key( $item->post_name ) ),
322 'title' => wp_strip_all_tags( $item->post_title ),
323 'postContent' => $item->post_content,
324 'blocks' => get_post_meta( $item->ID, 'boldblocks_block_blocks', true ),
325 'description' => $item->post_excerpt,
326 'templateLock' => get_post_meta( $item->ID, 'boldblocks_template_lock', true ),
327 'className' => get_post_meta( $item->ID, 'boldblocks_block_class', true ),
328 ];
329
330 // Get parent block parameters.
331 $enable_repeater = get_post_meta( $item->ID, 'boldblocks_enable_repeater', true );
332 if ( $enable_repeater ) {
333 $parent_name = get_post_meta( $item->ID, 'boldblocks_parent_block_name', true );
334 if ( ! $parent_name ) {
335 $parent_name = $item->post_name;
336 }
337
338 $parent_title = get_post_meta( $item->ID, 'boldblocks_parent_block_title', true );
339 if ( ! $parent_title ) {
340 // translators: Repeater block title.
341 $parent_title = sprintf( __( 'Repeater: %s', 'boldblocks' ), $item->post_title );
342 }
343
344 $parent_description = get_post_meta( $item->ID, 'boldblocks_parent_block_description', true );
345 if ( ! $parent_description ) {
346 $parent_description = $item->post_excerpt;
347 }
348
349 $disable_standalone = get_post_meta( $item->ID, 'boldblocks_disable_standalone', true );
350
351 $parent_args = [
352 'name' => sprintf( 'boldblocks/%s-repeater', sanitize_key( $parent_name ) ),
353 'title' => wp_strip_all_tags( $parent_title ),
354 'description' => $parent_description,
355 'className' => get_post_meta( $item->ID, 'boldblocks_parent_block_class', true ) ?? '',
356 ];
357
358 $is_fixed_nested_item_count = get_post_meta( $item->ID, 'boldblocks_is_fixed_nested_item_count', true );
359 if ( $is_fixed_nested_item_count ) {
360 $nested_item_count = get_post_meta( $item->ID, 'boldblocks_nested_item_count', true );
361 $nested_item_count = $nested_item_count ? absint( $nested_item_count ) : 1;
362 $parent_args['nestedItemCount'] = $nested_item_count ? $nested_item_count : 1;
363 }
364
365 $block_args['standalone'] = ! $disable_standalone;
366 $block_args['parent'] = $parent_args;
367 }
368
369 return $block_args;
370 },
371 $block_posts
372 );
373 }
374
375 /**
376 * Get list of blocks that allow creating custom blocks from toolbar menu.
377 */
378 public function get_custom_blocks_allowed_blocks() {
379 return apply_filters(
380 'boldblocks/get_custom_block_allowed_blocks',
381 [
382 'core/group',
383 'core/cover',
384 'core/columns',
385 ]
386 );
387 }
388 }
389 endif;
390