From b7ed8b86a74dd02f10448303d36527ca017ac9ad Mon Sep 17 00:00:00 2001 From: Bart Jaskulski <bjaskulski@protonmail.com> Date: Fri, 27 Sep 2024 12:07:58 +0200 Subject: [PATCH] fix: defer hook execution to `plugins_loaded` event Signed-off-by: Bart Jaskulski <bjaskulski@protonmail.com> --- src/HookDriver/GenericDriver.php | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/HookDriver/GenericDriver.php b/src/HookDriver/GenericDriver.php index 9602623..5116742 100644 --- a/src/HookDriver/GenericDriver.php +++ b/src/HookDriver/GenericDriver.php @@ -20,17 +20,23 @@ class GenericDriver implements HookDriver { } public function register_hooks(): void { - foreach ( $this->definitions->load() as $definition ) { - if ( $definition->hook() ) { - add_action( - $definition->hook(), - function () use ( $definition ) { + // Load has to be deffered until plugins_loaded because classes may implement or extend interfaces/classes which doesn't exist yet. + add_action( + 'plugins_loaded', + function () { + foreach ( $this->definitions->load() as $definition ) { + if ( $definition->hook() === null ) { $this->binder->bind( $definition ); + continue; } - ); - } else { - $this->binder->bind( $definition ); - } - } + + add_action( + $definition->hook(), + fn () => $this->binder->bind( $definition ) + ); + } + }, + -50 + ); } } -- GitLab