Skip to content

Commit

Permalink
🎨 Improve the Block class (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x authored Mar 2, 2024
2 parents e66d462 + 56a1307 commit 36916c5
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 68 deletions.
4 changes: 2 additions & 2 deletions src/AcfComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ public function boot(): void

foreach ($this->composers as $namespace => $composers) {
foreach ($composers as $i => $composer) {
$this->composers[$namespace][$i] = $composer->compose();
$this->composers[$namespace][$i] = $composer->handle();
}
}

foreach ($this->deferredComposers as $namespace => $composers) {
foreach ($composers as $index => $composer) {
$this->composers[$namespace][] = $composer->compose();
$this->composers[$namespace][] = $composer->handle();
}
}

Expand Down
115 changes: 65 additions & 50 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,15 @@ public function getStyle(): ?string
return Str::of($this->block->className ?? null)
->matchAll('/is-style-(\S+)/')
->get(0) ??
Arr::get(collect($this->getStyles())->firstWhere('isDefault'), 'name');
Arr::get($this->getDefaultStyle(), 'name');
}

/**
* Retrieve the default style.
*/
public function getDefaultStyle(): array
{
return collect($this->getStyles())->firstWhere('isDefault') ?? [];
}

/**
Expand Down Expand Up @@ -316,21 +324,65 @@ public function getInlineStyle(): string
{
return collect([
'padding' => ! empty($this->block->style['spacing']['padding'])
? collect($this->block->style['spacing']['padding'])->map(function ($value, $side) {
return $this->formatCss($value, $side);
})->implode(' ')
? collect($this->block->style['spacing']['padding'])
->map(fn ($value, $side) => $this->formatCss($value, $side))
->implode(' ')
: null,

'margin' => ! empty($this->block->style['spacing']['margin'])
? collect($this->block->style['spacing']['margin'])->map(function ($value, $side) {
return $this->formatCss($value, $side, 'margin');
})->implode(' ')
? collect($this->block->style['spacing']['margin'])
->map(fn ($value, $side) => $this->formatCss($value, $side, 'margin'))
->implode(' ')
: null,

'color' => ! empty($this->block->style['color']['gradient'])
? sprintf('background: %s;', $this->block->style['color']['gradient'])
: null,
])->filter()->implode(' ');
}

/**
* Retrieve the block classes.
*/
public function getClasses(): string
{
$classes = collect([
'slug' => Str::of($this->slug)->slug()->start('wp-block-')->toString(),

'className' => $this->block->className ?? null,

'align' => ! empty($this->block->align)
? Str::start($this->block->align, 'align')
: null,

'backgroundColor' => ! empty($this->block->backgroundColor)
? sprintf('has-background has-%s-background-color', $this->block->backgroundColor)
: null,

'textColor' => ! empty($this->block->textColor)
? sprintf('has-%s-color', $this->block->textColor)
: null,

'gradient' => ! empty($this->block->gradient)
? sprintf('has-%s-gradient-background', $this->block->gradient)
: null,
]);

if ($alignText = $this->block->alignText ?? $this->block->align_text ?? null) {
$classes->add(Str::start($alignText, 'align-text-'));
}

if ($alignContent = $this->block->alignContent ?? $this->block->align_content ?? null) {
$classes->add(Str::start($alignContent, 'is-position-'));
}

if ($this->block->fullHeight ?? $this->block->full_height ?? null) {
$classes->add('full-height');
}

return $classes->filter()->implode(' ');
}

/**
* Handle the block template.
*/
Expand Down Expand Up @@ -360,17 +412,9 @@ public function compose(): ?self
return null;
}

if (! empty($this->name) && empty($this->slug)) {
$this->slug = Str::slug(Str::kebab($this->name));
}

if (empty($this->view)) {
$this->view = Str::start($this->slug, 'blocks.');
}

if (empty($this->namespace)) {
$this->namespace = Str::start($this->slug, $this->prefix);
}
$this->slug = $this->slug ?: Str::slug(Str::kebab($this->name));
$this->view = $this->view ?: Str::start($this->slug, 'blocks.');
$this->namespace = $this->namespace ?? Str::start($this->slug, $this->prefix);

if (! Arr::has($this->fields, 'location.0.0')) {
Arr::set($this->fields, 'location.0.0', [
Expand Down Expand Up @@ -469,7 +513,7 @@ public function toJson(): string
])->put('acf', [
'mode' => $this->mode,
'renderTemplate' => $this::class,
]);
])->put('name', $this->namespace);

return $settings->filter()->toJson(JSON_PRETTY_PRINT);
}
Expand All @@ -483,7 +527,7 @@ public function jsonPath(): string
}

