From cf85e3585d08113f0a033d1c13d36483a1f1afb0 Mon Sep 17 00:00:00 2001 From: Philipp <32090713+ouun@users.noreply.github.com> Date: Tue, 21 Jan 2025 10:48:58 +0100 Subject: [PATCH 1/6] Enqueue block assets with 'enqueue_block_assets' action --- src/AcfComposer.php | 10 ++++-- src/Block.php | 44 ++++++++++++++++++++------ src/Console/stubs/block.localized.stub | 4 +-- src/Console/stubs/block.stub | 4 +-- 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/AcfComposer.php b/src/AcfComposer.php index 9203f80..68a2bcd 100644 --- a/src/AcfComposer.php +++ b/src/AcfComposer.php @@ -193,6 +193,14 @@ public function handleWidgets(): void */ protected function handleBlocks(): void { + add_action('enqueue_block_assets', function () { + foreach ($this->composers() as $composers) { + foreach ($composers as $composer) { + method_exists($composer, 'assets') && $composer->assets([]); + } + } + }); + 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 +212,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..93750eb 100644 --- a/src/Block.php +++ b/src/Block.php @@ -443,13 +443,41 @@ public function getInlineStyle(): string */ public function getClasses(): string { - $supports = $this->getHtmlAttributes(); + $classes = $this->collect([ + 'slug' => Str::of($this->slug)->slug()->start('wp-block-')->toString(), - return str_replace( - acf_slugify($this->namespace), - $this->slug, - $supports['class'] ?? "wp-block-{$this->slug}" - ); + 'className' => $this->block->className ?? null, + + 'align' => ! empty($this->block->align) + ? Str::start($this->block->align, 'align') + : null, + + 'backgroundColor' => ! empty($this->block->backgroundColor) + ? sprintf('has-background has-%s-background-color', $this->block->backgroundColor) + : null, + + 'textColor' => ! empty($this->block->textColor) + ? sprintf('has-%s-color', $this->block->textColor) + : null, + + 'gradient' => ! empty($this->block->gradient) + ? sprintf('has-%s-gradient-background', $this->block->gradient) + : null, + ]); + + if ($alignText = $this->block->alignText ?? $this->block->align_text ?? null) { + $classes->add(Str::start($alignText, 'align-text-')); + } + + if ($alignContent = $this->block->alignContent ?? $this->block->align_content ?? null) { + $classes->add(Str::start($alignContent, 'is-position-')); + } + + if ($this->block->fullHeight ?? $this->block->full_height ?? null) { + $classes->add('full-height'); + } + + return $classes->filter()->implode(' '); } /** @@ -601,7 +629,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 +698,6 @@ public function toJson(): string 'align', 'alignContent', 'alignText', - 'enqueue_assets', 'mode', 'post_types', 'render_callback', @@ -736,7 +762,7 @@ public function render($block, $content = '', $preview = false, $post_id = 0, $w * * @return void * - * @deprecated Use `assets($block)` instead. + * @deprecated Use `assets()` instead. */ public function enqueue() { diff --git a/src/Console/stubs/block.localized.stub b/src/Console/stubs/block.localized.stub index 48ffb55..48595cb 100644 --- a/src/Console/stubs/block.localized.stub +++ b/src/Console/stubs/block.localized.stub @@ -175,9 +175,9 @@ class DummyClass extends Block } /** - * Assets enqueued when rendering the block. + * Assets enqueued with 'enqueue_block_assets' when rendering the block. */ - public function assets(array $block): void + public function assets(): void { // } diff --git a/src/Console/stubs/block.stub b/src/Console/stubs/block.stub index f5dd4ed..e49b874 100644 --- a/src/Console/stubs/block.stub +++ b/src/Console/stubs/block.stub @@ -176,9 +176,9 @@ class DummyClass extends Block } /** - * Assets enqueued when rendering the block. + * Assets enqueued with 'enqueue_block_assets' when rendering the block. */ - public function assets(array $block): void + public function assets(): void { // } From 61d854388395d6a2fa479f64245d0583fd8b5b9c Mon Sep 17 00:00:00 2001 From: Philipp <32090713+ouun@users.noreply.github.com> Date: Tue, 21 Jan 2025 11:04:32 +0100 Subject: [PATCH 2/6] Revert changes on `getClasses()` --- src/Block.php | 40 ++++++---------------------------------- 1 file changed, 6 insertions(+), 34 deletions(-) diff --git a/src/Block.php b/src/Block.php index 93750eb..8ea60ea 100644 --- a/src/Block.php +++ b/src/Block.php @@ -443,41 +443,13 @@ public function getInlineStyle(): string */ public function getClasses(): string { - $classes = $this->collect([ - 'slug' => Str::of($this->slug)->slug()->start('wp-block-')->toString(), - - 'className' => $this->block->className ?? null, - - 'align' => ! empty($this->block->align) - ? Str::start($this->block->align, 'align') - : null, - - 'backgroundColor' => ! empty($this->block->backgroundColor) - ? sprintf('has-background has-%s-background-color', $this->block->backgroundColor) - : null, - - 'textColor' => ! empty($this->block->textColor) - ? sprintf('has-%s-color', $this->block->textColor) - : null, - - 'gradient' => ! empty($this->block->gradient) - ? sprintf('has-%s-gradient-background', $this->block->gradient) - : null, - ]); - - if ($alignText = $this->block->alignText ?? $this->block->align_text ?? null) { - $classes->add(Str::start($alignText, 'align-text-')); - } - - if ($alignContent = $this->block->alignContent ?? $this->block->align_content ?? null) { - $classes->add(Str::start($alignContent, 'is-position-')); - } - - if ($this->block->fullHeight ?? $this->block->full_height ?? null) { - $classes->add('full-height'); - } + $supports = $this->getHtmlAttributes(); - return $classes->filter()->implode(' '); + return str_replace( + acf_slugify($this->namespace), + $this->slug, + $supports['class'] ?? "wp-block-{$this->slug}" + ); } /** From 70dd276a318cfd34f8525ae66e32a1cdd81e81a0 Mon Sep 17 00:00:00 2001 From: Philipp <32090713+ouun@users.noreply.github.com> Date: Tue, 21 Jan 2025 11:06:38 +0100 Subject: [PATCH 3/6] Update Upgrade Command to remove $block param from `assets()` --- src/Console/UpgradeCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Console/UpgradeCommand.php b/src/Console/UpgradeCommand.php index dcd4b8c..723abb9 100644 --- a/src/Console/UpgradeCommand.php +++ b/src/Console/UpgradeCommand.php @@ -49,6 +49,7 @@ 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', '/->addFields\(\$this->get\((.*?)\)\)/' => fn ($match) => "->addPartial({$match[1]})", '/->addLayout\(\$this->get\((.*?)\)\)/' => fn ($match) => "->addLayout({$match[1]})", ]; From 34c8055ce2a6dd79a89baa434448a56e9cd43204 Mon Sep 17 00:00:00 2001 From: Philipp <32090713+ouun@users.noreply.github.com> Date: Fri, 24 Jan 2025 00:04:21 +0100 Subject: [PATCH 4/6] Upgrade command to replace `assets(array $block)` with `assets()` --- src/Console/UpgradeCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Console/UpgradeCommand.php b/src/Console/UpgradeCommand.php index 723abb9..d37deea 100644 --- a/src/Console/UpgradeCommand.php +++ b/src/Console/UpgradeCommand.php @@ -50,6 +50,7 @@ public function handle() '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]})", ]; From b917ee78efb98a03bcadd426eac41d75920f1c1b Mon Sep 17 00:00:00 2001 From: Philipp <32090713+ouun@users.noreply.github.com> Date: Sun, 26 Jan 2025 22:55:22 +0100 Subject: [PATCH 5/6] Use `acf/init` hook for flexibility and compatibility As discussed, we leave it to the devs to use the hooks they need in `assets()`. --- src/AcfComposer.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/AcfComposer.php b/src/AcfComposer.php index 68a2bcd..28fd076 100644 --- a/src/AcfComposer.php +++ b/src/AcfComposer.php @@ -193,13 +193,17 @@ public function handleWidgets(): void */ protected function handleBlocks(): void { - add_action('enqueue_block_assets', function () { + add_action('acf/init', function () { foreach ($this->composers() as $composers) { foreach ($composers as $composer) { - method_exists($composer, 'assets') && $composer->assets([]); + if (! is_a($composer, Block::class)) { + continue; + } + + method_exists($composer, 'assets') && $composer->assets(); } } - }); + }, 101); add_action('acf_block_render_template', function ($block, $content, $is_preview, $post_id, $wp_block, $context) { if (! class_exists($composer = $block['render_template'] ?? '')) { 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 6/6] 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 { // }