Skip to content

Commit 0414ef0

Browse files
authored
Use update check data to validate installed theme versions (#559)
1 parent 5767483 commit 0414ef0

2 files changed

Lines changed: 65 additions & 6 deletions

File tree

‎features/theme.feature‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,3 +909,41 @@ Feature: Manage WordPress themes
909909
| name | status |
910910
| twentyeleven | active |
911911
| twentytwelve | inactive |
912+
913+
# The update check only lists themes without an update in `no_update` since WordPress 5.5.
914+
@require-wp-5.5
915+
Scenario: Flag themes whose version is higher than the one on WordPress.org without a request per theme
916+
Given a WP install
917+
And I run `wp theme install twentytwelve --force`
918+
And I run `sed -i.bak 's/^Version: .*/Version: 99.0/' wp-content/themes/twentytwelve/style.css`
919+
And a wp-content/themes/my-local-theme/style.css file:
920+
"""
921+
/*
922+
Theme Name: My Local Theme
923+
Version: 1.0
924+
*/
925+
"""
926+
And a wp-content/themes/my-local-theme/index.php file:
927+
"""
928+
<?php
929+
"""
930+
# The latest versions come from the update check, not from a theme information request per theme.
931+
And that HTTP requests to api.wordpress.org/themes/info/ will respond with:
932+
"""
933+
HTTP/1.1 500 Internal Server Error
934+
Content-Type: text/plain
935+
936+
Error
937+
"""
938+
939+
When I run `wp theme list --fields=name,update,version`
940+
Then STDOUT should be a table containing rows:
941+
| name | update | version |
942+
| twentytwelve | version higher than expected | 99.0 |
943+
| my-local-theme | none | 1.0 |
944+
945+
When I try `wp theme update twentytwelve`
946+
Then STDERR should contain:
947+
"""
948+
Warning: twentytwelve: version higher than expected.
949+
"""

‎src/WP_CLI/ParseThemeNameInput.php‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private function get_all_themes() {
7979

8080
if ( ! empty( $checked_themes ) ) {
8181
foreach ( $checked_themes as $slug => $version ) {
82-
$theme_version_info[ $slug ] = $this->is_theme_version_valid( $slug, $version );
82+
$theme_version_info[ $slug ] = $this->is_theme_version_valid( $slug, $version, $all_update_info );
8383
}
8484
}
8585

@@ -184,14 +184,35 @@ private function get_all_themes() {
184184
}
185185

186186
/**
187-
* Check if current version of the theme is higher than the one available at WP.org.
187+
* Check whether the installed version of a theme is not higher than the latest version on WordPress.org.
188188
*
189-
* @param string $slug Theme slug.
190-
* @param string $version Theme current version.
189+
* The update check already returns the latest version of every theme hosted on WordPress.org,
190+
* in `response` for themes with an update and in `no_update` for the others, so this does not
191+
* need a request per theme unless that information is missing.
191192
*
192-
* @return bool|string
193+
* @param string $slug Theme slug.
194+
* @param string $version Installed theme version.
195+
* @param object|null $update_info Optional. Value of the `update_themes` site transient.
196+
*
197+
* @return bool|string Whether the version is valid, or an empty string for themes not on WordPress.org.
193198
*/
194-
protected function is_theme_version_valid( $slug, $version ) {
199+
protected function is_theme_version_valid( $slug, $version, $update_info = null ) {
200+
if ( is_object( $update_info ) && isset( $update_info->no_update ) ) {
201+
$latest = null;
202+
foreach ( [ 'response', 'no_update' ] as $key ) {
203+
$themes = isset( $update_info->$key ) ? (array) $update_info->$key : [];
204+
// Entries are arrays in core, but filters can turn them into objects.
205+
$theme = isset( $themes[ $slug ] ) ? (array) $themes[ $slug ] : [];
206+
if ( isset( $theme['new_version'] ) ) {
207+
$latest = (string) $theme['new_version'];
208+
break;
209+
}
210+
}
211+
212+
// Themes that are not on WordPress.org are not part of the update check response.
213+
return null === $latest ? '' : ! version_compare( $version, $latest, '>' );
214+
}
215+
195216
/**
196217
* @var \WP_Error|object{name: string, slug: string, version: string, download_link: string} $theme_info
197218
*/

0 commit comments

Comments
 (0)