From b5e1e3e880c03c2761cbbb4f62843f2e7a930dcc Mon Sep 17 00:00:00 2001 From: Utsav Patel Date: Sat, 20 Jan 2024 17:05:39 +0530 Subject: [PATCH 1/9] Feature: added CLI option for minor and patch with proper description --- src/Theme_Command.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Theme_Command.php b/src/Theme_Command.php index 9d55b50b..e9a1bdc0 100644 --- a/src/Theme_Command.php +++ b/src/Theme_Command.php @@ -588,6 +588,12 @@ public function get( $args, $assoc_args ) { * * [--exclude=] * : Comma separated list of theme names that should be excluded from updating. + * + * [--minor] + * : Only perform updates for minor releases (e.g. from 1.3 to 1.4 instead of 2.0) + * + * [--patch] + * : Only perform updates for patch releases (e.g. from 1.3 to 1.3.3 instead of 1.4) * * [--format=] * : Render output in a particular format. From 7b9567bc1713960e61245d9d69201c13173dc03e Mon Sep 17 00:00:00 2001 From: Utsav Patel Date: Sat, 20 Jan 2024 19:01:20 +0530 Subject: [PATCH 2/9] Fix: phpcs issue of extra whitespace --- src/Theme_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Theme_Command.php b/src/Theme_Command.php index e9a1bdc0..2cde5b4a 100644 --- a/src/Theme_Command.php +++ b/src/Theme_Command.php @@ -588,7 +588,7 @@ public function get( $args, $assoc_args ) { * * [--exclude=] * : Comma separated list of theme names that should be excluded from updating. - * + * * [--minor] * : Only perform updates for minor releases (e.g. from 1.3 to 1.4 instead of 2.0) * From f079a558d8c5b4480f937c63255730fbdd64230f Mon Sep 17 00:00:00 2001 From: Utsav Patel Date: Sat, 20 Jan 2024 19:20:45 +0530 Subject: [PATCH 3/9] Feature: updated README.md file for wp theme update command --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index a407ea1f..55b944a3 100644 --- a/README.md +++ b/README.md @@ -1378,6 +1378,12 @@ wp theme update [...] [--all] [--exclude=] [--format=] Comma separated list of theme names that should be excluded from updating. + [--minor] + Only perform updates for minor releases (e.g. from 1.3 to 1.4 instead of 2.0) + + [--patch] + Only perform updates for patch releases (e.g. from 1.3 to 1.3.3 instead of 1.4) + [--format=] Render output in a particular format. --- From d03027f27b286babd2b1dfa89f2574d080f6db3b Mon Sep 17 00:00:00 2001 From: Utsav Patel Date: Mon, 22 Jan 2024 09:56:27 +0530 Subject: [PATCH 4/9] Feature: added functional tests for minor and patch to theme update --- features/theme-update.feature | 42 +++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/features/theme-update.feature b/features/theme-update.feature index 99330110..9f783764 100644 --- a/features/theme-update.feature +++ b/features/theme-update.feature @@ -113,3 +113,45 @@ Feature: Update WordPress themes """ Error: Can't find the requested theme's version 1.4.2 in the WordPress.org theme repository (HTTP code 404). """ + + Scenario: Error when both --minor and --patch are provided + Given a WP install + + When I try `wp theme update --patch --minor --all` + Then STDERR should be: + """ + Error: --minor and --patch cannot be used together. + """ + And the return code should be 1 + + Scenario: Update a theme to its latest minor release + Given a WP install + And I run `wp theme install --force twentytwelve --version=2.5.4` + + When I run `wp theme update twentytwelve --minor` + Then STDOUT should contain: + """ + Success: Updated 1 of 1 themes. + """ + + When I run `wp theme get twentytwelve --field=version` + Then STDOUT should be: + """ + 2.6.1 + """ + + Scenario: Update a theme to its latest patch release + Given a WP install + And I run `wp theme install --force twentytwelve --version=0.5` + + When I run `wp theme update twentytwelve --patch` + Then STDOUT should contain: + """ + Success: Updated 1 of 1 themes. + """ + + When I run `wp theme get twentytwelve --field=version` + Then STDOUT should be: + """ + 0.5.2 + """ From bfcae61adeed187758a1a0f90a6944ea8ae63472 Mon Sep 17 00:00:00 2001 From: Utsav Patel Date: Mon, 22 Jan 2024 15:29:16 +0530 Subject: [PATCH 5/9] Fix: version issue of twentytwelve theme of theme-update test --- features/theme-update.feature | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/features/theme-update.feature b/features/theme-update.feature index 9f783764..a9969804 100644 --- a/features/theme-update.feature +++ b/features/theme-update.feature @@ -126,7 +126,7 @@ Feature: Update WordPress themes Scenario: Update a theme to its latest minor release Given a WP install - And I run `wp theme install --force twentytwelve --version=2.5.4` + And I run `wp theme install --force twentytwelve --version=3.0` When I run `wp theme update twentytwelve --minor` Then STDOUT should contain: @@ -137,12 +137,12 @@ Feature: Update WordPress themes When I run `wp theme get twentytwelve --field=version` Then STDOUT should be: """ - 2.6.1 + 3.9 """ Scenario: Update a theme to its latest patch release Given a WP install - And I run `wp theme install --force twentytwelve --version=0.5` + And I run `wp theme install --force twentytwelve --version=1.1` When I run `wp theme update twentytwelve --patch` Then STDOUT should contain: @@ -153,5 +153,5 @@ Feature: Update WordPress themes When I run `wp theme get twentytwelve --field=version` Then STDOUT should be: """ - 0.5.2 + 1.1.1 """ From 895e6ad5e406c9ca7b5de3933af5cd53147d1029 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 22 Jan 2024 15:08:40 +0100 Subject: [PATCH 6/9] Add required code changes --- src/WP_CLI/CommandWithUpgrade.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/WP_CLI/CommandWithUpgrade.php b/src/WP_CLI/CommandWithUpgrade.php index 2e704544..10c7eed7 100755 --- a/src/WP_CLI/CommandWithUpgrade.php +++ b/src/WP_CLI/CommandWithUpgrade.php @@ -348,12 +348,14 @@ protected function update_many( $args, $assoc_args ) { $minor = (bool) Utils\get_flag_value( $assoc_args, 'minor', false ); $patch = (bool) Utils\get_flag_value( $assoc_args, 'patch', false ); - if ( 'plugin' === $this->item_type - && ( $minor || $patch ) ) { + if ( + in_array( $this->item_type, [ 'plugin', 'theme' ], true ) && + ( $minor || $patch ) + ) { $type = $minor ? 'minor' : 'patch'; $insecure = (bool) Utils\get_flag_value( $assoc_args, 'insecure', false ); - $items_to_update = self::get_minor_or_patch_updates( $items_to_update, $type, $insecure, true ); + $items_to_update = self::get_minor_or_patch_updates( $items_to_update, $type, $insecure, true, $this->item_type ); } $exclude = Utils\get_flag_value( $assoc_args, 'exclude' ); @@ -611,19 +613,27 @@ private function get_color( $status ) { } /** - * Get the minor or patch version for plugins with available updates + * Get the minor or patch version for plugins and themes with available updates * - * @param array $items Plugins with updates. + * @param array $items Items with updates. * @param string $type Either 'minor' or 'patch'. * @param bool $insecure Whether to retry without certificate validation on TLS handshake failure. * @param bool $require_stable Whether to require stable version when comparing versions. + * @param string $item_type Item type, either 'plugin' or 'theme'. * @return array */ - private function get_minor_or_patch_updates( $items, $type, $insecure, $require_stable ) { + private function get_minor_or_patch_updates( $items, $type, $insecure, $require_stable, $item_type ) { $wp_org_api = new WpOrgApi( [ 'insecure' => $insecure ] ); foreach ( $items as $i => $item ) { try { - $data = $wp_org_api->get_plugin_info( $item['name'] ); + $data = call_user_func( + [ $wp_org_api, "get_{$item_type}_info"], + $item['name'], + // The default. + 'en_US', + // We are only interested in the versions field. + [ 'versions' => true ] + ); } catch ( Exception $exception ) { unset( $items[ $i ] ); continue; From c3681b4e726802f1c6d0cdfe6eed636501850754 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 22 Jan 2024 15:09:47 +0100 Subject: [PATCH 7/9] Add missing space --- src/WP_CLI/CommandWithUpgrade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WP_CLI/CommandWithUpgrade.php b/src/WP_CLI/CommandWithUpgrade.php index 10c7eed7..a759d541 100755 --- a/src/WP_CLI/CommandWithUpgrade.php +++ b/src/WP_CLI/CommandWithUpgrade.php @@ -627,7 +627,7 @@ private function get_minor_or_patch_updates( $items, $type, $insecure, $require_ foreach ( $items as $i => $item ) { try { $data = call_user_func( - [ $wp_org_api, "get_{$item_type}_info"], + [ $wp_org_api, "get_{$item_type}_info" ], $item['name'], // The default. 'en_US', From 5fbc37e05266f6ee4d2540a5d445377ddfcf16f1 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 1 Feb 2024 15:27:01 +0100 Subject: [PATCH 8/9] Require `wp-cli/wp-cli` 2.10 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 54b2863d..d68f3493 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ ], "require": { "composer/semver": "^1.4 || ^2 || ^3", - "wp-cli/wp-cli": "^2.5.1" + "wp-cli/wp-cli": "^2.10" }, "require-dev": { "wp-cli/cache-command": "^2.0", From 6925f2c44831b2ed97d32ac84653e5d959eeeb76 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 1 Feb 2024 17:28:10 +0100 Subject: [PATCH 9/9] Fix theme update logic --- src/Theme_Command.php | 2 +- src/WP_CLI/CommandWithUpgrade.php | 16 ++++++++++------ src/WP_CLI/ParseThemeNameInput.php | 20 ++++++++++---------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/Theme_Command.php b/src/Theme_Command.php index 2cde5b4a..d65d97fa 100644 --- a/src/Theme_Command.php +++ b/src/Theme_Command.php @@ -439,7 +439,7 @@ protected function get_item_list() { protected function filter_item_list( $items, $args ) { $theme_files = array(); foreach ( $args as $arg ) { - $theme_files[] = $this->fetcher->get_check( $arg )->get_stylesheet_directory(); + $theme_files[] = $this->fetcher->get_check( $arg )->get_stylesheet(); } return Utils\pick_fields( $items, $theme_files ); diff --git a/src/WP_CLI/CommandWithUpgrade.php b/src/WP_CLI/CommandWithUpgrade.php index a759d541..566c25fc 100755 --- a/src/WP_CLI/CommandWithUpgrade.php +++ b/src/WP_CLI/CommandWithUpgrade.php @@ -370,11 +370,10 @@ protected function update_many( $args, $assoc_args ) { } unset( $items_to_update[ $plugin->file ] ); } elseif ( 'theme' === $this->item_type ) { - $theme_root = get_theme_root() . '/' . $item; - if ( ! is_dir( $theme_root ) ) { - continue; + $theme = wp_get_theme( $item ); + if ( $theme->exists() ) { + unset( $items_to_update[ $theme->get_stylesheet() ] ); } - unset( $items_to_update[ $theme_root ] ); } } } @@ -431,8 +430,13 @@ protected function update_many( $args, $assoc_args ) { $transient_filter = function ( $transient ) use ( $items_to_update ) { foreach ( $items_to_update as $name => $item_data ) { if ( isset( $transient->response[ $name ] ) ) { - $transient->response[ $name ]->new_version = $item_data['update_version']; - $transient->response[ $name ]->package = $item_data['update_package']; + if ( is_object( $transient->response[ $name ] ) ) { + $transient->response[ $name ]->new_version = $item_data['update_version']; + $transient->response[ $name ]->package = $item_data['update_package']; + } else { + $transient->response[ $name ]['new_version'] = $item_data['update_version']; + $transient->response[ $name ]['package'] = $item_data['update_package']; + } } } return $transient; diff --git a/src/WP_CLI/ParseThemeNameInput.php b/src/WP_CLI/ParseThemeNameInput.php index a5924ee2..34d75a82 100644 --- a/src/WP_CLI/ParseThemeNameInput.php +++ b/src/WP_CLI/ParseThemeNameInput.php @@ -76,38 +76,38 @@ private function get_all_themes() { } foreach ( wp_get_themes() as $key => $theme ) { - $file = $theme->get_stylesheet_directory(); + $stylesheet = $theme->get_stylesheet(); - $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; + $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; - $items[ $file ] = [ + $items[ $stylesheet ] = [ 'name' => $key, 'status' => $this->get_status( $theme ), 'update' => (bool) $update_info, 'update_version' => isset( $update_info['new_version'] ) ? $update_info['new_version'] : null, 'update_package' => isset( $update_info['package'] ) ? $update_info['package'] : null, 'version' => $theme->get( 'Version' ), - 'update_id' => $theme->get_stylesheet(), + 'update_id' => $stylesheet, 'title' => $theme->get( 'Name' ), 'description' => wordwrap( $theme->get( 'Description' ) ), 'author' => $theme->get( 'Author' ), - 'auto_update' => in_array( $theme->get_stylesheet(), $auto_updates, true ), + 'auto_update' => in_array( $stylesheet, $auto_updates, true ), ]; // Compare version and update information in theme list. if ( isset( $theme_version_info[ $key ] ) && false === $theme_version_info[ $key ] ) { - $items[ $file ]['update'] = 'version higher than expected'; + $items[ $stylesheet ]['update'] = 'version higher than expected'; } if ( is_multisite() ) { if ( ! empty( $site_enabled[ $key ] ) && ! empty( $network_enabled[ $key ] ) ) { - $items[ $file ]['enabled'] = 'network,site'; + $items[ $stylesheet ]['enabled'] = 'network,site'; } elseif ( ! empty( $network_enabled[ $key ] ) ) { - $items[ $file ]['enabled'] = 'network'; + $items[ $stylesheet ]['enabled'] = 'network'; } elseif ( ! empty( $site_enabled[ $key ] ) ) { - $items[ $file ]['enabled'] = 'site'; + $items[ $stylesheet ]['enabled'] = 'site'; } else { - $items[ $file ]['enabled'] = 'no'; + $items[ $stylesheet ]['enabled'] = 'no'; } } }