From 08bacfdbfc57db2cca7099368d619b46e96aaff8 Mon Sep 17 00:00:00 2001 From: Brandon Date: Tue, 16 Jul 2024 08:11:40 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20Improve=20t?= =?UTF-8?q?emplate=20syntax=20handling=20(Fixes=20#239)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Block.php | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Block.php b/src/Block.php index d384a42e..253df5ac 100644 --- a/src/Block.php +++ b/src/Block.php @@ -412,16 +412,22 @@ public function getTextDomain(): string */ public function handleTemplate(array $template = []): Collection { - return collect($template)->map(function ($value, $key) { - if (is_array($value) && Arr::has($value, 'innerBlocks')) { - $blocks = collect($value['innerBlocks']) - ->map(fn ($block) => $this->handleTemplate($block)->all()) - ->collapse(); - - return [$key, Arr::except($value, 'innerBlocks') ?? [], $blocks->all()]; - } - - return [$key, $value]; + return collect($template)->map(function ($block, $key) { + $name = is_numeric($key) + ? (is_string($block) ? $block : array_key_first($block)) + : $key; + + $value = is_numeric($key) + ? (is_string($block) ? [] : $block[$name]) + : $block; + + $value = is_array($value) && Arr::has($value, 'innerBlocks') + ? array_merge($value, [ + 'innerBlocks' => $this->handleTemplate($value['innerBlocks'])->all(), + ]) + : $value; + + return [$name, $value]; })->values(); } @@ -588,9 +594,7 @@ public function render($block, $content = '', $preview = false, $post_id = 0, $w $this->post = get_post($post_id); - $this->template = is_array($this->template) - ? $this->handleTemplate($this->template)->toJson() - : $this->template; + $this->template = $this->handleTemplate($this->template)->toJson(); $this->classes = $this->getClasses(); $this->style = $this->getStyle();