Skip to content

Commit 7c9bfe0

Browse files
authored
Merge pull request #475 from wp-cli/copilot/add-plugin-theme-language-update-check
Add check-update commands for plugins and themes
2 parents c6618db + 3d67d7e commit 7c9bfe0

5 files changed

Lines changed: 390 additions & 0 deletions

File tree

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"plugin path",
5757
"plugin search",
5858
"plugin status",
59+
"plugin check-update",
5960
"plugin toggle",
6061
"plugin uninstall",
6162
"plugin update",
@@ -76,6 +77,7 @@
7677
"theme path",
7778
"theme search",
7879
"theme status",
80+
"theme check-update",
7981
"theme update",
8082
"theme mod list"
8183
]
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
Feature: Check for plugin updates
2+
3+
@require-wp-5.2
4+
Scenario: Check for plugin updates with no updates available
5+
Given a WP install
6+
7+
When I run `wp plugin install wordpress-importer --activate`
8+
Then STDOUT should not be empty
9+
10+
When I run `wp plugin check-update --all`
11+
Then STDOUT should contain:
12+
"""
13+
Success: All plugins are up to date.
14+
"""
15+
16+
When I run `wp plugin check-update wordpress-importer`
17+
Then STDOUT should contain:
18+
"""
19+
Success: All plugins are up to date.
20+
"""
21+
22+
Scenario: Check for plugin updates should throw an error unless --all given
23+
Given a WP install
24+
25+
When I try `wp plugin check-update`
26+
Then the return code should be 1
27+
And STDERR should be:
28+
"""
29+
Error: Please specify one or more plugins, or use --all.
30+
"""
31+
And STDOUT should be empty
32+
33+
@require-wp-5.2
34+
Scenario: Check for specific plugin updates
35+
Given a WP install
36+
37+
When I run `wp plugin install wordpress-importer --version=0.5`
38+
Then STDOUT should not be empty
39+
40+
When I run `wp plugin check-update wordpress-importer --format=csv`
41+
Then STDOUT should contain:
42+
"""
43+
wordpress-importer,inactive,0.5,
44+
"""
45+
46+
@require-wp-5.2
47+
Scenario: Check for all plugin updates with --all flag
48+
Given a WP install
49+
50+
When I run `wp plugin install wordpress-importer --version=0.5 --activate`
51+
Then STDOUT should not be empty
52+
53+
When I run `wp plugin check-update --all --format=csv`
54+
Then STDOUT should contain:
55+
"""
56+
wordpress-importer,active,0.5,
57+
"""
58+
59+
@require-wp-5.2
60+
Scenario: Check for plugin updates in different output formats
61+
Given a WP install
62+
63+
When I run `wp plugin install wordpress-importer --version=0.5`
64+
Then STDOUT should not be empty
65+
66+
When I run `wp plugin check-update wordpress-importer --format=json`
67+
Then STDOUT should be JSON containing:
68+
"""
69+
[{"name":"wordpress-importer","status":"inactive","version":"0.5"}]
70+
"""
71+
72+
When I run `wp plugin check-update wordpress-importer --format=csv`
73+
Then STDOUT should contain:
74+
"""
75+
name,status,version,update_version
76+
"""
77+
And STDOUT should contain:
78+
"""
79+
wordpress-importer,inactive,0.5
80+
"""
81+
82+
@require-wp-5.2
83+
Scenario: Check for plugin updates with custom fields
84+
Given a WP install
85+
86+
When I run `wp plugin install wordpress-importer --version=0.5`
87+
Then STDOUT should not be empty
88+
89+
When I run `wp plugin check-update wordpress-importer --fields=name,version`
90+
Then STDOUT should be a table containing rows:
91+
| name | version |
92+
| wordpress-importer | 0.5 |
93+
94+
Scenario: Check for invalid plugin should error
95+
Given a WP install
96+
97+
When I try `wp plugin check-update invalid-plugin-name`
98+
Then STDERR should contain:
99+
"""
100+
Warning: The 'invalid-plugin-name' plugin could not be found.
101+
"""
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
Feature: Check for theme updates
2+
3+
Scenario: Check for theme updates with no updates available
4+
Given a WP install
5+
6+
When I run `wp theme install twentytwelve --force`
7+
And I run `wp theme update --all`
8+
And I run `wp theme check-update --all`
9+
Then STDOUT should contain:
10+
"""
11+
Success: All themes are up to date.
12+
"""
13+
14+
When I run `wp theme check-update twentytwelve`
15+
Then STDOUT should contain:
16+
"""
17+
Success: All themes are up to date.
18+
"""
19+
20+
Scenario: Check for theme updates should throw an error unless --all given
21+
Given a WP install
22+
23+
When I try `wp theme check-update`
24+
Then the return code should be 1
25+
And STDERR should be:
26+
"""
27+
Error: Please specify one or more themes, or use --all.
28+
"""
29+
And STDOUT should be empty
30+
31+
Scenario: Check for specific theme updates
32+
Given a WP install
33+
34+
When I run `wp theme install twentyfourteen --version=1.0 --force`
35+
Then STDOUT should not be empty
36+
37+
When I run `wp theme install twentytwelve --force`
38+
Then STDOUT should not be empty
39+
40+
When I run `wp theme check-update twentyfourteen --format=csv`
41+
Then STDOUT should contain:
42+
"""
43+
twentyfourteen,inactive,1.0,
44+
"""
45+
46+
Scenario: Check for all theme updates with --all flag
47+
Given a WP install
48+
49+
When I run `wp theme install twentyfourteen --version=1.0 --force`
50+
Then STDOUT should not be empty
51+
52+
When I run `wp theme check-update --all --format=csv`
53+
Then STDOUT should contain:
54+
"""
55+
twentyfourteen,inactive,1.0,
56+
"""
57+
58+
Scenario: Check for theme updates in different output formats
59+
Given a WP install
60+
61+
When I run `wp theme install twentyfourteen --version=1.0 --force`
62+
Then STDOUT should not be empty
63+
64+
When I run `wp theme check-update twentyfourteen --format=json`
65+
Then STDOUT should be JSON containing:
66+
"""
67+
[{"name":"twentyfourteen","status":"inactive","version":"1.0"}]
68+
"""
69+
70+
When I run `wp theme check-update twentyfourteen --format=csv`
71+
Then STDOUT should contain:
72+
"""
73+
name,status,version,update_version
74+
"""
75+
And STDOUT should contain:
76+
"""
77+
twentyfourteen,inactive,1.0
78+
"""
79+
80+
Scenario: Check for theme updates with custom fields
81+
Given a WP install
82+
83+
When I run `wp theme install twentyfourteen --version=1.0 --force`
84+
Then STDOUT should not be empty
85+
86+
When I run `wp theme check-update twentyfourteen --fields=name,version`
87+
Then STDOUT should be a table containing rows:
88+
| name | version |
89+
| twentyfourteen | 1.0 |
90+
91+
Scenario: Check for invalid theme should error
92+
Given a WP install
93+
94+
When I try `wp theme check-update invalid-theme-name`
95+
Then STDERR should contain:
96+
"""
97+
Warning: The 'invalid-theme-name' theme could not be found.
98+
"""

