From c07d7447627da296144efb5071bf16118cdac771 Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Tue, 19 May 2026 23:09:23 -0400 Subject: [PATCH] fix: defer code ability registration until api loads --- inc/Abilities/CodeTaskAbilities.php | 14 ++- inc/Abilities/GitHubAbilities.php | 12 ++- inc/Abilities/GitSyncAbilities.php | 12 ++- inc/Abilities/WordPressRuntimeAbilities.php | 12 ++- inc/Abilities/WorkspaceAbilities.php | 12 ++- tests/smoke-deferred-ability-registration.php | 88 +++++++++++++++++++ 6 files changed, 141 insertions(+), 9 deletions(-) create mode 100644 tests/smoke-deferred-ability-registration.php diff --git a/inc/Abilities/CodeTaskAbilities.php b/inc/Abilities/CodeTaskAbilities.php index 30e7b85..3e053fa 100644 --- a/inc/Abilities/CodeTaskAbilities.php +++ b/inc/Abilities/CodeTaskAbilities.php @@ -18,7 +18,19 @@ class CodeTaskAbilities { private static bool $registered = false; public function __construct() { - if ( ! class_exists( 'WP_Ability' ) || self::$registered ) { + if ( self::$registered ) { + return; + } + + if ( ! class_exists( 'WP_Ability' ) ) { + add_action( 'wp_abilities_api_init', function (): void { + if ( self::$registered || ! class_exists( 'WP_Ability' ) ) { + return; + } + + $this->register(); + self::$registered = true; + } ); return; } diff --git a/inc/Abilities/GitHubAbilities.php b/inc/Abilities/GitHubAbilities.php index e10719a..6e1c06d 100644 --- a/inc/Abilities/GitHubAbilities.php +++ b/inc/Abilities/GitHubAbilities.php @@ -83,10 +83,18 @@ class GitHubAbilities { const MAX_PER_PAGE = 100; public function __construct() { - if ( ! class_exists( 'WP_Ability' ) ) { + if ( self::$registered ) { return; } - if ( self::$registered ) { + if ( ! class_exists( 'WP_Ability' ) ) { + add_action( 'wp_abilities_api_init', function (): void { + if ( self::$registered || ! class_exists( 'WP_Ability' ) ) { + return; + } + + $this->registerAbilities(); + self::$registered = true; + } ); return; } $this->registerAbilities(); diff --git a/inc/Abilities/GitSyncAbilities.php b/inc/Abilities/GitSyncAbilities.php index db9a4e2..c30a41a 100644 --- a/inc/Abilities/GitSyncAbilities.php +++ b/inc/Abilities/GitSyncAbilities.php @@ -27,10 +27,18 @@ class GitSyncAbilities { private static bool $registered = false; public function __construct() { - if ( ! class_exists( 'WP_Ability' ) ) { + if ( self::$registered ) { return; } - if ( self::$registered ) { + if ( ! class_exists( 'WP_Ability' ) ) { + add_action( 'wp_abilities_api_init', function (): void { + if ( self::$registered || ! class_exists( 'WP_Ability' ) ) { + return; + } + + $this->registerAbilities(); + self::$registered = true; + } ); return; } $this->registerAbilities(); diff --git a/inc/Abilities/WordPressRuntimeAbilities.php b/inc/Abilities/WordPressRuntimeAbilities.php index 31089aa..0b039cd 100644 --- a/inc/Abilities/WordPressRuntimeAbilities.php +++ b/inc/Abilities/WordPressRuntimeAbilities.php @@ -17,11 +17,19 @@ class WordPressRuntimeAbilities { private static bool $registered = false; public function __construct() { - if ( ! class_exists( 'WP_Ability' ) ) { + if ( self::$registered ) { return; } - if ( self::$registered ) { + if ( ! class_exists( 'WP_Ability' ) ) { + add_action( 'wp_abilities_api_init', function (): void { + if ( self::$registered || ! class_exists( 'WP_Ability' ) ) { + return; + } + + $this->registerAbilities(); + self::$registered = true; + } ); return; } diff --git a/inc/Abilities/WorkspaceAbilities.php b/inc/Abilities/WorkspaceAbilities.php index e471d77..f55acad 100644 --- a/inc/Abilities/WorkspaceAbilities.php +++ b/inc/Abilities/WorkspaceAbilities.php @@ -29,11 +29,19 @@ class WorkspaceAbilities { private static bool $registered = false; public function __construct() { - if ( ! class_exists( 'WP_Ability' ) ) { + if ( self::$registered ) { return; } - if ( self::$registered ) { + if ( ! class_exists( 'WP_Ability' ) ) { + add_action( 'wp_abilities_api_init', function (): void { + if ( self::$registered || ! class_exists( 'WP_Ability' ) ) { + return; + } + + $this->registerAbilities(); + self::$registered = true; + } ); return; } diff --git a/tests/smoke-deferred-ability-registration.php b/tests/smoke-deferred-ability-registration.php new file mode 100644 index 0000000..51f8bbb --- /dev/null +++ b/tests/smoke-deferred-ability-registration.php @@ -0,0 +1,88 @@ +