Skip to content

Commit

Permalink
BlockServiceProvider: added type for the block instance to avoid some…
Browse files Browse the repository at this point in the history
… erors when uses $this->app->bind with others providers
  • Loading branch information
yarovikov committed Oct 24, 2024
1 parent 280575f commit 2ab7a02
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions src/Providers/BlockServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ function (string $file): void {
$src = $this->formatFile($this->folder, $file);

$this->app->bind($src->handle, function () use ($src) {
return new $src->class();
$instance = new $src->class();
$instance->type = 'block';

return $instance;
});

$this->blocks->push($src->handle);
Expand All @@ -76,7 +79,7 @@ public function registerBlocks(): void
}

foreach ($blocks as $block) {
if (!WP_Block_Type_Registry::get_instance()->is_registered($this->app[$block]->name)) {
if (true === $this->checkIfIsBlock($block) && !WP_Block_Type_Registry::get_instance()->is_registered($this->app[$block]->name)) {
$this->app[$block]->registerBlockType();
}
}
Expand All @@ -96,7 +99,9 @@ public function enqueue(): void
}

foreach ($blocks as $block) {
$this->app[$block]->enqueue();
if (true === $this->checkIfIsBlock($block)) {
$this->app[$block]->enqueue();
}
}
}

Expand All @@ -111,7 +116,7 @@ public function enqueueBlockEditorAssets(): void
$gutengood_blocks = [];

foreach ($blocks as $block) {
if (false === $this->app[$block]->editor_script) {
if (true === $this->checkIfIsBlock($block) && false === $this->app[$block]->editor_script) {
$gutengood_blocks[] = (object) [
'title' => $this->app[$block]->title,
'name' => $this->app[$block]->name,
Expand All @@ -135,7 +140,9 @@ public function blockEndpoint(): void
}

foreach ($blocks as $block) {
$this->app[$block]->blockEndpoint();
if (true === $this->checkIfIsBlock($block)) {
$this->app[$block]->blockEndpoint();
}
}
}

Expand All @@ -153,22 +160,29 @@ public function registerMeta(): void
}

foreach ($blocks as $block) {
array_map(function (array $meta): void {
if (empty($meta) || empty($meta['post_type']) || empty($meta['meta_key'])) {
return;
}

register_meta(
$meta['post_type'],
$meta['meta_key'],
[
'show_in_rest' => true,
'single' => true,
'type' => $meta['type'],
'default' => $meta['default'],
]
);
}, $this->app[$block]->blockMeta());
if (true === $this->checkIfIsBlock($block)) {
array_map(function (array $meta): void {
if (empty($meta) || empty($meta['post_type']) || empty($meta['meta_key'])) {
return;
}

register_meta(
$meta['post_type'],
$meta['meta_key'],
[
'show_in_rest' => true,
'single' => true,
'type' => $meta['type'],
'default' => $meta['default'],
]
);
}, $this->app[$block]->blockMeta());
}
}
}

public function checkIfIsBlock(string $block): bool
{
return 'block' === ($this->app[$block]->type ?? '');
}
}

0 comments on commit 2ab7a02

Please sign in to comment.