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

Merged
merged 9 commits into from Jul 20, 2018

Conversation

2 participants
@jmichaelward
Copy link
Contributor

jmichaelward commented Jul 20, 2018

This PR will call do_action( 'cli_init' ) during the plugins_loaded event, which will allow developers to hook their custom WP-CLI commands into
WordPress without having to check for the definition and existence of the WP_CLI constant.

It includes one Behat test to validate that the action is fired.

Closes #4818.

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

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

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

@schlessera

schlessera Jul 20, 2018

Member

CS: No period after the scenario name.

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.

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

@@ -1487,6 +1486,10 @@ function( $url, $path, $scheme, $blog_id ) {
4
);
// Set up hook for WP-CLI packages to use.

This comment has been minimized.

@schlessera

schlessera Jul 20, 2018

Member

This is misleading, WP-CLI packages don't need this.
It should be something like:

// Set up hook for plugins & themes to conditionally add WP-CLI commands.
Then STDOUT should contain:
"""
wp custom
"""

This comment has been minimized.

@schlessera

schlessera Jul 20, 2018

Member

CS: Missing new line.

@schlessera schlessera added this to the 2.0.0 milestone Jul 20, 2018

@schlessera schlessera merged commit c988a2c into wp-cli:master Jul 20, 2018

1 check failed

continuous-integration/travis-ci/pr The Travis CI build could not complete due to an error
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.