/**
* Determine if the block has a JSON file.
* Determine if the Block has a JSON file.
*/
public function hasJson(): bool
{
Expand Down Expand Up @@ -512,40 +556,11 @@ public function render($block, $content = '', $preview = false, $post_id = 0, $w

$this->post = get_post($post_id);

$this->classes = collect([
'slug' => Str::start(
Str::slug($this->slug),
'wp-block-'
),
'align' => ! empty($this->block->align) ?
Str::start($this->block->align, 'align') :
false,
'align_text' => ! empty($this->supports['align_text']) ?
Str::start($this->block->align_text, 'align-text-') :
false,
'align_content' => ! empty($this->supports['align_content']) ?
Str::start($this->block->align_content, 'is-position-') :
false,
'full_height' => ! empty($this->supports['full_height'])
&& ! empty($this->block->full_height) ?
'full-height' :
false,
'classes' => $this->block->className ?? false,
'backgroundColor' => ! empty($this->block->backgroundColor) ?
sprintf('has-background has-%s-background-color', $this->block->backgroundColor) :
false,
'textColor' => ! empty($this->block->textColor) ?
sprintf('has-%s-color', $this->block->textColor) :
false,
'gradient' => ! empty($this->block->gradient) ?
sprintf('has-%s-gradient-background', $this->block->gradient) :
false,
])->filter()->implode(' ');

$this->template = is_array($this->template)
? $this->handleTemplate($this->template)->toJson()
: $this->template;

$this->classes = $this->getClasses();
$this->style = $this->getStyle();
$this->inlineStyle = $this->getInlineStyle();

Expand Down
8 changes: 3 additions & 5 deletions src/Builder/FlexibleContentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ class FlexibleContentBuilder extends FieldBuilder
*/
public function addLayout($layout, $args = [])
{
if ($layout instanceof FieldsBuilder) {
$layout = clone $layout;
} else {
$layout = Builder::make($layout, $args);
}
$layout = is_a($layout, FieldsBuilder::class)
? clone $layout
: Builder::make($layout, $args);

$layout = $this->initializeLayout($layout, $args);

Expand Down
33 changes: 32 additions & 1 deletion src/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Log1x\AcfComposer\Concerns\InteractsWithPartial;
use Log1x\AcfComposer\Contracts\Composer as ComposerContract;
use Log1x\AcfComposer\Contracts\Field as FieldContract;
use Roots\Acorn\Application;
use StoutLogic\AcfBuilder\FieldsBuilder;

abstract class Composer implements FieldContract
abstract class Composer implements ComposerContract, FieldContract
{
use InteractsWithPartial;

Expand Down Expand Up @@ -62,6 +63,36 @@ public static function make(AcfComposer $composer): self
return new static($composer);
}

/**
* Handle the Composer instance.
*/
public function handle(): self
{
$this->beforeRegister();

$this->compose();

$this->afterRegister();

return $this;
}

/**
* Actions to run before registering the Composer.
*/
public function beforeRegister(): void
{
//
}

/**
* Actions to run after registering the Composer.
*/
public function afterRegister(): void
{
//
}

/**
* Register the field group with Advanced Custom Fields.
*/
Expand Down
10 changes: 6 additions & 4 deletions src/Console/CacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public function handle()
}

if ($this->option('status')) {
return $this->composer->manifest()->exists()
? $this->components->info('The <fg=blue>ACF Composer</> field groups are currently <fg=green;options=bold>cached</>.')
: $this->components->info('The <fg=blue>ACF Composer</> field groups are currently <fg=red;options=bold>not cached</>.');
$status = $this->composer->manifest()->exists()
? '<fg=green;options=bold>cached</>'
: '<fg=red;options=bold>not cached</>';

return $this->components->info("<fg=blue>ACF Composer</> is currently {$status}.");
}

$composers = collect(
Expand Down Expand Up @@ -78,6 +80,6 @@ public function handle()
? $this->composer->manifest()->writeBlocks()
: 0;

$this->components->info("Successfully cached <fg=blue>{$manifest}</> field(s) and <fg=blue>{$blocks}</> block(s).");
$this->components->info("Successfully cached <fg=blue>{$manifest}</> field group(s) and <fg=blue>{$blocks}</> block(s).");
}
}
2 changes: 1 addition & 1 deletion src/Console/UpgradeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function handle()

$this->components->bulletList($files);

if (! $this->components->confirm("Found <fg=blue>{$classes->count()}</> ACF Composer classes to upgrade. Do you wish to <fg=blue>continue</>?")) {
if (! $this->components->confirm("Found <fg=blue>{$classes->count()}</> ACF Composer classes to upgrade. Do you wish to <fg=blue>continue</>?", true)) {
return $this->components->error('The ACF Composer upgrade has been <fg=red>cancelled</>.');
}

Expand Down
13 changes: 13 additions & 0 deletions src/Contracts/Composer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Log1x\AcfComposer\Contracts;

interface Composer
{
/**
* Compose the fields.
*
* @return mixed
*/
public function compose();
}
2 changes: 1 addition & 1 deletion src/Contracts/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface Field
/**
* The field group.
*
* @return \StoutLogic\AcfBuilder\FieldsBuilder|array
* @return \Log1x\AcfComposer\Builder|array
*/
public function fields();
}
2 changes: 1 addition & 1 deletion src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function getFields(bool $cache = true): array
/**
* Compose and register the defined field groups with ACF.
*
* @return void
* @return mixed
*/
public function compose()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function updatedMessage()
/**
* Compose and register the defined ACF field groups.
*
* @return void
* @return mixed
*/
public function compose()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Partial.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ abstract class Partial extends Composer
/**
* Compose and register the defined field groups with ACF.
*
* @return void
* @return mixed
*/
public function compose()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ abstract class Widget extends Composer implements WidgetContract
/**
* Compose and register the defined ACF field groups.
*
* @return void
* @return mixed
*/
public function compose()
{
Expand Down

0 comments on commit 36916c5

Please sign in to comment.