Skip to content

Commit

Permalink
Use enqueue_block_assets to pass $block to assets()
Browse files Browse the repository at this point in the history
  • Loading branch information
ouun authored Jan 27, 2025
1 parent b917ee7 commit f0356a8
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/AcfComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,19 @@ public function handleWidgets(): void
*/
protected function handleBlocks(): void
{
add_action('acf/init', function () {
add_action('enqueue_block_assets', function () {
foreach ($this->composers() as $composers) {
foreach ($composers as $composer) {
if (! is_a($composer, Block::class)) {
continue;
}

method_exists($composer, 'assets') && $composer->assets();
if (is_admin() || has_block($composer->namespace)) {
method_exists($composer, 'assets') && $composer->assets($composer->block);
}
}
}
}, 101);
});

add_action('acf_block_render_template', function ($block, $content, $is_preview, $post_id, $wp_block, $context) {
if (! class_exists($composer = $block['render_template'] ?? '')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ public function render($block, $content = '', $preview = false, $post_id = 0, $w
*
* @return void
*
* @deprecated Use `assets()` instead.
* @deprecated Use `assets($block)` instead.
*/
public function enqueue()
{
Expand Down
2 changes: 0 additions & 2 deletions src/Console/UpgradeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ public function handle()
'public function enqueue($block)' => 'public function assets(array $block): void',
'public function enqueue($block = [])' => 'public function assets(array $block): void',
'public function enqueue()' => 'public function assets(array $block): void',
'public function enqueue(array $block): void' => 'public function assets(): void',
'public function assets(array $block): void' => 'public function assets(): void',
'/->addFields\(\$this->get\((.*?)\)\)/' => fn ($match) => "->addPartial({$match[1]})",
'/->addLayout\(\$this->get\((.*?)\)\)/' => fn ($match) => "->addLayout({$match[1]})",
];
Expand Down
4 changes: 3 additions & 1 deletion src/Console/stubs/block.localized.stub
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ class DummyClass extends Block

/**
* Assets enqueued with 'enqueue_block_assets' when rendering the block.
*
* @link https://developer.wordpress.org/block-editor/how-to-guides/enqueueing-assets-in-the-editor/#editor-content-scripts-and-styles
*/
public function assets(): void
public function assets(array $block): void
{
//
}
Expand Down
4 changes: 3 additions & 1 deletion src/Console/stubs/block.stub
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ class DummyClass extends Block

/**
* Assets enqueued with 'enqueue_block_assets' when rendering the block.
*
* @link https://developer.wordpress.org/block-editor/how-to-guides/enqueueing-assets-in-the-editor/#editor-content-scripts-and-styles
*/
public function assets(): void
public function assets(array $block): void
{
//
}
Expand Down

0 comments on commit f0356a8

Please sign in to comment.