Add name context to before_invoke and after_invoke#5712
Add name context to before_invoke and after_invoke#5712danielbachhuber merged 1 commit intowp-cli:mainfrom
Conversation
|
PS: Passing I haven't done so in the PR in order to avoid too many modifications, but if there's interest I'll make another PR or edit this one. Thanks, |
danielbachhuber
left a comment
There was a problem hiding this comment.
Thanks for the PR, @coccoinomane !
It certainly seems relatively straightforward. Can you share a bit more detail on how you discovered this issue?
|
Ciao @danielbachhuber, and thank you for your reply! I had a command structured in this way: <?php
class TestHook
{
public function __invoke( $args )
{
WP_CLI::line( "In command" );
}
}
WP_CLI::add_command(
'test hook', // two words
'TestHook',
[
'before_invoke' => function() {
WP_CLI::line( "In before_invoke" );
},
'after_invoke' => function() {
WP_CLI::line( "In after_invoke" );
}
]
);When I invoked the command, I was suprised getting the following output: Please note that by changing the command's name to a single word (e.g. The problem originates here. The easiest fix would be to just remove the |
|
Thanks for the clarification, @coccoinomane ! I think the backwards-compat approach approach makes most sense too. The downside risk of unexpected explosions outweighs any upside of making this absolutely correct, in my opinion. I'll flag for a review from @schlessera too. |
|
Sure, thanks 👍 To add context, I am using the hooks in this new project > https://github.com/Idearia/wp-cli-command The idea is to register hooks by simply overriding a method 🙂 |
Hello!
Why: For
__invokecommands, thebefore_invokeandafter_invokehooks run twice, creating potential unexpected behavior.Solution: I propose an easy fix: pass the command name to the callback, so that it can choose when to run.
If you think it is a viable solution, I'll add tests :-)
Cheers,
Cocco