This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
class-loader.php
627 lines (540 loc) · 17.7 KB
/
class-loader.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<?php
/**
* Loader initiates the loading of new blocks.
*
* @package Block_Lab
*/
namespace Block_Lab\Blocks;
use Block_Lab\Component_Abstract;
/**
* Class Loader
*/
class Loader extends Component_Abstract {
/**
* Asset paths and urls for blocks.
*
* @var array
*/
protected $assets = [];
/**
* An associative array of block config data for the blocks that will be registered.
*
* The key of each item in the array is the block name.
*
* @var array
*/
protected $blocks = [];
/**
* A data store for sharing data to helper functions.
*
* @var array
*/
protected $data = [];
/**
* Load the Loader.
*
* @return $this
*/
public function init() {
$this->assets = [
'path' => [
'entry' => $this->plugin->get_path( 'js/editor.blocks.js' ),
'editor_style' => $this->plugin->get_path( 'css/blocks.editor.css' ),
],
'url' => [
'entry' => $this->plugin->get_url( 'js/editor.blocks.js' ),
'editor_style' => $this->plugin->get_url( 'css/blocks.editor.css' ),
],
];
return $this;
}
/**
* Register all the hooks.
*/
public function register_hooks() {
/**
* Gutenberg JS block loading.
*/
add_action( 'enqueue_block_editor_assets', $this->get_callback( 'editor_assets' ) );
/**
* Gutenberg custom categories.
*/
add_filter( 'block_categories', $this->get_callback( 'register_categories' ) );
/**
* Block retrieval.
*/
add_action( 'init', $this->get_callback( 'retrieve_blocks' ), 1 );
/**
* PHP block loading.
*/
add_action( 'init', $this->get_callback( 'dynamic_block_loader' ) );
}
/**
* Retrieve data from the Loader's data store.
*
* @param string $key The data key to retrieve.
* @return mixed
*/
public function get_data( $key ) {
$data = false;
if ( isset( $this->data[ $key ] ) ) {
$data = $this->data[ $key ];
}
/**
* Filters the data that gets returned.
*
* @param mixed $data The data from the Loader's data store.
* @param string $key The key for the data being retreived.
*/
$data = apply_filters( 'block_lab_data', $data, $key );
/**
* Filters the data that gets returned, specifically for a single key.
*
* @param mixed $data The data from the Loader's data store.
*/
$data = apply_filters( "block_lab_data_{$key}", $data );
return $data;
}
/**
* Gets the callback for an action or filter.
*
* Enables keeping these methods protected,
* while allowing actions and filters to call them.
*
* @param string $method_name The name of the method to get the callback for.
* @return callable An enclosure that calls the function.
*/
protected function get_callback( $method_name ) {
return function( $arg ) use ( $method_name ) {
return call_user_func( [ $this, $method_name ], $arg );
};
}
/**
* Launch the blocks inside Gutenberg.
*/
protected function editor_assets() {
$asset_config_file = $this->plugin->get_path( 'js/editor.blocks.asset.php' );
$asset_config = require $asset_config_file;
wp_enqueue_script(
'block-lab-blocks',
$this->assets['url']['entry'],
$asset_config['dependencies'],
$asset_config['version'],
true
);
// Add dynamic Gutenberg blocks.
wp_add_inline_script(
'block-lab-blocks',
'const blockLabBlocks = ' . wp_json_encode( $this->blocks ),
'before'
);
// Enqueue optional editor only styles.
wp_enqueue_style(
'block-lab-editor-css',
$this->assets['url']['editor_style'],
[],
$this->plugin->get_version()
);
$block_names = wp_list_pluck( $this->blocks, 'name' );
foreach ( $block_names as $block_name ) {
$this->enqueue_block_styles( $block_name, [ 'preview', 'block' ] );
}
$this->enqueue_global_styles();
// Used to conditionally show notices for blocks belonging to an author.
$author_blocks = get_posts(
[
'author' => get_current_user_id(),
'post_type' => 'block_lab',
// We could use -1 here, but that could be dangerous. 99 is more than enough.
'posts_per_page' => 99,
]
);
$author_block_slugs = wp_list_pluck( $author_blocks, 'post_name' );
// Used to conditionally exclude blocks from certain post types.
$post = get_post();
$post_type = $post->post_type;
wp_localize_script(
'block-lab-blocks',
'blockLab',
[
'authorBlocks' => $author_block_slugs,
'postType' => $post_type,
]
);
}
/**
* Loads dynamic blocks via render_callback for each block.
*/
protected function dynamic_block_loader() {
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
foreach ( $this->blocks as $block_name => $block_config ) {
$block = new Block();
$block->from_array( $block_config );
$this->register_block( $block_name, $block );
}
}
/**
* Registers a block.
*
* @param string $block_name The name of the block, including namespace.
* @param Block $block The block to register.
*/
protected function register_block( $block_name, $block ) {
$attributes = $this->get_block_attributes( $block );
// sanitize_title() allows underscores, but register_block_type doesn't.
$block_name = str_replace( '_', '-', $block_name );
// register_block_type doesn't allow slugs starting with a number.
if ( is_numeric( $block_name[0] ) ) {
$block_name = 'block-' . $block_name;
}
register_block_type(
$block_name,
[
'attributes' => $attributes,
// @see https://github.com/WordPress/gutenberg/issues/4671
'render_callback' => function ( $attributes ) use ( $block ) {
return $this->render_block_template( $block, $attributes );
},
]
);
}
/**
* Register custom block categories.
*
* @param array $categories Array of block categories.
*
* @return array
*/
protected function register_categories( $categories ) {
foreach ( $this->blocks as $block_config ) {
if ( ! isset( $block_config['category'] ) ) {
continue;
}
/*
* This is a backwards compatibility fix.
*
* Block categories used to be saved as strings, but were always included in
* the default list of categories, so it's safe to skip them.
*/
if ( ! is_array( $block_config['category'] ) || empty( $block_config['category'] ) ) {
continue;
}
if ( ! in_array( $block_config['category'], $categories, true ) ) {
$categories[] = $block_config['category'];
}
}
return $categories;
}
/**
* Gets block attributes.
*
* @param Block $block The block to get attributes from.
*
* @return array
*/
protected function get_block_attributes( $block ) {
$attributes = [];
// Default Editor attributes (applied to all blocks).
$attributes['className'] = [ 'type' => 'string' ];
foreach ( $block->fields as $field_name => $field ) {
$attributes = $this->get_attributes_from_field( $attributes, $field_name, $field );
}
/**
* Filters a given block's attributes.
*
* These are later passed to register_block_type() in $args['attributes'].
* Removing attributes here can cause 'Error loading block...' in the editor.
*
* @param array[] $attributes The attributes for a block.
* @param array $block Block data, including its name at $block['name'].
*/
return apply_filters( 'block_lab_get_block_attributes', $attributes, $block );
}
/**
* Sets the field values in the attributes, enabling them to appear in the block.
*
* @param array $attributes The attributes in which to store the field value.
* @param string $field_name The name of the field, like 'home-hero'.
* @param Field $field The Field to set the attributes from.
* @return array $attributes The attributes, with the new field value set.
*/
protected function get_attributes_from_field( $attributes, $field_name, $field ) {
$attributes[ $field_name ] = [
'type' => $field->type,
];
if ( ! empty( $field->settings['default'] ) ) {
$attributes[ $field_name ]['default'] = $field->settings['default'];
}
if ( 'array' === $field->type ) {
/**
* This is a workaround to allow empty array values. We unset the default value before registering the
* block so that the default isn't used to auto-correct empty arrays. This allows the default to be
* used only when creating the form.
*/
unset( $attributes[ $field_name ]['default'] );
$items_type = 'repeater' === $field->control ? 'object' : 'string';
$attributes[ $field_name ]['items'] = [ 'type' => $items_type ];
}
return $attributes;
}
/**
* Renders the block provided a template is provided.
*
* @param Block $block The block to render.
* @param array $attributes Attributes to render.
*
* @return mixed
*/
protected function render_block_template( $block, $attributes ) {
$type = 'block';
// This is hacky, but the editor doesn't send the original request along.
$context = filter_input( INPUT_GET, 'context', FILTER_SANITIZE_STRING );
if ( 'edit' === $context ) {
$type = [ 'preview', 'block' ];
}
if ( ! is_admin() ) {
/**
* The block has been added, but its values weren't saved (not even the defaults). This is a phenomenon
* unique to frontend output, as the editor fetches its attributes from the form fields themselves.
*/
$missing_schema_attributes = array_diff_key( $block->fields, $attributes );
foreach ( $missing_schema_attributes as $attribute_name => $schema ) {
if ( isset( $schema->settings['default'] ) ) {
$attributes[ $attribute_name ] = $schema->settings['default'];
}
}
// Similar to the logic above, populate the Repeater control's sub-fields with default values.
foreach ( $block->fields as $field ) {
if ( isset( $field->settings['sub_fields'] ) && isset( $attributes[ $field->name ]['rows'] ) ) {
$sub_field_settings = $field->settings['sub_fields'];
$rows = $attributes[ $field->name ]['rows'];
// In each row, apply a field's default value if a value doesn't exist in the attributes.
foreach ( $rows as $row_index => $row ) {
foreach ( $sub_field_settings as $sub_field_name => $sub_field ) {
if ( ! isset( $row[ $sub_field_name ] ) && isset( $sub_field_settings[ $sub_field_name ]->settings['default'] ) ) {
$rows[ $row_index ][ $sub_field_name ] = $sub_field_settings[ $sub_field_name ]->settings['default'];
}
}
}
$attributes[ $field->name ]['rows'] = $rows;
}
}
$this->enqueue_block_styles( $block->name, 'block' );
/**
* The wp_enqueue_style function handles duplicates, so we don't need to worry about multiple blocks
* loading the global styles more than once.
*/
$this->enqueue_global_styles();
}
$this->data['attributes'] = $attributes;
$this->data['config'] = $block;
if ( ! is_admin() && ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) && ! wp_doing_ajax() ) {
/**
* Runs in the 'render_callback' of the block, and only on the front-end, not in the editor.
*
* The block's name (slug) is in $block->name.
* If a block depends on a JavaScript file,
* this action is a good place to call wp_enqueue_script().
* In that case, pass true as the 5th argument ($in_footer) to wp_enqueue_script().
*
* @param Block $block The block that is rendered.
* @param array $attributes The block attributes.
*/
do_action( 'block_lab_render_template', $block, $attributes );
/**
* Runs in a block's 'render_callback', and only on the front-end.
*
* Same as the action above, but with a dynamic action name that has the block name.
*
* @param Block $block The block that is rendered.
* @param array $attributes The block attributes.
*/
do_action( "block_lab_render_template_{$block->name}", $block, $attributes );
}
ob_start();
$this->block_template( $block->name, $type );
$output = ob_get_clean();
return $output;
}
/**
* Enqueues styles for the block.
*
* @param string $name The name of the block (slug as defined in UI).
* @param string|array $type The type of template to load.
*/
protected function enqueue_block_styles( $name, $type = 'block' ) {
$locations = [];
$types = (array) $type;
foreach ( $types as $type ) {
$locations = array_merge(
$locations,
block_lab()->get_stylesheet_locations( $name, $type )
);
}
$stylesheet_path = block_lab()->locate_template( $locations );
$stylesheet_url = block_lab()->get_url_from_path( $stylesheet_path );
/**
* Enqueue the stylesheet, if it exists. The wp_enqueue_style function handles duplicates, so we don't need
* to worry about the same block loading its stylesheets more than once.
*/
if ( ! empty( $stylesheet_url ) ) {
wp_enqueue_style(
"block-lab__block-{$name}",
$stylesheet_url,
[],
wp_get_theme()->get( 'Version' )
);
}
}
/**
* Enqueues global block styles.
*/
protected function enqueue_global_styles() {
$locations = [
'blocks/css/blocks.css',
'blocks/blocks.css',
];
$stylesheet_path = block_lab()->locate_template( $locations );
$stylesheet_url = block_lab()->get_url_from_path( $stylesheet_path );
/**
* Enqueue the stylesheet, if it exists.
*/
if ( ! empty( $stylesheet_url ) ) {
wp_enqueue_style(
'block-lab__global-styles',
$stylesheet_url,
[],
wp_get_theme()->get( 'Version' )
);
}
}
/**
* Loads a block template to render the block.
*
* @param string $name The name of the block (slug as defined in UI).
* @param string|array $type The type of template to load.
*/
protected function block_template( $name, $type = 'block' ) {
// Loading async it might not come from a query, this breaks load_template().
global $wp_query;
// So lets fix it.
if ( empty( $wp_query ) ) {
$wp_query = new \WP_Query(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}
$types = (array) $type;
$located = '';
foreach ( $types as $type ) {
$templates = block_lab()->get_template_locations( $name, $type );
$located = block_lab()->locate_template( $templates );
if ( ! empty( $located ) ) {
break;
}
}
if ( ! empty( $located ) ) {
$theme_template = apply_filters( 'block_lab_override_theme_template', $located );
// This is not a load once template, so require_once is false.
load_template( $theme_template, false );
} else {
if ( ! current_user_can( 'edit_posts' ) || ! isset( $templates[0] ) ) {
return;
}
// Hide the template not found notice on the frontend, unless WP_DEBUG is enabled.
if ( ! is_admin() && ! ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) {
return;
}
printf(
'<div class="notice notice-warning">%s</div>',
wp_kses_post(
// Translators: Placeholder is a file path.
sprintf( __( 'Template file %s not found.', 'block-lab' ), '<code>' . esc_html( $templates[0] ) . '</code>' )
)
);
}
}
/**
* Load all the published blocks and blocks/block.json files.
*/
protected function retrieve_blocks() {
/**
* Retrieve blocks from blocks.json.
* Reverse to preserve order of preference when using array_merge.
*/
$blocks_files = array_reverse( (array) block_lab()->locate_template( 'blocks/blocks.json', '', false ) );
foreach ( $blocks_files as $blocks_file ) {
// This is expected to be on the local filesystem, so file_get_contents() is ok to use here.
$json = file_get_contents( $blocks_file ); // @codingStandardsIgnoreLine
$block_data = json_decode( $json, true );
// Merge if no json_decode error occurred.
if ( json_last_error() == JSON_ERROR_NONE ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
$this->blocks = array_merge( $this->blocks, $block_data );
}
}
/**
* Retrieve blocks stored as posts in the WordPress database.
*/
$block_posts = new \WP_Query(
[
'post_type' => block_lab()->get_post_type_slug(),
'post_status' => 'publish',
'posts_per_page' => 100, // This has to have a limit for this plugin to be scalable.
]
);
if ( 0 < $block_posts->post_count ) {
/** The WordPress Post object. @var \WP_Post $post */
foreach ( $block_posts->posts as $post ) {
$block_data = json_decode( $post->post_content, true );
// Merge if no json_decode error occurred.
if ( json_last_error() == JSON_ERROR_NONE ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
$this->blocks = array_merge( $this->blocks, $block_data );
}
}
}
/**
* Use this action to add new blocks and fields with the block_lab_add_block and block_lab_add_field helper functions.
*/
do_action( 'block_lab_add_blocks' );
/**
* Filter the available blocks.
*
* This is used internally by the block_lab_add_block and block_lab_add_field helper functions,
* but it can also be used to hide certain blocks if desired.
*
* @param array $blocks An associative array of blocks.
*/
$this->blocks = apply_filters( 'block_lab_blocks', $this->blocks );
}
/**
* Add a new block.
*
* This method should be called during the block_lab_add_blocks action, to ensure
* that the block isn't added too late.
*
* @param array $block_config The config of the block to add.
*/
public function add_block( $block_config ) {
if ( ! isset( $block_config['name'] ) ) {
return;
}
$this->blocks[ "block-lab/{$block_config['name']}" ] = $block_config;
}
/**
* Add a new field to an existing block.
*
* This method should be called during the block_lab_add_blocks action, to ensure
* that the block isn't added too late.
*
* @param string $block_name The name of the block that the field is added to.
* @param array $field_config The config of the field to add.
*/
public function add_field( $block_name, $field_config ) {
if ( ! isset( $this->blocks[ "block-lab/{$block_name}" ] ) ) {
return;
}
if ( ! isset( $field_config['name'] ) ) {
return;
}
$this->blocks[ "block-lab/{$block_name}" ]['fields'][ $field_config['name'] ] = $field_config;
}
}