Skip to content

Commit 6d78633

Browse files
mrsdizzieswissspidydanielbachhuber
authored
Include any non default hook information in CompositeCommand (#5921)
--------- Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com> Co-authored-by: Daniel Bachhuber <daniel.bachhuber@automattic.com>
1 parent d96b792 commit 6d78633

5 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎features/help.feature‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,28 @@ Feature: Get help about WP-CLI commands
5353
"""
5454
And STDERR should be empty
5555

56+
Scenario: Include when the command is run if a non-standard hook.
57+
Given an empty directory
58+
59+
When I run `COLUMNS=80 wp help db`
60+
Then STDOUT should contain:
61+
"""
62+
Unless overridden, these commands run on the 'after_wp_config_load' hook,
63+
after wp-config.php has been loaded into scope.
64+
"""
65+
66+
When I run `COLUMNS=150 wp help db check`
67+
Then STDOUT should contain:
68+
"""
69+
This command runs on the 'after_wp_config_load' hook, after wp-config.php has been loaded into scope.
70+
"""
71+
72+
When I run `COLUMNS=150 wp help db size`
73+
Then STDOUT should not contain:
74+
"""
75+
This command runs on the
76+
"""
77+
5678
Scenario: Hide Global parameters when requested
5779
Given an empty directory
5880

‎php/WP_CLI/Dispatcher/CompositeCommand.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CompositeCommand {
1818
protected $shortdesc;
1919
protected $longdesc;
2020
protected $synopsis;
21+
protected $hook;
2122
protected $docparser;
2223

2324
protected $parent;
@@ -38,9 +39,11 @@ public function __construct( $parent, $name, $docparser ) {
3839
$this->shortdesc = $docparser->get_shortdesc();
3940
$this->longdesc = $docparser->get_longdesc();
4041
$this->docparser = $docparser;
42+
$this->hook = $parent->get_hook();
4143

4244
$when_to_invoke = $docparser->get_tag( 'when' );
4345
if ( $when_to_invoke ) {
46+
$this->hook = $when_to_invoke;
4447
WP_CLI::get_runner()->register_early_invoke( $when_to_invoke, $this );
4548
}
4649
}
@@ -122,6 +125,16 @@ public function get_shortdesc() {
122125
return $this->shortdesc;
123126
}
124127

128+
/**
129+
* Get the hook name for this composite
130+
* command.
131+
*
132+
* @return string
133+
*/
134+
public function get_hook() {
135+
return $this->hook;
136+
}
137+
125138
/**
126139
* Set the short description for this composite command.
127140
*

‎php/commands/src/CLI_Command.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ private function command_to_array( $command ) {
3939
'name' => $command->get_name(),
4040
'description' => $command->get_shortdesc(),
4141
'longdesc' => $command->get_longdesc(),
42+
'hook' => $command->get_hook(),
4243
];
4344

4445
foreach ( $command->get_subcommands() as $subcommand ) {

‎php/commands/src/Help_Command.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ private static function get_initial_markdown( $command ) {
157157
if ( $alias ) {
158158
$binding['alias'] = $alias;
159159
}
160+
$hook_name = $command->get_hook();
161+
$hook_description = $hook_name ? Utils\get_hook_description( $hook_name ) : null;
162+
if ( $hook_description && 'after_wp_load' !== $hook_name ) {
163+
if ( $command->can_have_subcommands() ) {
164+
$binding['shortdesc'] .= "\n\nUnless overridden, these commands run on the '$hook_name' hook, $hook_description";
165+
} else {
166+
$binding['shortdesc'] .= "\n\nThis command runs on the '$hook_name' hook, $hook_description";
167+
}
168+
}
160169

161170
if ( $command->can_have_subcommands() ) {
162171
$binding['has-subcommands']['subcommands'] = self::render_subcommands( $command );

‎php/utils.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,3 +1865,26 @@ function has_stdin() {
18651865

18661866
return 1 === $streams;
18671867
}
1868+
1869+
/**
1870+
* Return description of WP_CLI hooks used in @when tag
1871+
*
1872+
* @param string $hook Name of WP_CLI hook
1873+
*
1874+
* @return string|null
1875+
*/
1876+
function get_hook_description( $hook ) {
1877+
$events = [
1878+
'find_command_to_run_pre' => 'just before WP-CLI finds the command to run.',
1879+
'before_registering_contexts' => 'before the contexts are registered.',
1880+
'before_wp_load' => 'just before the WP load process begins.',
1881+
'before_wp_config_load' => 'after wp-config.php has been located.',
1882+
'after_wp_config_load' => 'after wp-config.php has been loaded into scope.',
1883+
'after_wp_load' => 'just after the WP load process has completed.',
1884+
];
1885+
1886+
if ( array_key_exists( $hook, $events ) ) {
1887+
return $events[ $hook ];
1888+
}
1889+
return null;
1890+
}

0 commit comments

Comments
 (0)