Skip to content

Commit 5104a4b

Browse files
authored
Merge pull request #393 from up1512001/up1512001/fix/5860
Added `--minor` and `--patch` CLI option in `wp theme update`
2 parents 955bd94 + 6925f2c commit 5104a4b

File tree

6 files changed

+93
-25
lines changed

6 files changed

+93
-25
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,12 @@ wp theme update [<theme>...] [--all] [--exclude=<theme-names>] [--format=<format
13781378
[--exclude=<theme-names>]
13791379
Comma separated list of theme names that should be excluded from updating.
13801380

1381+
[--minor]
1382+
Only perform updates for minor releases (e.g. from 1.3 to 1.4 instead of 2.0)
1383+
1384+
[--patch]
1385+
Only perform updates for patch releases (e.g. from 1.3 to 1.3.3 instead of 1.4)
1386+
13811387
[--format=<format>]
13821388
Render output in a particular format.
13831389
---

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
],
1919
"require": {
2020
"composer/semver": "^1.4 || ^2 || ^3",
21-
"wp-cli/wp-cli": "^2.5.1"
21+
"wp-cli/wp-cli": "^2.10"
2222
},
2323
"require-dev": {
2424
"wp-cli/cache-command": "^2.0",

features/theme-update.feature

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,45 @@ Feature: Update WordPress themes
113113
"""
114114
Error: Can't find the requested theme's version 1.4.2 in the WordPress.org theme repository (HTTP code 404).
115115
"""
116+
117+
Scenario: Error when both --minor and --patch are provided
118+
Given a WP install
119+
120+
When I try `wp theme update --patch --minor --all`
121+
Then STDERR should be:
122+
"""
123+
Error: --minor and --patch cannot be used together.
124+
"""
125+
And the return code should be 1
126+
127+
Scenario: Update a theme to its latest minor release
128+
Given a WP install
129+
And I run `wp theme install --force twentytwelve --version=3.0`
130+
131+
When I run `wp theme update twentytwelve --minor`
132+
Then STDOUT should contain:
133+
"""
134+
Success: Updated 1 of 1 themes.
135+
"""
136+
137+
When I run `wp theme get twentytwelve --field=version`
138+
Then STDOUT should be:
139+
"""
140+
3.9
141+
"""
142+
143+
Scenario: Update a theme to its latest patch release
144+
Given a WP install
145+
And I run `wp theme install --force twentytwelve --version=1.1`
146+
147+
When I run `wp theme update twentytwelve --patch`
148+
Then STDOUT should contain:
149+
"""
150+
Success: Updated 1 of 1 themes.
151+
"""
152+
153+
When I run `wp theme get twentytwelve --field=version`
154+
Then STDOUT should be:
155+
"""
156+
1.1.1
157+
"""

src/Theme_Command.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ protected function get_item_list() {
439439
protected function filter_item_list( $items, $args ) {
440440
$theme_files = array();
441441
foreach ( $args as $arg ) {
442-
$theme_files[] = $this->fetcher->get_check( $arg )->get_stylesheet_directory();
442+
$theme_files[] = $this->fetcher->get_check( $arg )->get_stylesheet();
443443
}
444444

445445
return Utils\pick_fields( $items, $theme_files );
@@ -589,6 +589,12 @@ public function get( $args, $assoc_args ) {
589589
* [--exclude=<theme-names>]
590590
* : Comma separated list of theme names that should be excluded from updating.
591591
*
592+
* [--minor]
593+
* : Only perform updates for minor releases (e.g. from 1.3 to 1.4 instead of 2.0)
594+
*
595+
* [--patch]
596+
* : Only perform updates for patch releases (e.g. from 1.3 to 1.3.3 instead of 1.4)
597+
*
592598
* [--format=<format>]
593599
* : Render output in a particular format.
594600
* ---

src/WP_CLI/CommandWithUpgrade.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,14 @@ protected function update_many( $args, $assoc_args ) {
348348
$minor = (bool) Utils\get_flag_value( $assoc_args, 'minor', false );
349349
$patch = (bool) Utils\get_flag_value( $assoc_args, 'patch', false );
350350

351-
if ( 'plugin' === $this->item_type
352-
&& ( $minor || $patch ) ) {
351+
if (
352+
in_array( $this->item_type, [ 'plugin', 'theme' ], true ) &&
353+
( $minor || $patch )
354+
) {
353355
$type = $minor ? 'minor' : 'patch';
354356
$insecure = (bool) Utils\get_flag_value( $assoc_args, 'insecure', false );
355357

356-
$items_to_update = self::get_minor_or_patch_updates( $items_to_update, $type, $insecure, true );
358+
$items_to_update = self::get_minor_or_patch_updates( $items_to_update, $type, $insecure, true, $this->item_type );
357359
}
358360

359361
$exclude = Utils\get_flag_value( $assoc_args, 'exclude' );
@@ -368,11 +370,10 @@ protected function update_many( $args, $assoc_args ) {
368370
}
369371
unset( $items_to_update[ $plugin->file ] );
370372
} elseif ( 'theme' === $this->item_type ) {
371-
$theme_root = get_theme_root() . '/' . $item;
372-
if ( ! is_dir( $theme_root ) ) {
373-
continue;
373+
$theme = wp_get_theme( $item );
374+
if ( $theme->exists() ) {
375+
unset( $items_to_update[ $theme->get_stylesheet() ] );
374376
}
375-
unset( $items_to_update[ $theme_root ] );
376377
}
377378
}
378379
}
@@ -429,8 +430,13 @@ protected function update_many( $args, $assoc_args ) {
429430
$transient_filter = function ( $transient ) use ( $items_to_update ) {
430431
foreach ( $items_to_update as $name => $item_data ) {
431432
if ( isset( $transient->response[ $name ] ) ) {
432-
$transient->response[ $name ]->new_version = $item_data['update_version'];
433-
$transient->response[ $name ]->package = $item_data['update_package'];
433+
if ( is_object( $transient->response[ $name ] ) ) {
434+
$transient->response[ $name ]->new_version = $item_data['update_version'];
435+
$transient->response[ $name ]->package = $item_data['update_package'];
436+
} else {
437+
$transient->response[ $name ]['new_version'] = $item_data['update_version'];
438+
$transient->response[ $name ]['package'] = $item_data['update_package'];
439+
}
434440
}
435441
}
436442
return $transient;
@@ -611,19 +617,27 @@ private function get_color( $status ) {
611617
}
612618

613619
/**
614-
* Get the minor or patch version for plugins with available updates
620+
* Get the minor or patch version for plugins and themes with available updates
615621
*
616-
* @param array $items Plugins with updates.
622+
* @param array $items Items with updates.
617623
* @param string $type Either 'minor' or 'patch'.
618624
* @param bool $insecure Whether to retry without certificate validation on TLS handshake failure.
619625
* @param bool $require_stable Whether to require stable version when comparing versions.
626+
* @param string $item_type Item type, either 'plugin' or 'theme'.
620627
* @return array
621628
*/
622-
private function get_minor_or_patch_updates( $items, $type, $insecure, $require_stable ) {
629+
private function get_minor_or_patch_updates( $items, $type, $insecure, $require_stable, $item_type ) {
623630
$wp_org_api = new WpOrgApi( [ 'insecure' => $insecure ] );
624631
foreach ( $items as $i => $item ) {
625632
try {
626-
$data = $wp_org_api->get_plugin_info( $item['name'] );
633+
$data = call_user_func(
634+
[ $wp_org_api, "get_{$item_type}_info" ],
635+
$item['name'],
636+
// The default.
637+
'en_US',
638+
// We are only interested in the versions field.
639+
[ 'versions' => true ]
640+
);
627641
} catch ( Exception $exception ) {
628642
unset( $items[ $i ] );
629643
continue;

src/WP_CLI/ParseThemeNameInput.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,38 +76,38 @@ private function get_all_themes() {
7676
}
7777

7878
foreach ( wp_get_themes() as $key => $theme ) {
79-
$file = $theme->get_stylesheet_directory();
79+
$stylesheet = $theme->get_stylesheet();
8080

81-
$update_info = ( isset( $all_update_info->response[ $theme->get_stylesheet() ] ) && null !== $all_update_info->response[ $theme->get_stylesheet() ] ) ? (array) $all_update_info->response[ $theme->get_stylesheet() ] : null;
81+
$update_info = ( isset( $all_update_info->response[ $stylesheet ] ) && null !== $all_update_info->response[ $theme->get_stylesheet() ] ) ? (array) $all_update_info->response[ $theme->get_stylesheet() ] : null;
8282

83-
$items[ $file ] = [
83+
$items[ $stylesheet ] = [
8484
'name' => $key,
8585
'status' => $this->get_status( $theme ),
8686
'update' => (bool) $update_info,
8787
'update_version' => isset( $update_info['new_version'] ) ? $update_info['new_version'] : null,
8888
'update_package' => isset( $update_info['package'] ) ? $update_info['package'] : null,
8989
'version' => $theme->get( 'Version' ),
90-
'update_id' => $theme->get_stylesheet(),
90+
'update_id' => $stylesheet,
9191
'title' => $theme->get( 'Name' ),
9292
'description' => wordwrap( $theme->get( 'Description' ) ),
9393
'author' => $theme->get( 'Author' ),
94-
'auto_update' => in_array( $theme->get_stylesheet(), $auto_updates, true ),
94+
'auto_update' => in_array( $stylesheet, $auto_updates, true ),
9595
];
9696

9797
// Compare version and update information in theme list.
9898
if ( isset( $theme_version_info[ $key ] ) && false === $theme_version_info[ $key ] ) {
99-
$items[ $file ]['update'] = 'version higher than expected';
99+
$items[ $stylesheet ]['update'] = 'version higher than expected';
100100
}
101101

102102
if ( is_multisite() ) {
103103
if ( ! empty( $site_enabled[ $key ] ) && ! empty( $network_enabled[ $key ] ) ) {
104-
$items[ $file ]['enabled'] = 'network,site';
104+
$items[ $stylesheet ]['enabled'] = 'network,site';
105105
} elseif ( ! empty( $network_enabled[ $key ] ) ) {
106-
$items[ $file ]['enabled'] = 'network';
106+
$items[ $stylesheet ]['enabled'] = 'network';
107107
} elseif ( ! empty( $site_enabled[ $key ] ) ) {
108-
$items[ $file ]['enabled'] = 'site';
108+
$items[ $stylesheet ]['enabled'] = 'site';
109109
} else {
110-
$items[ $file ]['enabled'] = 'no';
110+
$items[ $stylesheet ]['enabled'] = 'no';
111111
}
112112
}
113113
}

0 commit comments

Comments
 (0)