src/Plugin_Command.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,100 @@ public function status( $args ) {
115115
parent::status( $args );
116116
}
117117

118+
/**
119+
* Checks for plugin updates without performing them.
120+
*
121+
* Lists the available plugin updates. Similar to `wp core check-update`.
122+
*
123+
* ## OPTIONS
124+
*
125+
* [<plugin>...]
126+
* : One or more plugins to check for updates.
127+
*
128+
* [--all]
129+
* : If set, all plugins will be checked for updates.
130+
*
131+
* [--field=<field>]
132+
* : Prints the value of a single field for each update.
133+
*
134+
* [--fields=<fields>]
135+
* : Limit the output to specific object fields. Defaults to name,status,version,update_version.
136+
*
137+
* [--format=<format>]
138+
* : Render output in a particular format.
139+
* ---
140+
* default: table
141+
* options:
142+
* - table
143+
* - csv
144+
* - json
145+
* - yaml
146+
* ---
147+
*
148+
* ## EXAMPLES
149+
*
150+
* # Check for plugin updates
151+
* $ wp plugin check-update
152+
* +-----------+--------+---------+----------------+
153+
* | name | status | version | update_version |
154+
* +-----------+--------+---------+----------------+
155+
* | akismet | active | 4.1.0 | 4.1.1 |
156+
* +-----------+--------+---------+----------------+
157+
*
158+
* # List plugins with available updates in JSON format
159+
* $ wp plugin check-update --format=json
160+
* [{"name":"akismet","status":"active","version":"4.1.0","update_version":"4.1.1"}]
161+
*
162+
* @subcommand check-update
163+
*/
164+
public function check_update( $args, $assoc_args ) {
165+
$all = Utils\get_flag_value( $assoc_args, 'all', false );
166+
167+
$args = $this->check_optional_args_and_all( $args, $all );
168+
if ( ! $args ) {
169+
return;
170+
}
171+
172+
// Force WordPress to check for updates.
173+
call_user_func( $this->upgrade_refresh );
174+
175+
if ( $all ) {
176+
// Get all plugins
177+
$items = $this->get_item_list();
178+
} else {
179+
// Get specific plugins and their update info
180+
$plugins = $this->fetcher->get_many( $args );
181+
$all_items = $this->get_item_list();
182+
$items = [];
183+
foreach ( $plugins as $plugin ) {
184+
if ( isset( $all_items[ $plugin->file ] ) ) {
185+
$items[ $plugin->file ] = $all_items[ $plugin->file ];
186+
}
187+
}
188+
}
189+
190+
// Filter to only plugins with available updates
191+
$items_with_updates = array_filter(
192+
$items,
193+
function ( $item ) {
194+
return 'available' === $item['update'];
195+
}
196+
);
197+
198+
if ( empty( $items_with_updates ) ) {
199+
WP_CLI::success( 'All plugins are up to date.' );
200+
return;
201+
}
202+
203+
// Set default fields for check-update output
204+
if ( ! isset( $assoc_args['fields'] ) ) {
205+
$assoc_args['fields'] = 'name,status,version,update_version';
206+
}
207+
208+
$formatter = $this->get_formatter( $assoc_args );
209+
$formatter->display_items( array_values( $items_with_updates ) );
210+
}
211+
118212
/**
119213
* Searches the WordPress.org plugin directory.
120214
*

0 commit comments

Comments
 (0)