-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.php
More file actions
400 lines (342 loc) · 9.94 KB
/
Copy pathBlock.php
File metadata and controls
400 lines (342 loc) · 9.94 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
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
<?php
/**
* Abstract Block class
*
* @package Mantle
*/
namespace Mantle\Blocks;
use InvalidArgumentException;
use Mantle\Contracts\Block as Block_Contract;
use Mantle\Facade\Storage;
use Mantle\Facade\View_Loader;
use Mantle\Http\View\View;
use Mantle\Http\View\Factory as View_Factory;
use Mantle\Support\Str;
use Throwable;
/**
* Abstract class that handles the majority of Gutenberg Block
* registration.
*/
abstract class Block implements Block_Contract {
/**
* The block attributes.
*
* @var mixed[]
*/
protected array $attributes = [];
/**
* A custom override value for the block's Editor script location.
*/
protected string $editor_script = '';
/**
* A custom override value for the block's Editor script asset file location.
*/
protected string $editor_script_asset = '';
/**
* A custom override value for the block's Editor script dependencies.
*/
protected array $editor_script_dependencies = [];
/**
* A custom override value for the block's Editor script handle.
*/
protected string $editor_script_handle = '';
/**
* A custom override value for the block's Editor style location.
*/
protected string $editor_style = '';
/**
* A custom override value for the block's Editor style handle.
*/
protected string $editor_style_handle = '';
/**
* The block's entry file without the extension.
*
* Generally this is going to be an index file (e.g. index.js, index.jsx)
*/
protected string $entry_filename = 'index';
/**
* A custom override value for the block's Frontend script location.
*/
protected string $frontend_script = '';
/**
* A custom override value for the block's Frontend script handle.
*/
protected string $frontend_script_handle = '';
/**
* A custom override value for the block's Frontend style location.
*/
protected string $frontend_style = '';
/**
* A custom override value for the block's Frontend style handle.
*/
protected string $frontend_style_handle = '';
/**
* Whether the block is a dynamic block or not.
* Default is true.
*/
protected bool $is_dynamic = true;
/**
* The name of the block.
*/
protected string $name = '';
/**
* The namespace of the block.
*/
protected string $namespace = '';
/**
* Post types that support this block.
*
* Defaults to [ 'all' ], which is all registered post types.
*
* @var string[]
*/
protected array $post_types = [ 'all' ];
/**
* Executed by the Block Service Provider to handle registering the block
* with Mantle and WordPress.
*/
public function register(): void {
if ( ! $this->should_register() ) {
return;
}
add_action(
'enqueue_block_editor_assets',
function (): void {
$this->register_editor_assets();
$this->register_frontend_assets();
}
);
add_action(
'init',
function (): void {
$args = wp_parse_args(
[
'attributes' => $this->get_attributes(),
'editor_script' => $this->get_editor_script_handle(),
'render_callback' => $this->is_dynamic ? fn ( array $attributes, string $content ) => $this->render( $attributes, $content ) : null,
'editor_style' => $this->get_editor_style_handle(),
]
);
// Register the block.
register_block_type(
$this->get_block_name(),
$args
);
}
);
}
/**
* Whether or not a view was found for the block.
*
* @param string $name The name of the view to attempt to locate.
*/
protected function block_view_exists( $name ): bool {
try {
View_Loader::find( $name );
} catch ( InvalidArgumentException ) {
return false;
}
return true;
}
/**
* A helper function for formatting script handles correctly.
*
* @param string $type The type of handle being formatted.
*/
protected function format_handle( string $type ): string {
$name = Str::replace( '/', '-', $this->get_block_name() );
return sprintf( '%1$s-%2$s', $name, $type );
}
/**
* Returns the blocks attributes array.
*/
protected function get_attributes(): array {
return $this->attributes;
}
/**
* Get the block assets object from the block assets JSON file.
*/
protected function get_block_assets(): object {
$root = \trailingslashit( MANTLE_BASE_DIR ) . \trailingslashit( app( 'config' )->get( 'assets.path' ) );
$disk = Storage::create_local_driver(
[
'root' => $root,
]
);
try {
$assets = $disk->get( "blocks/{$this->name}/{$this->entry_filename}.asset.json" );
$assets = \json_decode( (string) $assets );
} catch ( Throwable ) {
return new \stdClass();
}
/**
* Fallback to an empty object if the JSON is malformed.
*/
if ( ! is_object( $assets ) ) {
return new \stdClass();
}
return $assets;
}
/**
* Gets the block name with the proper namespace and block name.
* e.g. `namespace/name` if a namespace is defined, or simply
* `name` if a namespace is not defined.
*/
protected function get_block_name(): string {
return sprintf( '%1$s/%2$s', $this->get_namespace(), $this->name );
}
/**
* Get the editor scripts array of dependencies.
*/
protected function get_editor_script_dependencies(): array {
if (
is_array( $this->editor_script_dependencies )
&& ! empty( $this->editor_script_dependencies )
) {
return $this->editor_script_dependencies;
}
$assets = $this->get_block_assets();
if ( empty( $assets->dependencies ) ) {
return [];
}
return $assets->dependencies;
}
/**
* Get the version value from the Editor script's asset file.
*/
protected function get_editor_script_version(): string {
if (
is_array( $this->editor_script_dependencies )
&& ! empty( $this->editor_script_dependencies )
) {
return $this->editor_script_dependencies;
}
$assets = $this->get_block_assets();
return $assets->version ?? \sha1( \time() );
}
/**
* Get the editor script handle. Use override if provided, otherwise generate
* a handle.
*/
protected function get_editor_script_handle(): string {
return $this->editor_script_handle ?: $this->format_handle( 'editor-script' );
}
/**
* Get the editor style handle. Use override if provided, otherwise generate
* a handle.
*/
protected function get_editor_style_handle(): string {
return $this->editor_style_handle ?: $this->format_handle( 'editor-style' );
}
/**
* Get the frontend script handle. Use override if provided, otherwise generate
* a handle.
*/
protected function get_frontend_script_handle(): string {
return $this->frontend_script_handle ?: $this->format_handle( 'frontend-script' );
}
/**
* Get the frontend style handle. Use override if provided, otherwise generate
* a handle.
*/
protected function get_frontend_style_handle(): string {
return $this->frontend_style_handle ?: $this->format_handle( 'frontend-style' );
}
/**
* Return the namespace for the block. Generate one if necessary.
*/
protected function get_namespace(): string {
return $this->namespace ?: Str::lower( app( 'config' )->get( 'app.namespace', 'app' ) );
}
/**
* Handle registering the Block Editor assets.
*/
protected function register_editor_assets(): void {
asset()
->script(
$this->get_editor_script_handle(),
$this->editor_script ?: mix( "blocks/{$this->name}/index.js" )
)
->dependencies( $this->get_editor_script_dependencies() )
->version( $this->get_editor_script_version() )
->only_block_editor();
if ( ! empty( $this->editor_style ) ) {
asset()
->style( $this->get_editor_style_handle(), $this->editor_style )
->only_block_editor();
}
}
/**
* Handle registering the block's frontend assets.
*/
protected function register_frontend_assets(): void {
if ( ! empty( $this->frontend_script ) ) {
asset()
->script( $this->get_frontend_script_handle(), $this->frontend_script )
->only_frontend();
}
if ( ! empty( $this->frontend_style ) ) {
asset()
->style( $this->get_frontend_style_handle(), $this->frontend_style )
->only_frontend();
}
}
/**
* Default render function for dynamic blocks.
*
* @param array $attributes The attributes for this block.
* @param string $content The inner content for this block. Used when using InnerBlocks.
* @return View The content for the block.
*/
protected function render( array $attributes, string $content ): View|string {
$templates = [
$this->get_block_name(),
"blocks/{$this->get_block_name()}",
];
$found = false;
foreach ( $templates as $template ) {
if ( $this->block_view_exists( $template ) ) {
$found = true;
break;
}
}
if ( ! $found ) {
return '';
}
/**
* Filter the block attributes before passing them to the template part.
*
* @param array $attributes the block attributes.
*/
$attributes = apply_filters( "{$this->get_block_name()}_attributes", $attributes ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound
/**
* Execute an action before a block renders. Has access to the
* filtered block attributes.
*
* @param array $attributes The filtered block attributes.
* @param string $contents The contents of the block.
*/
do_action( "{$this->get_block_name()}_pre_render", $attributes, $content ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound
return app( View_Factory::class )->make(
$template,
[
'attributes' => $attributes,
'content' => $content,
]
);
}
/**
* Helper function to determine if this block should be registered.
*
* @return boolean True if this block should be registered for the current post type, false otherwise.
*/
protected function should_register() {
$should_register = in_array( 'all', $this->post_types, true ) || in_array( get_post_type(), $this->post_types, true );
/**
* Allow blocks to be disabled programmatically.
*
* @param bool $should_register Whether or not the block should be registered on this post type.
*/
return apply_filters( "{$this->get_block_name()}_should_register", $should_register ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound
}
}