Skip to content

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

Merged
schlessera merged 9 commits intowp-cli:masterfrom
jmichaelward:issue-4818-add-wordpress-action-to-initialize-plugin-commands
Jul 20, 2018
Merged

Trigger new 'cli_init' hook during WordPress plugins_loaded action.#4861
schlessera merged 9 commits intowp-cli:masterfrom
jmichaelward:issue-4818-add-wordpress-action-to-initialize-plugin-commands

Conversation

@jmichaelward
Copy link

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.

}

public function start() {
WP_CLI::add_wp_hook( 'plugins_loaded', function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

core custom-subcommand
"""

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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' ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

4
);

// Set up hook for WP-CLI packages to use.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
""" No newline at end of file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants