@@ -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