Skip to content

Commit 1231ec3

Browse files
authored
🧑‍💻 Utilize native WordPress handling of block wrapper HTML attributes (#313)
2 parents 187fa09 + 29c26bd commit 1231ec3

File tree

3 files changed

+21
-82
lines changed

3 files changed

+21
-82
lines changed

‎composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"require": {
1818
"php": "^8.0",
1919
"stoutlogic/acf-builder": "^1.11",
20-
"laravel/prompts": "*"
20+
"laravel/prompts": "*",
21+
"spatie/once": "*"
2122
},
2223
"require-dev": {
2324
"laravel/pint": "^1.14",

‎src/Block.php‎

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
use Illuminate\Support\Arr;
77
use Illuminate\Support\Collection;
88
use Illuminate\Support\Str;
9-
use Log1x\AcfComposer\Concerns\FormatsCss;
109
use Log1x\AcfComposer\Concerns\InteractsWithBlade;
1110
use Log1x\AcfComposer\Contracts\Block as BlockContract;
11+
use WP_Block_Supports;
1212

1313
use function Roots\asset;
1414

1515
abstract class Block extends Composer implements BlockContract
1616
{
17-
use FormatsCss, InteractsWithBlade;
17+
use InteractsWithBlade;
1818

1919
/**
2020
* The block properties.
@@ -416,70 +416,36 @@ public function getSupportAttributes(): array
416416
return $attributes;
417417
}
418418

419+
/**
420+
* Retrieve the block HTML attributes.
421+
*/
422+
public function getSupportHtmlAttributes(): array
423+
{
424+
return once(fn () => WP_Block_Supports::get_instance()->apply_block_supports());
425+
}
426+
419427
/**
420428
* Retrieve the inline block styles.
421429
*/
422430
public function getInlineStyle(): string
423431
{
424-
return $this->collect([
425-
'padding' => ! empty($this->block->style['spacing']['padding'])
426-
? $this->collect($this->block->style['spacing']['padding'])
427-
->map(fn ($value, $side) => $this->formatCss($value, $side))
428-
->implode(' ')
429-
: null,
430-
431-
'margin' => ! empty($this->block->style['spacing']['margin'])
432-
? $this->collect($this->block->style['spacing']['margin'])
433-
->map(fn ($value, $side) => $this->formatCss($value, $side, 'margin'))
434-
->implode(' ')
435-
: null,
436-
437-
'color' => ! empty($this->block->style['color']['gradient'])
438-
? sprintf('background: %s;', $this->block->style['color']['gradient'])
439-
: null,
440-
])->filter()->implode(' ');
432+
$supports = $this->getSupportHtmlAttributes();
433+
434+
return $supports['style'] ?? '';
441435
}
442436

443437
/**
444438
* Retrieve the block classes.
445439
*/
446440
public function getClasses(): string
447441
{
448-
$classes = $this->collect([
449-
'slug' => Str::of($this->slug)->slug()->start('wp-block-')->toString(),
450-
451-
'className' => $this->block->className ?? null,
452-
453-
'align' => ! empty($this->block->align)
454-
? Str::start($this->block->align, 'align')
455-
: null,
456-
457-
'backgroundColor' => ! empty($this->block->backgroundColor)
458-
? sprintf('has-background has-%s-background-color', $this->block->backgroundColor)
459-
: null,
460-
461-
'textColor' => ! empty($this->block->textColor)
462-
? sprintf('has-%s-color', $this->block->textColor)
463-
: null,
442+
$supports = $this->getSupportHtmlAttributes();
464443

465-
'gradient' => ! empty($this->block->gradient)
466-
? sprintf('has-%s-gradient-background', $this->block->gradient)
467-
: null,
468-
]);
469-
470-
if ($alignText = $this->block->alignText ?? $this->block->align_text ?? null) {
471-
$classes->add(Str::start($alignText, 'align-text-'));
472-
}
473-
474-
if ($alignContent = $this->block->alignContent ?? $this->block->align_content ?? null) {
475-
$classes->add(Str::start($alignContent, 'is-position-'));
476-
}
477-
478-
if ($this->block->fullHeight ?? $this->block->full_height ?? null) {
479-
$classes->add('full-height');
480-
}
481-
482-
return $classes->filter()->implode(' ');
444+
return str_replace(
445+
acf_slugify($this->namespace),
446+
$this->slug,
447+
$supports['class'] ?? ''
448+
);
483449
}
484450

485451
/**

‎src/Concerns/FormatsCss.php‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)