Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Utilize native WordPress handling of block wrapper HTML attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed Jan 18, 2025
1 parent 310cde2 commit 29c26bd
Showing 1 changed file with 19 additions and 53 deletions.
72 changes: 19 additions & 53 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Log1x\AcfComposer\Concerns\FormatsCss;
use Log1x\AcfComposer\Concerns\InteractsWithBlade;
use Log1x\AcfComposer\Contracts\Block as BlockContract;
use WP_Block_Supports;

use function Roots\asset;

abstract class Block extends Composer implements BlockContract
{
use FormatsCss, InteractsWithBlade;
use InteractsWithBlade;

/**
* The block properties.
Expand Down Expand Up @@ -416,70 +416,36 @@ public function getSupportAttributes(): array
return $attributes;
}

/**
* Retrieve the block HTML attributes.
*/
public function getSupportHtmlAttributes(): array
{
return once(fn () => WP_Block_Supports::get_instance()->apply_block_supports());
}

/**
* Retrieve the inline block styles.
*/
public function getInlineStyle(): string
{
return $this->collect([
'padding' => ! empty($this->block->style['spacing']['padding'])
? $this->collect($this->block->style['spacing']['padding'])
->map(fn ($value, $side) => $this->formatCss($value, $side))
->implode(' ')
: null,

'margin' => ! empty($this->block->style['spacing']['margin'])
? $this->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(' ');
$supports = $this->getSupportHtmlAttributes();

return $supports['style'] ?? '';
}

/**
* Retrieve the block classes.
*/
public function getClasses(): string
{
$classes = $this->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,
$supports = $this->getSupportHtmlAttributes();

'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(' ');
return str_replace(
acf_slugify($this->namespace),
$this->slug,
$supports['class'] ?? ''
);
}

/**
Expand Down

0 comments on commit 29c26bd

Please sign in to comment.