Skip to content

Commit 6dab878

Browse files
authored
fix: register code abilities after init (#437)
1 parent 541e391 commit 6dab878

6 files changed

Lines changed: 94 additions & 8 deletions

File tree

inc/Abilities/CodeTaskAbilities.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ public function __construct() {
2222
return;
2323
}
2424

25-
add_action( 'wp_abilities_api_init', array( $this, 'register' ) );
25+
if ( function_exists( 'doing_action' ) && ( doing_action( 'wp_abilities_api_init' ) || did_action( 'wp_abilities_api_init' ) ) ) {
26+
$this->register();
27+
} else {
28+
add_action( 'wp_abilities_api_init', array( $this, 'register' ) );
29+
}
2630
self::$registered = true;
2731
}
2832

inc/Abilities/GitHubAbilities.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,9 +1324,9 @@ private function registerAbilities(): void {
13241324
);
13251325
};
13261326

1327-
if ( doing_action( 'wp_abilities_api_init' ) ) {
1327+
if ( doing_action( 'wp_abilities_api_init' ) || did_action( 'wp_abilities_api_init' ) ) {
13281328
$register_callback();
1329-
} elseif ( ! did_action( 'wp_abilities_api_init' ) ) {
1329+
} else {
13301330
add_action( 'wp_abilities_api_init', $register_callback );
13311331
}
13321332
}

inc/Abilities/GitSyncAbilities.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,9 @@ private function registerAbilities(): void {
303303
);
304304
};
305305

306-
if ( doing_action( 'wp_abilities_api_init' ) ) {
306+
if ( doing_action( 'wp_abilities_api_init' ) || did_action( 'wp_abilities_api_init' ) ) {
307307
$register_callback();
308-
} elseif ( ! did_action( 'wp_abilities_api_init' ) ) {
308+
} else {
309309
add_action( 'wp_abilities_api_init', $register_callback );
310310
}
311311
}

inc/Abilities/WordPressRuntimeAbilities.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private function registerAbilities(): void {
147147
);
148148
};
149149

150-
if ( function_exists( 'doing_action' ) && doing_action( 'wp_abilities_api_init' ) ) {
150+
if ( function_exists( 'doing_action' ) && ( doing_action( 'wp_abilities_api_init' ) || did_action( 'wp_abilities_api_init' ) ) ) {
151151
$register_callback();
152152
return;
153153
}

inc/Abilities/WorkspaceAbilities.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,9 +2159,9 @@ private function registerAbilities(): void {
21592159
}
21602160
};
21612161

2162-
if ( doing_action( 'wp_abilities_api_init' ) ) {
2162+
if ( doing_action( 'wp_abilities_api_init' ) || did_action( 'wp_abilities_api_init' ) ) {
21632163
$register_callback();
2164-
} elseif ( ! did_action( 'wp_abilities_api_init' ) ) {
2164+
} else {
21652165
add_action( 'wp_abilities_api_init', $register_callback );
21662166
}
21672167
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Smoke test for ability classes instantiated after wp_abilities_api_init.
4+
*
5+
* Run: php tests/smoke-late-ability-registration.php
6+
*/
7+
8+
declare( strict_types=1 );
9+
10+
namespace DataMachine\Abilities {
11+
class PermissionHelper {
12+
public static function can_manage(): bool {
13+
return true;
14+
}
15+
}
16+
}
17+
18+
namespace DataMachineCode\Workspace {
19+
class Workspace {
20+
public const ARTIFACT_CLEANUP_DEFAULT_LIMIT = 100;
21+
}
22+
}
23+
24+
namespace {
25+
if ( ! defined( 'ABSPATH' ) ) {
26+
define( 'ABSPATH', sys_get_temp_dir() . '/data-machine-code-late-ability-registration/' );
27+
}
28+
29+
if ( ! class_exists( 'WP_Ability' ) ) {
30+
class WP_Ability {}
31+
}
32+
33+
$GLOBALS['datamachine_code_registered_abilities'] = array();
34+
$GLOBALS['datamachine_code_added_actions'] = array();
35+
36+
function wp_register_ability( string $name, array $definition ): void {
37+
$GLOBALS['datamachine_code_registered_abilities'][ $name ] = $definition;
38+
}
39+
40+
function doing_action( string $hook ): bool {
41+
return false;
42+
}
43+
44+
function did_action( string $hook ): int {
45+
return 'wp_abilities_api_init' === $hook ? 1 : 0;
46+
}
47+
48+
function add_action( string $hook, callable $callback, int $priority = 10 ): void {
49+
$GLOBALS['datamachine_code_added_actions'][] = compact( 'hook', 'callback', 'priority' );
50+
}
51+
52+
require __DIR__ . '/../inc/Abilities/WorkspaceAbilities.php';
53+
require __DIR__ . '/../inc/Abilities/CodeTaskAbilities.php';
54+
55+
new \DataMachineCode\Abilities\WorkspaceAbilities();
56+
new \DataMachineCode\Abilities\CodeTaskAbilities();
57+
58+
$failures = array();
59+
$assert = static function ( string $label, bool $condition ) use ( &$failures ): void {
60+
if ( $condition ) {
61+
echo " ok {$label}\n";
62+
return;
63+
}
64+
65+
$failures[] = $label;
66+
echo " fail {$label}\n";
67+
};
68+
69+
echo "Data Machine Code late ability registration - smoke\n";
70+
71+
$assert( 'workspace-show registers after wp_abilities_api_init fired', isset( $GLOBALS['datamachine_code_registered_abilities']['datamachine/workspace-show'] ) );
72+
$assert( 'workspace-worktree-add registers after wp_abilities_api_init fired', isset( $GLOBALS['datamachine_code_registered_abilities']['datamachine/workspace-worktree-add'] ) );
73+
$assert( 'create-code-task registers after wp_abilities_api_init fired', isset( $GLOBALS['datamachine_code_registered_abilities']['datamachine/create-code-task'] ) );
74+
$assert( 'late constructors do not add inert wp_abilities_api_init callbacks', array() === $GLOBALS['datamachine_code_added_actions'] );
75+
76+
if ( ! empty( $failures ) ) {
77+
echo "\nFAIL: " . count( $failures ) . " assertion(s) failed\n";
78+
exit( 1 );
79+
}
80+
81+
echo "\nOK\n";
82+
}

0 commit comments

Comments
 (0)