From c2eef0354c4473fbd12e4728f015ba4697340d29 Mon Sep 17 00:00:00 2001 From: Brandon Date: Thu, 29 Feb 2024 06:28:57 -0600 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20Fix=20block=20template=20han?= =?UTF-8?q?dling=20(Fixes=20#212)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Block.php | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Block.php b/src/Block.php index b2353f9c..890c59ae 100644 --- a/src/Block.php +++ b/src/Block.php @@ -3,6 +3,7 @@ namespace Log1x\AcfComposer; use Illuminate\Support\Arr; +use Illuminate\Support\Collection; use Illuminate\Support\Str; use Log1x\AcfComposer\Concerns\FormatsCss; use Log1x\AcfComposer\Concerns\InteractsWithBlade; @@ -326,25 +327,21 @@ public function getInlineStyle(): string } /** - * Retrieve the block template. + * Handle the block template. */ - public function getTemplate(string|array $template = []): string + public function handleTemplate(array $template = []): Collection { - if (is_string($template)) { - return $template; - } - return collect($template)->map(function ($value, $key) { if (is_array($value) && Arr::has($value, 'innerBlocks')) { - $innerBlocks = collect($value['innerBlocks'])->map(function ($innerBlock) { - return $this->getTemplate($innerBlock)->all(); - })->collapse(); + $blocks = collect($value['innerBlocks']) + ->map(fn ($block) => $this->handleTemplate($block)->all()) + ->collapse(); - return [$key, Arr::except($value, 'innerBlocks') ?? [], $innerBlocks->all()]; + return [$key, Arr::except($value, 'innerBlocks') ?? [], $blocks->all()]; } return [$key, $value]; - })->values()->toJson(); + })->values(); } /** @@ -483,9 +480,11 @@ public function render($block, $content = '', $preview = false, $post_id = 0, $w false, ])->filter()->implode(' '); - $this->style = $this->getStyle(); - $this->template = $this->getTemplate($this->template); + $this->template = is_array($this->template) + ? $this->handleTemplate($this->template)->toJson() + : $this->template; + $this->style = $this->getStyle(); $this->inlineStyle = $this->getInlineStyle(); return $this->view($this->view, ['block' => $this]); From 20d811b345d6afbefb64637330f8cb73cd7a0374 Mon Sep 17 00:00:00 2001 From: Brandon Date: Thu, 29 Feb 2024 06:38:10 -0600 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A9=B9=20Properly=20merge=20the=20`ex?= =?UTF-8?q?ample()`=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Block.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Block.php b/src/Block.php index 890c59ae..efd9202d 100644 --- a/src/Block.php +++ b/src/Block.php @@ -405,11 +405,15 @@ public function compose(): ?self }, ]; - if ($this->example !== false || method_exists($this, 'example')) { + if ($this->example !== false) { + if (method_exists($this, 'example') && is_array($example = $this->example())) { + $this->example = array_merge($this->example, $example); + } + $settings = Arr::add($settings, 'example', [ 'attributes' => [ 'mode' => 'preview', - 'data' => method_exists($this, 'example') ? $this->example() : $this->example, + 'data' => $this->example, ], ]); }