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

Add --format flag in core update #254

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions features/core-update.feature
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ Feature: Update WordPress core
4.0
"""

@require-php-7.0
Scenario: Output in JSON format
ernilambar marked this conversation as resolved.
Show resolved Hide resolved
Given a WP install
And I try `wp theme install twentytwenty --activate`

When I run `wp core download --version=6.0 --force`
Then STDOUT should not be empty

When I run `wp eval 'echo $GLOBALS["wp_version"];'`
Then STDOUT should be:
"""
6.0
"""

When I run `wget http://wordpress.org/wordpress-6.1.zip --quiet`
And I run `wp core update wordpress-6.1.zip --format=json`
Then STDOUT should be:
"""
[{"name":"core","old_version":"6.0","new_version":"6.1","status":"Updated"}]
"""

# PHP 7.1 needs WP 3.9 (due to wp_check_php_mysql_versions(), see trac changeset [27257]),
# and travis doesn't install mysql extension by default for PHP 7.0.
@less-than-php-7
Expand Down
30 changes: 30 additions & 0 deletions src/Core_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use WP_CLI\Iterators\Table as TableIterator;
use WP_CLI\Utils;
use WP_CLI\Formatter;
use WP_CLI\Loggers;
use WP_CLI\WpOrgApi;

/**
Expand Down Expand Up @@ -1064,6 +1065,15 @@ private static function get_core_checksums( $version, $locale, $insecure ) {
* [--locale=<locale>]
* : Select which language you want to download.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* options:
* - table
* - csv
* - json
* ---
*
* [--insecure]
* : Retry download without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
*
Expand Down Expand Up @@ -1109,6 +1119,11 @@ public function update( $args, $assoc_args ) {
$assoc_args['version'] = 'nightly';
}

if ( ! empty( $assoc_args['format'] ) && in_array( $assoc_args['format'], [ 'json', 'csv' ], true ) ) {
$logger = new Loggers\Quiet( WP_CLI::get_runner()->in_color() );
WP_CLI::set_logger( $logger );
}

if ( ! empty( $args[0] ) ) {

// ZIP path or URL is given
Expand Down Expand Up @@ -1214,6 +1229,21 @@ public function update( $args, $assoc_args ) {
$locale = (string) Utils\get_flag_value( $assoc_args, 'locale', get_locale() );
$this->cleanup_extra_files( $from_version, $to_version, $locale, $insecure );

$data = [
[
'name' => 'core',
'old_version' => $from_version,
'new_version' => $to_version,
'status' => 'Updated',
],
];

$format = Utils\get_flag_value( $assoc_args, 'format' );

if ( ! empty( $format ) ) {
Utils\format_items( $format, $data, [ 'name', 'old_version', 'new_version', 'status' ] );
}

WP_CLI::success( 'WordPress updated successfully.' );
}
} else {
Expand Down