Skip to content

Commit 31dd20b

Browse files
authored
fix: register code abilities during api hook replay (#439)
1 parent 765a341 commit 31dd20b

9 files changed

Lines changed: 31 additions & 22 deletions

data-machine-code.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,16 @@ function datamachine_code_maybe_upgrade_schema(): void {
6767
* data-machine-code loads before data-machine).
6868
*/
6969
function datamachine_code_bootstrap() {
70+
static $bootstrapped = false;
71+
72+
if ( $bootstrapped ) {
73+
return;
74+
}
75+
7076
if ( ! class_exists( 'DataMachine\Abilities\PermissionHelper' ) ) {
77+
add_action( 'init', 'datamachine_code_bootstrap', 1 );
78+
add_action( 'wp_abilities_api_init', 'datamachine_code_bootstrap', 1 );
79+
7180
add_action( 'admin_notices', function () {
7281
?>
7382
<div class="notice notice-error">
@@ -78,6 +87,8 @@ function datamachine_code_bootstrap() {
7887
return;
7988
}
8089

90+
$bootstrapped = true;
91+
8192
// Load Abilities (they self-register).
8293
new \DataMachineCode\Abilities\GitHubAbilities();
8394
new \DataMachineCode\Abilities\WorkspaceAbilities();

inc/Abilities/CodeTaskAbilities.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public function __construct() {
2222
return;
2323
}
2424

25-
if ( ! class_exists( 'WP_Ability' ) ) {
25+
if ( ! function_exists( 'wp_register_ability' ) ) {
2626
add_action( 'wp_abilities_api_init', function (): void {
27-
if ( self::$registered || ! class_exists( 'WP_Ability' ) ) {
27+
if ( self::$registered || ! function_exists( 'wp_register_ability' ) ) {
2828
return;
2929
}
3030

@@ -34,7 +34,7 @@ public function __construct() {
3434
return;
3535
}
3636

37-
if ( function_exists( 'doing_action' ) && ( doing_action( 'wp_abilities_api_init' ) || did_action( 'wp_abilities_api_init' ) ) ) {
37+
if ( function_exists( 'doing_action' ) && doing_action( 'wp_abilities_api_init' ) ) {
3838
$this->register();
3939
} else {
4040
add_action( 'wp_abilities_api_init', array( $this, 'register' ) );

inc/Abilities/GitHubAbilities.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ public function __construct() {
8686
if ( self::$registered ) {
8787
return;
8888
}
89-
if ( ! class_exists( 'WP_Ability' ) ) {
89+
if ( ! function_exists( 'wp_register_ability' ) ) {
9090
add_action( 'wp_abilities_api_init', function (): void {
91-
if ( self::$registered || ! class_exists( 'WP_Ability' ) ) {
91+
if ( self::$registered || ! function_exists( 'wp_register_ability' ) ) {
9292
return;
9393
}
9494

@@ -1332,7 +1332,7 @@ private function registerAbilities(): void {
13321332
);
13331333
};
13341334

1335-
if ( doing_action( 'wp_abilities_api_init' ) || did_action( 'wp_abilities_api_init' ) ) {
1335+
if ( doing_action( 'wp_abilities_api_init' ) ) {
13361336
$register_callback();
13371337
} else {
13381338
add_action( 'wp_abilities_api_init', $register_callback );

inc/Abilities/GitSyncAbilities.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public function __construct() {
3030
if ( self::$registered ) {
3131
return;
3232
}
33-
if ( ! class_exists( 'WP_Ability' ) ) {
33+
if ( ! function_exists( 'wp_register_ability' ) ) {
3434
add_action( 'wp_abilities_api_init', function (): void {
35-
if ( self::$registered || ! class_exists( 'WP_Ability' ) ) {
35+
if ( self::$registered || ! function_exists( 'wp_register_ability' ) ) {
3636
return;
3737
}
3838

@@ -311,7 +311,7 @@ private function registerAbilities(): void {
311311
);
312312
};
313313

314-
if ( doing_action( 'wp_abilities_api_init' ) || did_action( 'wp_abilities_api_init' ) ) {
314+
if ( doing_action( 'wp_abilities_api_init' ) ) {
315315
$register_callback();
316316
} else {
317317
add_action( 'wp_abilities_api_init', $register_callback );

inc/Abilities/WordPressRuntimeAbilities.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public function __construct() {
2121
return;
2222
}
2323

24-
if ( ! class_exists( 'WP_Ability' ) ) {
24+
if ( ! function_exists( 'wp_register_ability' ) ) {
2525
add_action( 'wp_abilities_api_init', function (): void {
26-
if ( self::$registered || ! class_exists( 'WP_Ability' ) ) {
26+
if ( self::$registered || ! function_exists( 'wp_register_ability' ) ) {
2727
return;
2828
}
2929

@@ -155,7 +155,7 @@ private function registerAbilities(): void {
155155
);
156156
};
157157

158-
if ( function_exists( 'doing_action' ) && ( doing_action( 'wp_abilities_api_init' ) || did_action( 'wp_abilities_api_init' ) ) ) {
158+
if ( function_exists( 'doing_action' ) && doing_action( 'wp_abilities_api_init' ) ) {
159159
$register_callback();
160160
return;
161161
}

inc/Abilities/WorkspaceAbilities.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public function __construct() {
3333
return;
3434
}
3535

36-
if ( ! class_exists( 'WP_Ability' ) ) {
36+
if ( ! function_exists( 'wp_register_ability' ) ) {
3737
add_action( 'wp_abilities_api_init', function (): void {
38-
if ( self::$registered || ! class_exists( 'WP_Ability' ) ) {
38+
if ( self::$registered || ! function_exists( 'wp_register_ability' ) ) {
3939
return;
4040
}
4141

@@ -2167,7 +2167,7 @@ private function registerAbilities(): void {
21672167
}
21682168
};
21692169

2170-
if ( doing_action( 'wp_abilities_api_init' ) || did_action( 'wp_abilities_api_init' ) ) {
2170+
if ( doing_action( 'wp_abilities_api_init' ) ) {
21712171
$register_callback();
21722172
} else {
21732173
add_action( 'wp_abilities_api_init', $register_callback );

inc/Abilities/WorkspaceDiffAbilities.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function __construct() {
9999
);
100100
};
101101

102-
if ( did_action( 'wp_abilities_api_init' ) ) {
102+
if ( doing_action( 'wp_abilities_api_init' ) ) {
103103
$register_callback();
104104
return;
105105
}

tests/smoke-deferred-ability-registration.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ class Workspace {
2929
$GLOBALS['datamachine_code_registered_abilities'] = array();
3030
$GLOBALS['datamachine_code_added_actions'] = array();
3131

32-
function wp_register_ability( string $name, array $definition ): void {
33-
$GLOBALS['datamachine_code_registered_abilities'][ $name ] = $definition;
34-
}
35-
3632
function doing_action( string $hook ): bool {
3733
return false;
3834
}
@@ -67,7 +63,9 @@ function add_action( string $hook, callable $callback, int $priority = 10 ): voi
6763
$assert( 'constructors defer when WP_Ability is unavailable', 2 === count( $GLOBALS['datamachine_code_added_actions'] ) );
6864
$assert( 'constructors do not register before WP_Ability is available', array() === $GLOBALS['datamachine_code_registered_abilities'] );
6965

70-
class WP_Ability {}
66+
function wp_register_ability( string $name, array $definition ): void {
67+
$GLOBALS['datamachine_code_registered_abilities'][ $name ] = $definition;
68+
}
7169

7270
foreach ( $GLOBALS['datamachine_code_added_actions'] as $action ) {
7371
if ( 'wp_abilities_api_init' === $action['hook'] ) {

tests/smoke-late-ability-registration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function wp_register_ability( string $name, array $definition ): void {
3838
}
3939

4040
function doing_action( string $hook ): bool {
41-
return false;
41+
return 'wp_abilities_api_init' === $hook;
4242
}
4343

4444
function did_action( string $hook ): int {

0 commit comments

Comments
 (0)