Skip to content

Commit f7d7505

Browse files
authored
Merge pull request #82 from wp-cli/copilot/add-hook-flag-to-eval-shell
2 parents e110626 + ee5a48c commit f7d7505

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

features/shell.feature

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,39 @@ Feature: WordPress REPL
7777
"""
7878
history: -1: invalid option
7979
"""
80+
81+
Scenario: Shell with hook parameter
82+
Given a WP install
83+
And a session file:
84+
"""
85+
did_action('init');
86+
"""
87+
88+
When I run `wp shell --basic --hook=init < session`
89+
Then STDOUT should contain:
90+
"""
91+
int(1)
92+
"""
93+
94+
Scenario: Shell with hook parameter using plugins_loaded hook
95+
Given a WP install
96+
And a session file:
97+
"""
98+
did_action('plugins_loaded');
99+
"""
100+
101+
When I run `wp shell --basic --hook=plugins_loaded < session`
102+
Then STDOUT should contain:
103+
"""
104+
int(1)
105+
"""
106+
107+
Scenario: Shell with hook parameter for hook that hasn't fired
108+
Given a WP install
109+
110+
When I try `wp shell --basic --hook=shutdown < /dev/null`
111+
Then STDERR should contain:
112+
"""
113+
Error: The 'shutdown' hook has not fired yet
114+
"""
115+
And the return code should be 1

src/Shell_Command.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,57 @@ class Shell_Command extends WP_CLI_Command {
1919
* : Force the use of WP-CLI's built-in PHP REPL, even if the Boris or
2020
* PsySH PHP REPLs are available.
2121
*
22+
* [--hook=<hook>]
23+
* : Ensure that a specific WordPress action hook has fired before starting the shell.
24+
* This validates that the preconditions associated with that hook are met.
25+
* Only hooks that have already been triggered can be used (e.g., init, plugins_loaded, wp_loaded).
26+
* ---
27+
* default: ''
28+
* ---
29+
*
2230
* ## EXAMPLES
2331
*
2432
* # Call get_bloginfo() to get the name of the site.
2533
* $ wp shell
2634
* wp> get_bloginfo( 'name' );
2735
* => string(6) "WP-CLI"
36+
*
37+
* # Start a shell, ensuring the 'init' hook has already fired.
38+
* $ wp shell --hook=init
2839
*/
2940
public function __invoke( $_, $assoc_args ) {
41+
$hook = Utils\get_flag_value( $assoc_args, 'hook', '' );
42+
43+
// No hook specified, start immediately.
44+
if ( ! $hook ) {
45+
$this->start_shell( $assoc_args );
46+
return;
47+
}
48+
49+
// Check if the hook has already fired.
50+
if ( did_action( $hook ) ) {
51+
// Hook already fired, start the shell immediately.
52+
$this->start_shell( $assoc_args );
53+
return;
54+
}
55+
56+
// Hook hasn't fired yet.
57+
WP_CLI::error(
58+
sprintf(
59+
"The '%s' hook has not fired yet. " .
60+
'The shell command runs after WordPress is loaded, so only hooks that have already been triggered can be used. ' .
61+
'Common hooks that are available include: init, plugins_loaded, wp_loaded.',
62+
$hook
63+
)
64+
);
65+
}
66+
67+
/**
68+
* Start the shell REPL.
69+
*
70+
* @param array<string,bool|string> $assoc_args Associative arguments.
71+
*/
72+
private function start_shell( $assoc_args ) {
3073
$class = WP_CLI\Shell\REPL::class;
3174

3275
$implementations = array(

0 commit comments

Comments
 (0)