diff --git a/src/AcfComposer.php b/src/AcfComposer.php index 9203f80..fc982dc 100644 --- a/src/AcfComposer.php +++ b/src/AcfComposer.php @@ -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; @@ -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); } diff --git a/src/Block.php b/src/Block.php index c7ae01b..08e7598 100644 --- a/src/Block.php +++ b/src/Block.php @@ -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, @@ -671,7 +670,6 @@ public function toJson(): string 'align', 'alignContent', 'alignText', - 'enqueue_assets', 'mode', 'post_types', 'render_callback', diff --git a/src/Console/stubs/block.localized.stub b/src/Console/stubs/block.localized.stub index 48ffb55..6cf48b0 100644 --- a/src/Console/stubs/block.localized.stub +++ b/src/Console/stubs/block.localized.stub @@ -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 { diff --git a/src/Console/stubs/block.stub b/src/Console/stubs/block.stub index f5dd4ed..efbf125 100644 --- a/src/Console/stubs/block.stub +++ b/src/Console/stubs/block.stub @@ -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 {