Skip to content

Commit 31bbe0b

Browse files
Copilotswissspidy
andcommitted
Refactor auto-update-indicated logic into shared method in CommandWithUpgrade
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 4b2e67b commit 31bbe0b

3 files changed

Lines changed: 62 additions & 80 deletions

File tree

src/Plugin_Command.php

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -778,47 +778,10 @@ protected function install_from_repo( $slug, $assoc_args ) {
778778
* @alias upgrade
779779
*/
780780
public function update( $args, $assoc_args ) {
781-
$all = Utils\get_flag_value( $assoc_args, 'all', false );
782-
$auto_update_indicated = Utils\get_flag_value( $assoc_args, 'auto-update-indicated', false );
781+
$all = Utils\get_flag_value( $assoc_args, 'all', false );
783782

784-
// Don't allow --version to be set with --auto-update-indicated, as the version comes from the server.
785-
if ( $auto_update_indicated && isset( $assoc_args['version'] ) ) {
786-
WP_CLI::error( 'Cannot use --version with --auto-update-indicated. The version is determined by the server.' );
787-
}
788-
789-
// Don't allow --minor or --patch to be set with --auto-update-indicated, as the version comes from the server.
790-
if ( $auto_update_indicated && ( isset( $assoc_args['minor'] ) || isset( $assoc_args['patch'] ) ) ) {
791-
WP_CLI::error( 'Cannot use --minor or --patch with --auto-update-indicated. The version is determined by the server.' );
792-
}
793-
794-
// Don't allow plugin names to be specified with --auto-update-indicated.
795-
if ( $auto_update_indicated && ! empty( $args ) ) {
796-
WP_CLI::error( 'Cannot specify plugin names with --auto-update-indicated. This flag updates all plugins with server-indicated automatic updates.' );
797-
}
798-
799-
// If --auto-update-indicated is set, we need to filter plugins by this flag.
800-
if ( $auto_update_indicated ) {
801-
// Get all plugins with their update info.
802-
$items = $this->get_item_list();
803-
804-
// Filter to only include plugins where auto_update_indicated is true.
805-
$auto_update_plugins = array_filter(
806-
$items,
807-
function ( $item ) {
808-
return ! empty( $item['auto_update_indicated'] );
809-
}
810-
);
811-
812-
// Get the plugin names to update.
813-
$args = array_values( wp_list_pluck( $auto_update_plugins, 'name' ) );
814-
815-
if ( empty( $args ) ) {
816-
WP_CLI::success( 'No plugins with server-indicated automatic updates available.' );
817-
return;
818-
}
819-
820-
// Process the updates.
821-
parent::update_many( $args, $assoc_args );
783+
// Handle --auto-update-indicated flag if present.
784+
if ( $this->handle_auto_update_indicated( $args, $assoc_args ) ) {
822785
return;
823786
}
824787

src/Theme_Command.php

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -742,47 +742,10 @@ public function get( $args, $assoc_args ) {
742742
* @alias upgrade
743743
*/
744744
public function update( $args, $assoc_args ) {
745-
$all = Utils\get_flag_value( $assoc_args, 'all', false );
746-
$auto_update_indicated = Utils\get_flag_value( $assoc_args, 'auto-update-indicated', false );
747-
748-
// Don't allow --version to be set with --auto-update-indicated, as the version comes from the server.
749-
if ( $auto_update_indicated && isset( $assoc_args['version'] ) ) {
750-
WP_CLI::error( 'Cannot use --version with --auto-update-indicated. The version is determined by the server.' );
751-
}
752-
753-
// Don't allow --minor or --patch to be set with --auto-update-indicated, as the version comes from the server.
754-
if ( $auto_update_indicated && ( isset( $assoc_args['minor'] ) || isset( $assoc_args['patch'] ) ) ) {
755-
WP_CLI::error( 'Cannot use --minor or --patch with --auto-update-indicated. The version is determined by the server.' );
756-
}
757-
758-
// Don't allow theme names to be specified with --auto-update-indicated.
759-
if ( $auto_update_indicated && ! empty( $args ) ) {
760-
WP_CLI::error( 'Cannot specify theme names with --auto-update-indicated. This flag updates all themes with server-indicated automatic updates.' );
761-
}
762-
763-
// If --auto-update-indicated is set, we need to filter themes by this flag.
764-
if ( $auto_update_indicated ) {
765-
// Get all themes with their update info.
766-
$items = $this->get_item_list();
767-
768-
// Filter to only include themes where auto_update_indicated is true.
769-
$auto_update_themes = array_filter(
770-
$items,
771-
function ( $item ) {
772-
return ! empty( $item['auto_update_indicated'] );
773-
}
774-
);
775-
776-
// Get the theme names to update.
777-
$args = array_values( wp_list_pluck( $auto_update_themes, 'name' ) );
778-
779-
if ( empty( $args ) ) {
780-
WP_CLI::success( 'No themes with server-indicated automatic updates available.' );
781-
return;
782-
}
745+
$all = Utils\get_flag_value( $assoc_args, 'all', false );
783746

784-
// Process the updates.
785-
parent::update_many( $args, $assoc_args );
747+
// Handle --auto-update-indicated flag if present.
748+
if ( $this->handle_auto_update_indicated( $args, $assoc_args ) ) {
786749
return;
787750
}
788751

src/WP_CLI/CommandWithUpgrade.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,62 @@ protected function get_upgrader( $assoc_args ) {
562562
return Utils\get_upgrader( $upgrader_class, $insecure, new ExtensionUpgraderSkin() );
563563
}
564564

565+
/**
566+
* Handles the --auto-update-indicated flag logic for both plugins and themes.
567+
*
568+
* This method validates flag combinations, filters items by auto_update_indicated,
569+
* and processes updates if any items are found.
570+
*
571+
* @param array $args Positional arguments (item names).
572+
* @param array $assoc_args Associative arguments (flags).
573+
* @return bool Returns true if auto-update-indicated was handled, false otherwise.
574+
*/
575+
protected function handle_auto_update_indicated( $args, $assoc_args ) {
576+
$auto_update_indicated = Utils\get_flag_value( $assoc_args, 'auto-update-indicated', false );
577+
578+
if ( ! $auto_update_indicated ) {
579+
return false;
580+
}
581+
582+
// Don't allow --version to be set with --auto-update-indicated, as the version comes from the server.
583+
if ( isset( $assoc_args['version'] ) ) {
584+
WP_CLI::error( 'Cannot use --version with --auto-update-indicated. The version is determined by the server.' );
585+
}
586+
587+
// Don't allow --minor or --patch to be set with --auto-update-indicated, as the version comes from the server.
588+
if ( isset( $assoc_args['minor'] ) || isset( $assoc_args['patch'] ) ) {
589+
WP_CLI::error( 'Cannot use --minor or --patch with --auto-update-indicated. The version is determined by the server.' );
590+
}
591+
592+
// Don't allow item names to be specified with --auto-update-indicated.
593+
if ( ! empty( $args ) ) {
594+
WP_CLI::error( "Cannot specify {$this->item_type} names with --auto-update-indicated. This flag updates all {$this->item_type}s with server-indicated automatic updates." );
595+
}
596+
597+
// Get all items with their update info.
598+
$items = $this->get_item_list();
599+
600+
// Filter to only include items where auto_update_indicated is true.
601+
$auto_update_items = array_filter(
602+
$items,
603+
function ( $item ) {
604+
return ! empty( $item['auto_update_indicated'] );
605+
}
606+
);
607+
608+
// Get the item names to update.
609+
$args = array_values( wp_list_pluck( $auto_update_items, 'name' ) );
610+
611+
if ( empty( $args ) ) {
612+
WP_CLI::success( "No {$this->item_type}s with server-indicated automatic updates available." );
613+
return true;
614+
}
615+
616+
// Process the updates.
617+
$this->update_many( $args, $assoc_args );
618+
return true;
619+
}
620+
565621
protected function update_many( $args, $assoc_args ) {
566622
call_user_func( $this->upgrade_refresh );
567623

0 commit comments

Comments
 (0)