Skip to content

Commit cd83498

Browse files
Copilotswissspidy
andcommitted
Add check-update commands for plugins and themes
Co-authored-by: swissspidy <[email protected]>
1 parent e6f3051 commit cd83498

File tree

5 files changed

+423
-0
lines changed

5 files changed

+423
-0
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"plugin path",
5555
"plugin search",
5656
"plugin status",
57+
"plugin check-update",
5758
"plugin toggle",
5859
"plugin uninstall",
5960
"plugin update",
@@ -73,6 +74,7 @@
7374
"theme path",
7475
"theme search",
7576
"theme status",
77+
"theme check-update",
7678
"theme update",
7779
"theme mod list"
7880
]
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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`
11+
Then STDOUT should contain:
12+
"""
13+
Success: All plugins are up to date.
14+
"""
15+
And the return code should be 0
16+
17+
@require-wp-5.2
18+
Scenario: Check for plugin updates with updates available
19+
Given a WP install
20+
21+
When I run `wp plugin install wordpress-importer --version=0.5 --activate`
22+
Then STDOUT should not be empty
23+
24+
When I run `wp plugin check-update`
25+
Then STDOUT should be a table containing rows:
26+
| name | status | version |
27+
| wordpress-importer | active | 0.5 |
28+
And STDOUT should contain:
29+
"""
30+
update_version
31+
"""
32+
And the return code should be 0
33+
34+
@require-wp-5.2
35+
Scenario: Check for specific plugin updates
36+
Given a WP install
37+
38+
When I run `wp plugin install wordpress-importer --version=0.5`
39+
Then STDOUT should not be empty
40+
41+
When I run `wp plugin install akismet`
42+
Then STDOUT should not be empty
43+
44+
When I run `wp plugin check-update wordpress-importer`
45+
Then STDOUT should be a table containing rows:
46+
| name | status | version |
47+
| wordpress-importer | inactive | 0.5 |
48+
And STDOUT should contain:
49+
"""
50+
update_version
51+
"""
52+
And the return code should be 0
53+
54+
@require-wp-5.2
55+
Scenario: Check for all plugin updates with --all flag
56+
Given a WP install
57+
58+
When I run `wp plugin install wordpress-importer --version=0.5 --activate`
59+
Then STDOUT should not be empty
60+
61+
When I run `wp plugin check-update --all`
62+
Then STDOUT should be a table containing rows:
63+
| name | status | version |
64+
| wordpress-importer | active | 0.5 |
65+
And STDOUT should contain:
66+
"""
67+
update_version
68+
"""
69+
And the return code should be 0
70+
71+
@require-wp-5.2
72+
Scenario: Check for plugin updates in different output formats
73+
Given a WP install
74+
75+
When I run `wp plugin install wordpress-importer --version=0.5`
76+
Then STDOUT should not be empty
77+
78+
When I run `wp plugin check-update --format=json`
79+
Then STDOUT should be JSON containing:
80+
"""
81+
[{"name":"wordpress-importer","status":"inactive","version":"0.5"}]
82+
"""
83+
And the return code should be 0
84+
85+
When I run `wp plugin check-update --format=csv`
86+
Then STDOUT should contain:
87+
"""
88+
name,status,version,update_version
89+
"""
90+
And STDOUT should contain:
91+
"""
92+
wordpress-importer,inactive,0.5
93+
"""
94+
And the return code should be 0
95+
96+
@require-wp-5.2
97+
Scenario: Check for plugin updates with custom fields
98+
Given a WP install
99+
100+
When I run `wp plugin install wordpress-importer --version=0.5`
101+
Then STDOUT should not be empty
102+
103+
When I run `wp plugin check-update --fields=name,version`
104+
Then STDOUT should be a table containing rows:
105+
| name | version |
106+
| wordpress-importer | 0.5 |
107+
And the return code should be 0
108+
109+
@require-wp-5.2
110+
Scenario: Check for plugin updates when no specific plugin has updates
111+
Given a WP install
112+
113+
When I run `wp plugin install wordpress-importer`
114+
Then STDOUT should not be empty
115+
116+
When I run `wp plugin check-update wordpress-importer`
117+
Then STDOUT should contain:
118+
"""
119+
Success: All plugins are up to date.
120+
"""
121+
And the return code should be 0
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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`
7+
Then STDOUT should not be empty
8+
9+
When I run `wp theme check-update`
10+
Then STDOUT should contain:
11+
"""
12+
Success: All themes are up to date.
13+
"""
14+
And the return code should be 0
15+
16+
Scenario: Check for theme updates with updates available
17+
Given a WP install
18+
19+
When I run `wp theme install twentyfourteen --version=1.0`
20+
Then STDOUT should not be empty
21+
22+
When I run `wp theme check-update`
23+
Then STDOUT should be a table containing rows:
24+
| name | status | version |
25+
| twentyfourteen | inactive | 1.0 |
26+
And STDOUT should contain:
27+
"""
28+
update_version
29+
"""
30+
And the return code should be 0
31+
32+
Scenario: Check for specific theme updates
33+
Given a WP install
34+
35+
When I run `wp theme install twentyfourteen --version=1.0`
36+
Then STDOUT should not be empty
37+
38+
When I run `wp theme install twentytwelve`
39+
Then STDOUT should not be empty
40+
41+
When I run `wp theme check-update twentyfourteen`
42+
Then STDOUT should be a table containing rows:
43+
| name | status | version |
44+
| twentyfourteen | inactive | 1.0 |
45+
And STDOUT should contain:
46+
"""
47+
update_version
48+
"""
49+
And the return code should be 0
50+
51+
Scenario: Check for all theme updates with --all flag
52+
Given a WP install
53+
54+
When I run `wp theme install twentyfourteen --version=1.0`
55+
Then STDOUT should not be empty
56+
57+
When I run `wp theme check-update --all`
58+
Then STDOUT should be a table containing rows:
59+
| name | status | version |
60+
| twentyfourteen | inactive | 1.0 |
61+
And STDOUT should contain:
62+
"""
63+
update_version
64+
"""
65+
And the return code should be 0
66+
67+
Scenario: Check for theme updates in different output formats
68+
Given a WP install
69+
70+
When I run `wp theme install twentyfourteen --version=1.0`
71+
Then STDOUT should not be empty
72+
73+
When I run `wp theme check-update --format=json`
74+
Then STDOUT should be JSON containing:
75+
"""
76+
[{"name":"twentyfourteen","status":"inactive","version":"1.0"}]
77+
"""
78+
And the return code should be 0
79+
80+
When I run `wp theme check-update --format=csv`
81+
Then STDOUT should contain:
82+
"""
83+
name,status,version,update_version
84+
"""
85+
And STDOUT should contain:
86+
"""
87+
twentyfourteen,inactive,1.0
88+
"""
89+
And the return code should be 0
90+
91+
Scenario: Check for theme updates with custom fields
92+
Given a WP install
93+
94+
When I run `wp theme install twentyfourteen --version=1.0`
95+
Then STDOUT should not be empty
96+
97+
When I run `wp theme check-update --fields=name,version`
98+
Then STDOUT should be a table containing rows:
99+
| name | version |
100+
| twentyfourteen | 1.0 |
101+
And the return code should be 0
102+
103+
Scenario: Check for theme updates when no specific theme has updates
104+
Given a WP install
105+
106+
When I run `wp theme install twentytwelve`
107+
Then STDOUT should not be empty
108+
109+
When I run `wp theme check-update twentytwelve`
110+
Then STDOUT should contain:
111+
"""
112+
Success: All themes are up to date.
113+
"""
114+
And the return code should be 0

