Skip to content

Commit 6f95562

Browse files
authored
πŸ§‘β€πŸ’» Improve block asset handling (#315)
1 parent bbf5642 commit 6f95562

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

β€Žsrc/AcfComposer.phpβ€Ž

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,20 @@ public function handleWidgets(): void
193193
*/
194194
protected function handleBlocks(): void
195195
{
196+
add_action('enqueue_block_assets', function () {
197+
foreach ($this->composers() as $composers) {
198+
foreach ($composers as $composer) {
199+
if (! is_a($composer, Block::class)) {
200+
continue;
201+
}
202+
203+
if (is_admin() || has_block($composer->namespace)) {
204+
method_exists($composer, 'assets') && $composer->assets($composer->block);
205+
}
206+
}
207+
}
208+
});
209+
196210
add_action('acf_block_render_template', function ($block, $content, $is_preview, $post_id, $wp_block, $context) {
197211
if (! class_exists($composer = $block['render_template'] ?? '')) {
198212
return;
@@ -204,8 +218,6 @@ protected function handleBlocks(): void
204218

205219
add_filter('acf/blocks/template_not_found_message', fn () => '');
206220

207-
method_exists($composer, 'assets') && $composer->assets($block);
208-
209221
echo $composer->render($block, $content, $is_preview, $post_id, $wp_block, $context);
210222
}, 9, 6);
211223
}

β€Žsrc/Block.phpβ€Ž

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,6 @@ public function settings(): Collection
601601
'alignContent' => $this->align_content,
602602
'styles' => $this->getStyles(),
603603
'supports' => $this->supports,
604-
'enqueue_assets' => fn ($block) => method_exists($this, 'assets') ? $this->assets($block) : null,
605604
'textdomain' => $this->getTextDomain(),
606605
'acf_block_version' => $this->blockVersion,
607606
'api_version' => 2,
@@ -671,7 +670,6 @@ public function toJson(): string
671670
'align',
672671
'alignContent',
673672
'alignText',
674-
'enqueue_assets',
675673
'mode',
676674
'post_types',
677675
'render_callback',

β€Žsrc/Console/stubs/block.localized.stubβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ class DummyClass extends Block
175175
}
176176

177177
/**
178-
* Assets enqueued when rendering the block.
178+
* Assets enqueued with 'enqueue_block_assets' when rendering the block.
179+
*
180+
* @link https://developer.wordpress.org/block-editor/how-to-guides/enqueueing-assets-in-the-editor/#editor-content-scripts-and-styles
179181
*/
180182
public function assets(array $block): void
181183
{

β€Žsrc/Console/stubs/block.stubβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ class DummyClass extends Block
176176
}
177177

178178
/**
179-
* Assets enqueued when rendering the block.
179+
* Assets enqueued with 'enqueue_block_assets' when rendering the block.
180+
*
181+
* @link https://developer.wordpress.org/block-editor/how-to-guides/enqueueing-assets-in-the-editor/#editor-content-scripts-and-styles
180182
*/
181183
public function assets(array $block): void
182184
{

0 commit comments

Comments
Β (0)