Skip to content

Commit cef0970

Browse files
authored
Merge branch 'main' into copilot/fix-internal-variables-conflict
2 parents ecaf2e7 + de92dc6 commit cef0970

5 files changed

Lines changed: 94 additions & 2 deletions

File tree

.github/workflows/code-quality.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ on:
66
branches:
77
- main
88
- master
9+
schedule:
10+
- cron: '17 2 * * *' # Run every day on a seemly random time.
911

1012
jobs:
1113
code-quality:

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Quick links: [Using](#using) | [Installing](#installing) | [Contributing](#contr
1010
## Using
1111

1212
~~~
13-
wp shell [--basic]
13+
wp shell [--basic] [--hook=<hook>]
1414
~~~
1515

1616
`wp shell` allows you to evaluate PHP statements and expressions
@@ -25,13 +25,24 @@ that you can use within a WordPress plugin, for example.
2525
Force the use of WP-CLI's built-in PHP REPL, even if the Boris or
2626
PsySH PHP REPLs are available.
2727

28+
[--hook=<hook>]
29+
Ensure that a specific WordPress action hook has fired before starting the shell.
30+
This validates that the preconditions associated with that hook are met.
31+
Only hooks that have already been triggered can be used (e.g., init, plugins_loaded, wp_loaded).
32+
---
33+
default: ''
34+
---
35+
2836
**EXAMPLES**
2937

3038
# Call get_bloginfo() to get the name of the site.
3139
$ wp shell
3240
wp> get_bloginfo( 'name' );
3341
=> string(6) "WP-CLI"
3442

43+
# Start a shell, ensuring the 'init' hook has already fired.
44+
$ wp shell --hook=init
45+
3546
## Installing
3647

3748
This package is included with WP-CLI itself, no additional installation necessary.

features/shell.feature

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,39 @@ Feature: WordPress REPL
115115
"""
116116
=> string(15) "evl should work"
117117
"""
118+
119+
Scenario: Shell with hook parameter
120+
Given a WP install
121+
And a session file:
122+
"""
123+
did_action('init');
124+
"""
125+
126+
When I run `wp shell --basic --hook=init < session`
127+
Then STDOUT should contain:
128+
"""
129+
int(1)
130+
"""
131+
132+
Scenario: Shell with hook parameter using plugins_loaded hook
133+
Given a WP install
134+
And a session file:
135+
"""
136+
did_action('plugins_loaded');
137+
"""
138+
139+
When I run `wp shell --basic --hook=plugins_loaded < session`
140+
Then STDOUT should contain:
141+
"""
142+
int(1)
143+
"""
144+
145+
Scenario: Shell with hook parameter for hook that hasn't fired
146+
Given a WP install
147+
148+
When I try `wp shell --basic --hook=shutdown < /dev/null`
149+
Then STDERR should contain:
150+
"""
151+
Error: The 'shutdown' hook has not fired yet
152+
"""
153+
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(

src/WP_CLI/Shell/REPL.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private static function create_prompt_cmd( $prompt, $history_path ) {
121121
$prompt = escapeshellarg( $prompt );
122122
$history_path = escapeshellarg( $history_path );
123123
if ( getenv( 'WP_CLI_CUSTOM_SHELL' ) ) {
124-
$shell_binary = getenv( 'WP_CLI_CUSTOM_SHELL' );
124+
$shell_binary = (string) getenv( 'WP_CLI_CUSTOM_SHELL' );
125125
} else {
126126
$shell_binary = '/bin/bash';
127127
}

0 commit comments

Comments
 (0)