@@ -124,22 +124,14 @@ public function regenerate( $args, $assoc_args = array() ) {
124124 $ skip_delete = true ;
125125 }
126126
127- $ mime_types = array ( 'image ' );
127+ $ additional_mime_types = array ();
128+
128129 if ( Utils \wp_version_compare ( '4.7 ' , '>= ' ) ) {
129- $ mime_types [] = 'application/pdf ' ;
130+ $ additional_mime_types [] = 'application/pdf ' ;
130131 }
131- $ query_args = array (
132- 'post_type ' => 'attachment ' ,
133- 'post__in ' => $ args ,
134- 'post_mime_type ' => $ mime_types ,
135- 'post_status ' => 'any ' ,
136- 'posts_per_page ' => - 1 ,
137- 'fields ' => 'ids ' ,
138- );
139-
140- $ images = new WP_Query ( $ query_args );
141132
142- $ count = $ images ->post_count ;
133+ $ images = $ this ->get_images ( $ args , $ additional_mime_types );
134+ $ count = $ images ->post_count ;
143135
144136 if ( ! $ count ) {
145137 WP_CLI ::warning ( 'No images found. ' );
@@ -162,12 +154,12 @@ public function regenerate( $args, $assoc_args = array() ) {
162154 $ successes = 0 ;
163155 $ errors = 0 ;
164156 $ skips = 0 ;
165- foreach ( $ images ->posts as $ id ) {
157+ foreach ( $ images ->posts as $ post ) {
166158 $ number ++;
167159 if ( 0 === $ number % self ::WP_CLEAR_OBJECT_CACHE_INTERVAL ) {
168160 Utils \wp_clear_object_cache ();
169161 }
170- $ this ->process_regeneration ( $ id , $ skip_delete , $ only_missing , $ image_size , $ number . '/ ' . $ count , $ successes , $ errors , $ skips );
162+ $ this ->process_regeneration ( $ post -> ID , $ skip_delete , $ only_missing , $ image_size , $ number . '/ ' . $ count , $ successes , $ errors , $ skips );
171163 }
172164
173165 if ( $ image_size ) {
@@ -484,59 +476,15 @@ public function import( $args, $assoc_args = array() ) {
484476 * @subcommand image-size
485477 */
486478 public function image_size ( $ args , $ assoc_args ) {
487- global $ _wp_additional_image_sizes ;
488-
489- $ soft_ratio_text = 'N/A ' ;
490-
491479 $ assoc_args = array_merge (
492480 array (
493481 'fields ' => 'name,width,height,crop,ratio ' ,
494482 ),
495483 $ assoc_args
496484 );
497485
498- $ sizes = array (
499- array (
500- 'name ' => 'large ' ,
501- 'width ' => intval ( get_option ( 'large_size_w ' ) ),
502- 'height ' => intval ( get_option ( 'large_size_h ' ) ),
503- 'crop ' => false !== get_option ( 'large_crop ' ) ? 'hard ' : 'soft ' ,
504- 'ratio ' => false !== get_option ( 'large_crop ' ) ? $ this ->get_ratio ( intval ( get_option ( 'large_size_w ' ) ), intval ( get_option ( 'large_size_h ' ) ) ) : $ soft_ratio_text ,
505- ),
506- array (
507- 'name ' => 'medium_large ' ,
508- 'width ' => intval ( get_option ( 'medium_large_size_w ' ) ),
509- 'height ' => intval ( get_option ( 'medium_large_size_h ' ) ),
510- 'crop ' => false !== get_option ( 'medium_large_crop ' ) ? 'hard ' : 'soft ' ,
511- 'ratio ' => false !== get_option ( 'medium_large_crop ' ) ? $ this ->get_ratio ( intval ( get_option ( 'medium_large_size_w ' ) ), intval ( get_option ( 'medium_large_size_h ' ) ) ) : $ soft_ratio_text ,
512- ),
513- array (
514- 'name ' => 'medium ' ,
515- 'width ' => intval ( get_option ( 'medium_size_w ' ) ),
516- 'height ' => intval ( get_option ( 'medium_size_h ' ) ),
517- 'crop ' => false !== get_option ( 'medium_crop ' ) ? 'hard ' : 'soft ' ,
518- 'ratio ' => false !== get_option ( 'medium_crop ' ) ? $ this ->get_ratio ( intval ( get_option ( 'medium_size_w ' ) ), intval ( get_option ( 'medium_size_h ' ) ) ) : $ soft_ratio_text ,
519- ),
520- array (
521- 'name ' => 'thumbnail ' ,
522- 'width ' => intval ( get_option ( 'thumbnail_size_w ' ) ),
523- 'height ' => intval ( get_option ( 'thumbnail_size_h ' ) ),
524- 'crop ' => false !== get_option ( 'thumbnail_crop ' ) ? 'hard ' : 'soft ' ,
525- 'ratio ' => false !== get_option ( 'thumbnail_crop ' ) ? $ this ->get_ratio ( intval ( get_option ( 'thumbnail_size_w ' ) ), intval ( get_option ( 'thumbnail_size_h ' ) ) ) : $ soft_ratio_text ,
526- ),
527- );
528- if ( is_array ( $ _wp_additional_image_sizes ) ) {
529- foreach ( $ _wp_additional_image_sizes as $ size => $ size_args ) {
530- $ crop = filter_var ( $ size_args ['crop ' ], FILTER_VALIDATE_BOOLEAN );
531- $ sizes [] = array (
532- 'name ' => $ size ,
533- 'width ' => $ size_args ['width ' ],
534- 'height ' => $ size_args ['height ' ],
535- 'crop ' => empty ( $ crop ) || is_array ( $ size_args ['crop ' ] ) ? 'soft ' : 'hard ' ,
536- 'ratio ' => empty ( $ crop ) || is_array ( $ size_args ['crop ' ] ) ? $ soft_ratio_text : $ this ->get_ratio ( $ size_args ['width ' ], $ size_args ['height ' ] ),
537- );
538- }
539- }
486+ $ sizes = $ this ->get_registered_image_sizes ();
487+
540488 usort (
541489 $ sizes ,
542490 function ( $ a , $ b ) {
@@ -553,7 +501,7 @@ function( $a, $b ) {
553501 'width ' => '' ,
554502 'height ' => '' ,
555503 'crop ' => 'N/A ' ,
556- 'ratio ' => $ soft_ratio_text ,
504+ 'ratio ' => ' N/A ' ,
557505 )
558506 );
559507 WP_CLI \Utils \format_items ( $ assoc_args ['format ' ], $ sizes , explode ( ', ' , $ assoc_args ['fields ' ] ) );
@@ -942,4 +890,82 @@ private function update_attachment_metadata_for_image_size( $id, $new_metadata,
942890 return false ;
943891 }
944892
893+ /**
894+ * Get images from the installation.
895+ *
896+ * @param array $args The query arguments to use. Optional.
897+ * @param array $additional_mime_types The additional mime types to search for. Optional.
898+ *
899+ * @return WP_Query The query result.
900+ */
901+ private function get_images ( $ args = array (), $ additional_mime_types = array () ) {
902+ $ mime_types = array_merge ( array ( 'image ' ), $ additional_mime_types );
903+
904+ $ query_args = array (
905+ 'post_type ' => 'attachment ' ,
906+ 'post__in ' => $ args ,
907+ 'post_mime_type ' => $ mime_types ,
908+ 'post_status ' => 'any ' ,
909+ 'posts_per_page ' => -1 ,
910+ );
911+
912+ return new WP_Query ( $ query_args );
913+ }
914+
915+ /**
916+ * Get the metadata for the passed intermediate image size.
917+ *
918+ * @param string $size The image size to get the metadata for.
919+ *
920+ * @return array The image size metadata.
921+ */
922+ private function get_intermediate_size_metadata ( $ size ) {
923+ $ width = intval ( get_option ( "{$ size }_size_w " ) );
924+ $ height = intval ( get_option ( "{$ size }_size_h " ) );
925+ $ crop = get_option ( "{$ size }_crop " );
926+
927+ return array (
928+ 'name ' => $ size ,
929+ 'width ' => $ width ,
930+ 'height ' => $ height ,
931+ 'crop ' => false !== $ crop ? 'hard ' : 'soft ' ,
932+ 'ratio ' => false !== $ crop ? $ this ->get_ratio ( $ width , $ height ) : 'N/A ' ,
933+ );
934+ }
935+
936+ /**
937+ * Get all the registered image sizes along with their dimensions.
938+ *
939+ * @global array $_wp_additional_image_sizes The additional image sizes to parse.
940+ *
941+ * @link https://wordpress.stackexchange.com/a/251602 Original solution.
942+ *
943+ * @return array $image_sizes The image sizes
944+ */
945+ private function get_registered_image_sizes () {
946+ global $ _wp_additional_image_sizes ;
947+
948+ $ image_sizes = array ();
949+ $ default_image_sizes = get_intermediate_image_sizes ();
950+
951+ foreach ( $ default_image_sizes as $ size ) {
952+ $ image_sizes [] = $ this ->get_intermediate_size_metadata ( $ size );
953+ }
954+
955+ if ( is_array ( $ _wp_additional_image_sizes ) ) {
956+ foreach ( $ _wp_additional_image_sizes as $ size => $ size_args ) {
957+ $ crop = filter_var ( $ size_args ['crop ' ], FILTER_VALIDATE_BOOLEAN );
958+ $ image_sizes [] = array (
959+ 'name ' => $ size ,
960+ 'width ' => $ size_args ['width ' ],
961+ 'height ' => $ size_args ['height ' ],
962+ 'crop ' => empty ( $ crop ) || is_array ( $ size_args ['crop ' ] ) ? 'soft ' : 'hard ' ,
963+ 'ratio ' => empty ( $ crop ) || is_array ( $ size_args ['crop ' ] ) ? 'N/A ' : $ this ->get_ratio ( $ size_args ['width ' ], $ size_args ['height ' ] ),
964+ );
965+ }
966+ }
967+
968+ return $ image_sizes ;
969+ }
970+
945971}
0 commit comments