src/Plugin_Command.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,99 @@ 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+
public function check_update( $args, $assoc_args ) {
163+
// Force WordPress to check for updates.
164+
call_user_func( $this->upgrade_refresh );
165+
166+
$all = Utils\get_flag_value( $assoc_args, 'all', false );
167+
168+
if ( $all ) {
169+
$args = array_map(
170+
function ( $file ) {
171+
return Utils\get_plugin_name( $file );
172+
},
173+
array_keys( $this->get_all_plugins() )
174+
);
175+
}
176+
177+
$items = $this->get_item_list();
178+
179+
// Filter to only plugins with available updates
180+
$items_with_updates = array_filter(
181+
$items,
182+
function ( $item ) {
183+
return 'available' === $item['update'];
184+
}
185+
);
186+
187+
// If specific plugins requested, filter to those
188+
if ( ! empty( $args ) ) {
189+
$items_with_updates = array_filter(
190+
$items_with_updates,
191+
function ( $item ) use ( $args ) {
192+
return in_array( $item['name'], $args, true );
193+
}
194+
);
195+
}
196+
197+
if ( empty( $items_with_updates ) ) {
198+
WP_CLI::success( 'All plugins are up to date.' );
199+
return;
200+
}
201+
202+
// Set default fields for check-update output
203+
if ( ! isset( $assoc_args['fields'] ) ) {
204+
$assoc_args['fields'] = 'name,status,version,update_version';
205+
}
206+
207+
$formatter = $this->get_formatter( $assoc_args );
208+
$formatter->display_items( array_values( $items_with_updates ) );
209+
}
210+
118211
/**
119212
* Searches the WordPress.org plugin directory.
120213
*

0 commit comments

Comments
 (0)