Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of report_batch_operation_results() for install commands #63

Merged
merged 9 commits into from Nov 30, 2018
15 changes: 11 additions & 4 deletions src/Core_Language_Command.php
Expand Up @@ -174,31 +174,38 @@ public function is_installed( $args, $assoc_args = array() ) {
*/
public function install( $args, $assoc_args ) {
$language_codes = (array) $args;
$count = count( $language_codes );

if( 1 < count( $language_codes ) && in_array( true , $assoc_args , true ) ){
if ( $count > 1 && in_array( true, $assoc_args, true ) ) {
WP_CLI::error( 'Only a single language can be active.' );
}

$available = $this->get_installed_languages();

$successes = $errors = $skips = 0;
foreach ( $language_codes as $language_code ) {

if ( in_array( $language_code, $available, true ) ) {
WP_CLI::warning( "Language '{$language_code}' already installed." );
\WP_CLI::log( "Language '{$language_code}' already installed." );
$skips++;
} else {
$response = $this->download_language_pack( $language_code );

if ( is_wp_error( $response ) ) {
WP_CLI::error( $response );
WP_CLI::warning( $response );
$errors++;
} else {
WP_CLI::success( 'Language installed.' );
\WP_CLI::log( 'Language installed.' );
$successes++;
}
}

if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'activate' ) ) {
$this->activate_language( $language_code );
}
}

\WP_CLI\Utils\report_batch_operation_results( 'language', 'install', $count, $successes, $errors, $skips );
}

/**
Expand Down
13 changes: 10 additions & 3 deletions src/Plugin_Language_Command.php
Expand Up @@ -206,23 +206,30 @@ public function is_installed( $args, $assoc_args = array() ) {
public function install( $args, $assoc_args ) {
$plugin = array_shift( $args );
$language_codes = (array) $args;
$count = count( $language_codes );

$available = $this->get_installed_languages( $plugin );

$successes = $errors = $skips = 0;
foreach ( $language_codes as $language_code ) {

if ( in_array( $language_code, $available, true ) ) {
\WP_CLI::warning( "Language '{$language_code}' already installed." );
\WP_CLI::log( "Language '{$language_code}' already installed." );
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this a skip or a success? Looks like media would treat it as a success.

https://github.com/wp-cli/media-command/blob/337459d/src/Media_Command.php#L590-L600

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking at the success message, I think we can leave it as is.

Success: Installed 0 of 1 languages (1 skipped).

Copy link
Member

Choose a reason for hiding this comment

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

Yes, makes sense to me.

$skips++;
} else {
$response = $this->download_language_pack( $language_code, $plugin );

if ( is_wp_error( $response ) ) {
\WP_CLI::error( $response );
WP_CLI::warning( $response );
$errors++;
} else {
\WP_CLI::success( 'Language installed.' );
\WP_CLI::log( 'Language installed.' );
$successes++;
}
}
}

\WP_CLI\Utils\report_batch_operation_results( 'language', 'install', $count, $successes, $errors, $skips );
}

/**
Expand Down
13 changes: 10 additions & 3 deletions src/Theme_Language_Command.php
Expand Up @@ -208,23 +208,30 @@ public function is_installed( $args, $assoc_args = array() ) {
public function install( $args, $assoc_args ) {
$theme = array_shift( $args );
$language_codes = (array) $args;
$count = count( $language_codes );

$available = $this->get_installed_languages( $theme );

$successes = $errors = $skips = 0;
foreach ( $language_codes as $language_code ) {

if ( in_array( $language_code, $available, true ) ) {
\WP_CLI::warning( "Language '{$language_code}' already installed." );
\WP_CLI::log( "Language '{$language_code}' already installed." );
$skips++;
} else {
$response = $this->download_language_pack( $language_code, $theme );

if ( is_wp_error( $response ) ) {
\WP_CLI::error( $response );
WP_CLI::warning( $response );
$errors++;
} else {
\WP_CLI::success( 'Language installed.' );
\WP_CLI::log( 'Language installed.' );
$successes++;
}
}
}

\WP_CLI\Utils\report_batch_operation_results( 'language', 'install', $count, $successes, $errors, $skips );
}

/**
Expand Down