From f0356a8f7747c853c684ce975580f76f1eb1ff73 Mon Sep 17 00:00:00 2001 From: Philipp <32090713+ouun@users.noreply.github.com> Date: Mon, 27 Jan 2025 13:19:23 +0100 Subject: [PATCH] Use `enqueue_block_assets` to pass `$block` to `assets()` --- src/AcfComposer.php | 8 +++++--- src/Block.php | 2 +- src/Console/UpgradeCommand.php | 2 -- src/Console/stubs/block.localized.stub | 4 +++- src/Console/stubs/block.stub | 4 +++- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/AcfComposer.php b/src/AcfComposer.php index 28fd076..fc982dc 100644 --- a/src/AcfComposer.php +++ b/src/AcfComposer.php @@ -193,17 +193,19 @@ public function handleWidgets(): void */ protected function handleBlocks(): void { - add_action('acf/init', function () { + add_action('enqueue_block_assets', function () { foreach ($this->composers() as $composers) { foreach ($composers as $composer) { if (! is_a($composer, Block::class)) { continue; } - method_exists($composer, 'assets') && $composer->assets(); + if (is_admin() || has_block($composer->namespace)) { + method_exists($composer, 'assets') && $composer->assets($composer->block); + } } } - }, 101); + }); add_action('acf_block_render_template', function ($block, $content, $is_preview, $post_id, $wp_block, $context) { if (! class_exists($composer = $block['render_template'] ?? '')) { diff --git a/src/Block.php b/src/Block.php index 8ea60ea..08e7598 100644 --- a/src/Block.php +++ b/src/Block.php @@ -734,7 +734,7 @@ public function render($block, $content = '', $preview = false, $post_id = 0, $w * * @return void * - * @deprecated Use `assets()` instead. + * @deprecated Use `assets($block)` instead. */ public function enqueue() { diff --git a/src/Console/UpgradeCommand.php b/src/Console/UpgradeCommand.php index d37deea..dcd4b8c 100644 --- a/src/Console/UpgradeCommand.php +++ b/src/Console/UpgradeCommand.php @@ -49,8 +49,6 @@ public function handle() 'public function enqueue($block)' => 'public function assets(array $block): void', 'public function enqueue($block = [])' => 'public function assets(array $block): void', 'public function enqueue()' => 'public function assets(array $block): void', - 'public function enqueue(array $block): void' => 'public function assets(): void', - 'public function assets(array $block): void' => 'public function assets(): void', '/->addFields\(\$this->get\((.*?)\)\)/' => fn ($match) => "->addPartial({$match[1]})", '/->addLayout\(\$this->get\((.*?)\)\)/' => fn ($match) => "->addLayout({$match[1]})", ]; diff --git a/src/Console/stubs/block.localized.stub b/src/Console/stubs/block.localized.stub index 48595cb..6cf48b0 100644 --- a/src/Console/stubs/block.localized.stub +++ b/src/Console/stubs/block.localized.stub @@ -176,8 +176,10 @@ class DummyClass extends 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(): void + public function assets(array $block): void { // } diff --git a/src/Console/stubs/block.stub b/src/Console/stubs/block.stub index e49b874..efbf125 100644 --- a/src/Console/stubs/block.stub +++ b/src/Console/stubs/block.stub @@ -177,8 +177,10 @@ class DummyClass extends 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(): void + public function assets(array $block): void { // }