Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger new 'cli_init' hook during WordPress plugins_loaded action. #4861

Copy path View file
@@ -1666,3 +1666,35 @@ Feature: WP-CLI Commands
"""
core custom-subcommand
"""

Scenario: An activated plugin should successfully add custom commands when hooked on the cli_init action.

This comment has been minimized.

Copy link
@schlessera

schlessera Jul 20, 2018

Member

CS: No period after the scenario name.

Given a WP installation
And a wp-content/plugins/custom-command/custom-cmd.php file:
"""
<?php
// Plugin Name: Custom Command
add_action( 'cli_init', function() {
require_once plugin_dir_path( __FILE__ ) . '/class-custom-command.php';
$command = new Custom_Command();
WP_CLI::add_command( 'custom', array( $command, 'do_custom_command' ) );

This comment has been minimized.

Copy link
@schlessera

schlessera Jul 20, 2018

Member

If you are not actually running the command, there's no point in creating a command implementation. This is probably the shortest form of a command that you can verify on wp help:

WP_CLI::add_command( 'custom', function () {} );

This way, you can get rid of the class-custom-command.php file and everything related.

} );
"""
And a wp-content/plugins/custom-command/class-custom-command.php file:
"""
<?php
class Custom_Command extends WP_CLI_Command {
/**
* Tell the whole world!
*/
public function do_custom_command() {
WP_CLI::success( "This command doesn't really do much, does it?" );
}
}
"""
And I run `wp plugin activate custom-command`
When I run `wp custom --help`
Then STDOUT should contain:
"""
wp custom
"""

This comment has been minimized.

Copy link
@schlessera

schlessera Jul 20, 2018

Member

CS: Missing new line.

Copy path View file
@@ -966,6 +966,9 @@ private function set_alias( $alias ) {
}
public function start() {
WP_CLI::add_wp_hook( 'plugins_loaded', function () {

This comment has been minimized.

Copy link
@schlessera

schlessera Jul 20, 2018

Member

I'm still not 100% certain this is the best possible timing for the action.
Here are some alternatives I'd like to discuss:

  1. Adding the action to the bootstrap process controlled here: https://github.com/wp-cli/wp-cli/blob/master/php/bootstrap.php#L15-L34 . This would let us provide the hook as early as possible. A good fit might be between RegisterFrameworkCommands and IncludeFallbackAutoloader.
  2. Add it after the $this->load_wordpress() call also found within the Runner::start() method. This would make the Plugin API be available without fail when cli_init is being used.

Both might be inconsequential if people only ever use the hook within plugins. But maybe they want to use it in a drop-in, for example? (not sure that even makes sense)

What are your thoughts on this, @jmichaelward?

do_action( 'cli_init' );
} );
// Enable PHP error reporting to stderr if testing. Will need to be re-enabled after WP loads.
if ( getenv( 'BEHAT_RUN' ) ) {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.