Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Improve block asset handling (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
ouun authored Jan 27, 2025
1 parent bbf5642 commit 6f95562
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
16 changes: 14 additions & 2 deletions src/AcfComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,20 @@ public function handleWidgets(): void
*/
protected function handleBlocks(): void
{
add_action('enqueue_block_assets', function () {
foreach ($this->composers() as $composers) {
foreach ($composers as $composer) {
if (! is_a($composer, Block::class)) {
continue;
}

if (is_admin() || has_block($composer->namespace)) {
method_exists($composer, 'assets') && $composer->assets($composer->block);
}
}
}
});

add_action('acf_block_render_template', function ($block, $content, $is_preview, $post_id, $wp_block, $context) {
if (! class_exists($composer = $block['render_template'] ?? '')) {
return;
Expand All @@ -204,8 +218,6 @@ protected function handleBlocks(): void

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

method_exists($composer, 'assets') && $composer->assets($block);

echo $composer->render($block, $content, $is_preview, $post_id, $wp_block, $context);
}, 9, 6);
}
Expand Down
2 changes: 0 additions & 2 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,6 @@ public function settings(): Collection
'alignContent' => $this->align_content,
'styles' => $this->getStyles(),
'supports' => $this->supports,
'enqueue_assets' => fn ($block) => method_exists($this, 'assets') ? $this->assets($block) : null,
'textdomain' => $this->getTextDomain(),
'acf_block_version' => $this->blockVersion,
'api_version' => 2,
Expand Down Expand Up @@ -671,7 +670,6 @@ public function toJson(): string
'align',
'alignContent',
'alignText',
'enqueue_assets',
'mode',
'post_types',
'render_callback',
Expand Down
4 changes: 3 additions & 1 deletion src/Console/stubs/block.localized.stub
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ class DummyClass extends Block
}

/**
* Assets enqueued when rendering the block.
* Assets enqueued with 'enqueue_block_assets' when rendering the block.
*
* @link https://developer.wordpress.org/block-editor/how-to-guides/enqueueing-assets-in-the-editor/#editor-content-scripts-and-styles
*/
public function assets(array $block): void
{
Expand Down
4 changes: 3 additions & 1 deletion src/Console/stubs/block.stub
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ class DummyClass extends Block
}

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

0 comments on commit 6f95562

Please sign in to comment.