Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix block template handling (Fixes #212) #213

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -408,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,
],
]);
}
Expand Down Expand Up @@ -483,9 +484,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]);
Expand Down