The class contains various public methods that are being resolved to subcommands as expected. A reduced example:
\nclass MyCommandClass {\n public function send_email(): void\n {\n wp_mail(\n $_ENV['LOGGER_MAIL_TO'],\n \"Hello there!\",\n \"Hello again :)\",\n );\n }\n}Now I need to overwrite the from header in this email. I'm using the filter wp_mail_from for this. But now the callback function is being listed as a subcommand, since it needs to be public (due to the way WP filters work):
class MyCommandClass {\n public function send_email(): void\n {\n add_filter('wp_mail_from', [$this, 'wp_mail_from']);\n wp_mail(\n $_ENV['LOGGER_MAIL_TO'],\n \"Hello there!\",\n \"Hello again :)\",\n );\n remove_filter('wp_mail_from', [$this, 'wp_mail_from']);\n }\n /**\n * Overwrite wp_mail_from\n */\n public function wp_mail_from(): string\n {\n return $_ENV['LOGGER_MAIL_FROM'];\n }\n}On the shell I now get this:
\n❯ wp my-command\nusage: wp my-command send_email\n or: wp my-command wp_mail_from # I don't want this!Is there a way to hide the filter callback wp_mail_from from WP_CLI?
If you're not opposed to a closure, I think this could work.
\n\\WP_CLI::add_command('my-command', 'MyCommandClass' );\n\nclass MyCommandClass {\n\tpublic function send_email(): void\n\t{\n\t\t$wp_mail_from = function(): string {\n\t\t\treturn $_ENV['LOGGER_MAIL_FROM'];\n\t\t};\n\t\tadd_filter('wp_mail_from', $wp_mail_from );\n\t\twp_mail(\n\t\t\t$_ENV['LOGGER_MAIL_TO'],\n\t\t\t\"Hello there!\",\n\t\t\t\"Hello again :)\",\n\t\t);\n\t\tremove_filter('wp_mail_from', $wp_mail_from );\n\t}\n}\n-
|
Hi there! I'd like to know if there is a way to hide public methods from a class-based custom WP_CLI command. I'm adding a custom command with subcommands like this: \WP_CLI::add_command('my-command', new MyCommandClass());The class contains various public methods that are being resolved to subcommands as expected. A reduced example: class MyCommandClass {
public function send_email(): void
{
wp_mail(
$_ENV['LOGGER_MAIL_TO'],
"Hello there!",
"Hello again :)",
);
}
}Now I need to overwrite the class MyCommandClass {
public function send_email(): void
{
add_filter('wp_mail_from', [$this, 'wp_mail_from']);
wp_mail(
$_ENV['LOGGER_MAIL_TO'],
"Hello there!",
"Hello again :)",
);
remove_filter('wp_mail_from', [$this, 'wp_mail_from']);
}
/**
* Overwrite wp_mail_from
*/
public function wp_mail_from(): string
{
return $_ENV['LOGGER_MAIL_FROM'];
}
}On the shell I now get this: ❯ wp my-command
usage: wp my-command send_email
or: wp my-command wp_mail_from # I don't want this!Is there a way to hide the filter callback |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
If you're not opposed to a closure, I think this could work. |
Beta Was this translation helpful? Give feedback.
If you're not opposed to a closure, I think this could work.