Fixed by WordPress update - cause unknown.
我有几个插件使用各种自定义
WP-CLI 我写的命令。
然后,我将WordPress项目从一个工作正常的WordPress项目复制到同一服务器上的另一个WordPress项目,该项目的输出顺序变得混乱。
(在我的开发VM和生产服务器上都会发生同样的情况。)
该函数混合了WP-CLI API 和功能来自php-cli-tools 这取决于它。
这是我的命令之一(调用了两个额外的函数来显示各种提示…)
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 ) );
}
以下是运行CLI时的CLI输入/输出:
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
您会注意到WP_CLI::line输出被延迟;这个
\\cli\\prompt
和
\\cli\\choose
调用(加上WP\\u CLI::success消息)似乎是首先发生的(例如,组列表是在最后打印的,这时应该在“Which group?”提示之前。)
If I change instances of WP_CLI::line
to `\\cli\\line\', this seems to fix them.
但这不应该是正确的语法,也不能解释为什么它在同一服务器上的另一个WordPress副本中工作。
多个插件使用WP\\u CLI,因此我使用require_once
将其包括在内,如下所示:
if ( defined( \'WP_CLI\' ) && WP_CLI ) {
// Command line support for development
require_once plugin_dir_path( dirname( __FILE__ ) ) . \'/wp-cli.php\';
}
另外,在PHPStorm中:尽管我已经定义了
/Users/wt/vm/wordpress/www/wp-cli/php
(这是我的WP-CLI副本所在的位置)位于包含路径中,位于调用有问题的安装上
\\cli\\choose
以红色突出显示为“未定义命名空间”错误。
有什么建议吗?