Skip to content

Commit 741d4d5

Browse files
Copilotswissspidy
andcommitted
Extract get_image_sizes_description() helper to remove duplicated string-building logic
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 21eb213 commit 741d4d5

1 file changed

Lines changed: 29 additions & 18 deletions

File tree

src/Media_Command.php

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,13 @@ public function regenerate( $args, $assoc_args = array() ) {
149149

150150
if ( empty( $args ) ) {
151151
if ( $image_sizes ) {
152-
if ( 1 === count( $image_sizes ) ) {
153-
WP_CLI::confirm( sprintf( 'Do you really want to regenerate the "%s" image size for all images?', reset( $image_sizes ) ), $assoc_args );
154-
} else {
155-
WP_CLI::confirm( sprintf( 'Do you really want to regenerate the "%s" image sizes for all images?', implode( '", "', $image_sizes ) ), $assoc_args );
156-
}
152+
WP_CLI::confirm(
153+
sprintf(
154+
'Do you really want to regenerate the %s for all images?',
155+
$this->get_image_sizes_description( $image_sizes, 'image size', 'image sizes' )
156+
),
157+
$assoc_args
158+
);
157159
} else {
158160
WP_CLI::confirm( 'Do you really want to regenerate all images?', $assoc_args );
159161
}
@@ -690,6 +692,26 @@ private function make_copy( $path ) {
690692
return $filename;
691693
}
692694

695+
/**
696+
* Returns a human-readable description for one or more image size names.
697+
*
698+
* @param string[] $sizes The size names.
699+
* @param string $singular_noun Noun to use when exactly one size is given (e.g. 'thumbnail').
700+
* @param string $plural_noun Noun to use when more than one size is given (e.g. 'thumbnails').
701+
* @param string $default_if_empty String to return when $sizes is empty.
702+
* @return string
703+
*/
704+
private function get_image_sizes_description( array $sizes, $singular_noun, $plural_noun, $default_if_empty = '' ) {
705+
$count = count( $sizes );
706+
if ( 0 === $count ) {
707+
return $default_if_empty;
708+
}
709+
if ( 1 === $count ) {
710+
return sprintf( '"%s" %s', reset( $sizes ), $singular_noun );
711+
}
712+
return sprintf( '"%s" %s', implode( '", "', $sizes ), $plural_noun );
713+
}
714+
693715
/**
694716
* Process media regeneration
695717
*
@@ -721,13 +743,7 @@ private function process_regeneration( $id, $skip_delete, $only_missing, $delete
721743
} else {
722744
$att_desc = sprintf( '"%1$s" (ID %2$d)', $title, $id );
723745
}
724-
if ( count( $image_sizes ) === 1 ) {
725-
$thumbnail_desc = sprintf( '"%s" thumbnail', reset( $image_sizes ) );
726-
} elseif ( count( $image_sizes ) > 1 ) {
727-
$thumbnail_desc = sprintf( '"%s" thumbnails', implode( '", "', $image_sizes ) );
728-
} else {
729-
$thumbnail_desc = 'thumbnail';
730-
}
746+
$thumbnail_desc = $this->get_image_sizes_description( $image_sizes, 'thumbnail', 'thumbnails', 'thumbnail' );
731747

732748
$fullsizepath = $this->get_attached_file( $id );
733749

@@ -801,12 +817,7 @@ private function process_regeneration( $id, $skip_delete, $only_missing, $delete
801817
if ( $image_sizes ) {
802818
$regenerated_sizes = $this->update_attachment_metadata_for_image_size( $id, $metadata, $image_sizes, $original_meta );
803819
if ( $regenerated_sizes ) {
804-
if ( count( $regenerated_sizes ) === 1 ) {
805-
$regenerated_desc = sprintf( '"%s" thumbnail', reset( $regenerated_sizes ) );
806-
} else {
807-
$regenerated_desc = sprintf( '"%s" thumbnails', implode( '", "', $regenerated_sizes ) );
808-
}
809-
WP_CLI::log( "$progress Regenerated $regenerated_desc for $att_desc." );
820+
WP_CLI::log( "$progress Regenerated {$this->get_image_sizes_description( $regenerated_sizes, 'thumbnail', 'thumbnails' )} for $att_desc." );
810821
} else {
811822
WP_CLI::log( "$progress No $thumbnail_desc regeneration needed for $att_desc." );
812823
}

0 commit comments

Comments
 (0)