OSDN Git Service

use attachment metadata for deleting old images, instead of reading through the exist...
authorscribu <mail@scribu.net>
Fri, 15 Mar 2013 03:23:18 +0000 (05:23 +0200)
committerscribu <mail@scribu.net>
Fri, 15 Mar 2013 03:23:18 +0000 (05:23 +0200)
php/commands/media.php

index 44007ee..199b106 100644 (file)
@@ -65,46 +65,7 @@ class Media_Command extends WP_CLI_Command {
 
         WP_CLI::line( sprintf( 'Start processing of "%1$s" (ID %2$d).', get_the_title( $image->ID ), $image->ID ) );
 
-        $a_path = explode( DIRECTORY_SEPARATOR, $fullsizepath );
-        $a_file = explode( '.', $a_path[ count( $a_path ) - 1 ] );
-
-        unset( $a_path );
-        unset( $a_file[ count( $a_file ) - 1 ] );
-
-        $image_name = $a_file[ count( $a_file ) - 1 ] . '-';
-        $dir_path   = wp_upload_dir();
-
-        // Read and delete files
-        $dir   = opendir( $dir_path['basedir'] );
-        $files = array();
-
-        while ( $file = readdir( $dir ) ) {
-
-            if ( !( strrpos( $file, $image_name ) === false ) ) {
-
-                $thumbnail  = explode( $image_name, $file );
-                $filename   = $thumbnail[ 1 ];
-
-                //If we got the original / full image
-                if ( "" == $thumbnail[ 0 ] ) {
-                    $filetype       = wp_check_filetype($file);
-                    $thumbnail_ext  = ".{$filetype['ext']}";
-                    $thumbnail_name = basename( $filename, $thumbnail_ext );
-
-                    $sizes          = explode( 'x', $thumbnail_name );
-
-                    // If not cropped by WP
-                    if ( 2 == count( $sizes ) ) {
-                        $width  = $sizes[0];
-                        $height = $sizes[1];
-                        if ( is_numeric( $width ) && is_numeric( $height ) ) {
-                            WP_CLI::line( "Thumbnail: {$width} x {$height} was deleted." );
-                            unlink( $dir_path['basedir'] . DIRECTORY_SEPARATOR . $image_name . $thumbnail_name . $thumbnail_ext );
-                        }
-                    }
-                }
-            }
-        }
+               $this->remove_old_images( $image->ID );
 
         $metadata = wp_generate_attachment_metadata( $image->ID, $fullsizepath );
 
@@ -123,6 +84,27 @@ class Media_Command extends WP_CLI_Command {
         WP_CLI::success( "All thumbnails were successfully regenerated in " . timer_stop() . " seconds." );
     }
 
+       private function remove_old_images( $att_id ) {
+               $wud = wp_upload_dir();
+
+               $metadata = wp_get_attachment_metadata( $att_id );
+
+               $dir_path = $wud['basedir'] . '/' . dirname( $metadata['file'] ) . '/';
+               $original_path = $dir_path . basename( $metadata['file'] );
+
+               foreach ( $metadata['sizes'] as $size => $size_info ) {
+                       $intermediate_path = $dir_path . $size_info['file'];
+
+                       if ( $intermediate_path == $original_path )
+                               continue;
+
+                       if ( unlink( $intermediate_path ) ) {
+                               WP_CLI::line( sprintf( "Thumbnail %s x %s was deleted.",
+                                       $size_info['width'], $size_info['height'] ) );
+                       }
+               }
+       }
+
     private function _not_found_message( $not_found_ids ){
         $count = count($not_found_ids);
         return vsprintf( 'Unable to find the %1$s (%2$s). Are you sure %3$s %4$s?', array(