Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Improve option page awareness in fields and blocks (#250)
Browse files Browse the repository at this point in the history
* πŸ§‘β€πŸ’» Ensure option pages are loaded before fields and blocks
* πŸ”§ Set the `acf/init` priority to `100` to improve compatibility
* πŸ”§ Allow the `acf/init` hook priority to be overridden if needed
  • Loading branch information
Log1x committed Jun 6, 2024
1 parent d380557 commit 7e3c6fa
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/AcfComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ class AcfComposer
/**
* The deferred composers.
*/
protected array $deferredComposers = [];
protected array $deferredOptions = [];

/**
* The pending composers.
*/
protected array $pendingComposers = [];

/**
* The legacy widgets.
Expand Down Expand Up @@ -93,7 +98,7 @@ public function boot(): void
$this->handleBlocks();
$this->handleWidgets();

add_filter('acf/init', fn () => $this->handleComposers());
add_filter('acf/init', fn () => $this->handleComposers(), config('acf.hookPriority', 100));

$this->booted = true;
}
Expand All @@ -105,10 +110,18 @@ public function handleComposers(): void
{
foreach ($this->composers as $namespace => $composers) {
foreach ($composers as $i => $composer) {
if (! is_subclass_of($composer, Options::class)) {
$this->pendingComposers[$namespace][] = $composer;

unset($this->composers[$namespace][$i]);

continue;
}

$composer = $composer::make($this);

if (is_subclass_of($composer, Options::class) && ! is_null($composer->parent)) {
$this->deferredComposers[$namespace][] = $composer;
if (! is_null($composer->parent)) {
$this->deferredOptions[$namespace][] = $composer;

unset($this->composers[$namespace][$i]);

Expand All @@ -119,13 +132,20 @@ public function handleComposers(): void
}
}

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

$this->deferredComposers = [];
foreach ($this->pendingComposers as $namespace => $composers) {
foreach ($composers as $composer) {
$this->composers[$namespace][] = $composer::make($this)->handle();
}
}

$this->deferredOptions = [];
$this->pendingComposers = [];
}

/**
Expand Down

0 comments on commit 7e3c6fa

Please sign in to comment.