Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions data-machine-code.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,16 @@ function datamachine_code_maybe_upgrade_schema(): void {
* data-machine-code loads before data-machine).
*/
function datamachine_code_bootstrap() {
static $bootstrapped = false;

if ( $bootstrapped ) {
return;
}

if ( ! class_exists( 'DataMachine\Abilities\PermissionHelper' ) ) {
add_action( 'init', 'datamachine_code_bootstrap', 1 );
add_action( 'wp_abilities_api_init', 'datamachine_code_bootstrap', 1 );

add_action( 'admin_notices', function () {
?>
<div class="notice notice-error">
Expand All @@ -78,6 +87,8 @@ function datamachine_code_bootstrap() {
return;
}

$bootstrapped = true;

// Load Abilities (they self-register).
new \DataMachineCode\Abilities\GitHubAbilities();
new \DataMachineCode\Abilities\WorkspaceAbilities();
Expand Down
6 changes: 3 additions & 3 deletions inc/Abilities/CodeTaskAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public function __construct() {
return;
}

if ( ! class_exists( 'WP_Ability' ) ) {
if ( ! function_exists( 'wp_register_ability' ) ) {
add_action( 'wp_abilities_api_init', function (): void {
if ( self::$registered || ! class_exists( 'WP_Ability' ) ) {
if ( self::$registered || ! function_exists( 'wp_register_ability' ) ) {
return;
}

Expand All @@ -34,7 +34,7 @@ public function __construct() {
return;
}

if ( function_exists( 'doing_action' ) && ( doing_action( 'wp_abilities_api_init' ) || did_action( 'wp_abilities_api_init' ) ) ) {
if ( function_exists( 'doing_action' ) && doing_action( 'wp_abilities_api_init' ) ) {
$this->register();
} else {
add_action( 'wp_abilities_api_init', array( $this, 'register' ) );
Expand Down
6 changes: 3 additions & 3 deletions inc/Abilities/GitHubAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public function __construct() {
if ( self::$registered ) {
return;
}
if ( ! class_exists( 'WP_Ability' ) ) {
if ( ! function_exists( 'wp_register_ability' ) ) {
add_action( 'wp_abilities_api_init', function (): void {
if ( self::$registered || ! class_exists( 'WP_Ability' ) ) {
if ( self::$registered || ! function_exists( 'wp_register_ability' ) ) {
return;
}

Expand Down Expand Up @@ -1332,7 +1332,7 @@ private function registerAbilities(): void {
);
};

if ( doing_action( 'wp_abilities_api_init' ) || did_action( 'wp_abilities_api_init' ) ) {
if ( doing_action( 'wp_abilities_api_init' ) ) {
$register_callback();
} else {
add_action( 'wp_abilities_api_init', $register_callback );
Expand Down
6 changes: 3 additions & 3 deletions inc/Abilities/GitSyncAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function __construct() {
if ( self::$registered ) {
return;
}
if ( ! class_exists( 'WP_Ability' ) ) {
if ( ! function_exists( 'wp_register_ability' ) ) {
add_action( 'wp_abilities_api_init', function (): void {
if ( self::$registered || ! class_exists( 'WP_Ability' ) ) {
if ( self::$registered || ! function_exists( 'wp_register_ability' ) ) {
return;
}

Expand Down Expand Up @@ -311,7 +311,7 @@ private function registerAbilities(): void {
);
};

if ( doing_action( 'wp_abilities_api_init' ) || did_action( 'wp_abilities_api_init' ) ) {
if ( doing_action( 'wp_abilities_api_init' ) ) {
$register_callback();
} else {
add_action( 'wp_abilities_api_init', $register_callback );
Expand Down
6 changes: 3 additions & 3 deletions inc/Abilities/WordPressRuntimeAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function __construct() {
return;
}

if ( ! class_exists( 'WP_Ability' ) ) {
if ( ! function_exists( 'wp_register_ability' ) ) {
add_action( 'wp_abilities_api_init', function (): void {
if ( self::$registered || ! class_exists( 'WP_Ability' ) ) {
if ( self::$registered || ! function_exists( 'wp_register_ability' ) ) {
return;
}

Expand Down Expand Up @@ -155,7 +155,7 @@ private function registerAbilities(): void {
);
};

if ( function_exists( 'doing_action' ) && ( doing_action( 'wp_abilities_api_init' ) || did_action( 'wp_abilities_api_init' ) ) ) {
if ( function_exists( 'doing_action' ) && doing_action( 'wp_abilities_api_init' ) ) {
$register_callback();
return;
}
Expand Down
6 changes: 3 additions & 3 deletions inc/Abilities/WorkspaceAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function __construct() {
return;
}

if ( ! class_exists( 'WP_Ability' ) ) {
if ( ! function_exists( 'wp_register_ability' ) ) {
add_action( 'wp_abilities_api_init', function (): void {
if ( self::$registered || ! class_exists( 'WP_Ability' ) ) {
if ( self::$registered || ! function_exists( 'wp_register_ability' ) ) {
return;
}

Expand Down Expand Up @@ -2167,7 +2167,7 @@ private function registerAbilities(): void {
}
};

if ( doing_action( 'wp_abilities_api_init' ) || did_action( 'wp_abilities_api_init' ) ) {
if ( doing_action( 'wp_abilities_api_init' ) ) {
$register_callback();
} else {
add_action( 'wp_abilities_api_init', $register_callback );
Expand Down
2 changes: 1 addition & 1 deletion inc/Abilities/WorkspaceDiffAbilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function __construct() {
);
};

if ( did_action( 'wp_abilities_api_init' ) ) {
if ( doing_action( 'wp_abilities_api_init' ) ) {
$register_callback();
return;
}
Expand Down
8 changes: 3 additions & 5 deletions tests/smoke-deferred-ability-registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ class Workspace {
$GLOBALS['datamachine_code_registered_abilities'] = array();
$GLOBALS['datamachine_code_added_actions'] = array();

function wp_register_ability( string $name, array $definition ): void {
$GLOBALS['datamachine_code_registered_abilities'][ $name ] = $definition;
}

function doing_action( string $hook ): bool {
return false;
}
Expand Down Expand Up @@ -67,7 +63,9 @@ function add_action( string $hook, callable $callback, int $priority = 10 ): voi
$assert( 'constructors defer when WP_Ability is unavailable', 2 === count( $GLOBALS['datamachine_code_added_actions'] ) );
$assert( 'constructors do not register before WP_Ability is available', array() === $GLOBALS['datamachine_code_registered_abilities'] );

class WP_Ability {}
function wp_register_ability( string $name, array $definition ): void {
$GLOBALS['datamachine_code_registered_abilities'][ $name ] = $definition;
}

foreach ( $GLOBALS['datamachine_code_added_actions'] as $action ) {
if ( 'wp_abilities_api_init' === $action['hook'] ) {
Expand Down
2 changes: 1 addition & 1 deletion tests/smoke-late-ability-registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function wp_register_ability( string $name, array $definition ): void {
}

function doing_action( string $hook ): bool {
return false;
return 'wp_abilities_api_init' === $hook;
}

function did_action( string $hook ): int {
Expand Down
Loading