Skip to content

Commit f2e0cec

Browse files
authored
Merge pull request #254 from ernilambar/129-format-core-update
2 parents cd8cb68 + 598a5b7 commit f2e0cec

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

features/core-update.feature

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,69 @@ Feature: Update WordPress core
3030
6.2
3131
"""
3232

33+
@require-php-7.0
34+
Scenario: Output in JSON format
35+
Given a WP install
36+
And I try `wp theme install twentytwenty --activate`
37+
38+
When I run `wp core download --version=6.6 --force`
39+
Then STDOUT should not be empty
40+
41+
When I run `wp eval 'echo $GLOBALS["wp_version"];'`
42+
Then STDOUT should be:
43+
"""
44+
6.6
45+
"""
46+
47+
When I run `wget http://wordpress.org/wordpress-6.8.zip --quiet`
48+
And I run `wp core update wordpress-6.8.zip --format=json`
49+
Then STDOUT should be:
50+
"""
51+
[{"name":"core","old_version":"6.6","new_version":"6.8","status":"Updated"}]
52+
"""
53+
54+
@require-php-7.0
55+
Scenario: Output in CSV format
56+
Given a WP install
57+
And I try `wp theme install twentytwenty --activate`
58+
59+
When I run `wp core download --version=6.6 --force`
60+
Then STDOUT should not be empty
61+
62+
When I run `wp eval 'echo $GLOBALS["wp_version"];'`
63+
Then STDOUT should be:
64+
"""
65+
6.6
66+
"""
67+
68+
When I run `wget http://wordpress.org/wordpress-6.8.zip --quiet`
69+
And I run `wp core update wordpress-6.8.zip --format=csv`
70+
Then STDOUT should be:
71+
"""
72+
name,old_version,new_version,status
73+
core,6.6,6.8,Updated
74+
"""
75+
76+
@require-php-7.0
77+
Scenario: Output in table format
78+
Given a WP install
79+
And I try `wp theme install twentytwenty --activate`
80+
81+
When I run `wp core download --version=6.6 --force`
82+
Then STDOUT should not be empty
83+
84+
When I run `wp eval 'echo $GLOBALS["wp_version"];'`
85+
Then STDOUT should be:
86+
"""
87+
6.6
88+
"""
89+
90+
When I run `wget http://wordpress.org/wordpress-6.8.zip --quiet`
91+
And I run `wp core update wordpress-6.8.zip --format=table`
92+
Then STDOUT should end with a table containing rows:
93+
| name | old_version | new_version | status |
94+
| core | 6.6 | 6.8 | Updated |
95+
3396
# This test downgrades to an older WordPress version, but the SQLite plugin requires 6.4+
3497
@require-mysql
3598
Scenario: Update to the latest minor release (PHP 7.2 compatible with WP >= 4.9)

src/Core_Command.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use WP_CLI\Iterators\Table as TableIterator;
66
use WP_CLI\Utils;
77
use WP_CLI\Formatter;
8+
use WP_CLI\Loggers;
89
use WP_CLI\WpOrgApi;
910

1011
/**
@@ -1073,6 +1074,15 @@ private static function get_core_checksums( $version, $locale, $insecure ) {
10731074
* [--locale=<locale>]
10741075
* : Select which language you want to download.
10751076
*
1077+
* [--format=<format>]
1078+
* : Render output in a particular format.
1079+
* ---
1080+
* options:
1081+
* - table
1082+
* - csv
1083+
* - json
1084+
* ---
1085+
*
10761086
* [--insecure]
10771087
* : Retry download without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
10781088
*
@@ -1104,7 +1114,7 @@ private static function get_core_checksums( $version, $locale, $insecure ) {
11041114
* @alias upgrade
11051115
*
11061116
* @param array{0?: string} $args Positional arguments.
1107-
* @param array{minor?: bool, version?: string, force?: bool, locale?: string, insecure?: bool} $assoc_args Associative arguments.
1117+
* @param array{minor?: bool, version?: string, force?: bool, locale?: string, insecure?: bool, format?: string} $assoc_args Associative arguments.
11081118
*/
11091119
public function update( $args, $assoc_args ) {
11101120
global $wp_version;
@@ -1116,6 +1126,11 @@ public function update( $args, $assoc_args ) {
11161126
$assoc_args['version'] = 'nightly';
11171127
}
11181128

1129+
if ( ! empty( $assoc_args['format'] ) && in_array( $assoc_args['format'], [ 'json', 'csv' ], true ) ) {
1130+
$logger = new Loggers\Quiet( WP_CLI::get_runner()->in_color() );
1131+
WP_CLI::set_logger( $logger );
1132+
}
1133+
11191134
if ( ! empty( $args[0] ) ) {
11201135

11211136
// ZIP path or URL is given
@@ -1237,6 +1252,21 @@ public function update( $args, $assoc_args ) {
12371252
$locale = Utils\get_flag_value( $assoc_args, 'locale', get_locale() );
12381253
$this->cleanup_extra_files( $from_version, $to_version, $locale, $insecure );
12391254

1255+
$data = [
1256+
[
1257+
'name' => 'core',
1258+
'old_version' => $from_version,
1259+
'new_version' => $to_version,
1260+
'status' => 'Updated',
1261+
],
1262+
];
1263+
1264+
$format = Utils\get_flag_value( $assoc_args, 'format' );
1265+
1266+
if ( ! empty( $format ) ) {
1267+
Utils\format_items( $format, $data, [ 'name', 'old_version', 'new_version', 'status' ] );
1268+
}
1269+
12401270
WP_CLI::success( 'WordPress updated successfully.' );
12411271
}
12421272
} else {

0 commit comments

Comments
 (0)