Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Improve template syntax handling (Fixes #239) (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x authored Jul 16, 2024
1 parent 1dfc497 commit 6f26046
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 6f26046

Please sign in to comment.