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();