Skip to content

Commit bb60d94

Browse files
Copilotswissspidy
andcommitted
Refine hook implementation logic for shell command
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent ea344fb commit bb60d94

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

‎src/Shell_Command.php‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,29 @@ public function __invoke( $_, $assoc_args ) {
3939
$hook = Utils\get_flag_value( $assoc_args, 'hook', '' );
4040

4141
if ( $hook ) {
42-
// Attach shell execution to the specified hook.
43-
add_action(
44-
$hook,
45-
function () use ( $assoc_args ) {
46-
$this->start_shell( $assoc_args );
47-
}
48-
);
49-
50-
// Trigger WordPress lifecycle to reach the hook.
51-
if ( ! did_action( $hook ) ) {
52-
// Hook hasn't fired yet, so we need to let WordPress run.
53-
// The hook will be called during the normal WordPress lifecycle.
54-
return;
55-
} else {
42+
// Check if the hook has already fired.
43+
if ( did_action( $hook ) ) {
5644
// Hook already fired, start the shell immediately.
5745
$this->start_shell( $assoc_args );
46+
} else {
47+
// Hook hasn't fired yet. We need to attach a callback and let WordPress continue.
48+
// Since shell is interactive and blocks execution, we'll attach the callback
49+
// and let it start when the hook fires naturally.
50+
$shell_started = false;
51+
add_action(
52+
$hook,
53+
function () use ( $assoc_args, &$shell_started ) {
54+
if ( ! $shell_started ) {
55+
$shell_started = true;
56+
$this->start_shell( $assoc_args );
57+
}
58+
},
59+
10
60+
);
61+
// Note: The shell will start when the hook fires during WordPress lifecycle.
62+
// Since commands typically run after WordPress is loaded, most common hooks
63+
// will have already fired. For hooks that haven't fired yet, the shell will
64+
// start when they do fire.
5865
}
5966
} else {
6067
// No hook specified, start immediately.

0 commit comments

Comments
 (0)