Fixed by WordPress update - cause unknown.
I have a couple of plugins that use various custom WP-CLI commands I've written.
I've copied then from one WordPress project – where it's working correctly – to another on the same server, where the output order is getting jumbled up.
(The same thing happens both on my development VM and production server.)
The function mixes methods from the WP-CLI API and functions from php-cli-tools which it's dependent on.
This is one of my commands (there are couple of extra functions called to display various prompts...)
function add_user() {
WP_CLI::line( 'Add a user by hand. * indicates required fields. ' );
$questions = [
'id' => 'Unique ID (entirely optional, same as the S001,S002... column in CSV file)',
'first_name' => 'First name *',
'last_name' => 'Last name *',
'user_email' => 'Email address *',
'user_pass' => "Password (or leave blank and we'll create one",
'company' => 'Company',
'position' => 'Position (i.e. job)',
];
$data = $this->add_user_questions( $questions );
WP_CLI::line( ' ' );
foreach ( $questions as $k => $v ) {
WP_CLI::line( $v . ': ' . $data[ $k ] );
}
$confirmed = cli\choose( 'Are you happy with your answers?', 'yn', 'n' );
if ( 'y' !== strtolower( $confirmed ) ) {
WP_CLI::error( 'Cancelled.' );
}
$data['line'] = 1; // used to indicate line number when reading a file. we only have 1 user, so we set it to 1.
// Ask them to choose a group
$groups = [
'1' => 'sponsors',
'2' => 'exhibitors',
'3' => 'owners_sponsors',
'4' => 'owners_exhibitors'
];
foreach ( $groups as $k => $v ) {
WP_CLI::line( $k . '. ' . $v );
}
$group = \cli\choose( 'Which group?', implode( '', range( 1, 4 ) ), null ); // it'll keep asking if invalid
$data['group'] = $groups[ $group ]; // map back to array
$send_email = \cli\choose( 'Send them an automated email?', 'yn', null );
if ( strtoupper( $send_email ) == 'Y' ) {
$send_email = true;
} else {
$send_email = false;
}
$new_user = Nominations_Public::import_user( $data, $send_email );
if ( get_class( $new_user ) ) {
WP_CLI::success( 'The new WordPress user_id is ' . $new_user->ID );
} else {
WP_CLI::error( 'Unknown error.' );
}
}
private function add_user_questions( $questions ) {
$req_fields = [ 'first_name', 'last_name', 'user_email' ];
$data = [ ];
foreach ( $questions as $k => $v ) {
$data[ $k ] = $this->add_user_prompt_for_question( $v, in_array( $k, $req_fields ) );
switch ( $k ) {
case 'id':
$data[ $k ] = strtoupper( $data[ $k ] );
if ( ! Nominations_Public::verify_unique_csv_id( $data[ $k ] ) ) {
WP_CLI::error( 'Sorry, that unique ID is already taken. (You can leave it blank.)' );
}
break;
case 'user_email':
if ( email_exists( $data['user_email'] ) ) {
WP_CLI::error( 'Sorry, email address already exists. Remember you may be able to use a + sign.' );
}
break;
case 'user_pass':
if ( empty( $data[ $k ] ) ) {
$data[ $k ] = wp_generate_password();
}
break;
}
}
return $data;
}
private function add_user_prompt_for_question( $question, $required = false ) {
$default = ( true === $required ) ? false : ''; // if default not provided, \cli\prompt\ will force an answer
return trim( \cli\prompt( $question . '? ', $default ) );
}
Here's the CLI input/output when running it:
Unique ID (entirely optional, same as the S001,S002... column in CSV file)? :
First name *? : Foo
Last name *? : Bar
Email address *? : [email protected]
Password (or leave blank and we'll create one? :
Company? :
Position (i.e. job)? :
Are you happy with your answers?? [y/N]y
Which group?? [1/2/3/4]1
Send them an automated email?? [y/n]n
Success: The new WordPress user_id is 9
Add a user by hand. * indicates required fields.
Unique ID (entirely optional, same as the S001,S002... column in CSV file):
First name *: Foo
Last name *: Bar
Email address *: [email protected]
Password (or leave blank and we'll create one: WJ8bMnAb6bxD
Company:
Position (i.e. job):
1. sponsors
2. exhibitors
3. owners_sponsors
4. owners_exhibitors
(line 1) - added as WP_User 9
You'll note the WP_CLI::line output is being delayed; the \cli\prompt
and \cli\choose
calls (plus the WP_CLI::success message) seem to be happening first (for example, the list of groups is being printed at the very end, when it should come before the 'Which group?' prompt.)
If I change instances of WP_CLI::line
to `\cli\line', this seems to fix them.
But that's not supposed to be the correct syntax, and it doesn't explain why it's working in another copy of WordPress on the same server.
More than one plugin uses WP_CLI so I use require_once
to include it, like this:
if ( defined( 'WP_CLI' ) && WP_CLI ) {
// Command line support for development
require_once plugin_dir_path( dirname( __FILE__ ) ) . '/wp-cli.php';
}
Also, in PHPStorm: even though I've defined /Users/wt/vm/wordpress/www/wp-cli/php
(which is where my copy of WP-CLI is) in the Include Path, on the installation that has the problem the calls to \cli\choose
are highlighted in red as an "Undefined namespace" error.
Any suggestions?