diff --git a/src/AcfComposer.php b/src/AcfComposer.php index f8ddcb7f..3ab20400 100644 --- a/src/AcfComposer.php +++ b/src/AcfComposer.php @@ -37,6 +37,11 @@ class AcfComposer */ protected array $deferredComposers = []; + /** + * The legacy widgets. + */ + protected array $legacyWidgets = []; + /** * The registered plugin paths. */ @@ -74,14 +79,6 @@ public static function make(Application $app): self return new static($app); } - /** - * Handle the ACF Composer instance. - */ - public function handle(): void - { - add_action('acf/init', fn () => $this->boot()); - } - /** * Boot the registered Composers. */ @@ -91,11 +88,33 @@ public function boot(): void return; } - $this->handleBlocks(); $this->registerDefaultPath(); + $this->handleBlocks(); + $this->handleWidgets(); + + add_filter('acf/init', fn () => $this->handleComposers()); + + $this->booted = true; + } + + /** + * Handle the Composer registration. + */ + public function handleComposers(): void + { foreach ($this->composers as $namespace => $composers) { foreach ($composers as $i => $composer) { + $composer = $composer::make($this); + + if (is_subclass_of($composer, Options::class) && ! is_null($composer->parent)) { + $this->deferredComposers[$namespace][] = $composer; + + unset($this->composers[$namespace][$i]); + + continue; + } + $this->composers[$namespace][$i] = $composer->handle(); } } @@ -107,8 +126,22 @@ public function boot(): void } $this->deferredComposers = []; + } - $this->booted = true; + /** + * Handle the Widget Composer registration. + */ + public function handleWidgets(): void + { + foreach ($this->legacyWidgets as $namespace => $composers) { + foreach ($composers as $composer) { + $composer = $composer::make($this); + + $this->composers[$namespace][] = $composer->handle(); + } + } + + $this->legacyWidgets = []; } /** @@ -196,10 +229,8 @@ public function register(string $composer, string $namespace): bool return false; } - $composer = $composer::make($this); - - if (is_subclass_of($composer, Options::class) && ! is_null($composer->parent)) { - $this->deferredComposers[$namespace][] = $composer; + if (is_subclass_of($composer, Widget::class)) { + $this->legacyWidgets[$namespace][] = $composer; return true; } diff --git a/src/Providers/AcfComposerServiceProvider.php b/src/Providers/AcfComposerServiceProvider.php index 974894f4..7cc5f67a 100644 --- a/src/Providers/AcfComposerServiceProvider.php +++ b/src/Providers/AcfComposerServiceProvider.php @@ -35,7 +35,7 @@ public function boot() $composer = $this->app->make('AcfComposer'); - $composer->handle(); + $composer->boot(); if ($this->app->runningInConsole()) { $this->commands([