Plugin Directory

Changeset 1556512


Ignore:
Timestamp:
12/17/2016 02:01:02 AM (9 years ago)
Author:
OriginalEXE
Message:

Version 1.4.0

Location:
optimize-images-resizing
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • optimize-images-resizing/trunk/css/remove-image-sizes.css

    r1410429 r1556512  
    11#oir-status-message,
    22#oir-log {
    3     display: none;
     3  display: none;
    44}
     5
     6#oir-buttons {
     7  margin-bottom: 20px;
     8  margin-top: 5px;
     9}
  • optimize-images-resizing/trunk/inc/class-remove-image-sizes.php

    r1410429 r1556512  
    77if ( ! class_exists( 'OIR_Remove_Image_Sizes' ) ) :
    88
    9     final class OIR_Remove_Image_Sizes {
    10 
    11         // Will hold the only instance of our main plugin class
    12         private static $instance;
    13 
    14         // Instantiate the class and set up stuff
    15         public static function instantiate() {
    16 
    17             if ( ! isset( self::$instance ) && ! ( self::$instance instanceof OIR_Remove_Image_Sizes ) ) {
    18 
    19                 self::$instance = new OIR_Remove_Image_Sizes();
    20 
    21                 add_action( 'admin_init', array( self::$instance, 'add_media_settings' ) );
    22                 add_action( 'wp_ajax_oir_remove_image_sizes', array( self::$instance, 'remove_image_sizes' ) );
    23                 add_action( 'admin_enqueue_scripts', array( self::$instance, 'enqueue_assets' ) );
    24 
    25                 // Upon image upload, only generate default sizes
    26                 add_filter( 'intermediate_image_sizes_advanced', array( self::$instance, 'remove_intermediate_sizes' ), 10, 1 );
    27 
    28             }
    29 
    30             return self::$instance;
    31 
    32         }
    33 
    34         // Filter the sizes that get created initially     
    35         public function remove_intermediate_sizes( $sizes ) {
    36 
    37             $allowed_sizes = apply_filters( 'image_size_names_choose', array() );
    38 
    39             $allowed_sizes = array_merge( array_keys( $allowed_sizes ), array( 'thumbnail', 'medium', 'large' ) );
    40 
    41             return array_intersect_key( $sizes, array_flip( $allowed_sizes ) );
    42 
    43         }
    44 
    45         // register our media settings
    46         public function add_media_settings() {
    47 
    48             add_settings_field( 'oir_media_settings', __( 'Remove image sizes', 'optimize-images-resizing' ), array( $this, 'media_settings_output' ), 'media', 'default' );
    49 
    50         }
    51 
    52         // add a small output to media settings screen
    53         public function media_settings_output() {
    54 
    55             echo sprintf( '<button id="oir-remove-image-sizes" class="button">%s</button>', __( 'Start cleanup', 'optimize-images-resizing' ) );
    56             echo '<p id="oir-status-message"></p>';
    57             echo sprintf( '<p class="description">%s</p>',
    58                 __( 'Click this button to remove redundant image sizes. You only need to do this once.', 'optimize-images-resizing' )
    59             );
    60             echo '<div id="oir-log"></div>';
    61 
    62         }
    63 
    64         // cleans up extra image sizes when called via ajax
    65         public function remove_image_sizes( $__attachment_id ) {
    66 
    67             $paged = ! empty( $_POST['paged'] ) ? absint( $_POST['paged'] ) : 1;
    68             $removed = ! empty( $_POST['removed'] ) ? absint( $_POST['removed'] ) : 0;
    69             $removed_log = ! empty( $_POST['removed_log'] ) ? (array) $_POST['removed_log'] : array();
    70 
    71             if ( ! $__attachment_id ) {
    72 
    73                 check_ajax_referer( 'oir-nonce', 'nonce' );
    74 
    75                 $args = array(
    76                     'fields'         => 'ids',
    77                     'paged'          => $paged,
    78                     'post_mime_type' => 'image',
    79                     'post_status'    => 'any',
    80                     'post_type'      => 'attachment',
    81                     'posts_per_page' => 10,
    82                 );
    83 
    84             } else {
    85 
    86                 $args = array(
    87                     'fields'         => 'ids',
    88                     'post_mime_type' => 'image',
    89                     'post_status'    => 'any',
    90                     'post_type'      => 'attachment',
    91                     'post__in'       => array( absint( $__attachment_id ) ),
    92                 );
    93 
    94             }
    95 
    96             $query = new WP_Query( $args );
    97 
    98             $found = absint( $query->found_posts );
    99             $finished = empty( $query->posts ) ? true : false;
    100 
    101             if ( ! $finished ) {
    102 
    103                 $upload_dir = wp_upload_dir();
    104 
    105                 foreach ( $query->posts as $attachment_id ) {
    106 
    107                     $meta = wp_get_attachment_metadata( $attachment_id );
    108 
    109                     if ( empty( $meta['file'] ) ) {
    110 
    111                         continue;
    112 
    113                     }
    114 
    115                     $file_path = str_replace( basename( $meta['file'] ), '', trailingslashit( $upload_dir['basedir'] ) . $meta['file'] );
    116 
    117                     if ( empty( $meta['sizes'] ) || ! is_array( $meta['sizes'] ) ) {
    118 
    119                         continue;
    120 
    121                     }
    122 
    123                     // Don't remove images if they are used in default image sizes
    124                     // (happens when custom image size matches the dimensions of the default ones)
    125                     $do_not_delete = array();
    126 
    127                     $allowed_sizes = apply_filters( 'image_size_names_choose', array() );
    128 
    129                     $allowed_sizes = array_merge( array_keys( $allowed_sizes ), array( 'thumbnail', 'medium', 'large' ) );
    130 
    131                     foreach ( $meta['sizes'] as $size => $params ) {
    132 
    133                         $file = realpath( $file_path . $params['file'] );
    134 
    135                         // we don't want to delete thumbnails, they are used in admin area
    136                         if ( in_array( $size, $allowed_sizes ) ) {
    137 
    138                             $do_not_delete[] = $file;
    139 
    140                             continue;
    141 
    142                         }
    143 
    144                         if ( ! in_array( $file, $do_not_delete ) && is_readable( $file ) ) {
    145 
    146                             unlink( $file );
    147 
    148                             unset( $meta['sizes'][ $size ] );
    149 
    150                             $removed++;
    151                             $removed_log[] = $file;
    152 
    153                         }
    154 
    155                     }
    156 
    157                     wp_update_attachment_metadata( $attachment_id, $meta );
    158 
    159                 }
    160 
    161             }
    162 
    163             if ( ! $__attachment_id ) {
    164 
    165                 $response = array(
    166                     'finished'    => $finished,
    167                     'found'       => $found,
    168                     'paged'       => $paged,
    169                     'removed'     => $removed,
    170                     'removed_log' => $removed_log,
    171                     'success'     => true,
    172                 );
    173 
    174                 wp_send_json( $response );
    175 
    176             }
    177 
    178         }
    179 
    180         // add js and css needed on media settings screen
    181         public function enqueue_assets( $hook ) {
    182 
    183             if ( 'options-media.php' !== $hook ) {
    184 
    185                 // we only need this script in media settings
    186                 return;
    187 
    188             }
    189 
    190             wp_enqueue_script( 'oir_remove_image_sizes', OIR_JS_URL . 'remove-image-sizes.js' );
    191 
    192             $localize = array(
    193                 'l10n'  => array(
    194                     'something_wrong'  => __( 'Something went wrong, please try again or contact the developer!', 'optimize-images-resizing' ),
    195                     'process_finished' => __( 'Cleanup was successfully completed. Number of images removed: %d.', 'optimize-images-resizing' ),
    196                     'nothing_to_remove' => __( 'There was nothing to clean up, looks like you have no redundant image sizes. Good job!', 'optimize-images-resizing' ),
    197                     'cleanup_progress' => __( 'Cleanup in progress, leave this page open!', 'optimize-images-resizing' ),
    198                 ),
    199                 'nonce' => wp_create_nonce( 'oir-nonce' ),
    200             );
    201 
    202             wp_localize_script( 'oir_remove_image_sizes', 'oir_plugin', $localize );
    203 
    204             wp_enqueue_style( 'oir_remove_image_sizes', OIR_CSS_URL . 'remove-image-sizes.css' );
    205 
    206         }
    207 
    208     }
     9  final class OIR_Remove_Image_Sizes {
     10
     11    // Will hold the only instance of our main plugin class
     12    private static $instance;
     13
     14    // Instantiate the class and set up stuff
     15    public static function instantiate() {
     16
     17      if ( ! isset( self::$instance ) && ! ( self::$instance instanceof OIR_Remove_Image_Sizes ) ) {
     18
     19        self::$instance = new OIR_Remove_Image_Sizes();
     20
     21      }
     22
     23      return self::$instance;
     24
     25    }
     26
     27    public function __construct() {
     28
     29      add_action( 'admin_menu', array( $this, 'add_tools_subpage' ) );
     30      add_action( 'wp_ajax_oir_remove_image_sizes', array( $this, 'remove_image_sizes' ) );
     31      add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
     32
     33      // Upon image upload, only generate default sizes
     34      add_filter( 'intermediate_image_sizes_advanced', array( $this, 'remove_intermediate_sizes' ), 10, 1 );
     35
     36    }
     37
     38    // Returns image sizes that we don't want to remove
     39    public function get_ignored_image_sizes() {
     40
     41      $ignored_sizes = apply_filters( 'image_size_names_choose', array() );
     42
     43      $ignored_sizes = array_merge( array_keys( $ignored_sizes ), array( 'thumbnail', 'medium', 'large' ) );
     44
     45      return apply_filters( 'oir_ignored_sizes', $ignored_sizes );
     46
     47    }
     48
     49    // Filter the sizes that get created initially
     50    public function remove_intermediate_sizes( $sizes ) {
     51
     52      return array_intersect_key( $sizes, $this->get_ignored_image_sizes() );
     53
     54    }
     55
     56    public function add_tools_subpage() {
     57
     58      add_submenu_page(
     59        'tools.php',
     60        __( 'Remove image sizes', 'optimize-images-resizing' ),
     61        __( 'Remove image sizes', 'optimize-images-resizing' ),
     62        'manage_options',
     63        'optimize-images-resizing',
     64        array( $this, 'tools_subpage_output' )
     65      );
     66
     67    }
     68
     69    // add a small output to media settings screen
     70    public function tools_subpage_output() {
     71
     72      $cleanup_in_progress = get_option( 'oir_cleanup_progress_page', false );
     73
     74      if ( false !== $cleanup_in_progress ) {
     75
     76        $cleanup_in_progress = absint( $cleanup_in_progress );
     77
     78      }
     79
     80      ?>
     81
     82      <div class="wrap">
     83        <h1><?php _e( 'Remove image sizes', 'optimize-images-resizing' ); ?></h1>
     84        <p>
     85          <label>
     86            <input type="checkbox" id="oir-keep-the-log" value="1">
     87            <?php _e( 'Keep a record of removed image sizes (not recommended for huge media libraries)' ); ?>
     88          </label>
     89        </p>
     90
     91        <div id="oir-buttons">
     92
     93          <?php if ( $cleanup_in_progress ) : ?>
     94
     95            <button
     96            id="oir-resume-remove-image-sizes"
     97            class="button button-primary"
     98            data-page="<?php echo esc_attr( $cleanup_in_progress ); ?>"
     99            ><?php _e( 'Resume old cleanup', 'optimize-images-resizing' ); ?></button>
     100
     101          <?php endif; ?>
     102
     103          <button id="oir-remove-image-sizes" class="button"><?php _e( 'Start new cleanup', 'optimize-images-resizing' ); ?></button>
     104
     105        </div>
     106        <p id="oir-status-message"></p>
     107        <p class="description"><?php _e( 'Click the button above to remove redundant image sizes. You only need to do this once.', 'optimize-images-resizing' ); ?></p>
     108        <div id="oir-log"></div>
     109      </div>
     110
     111      <?php
     112
     113    }
     114
     115    // cleans up extra image sizes when called via ajax
     116    public function remove_image_sizes( $__attachment_id ) {
     117
     118      $paged = ! empty( $_POST['paged'] ) ? absint( $_POST['paged'] ) : 1;
     119      $removed = ! empty( $_POST['removed'] ) ? absint( $_POST['removed'] ) : 0;
     120      $record_log = ! empty( $_POST['record_log'] ) ? 'true' === $_POST['record_log'] : false;
     121      $removed_in_current_request = array();
     122
     123      update_option( 'oir_cleanup_progress_page', $paged );
     124
     125      if ( ! $__attachment_id ) {
     126
     127        check_ajax_referer( 'oir-nonce', 'nonce' );
     128
     129        $args = array(
     130          'fields'         => 'ids',
     131          'paged'          => $paged,
     132          'post_mime_type' => 'image',
     133          'post_status'    => 'any',
     134          'post_type'      => 'attachment',
     135          'posts_per_page' => 10,
     136        );
     137
     138      } else {
     139
     140        $args = array(
     141          'fields'         => 'ids',
     142          'post_mime_type' => 'image',
     143          'post_status'    => 'any',
     144          'post_type'      => 'attachment',
     145          'post__in'       => array( absint( $__attachment_id ) ),
     146        );
     147
     148      }
     149
     150      $query = new WP_Query( $args );
     151
     152      $found = absint( $query->found_posts );
     153      $finished = empty( $query->posts ) ? true : false;
     154
     155      if ( ! $finished ) {
     156
     157        $upload_dir = wp_upload_dir();
     158
     159        foreach ( $query->posts as $attachment_id ) {
     160
     161          $meta = wp_get_attachment_metadata( $attachment_id );
     162
     163          if ( empty( $meta['file'] ) ) {
     164
     165            continue;
     166
     167          }
     168
     169          $file_path = str_replace( basename( $meta['file'] ), '', trailingslashit( $upload_dir['basedir'] ) . $meta['file'] );
     170
     171          if ( empty( $meta['sizes'] ) || ! is_array( $meta['sizes'] ) ) {
     172
     173            continue;
     174
     175          }
     176
     177          // Don't remove images if they are used in default image sizes
     178          // (happens when custom image size matches the dimensions of the default ones)
     179          $do_not_delete = array();
     180
     181          $ignored_sizes = $this->get_ignored_image_sizes();
     182
     183          foreach ( $meta['sizes'] as $size => $params ) {
     184
     185            $file = realpath( $file_path . $params['file'] );
     186
     187            // we don't want to delete thumbnails, they are used in admin area
     188            if ( in_array( $size, $ignored_sizes ) ) {
     189
     190              $do_not_delete[] = $file;
     191
     192              continue;
     193
     194            }
     195
     196            if ( ! in_array( $file, $do_not_delete ) && is_readable( $file ) ) {
     197
     198              unlink( $file );
     199
     200              unset( $meta['sizes'][ $size ] );
     201
     202              $removed++;
     203
     204              $removed_in_current_request[] = $file;
     205
     206            }
     207
     208          }
     209
     210          wp_update_attachment_metadata( $attachment_id, $meta );
     211
     212        }
     213
     214        if ( $record_log ) {
     215
     216          $removed_so_far = get_option( 'oir_removed_log', array() );
     217
     218          $removed_log = array_merge( $removed_so_far, $removed_in_current_request );
     219
     220          update_option( 'oir_removed_log', $removed_log );
     221
     222        }
     223
     224      } else {
     225
     226        delete_option( 'oir_cleanup_progress_page' );
     227
     228      }
     229
     230      if ( ! $__attachment_id ) {
     231
     232        $response = array(
     233          'finished' => $finished,
     234          'found' => $found,
     235          'paged' => $paged,
     236          'removed' => $removed,
     237          'success' => true,
     238        );
     239
     240        if ( $record_log && $finished ) {
     241
     242          $removed_so_far = get_option( 'oir_removed_log', array() );
     243
     244          $response['removed_log'] = $removed_so_far;
     245
     246        }
     247
     248        wp_send_json( $response );
     249
     250      }
     251
     252    }
     253
     254    // add js and css needed on media settings screen
     255    public function enqueue_assets( $hook ) {
     256
     257      if ( 'tools_page_optimize-images-resizing' !== $hook ) {
     258
     259        // we only need this script in media settings
     260        return;
     261
     262      }
     263
     264      wp_enqueue_script( 'oir_remove_image_sizes', OIR_JS_URL . 'remove-image-sizes.js' );
     265
     266      $localize = array(
     267        'l10n'  => array(
     268          'something_wrong'  => __( 'Something went wrong, please try again or contact the developer!', 'optimize-images-resizing' ),
     269          'process_finished' => __( 'Cleanup was successfully completed. Number of images removed: %d.', 'optimize-images-resizing' ),
     270          'nothing_to_remove' => __( 'There was nothing to clean up, looks like you have no redundant image sizes. Good job!', 'optimize-images-resizing' ),
     271          'cleanup_progress' => __( 'Cleanup in progress, leave this page open!', 'optimize-images-resizing' ),
     272        ),
     273        'nonce' => wp_create_nonce( 'oir-nonce' ),
     274      );
     275
     276      wp_localize_script( 'oir_remove_image_sizes', 'oir_plugin', $localize );
     277
     278      wp_enqueue_style( 'oir_remove_image_sizes', OIR_CSS_URL . 'remove-image-sizes.css' );
     279
     280    }
     281
     282  }
    209283
    210284endif;
     
    217291if ( defined( 'WP_CLI' ) && WP_CLI ) {
    218292
    219     require_once 'inc/class-cli-command.php';
     293  require_once 'inc/class-cli-command.php';
    220294
    221295}
  • optimize-images-resizing/trunk/inc/class-resize-image.php

    r1270791 r1556512  
    77if ( ! class_exists( 'OIR_Resize_Image' ) ) :
    88
    9     final class OIR_Resize_Image {
     9  final class OIR_Resize_Image {
    1010
    11         // Will hold the only instance of our main plugin class
    12         private static $instance;
     11    // Will hold the only instance of our main plugin class
     12    private static $instance;
    1313
    14         // Instantiate the class and set up stuff
    15         public static function instantiate() {
     14    // Instantiate the class and set up stuff
     15    public static function instantiate() {
    1616
    17             if ( ! isset( self::$instance ) && ! ( self::$instance instanceof OIR_Resize_Image ) ) {
     17      if ( ! isset( self::$instance ) && ! ( self::$instance instanceof OIR_Resize_Image ) ) {
    1818
    19                 self::$instance = new OIR_Resize_Image();
     19        self::$instance = new OIR_Resize_Image();
    2020
    21                 add_filter( 'image_downsize', array( self::$instance, 'image_downsize' ), 10, 3 );
     21      }
    2222
    23             }
     23      return self::$instance;
    2424
    25             return self::$instance;
     25    }
    2626
    27         }
     27    public function __construct() {
    2828
    29         // we hook into the filter, check if image size exists, generate it if not and then bail out
    30         public function image_downsize( $out, $id, $size ) {
     29      add_filter( 'image_downsize', array( self::$instance, 'image_downsize' ), 10, 3 );
    3130
    32             // we don't handle this
    33             if ( is_array( $size ) ) return false;
     31    }
    3432
    35             $meta = wp_get_attachment_metadata( $id );
    36             $wanted_width = $wanted_height = 0;
     33    // we hook into the filter, check if image size exists, generate it if not and then bail out
     34    public function image_downsize( $out, $id, $size ) {
    3735
    38             if ( empty( $meta['file'] ) ) return false;
     36      // we don't handle this
     37      if ( is_array( $size ) ) return false;
    3938
    40             // get $size dimensions
    41             global $_wp_additional_image_sizes;
     39      $meta = wp_get_attachment_metadata( $id );
     40      $wanted_width = $wanted_height = 0;
    4241
    43             if ( isset( $_wp_additional_image_sizes ) && isset( $_wp_additional_image_sizes[ $size ] ) ) {
     42      if ( empty( $meta['file'] ) ) return false;
    4443
    45                 $wanted_width = $_wp_additional_image_sizes[ $size ]['width'];
    46                 $wanted_height = $_wp_additional_image_sizes[ $size ]['height'];
    47                 $wanted_crop = isset( $_wp_additional_image_sizes[ $size ]['crop'] ) ? $_wp_additional_image_sizes[ $size ]['crop'] : false;
     44      // get $size dimensions
     45      global $_wp_additional_image_sizes;
    4846
    49             } else if ( in_array( $size, array( 'thumbnail', 'medium', 'large' ) ) ) {
     47      if ( isset( $_wp_additional_image_sizes ) && isset( $_wp_additional_image_sizes[ $size ] ) ) {
    5048
    51                 $wanted_width  = get_option( $size . '_size_w' );
    52                 $wanted_height = get_option( $size . '_size_h' );
    53                 $wanted_crop   = ( 'thumbnail' === $size ) ? (bool) get_option( 'thumbnail_crop' ) : false;
     49        $wanted_width = $_wp_additional_image_sizes[ $size ]['width'];
     50        $wanted_height = $_wp_additional_image_sizes[ $size ]['height'];
     51        $wanted_crop = isset( $_wp_additional_image_sizes[ $size ]['crop'] ) ? $_wp_additional_image_sizes[ $size ]['crop'] : false;
    5452
    55             } else {
     53      } else if ( in_array( $size, array( 'thumbnail', 'medium', 'large' ) ) ) {
    5654
    57                 // unknown size, bail out
    58                 return false;
     55        $wanted_width  = get_option( $size . '_size_w' );
     56        $wanted_height = get_option( $size . '_size_h' );
     57        $wanted_crop   = ( 'thumbnail' === $size ) ? (bool) get_option( 'thumbnail_crop' ) : false;
    5958
    60             }
     59      } else {
    6160
    62             if ( 0 === absint( $wanted_width ) && 0 === absint( $wanted_height ) ) {
     61        // unknown size, bail out
     62        return false;
    6363
    64                 return false;
     64      }
    6565
    66             }
     66      if ( 0 === absint( $wanted_width ) && 0 === absint( $wanted_height ) ) {
    6767
    68             if ( $intermediate = image_get_intermediate_size( $id, $size ) ) {
     68        return false;
    6969
    70                 $img_url = wp_get_attachment_url( $id );
    71                 $img_url_basename = wp_basename( $img_url );
     70      }
    7271
    73                 $img_url = str_replace( $img_url_basename, $intermediate['file'], $img_url );
    74                 $result_width = $intermediate['width'];
    75                 $result_height = $intermediate['height'];
     72      if ( $intermediate = image_get_intermediate_size( $id, $size ) ) {
    7673
    77                 return array(
    78                     $img_url,
    79                     $result_width,
    80                     $result_height,
    81                     true,
    82                 );
     74        $img_url = wp_get_attachment_url( $id );
     75        $img_url_basename = wp_basename( $img_url );
    8376
    84             } else {
     77        $img_url = str_replace( $img_url_basename, $intermediate['file'], $img_url );
     78        $result_width = $intermediate['width'];
     79        $result_height = $intermediate['height'];
    8580
    86                 // image size not found, create it
    87                 $attachment_path = get_attached_file( $id );
     81        return array(
     82          $img_url,
     83          $result_width,
     84          $result_height,
     85          true,
     86        );
    8887
    89                 $image_editor = wp_get_image_editor( $attachment_path );
     88      } else {
    9089
    91                 if ( ! is_wp_error( $image_editor ) ) {
     90        // image size not found, create it
     91        $attachment_path = get_attached_file( $id );
    9292
    93                     $image_editor->resize( $wanted_width, $wanted_height, $wanted_crop );
    94                     $result_image_size = $image_editor->get_size();
     93        $image_editor = wp_get_image_editor( $attachment_path );
    9594
    96                     $result_width = $result_image_size['width'];
    97                     $result_height = $result_image_size['height'];
     95        if ( ! is_wp_error( $image_editor ) ) {
    9896
    99                     $suffix = $result_width . 'x' . $result_height;
    100                     $filename = $image_editor->generate_filename( $suffix );
     97          $image_editor->resize( $wanted_width, $wanted_height, $wanted_crop );
     98          $result_image_size = $image_editor->get_size();
    10199
    102                     $image_editor->save( $filename );
     100          $result_width = $result_image_size['width'];
     101          $result_height = $result_image_size['height'];
    103102
    104                     $result_filename = wp_basename( $filename );
     103          $suffix = $result_width . 'x' . $result_height;
     104          $filename = $image_editor->generate_filename( $suffix );
    105105
    106                     $meta['sizes'][ $size ] = array(
    107                         'file'      => $result_filename,
    108                         'width'     => $result_width,
    109                         'height'    => $result_height,
    110                         'mime-type' => get_post_mime_type( $id ),
    111                     );
     106          $image_editor->save( $filename );
    112107
    113                     wp_update_attachment_metadata( $id, $meta );
     108          $result_filename = wp_basename( $filename );
    114109
    115                     $img_url = wp_get_attachment_url( $id );
    116                     $img_url_basename = wp_basename( $img_url );
     110          $meta['sizes'][ $size ] = array(
     111            'file'      => $result_filename,
     112            'width'     => $result_width,
     113            'height'    => $result_height,
     114            'mime-type' => get_post_mime_type( $id ),
     115          );
    117116
    118                     $img_url = str_replace( $img_url_basename, $result_filename, $img_url );
     117          wp_update_attachment_metadata( $id, $meta );
    119118
    120                     return array(
    121                         $img_url,
    122                         $result_width,
    123                         $result_height,
    124                         true,
    125                     );
     119          $img_url = wp_get_attachment_url( $id );
     120          $img_url_basename = wp_basename( $img_url );
    126121
    127                 } else {
     122          $img_url = str_replace( $img_url_basename, $result_filename, $img_url );
    128123
    129                     return false;
     124          return array(
     125            $img_url,
     126            $result_width,
     127            $result_height,
     128            true,
     129          );
    130130
    131                 }
     131        } else {
    132132
    133             }
     133          return false;
    134134
    135             return false;
     135        }
    136136
    137         }
     137      }
    138138
    139     }
     139      return false;
     140
     141    }
     142
     143  }
    140144
    141145endif;
  • optimize-images-resizing/trunk/js/remove-image-sizes.js

    r1410429 r1556512  
    11jQuery( function( $ ) {
    22
    3     "use strict";
     3  "use strict";
    44
    5     var $body = $( 'body' ),
    6         $button = $( '#oir-remove-image-sizes' ),
    7         $message = $( '#oir-status-message' ),
    8         $log = $( '#oir-log' );
     5  var $body = $( 'body' ),
     6    $button_new_cleanup = $( '#oir-remove-image-sizes' ),
     7    $button_old_cleanup = $( '#oir-resume-remove-image-sizes' ),
     8    $buttons = $( '#oir-buttons' ),
     9    $message = $( '#oir-status-message' ),
     10    $log = $( '#oir-log' ),
     11    record_log = false;
    912
    10     $button.on( 'click', function( e ) {
     13  $button_new_cleanup.on( 'click', function( e ) {
    1114
    12         e.preventDefault();
     15    e.preventDefault();
    1316
    14         $button.hide();
    15         $message
    16             .html( oir_plugin.l10n.cleanup_progress )
    17             .show();
     17    $buttons.hide();
     18    $message
     19      .html( oir_plugin.l10n.cleanup_progress )
     20      .show();
    1821
    19         ajax_request();
     22    record_log = $( '#oir-keep-the-log' ).is( ':checked' );
    2023
    21     });
     24    ajax_request();
    2225
    23     $body.on( 'click', '.js-oir-show-log', function( e ) {
     26  });
    2427
    25         e.preventDefault();
     28  $button_old_cleanup.on( 'click', function( e ) {
    2629
    27         $log.stop().slideToggle();
     30    e.preventDefault();
    2831
    29     });
     32    $buttons.hide();
     33    $message
     34      .html( oir_plugin.l10n.cleanup_progress )
     35      .show();
    3036
    31     function ajax_request( paged, removed, removed_log ) {
     37    record_log = $( '#oir-keep-the-log' ).is( ':checked' );
    3238
    33         paged = 'undefined' == typeof paged ? 1 : parseInt( paged, 10 );
    34         removed = 'undefined' == typeof removed ? 0 : parseInt( removed, 10 );
    35         removed_log = 'undefined' == typeof removed_log ? [] : removed_log;
     39    var page = parseInt( $button_old_cleanup.attr( 'data-page' ), 10 );
    3640
    37         $.post(
    38             ajaxurl,
    39             {
    40                 action     : 'oir_remove_image_sizes',
    41                 nonce      : oir_plugin.nonce,
    42                 paged      : paged,
    43                 removed    : removed,
    44                 removed_log: removed_log
    45             },
    46             function( response ) {
     41    ajax_request( page );
    4742
    48                 if ( true !== response.success ) {
     43  });
    4944
    50                     // Looks like something went wrong
     45  $body.on( 'click', '.js-oir-show-log', function( e ) {
    5146
    52                     $message
    53                         .html( oir_plugin.l10n.something_wrong )
    54                         .show();
     47    e.preventDefault();
    5548
    56                     return;
     49    $log.stop().slideToggle();
    5750
    58                 }
     51  });
    5952
    60                 if ( true === response.finished ) {
     53  function ajax_request( paged, removed ) {
    6154
    62                     // Cleanup has finished
     55    paged = 'undefined' == typeof paged ? 1 : parseInt( paged, 10 );
     56    removed = 'undefined' == typeof removed ? 0 : parseInt( removed, 10 );
    6357
    64                     var message = 0 === parseInt( response.removed, 10 ) ? oir_plugin.l10n.nothing_to_remove : oir_plugin.l10n.process_finished.replace( '%d', '<a href="#" class="js-oir-show-log">' + response.removed + '</a>' );
     58    $.post(
     59      ajaxurl,
     60      {
     61        action: 'oir_remove_image_sizes',
     62        nonce: oir_plugin.nonce,
     63        paged: paged,
     64        removed: removed,
     65        record_log: record_log
     66      },
     67      function( response ) {
    6568
    66                     $message
    67                         .html( message );
     69        if ( true !== response.success ) {
    6870
    69                     if ( 0 !== parseInt( response.removed, 10 ) && response.removed_log.length ) {
     71          // Looks like something went wrong
    7072
    71                         var logHtml = '<pre>';
     73          $message
     74            .html( oir_plugin.l10n.something_wrong )
     75            .show();
    7276
    73                         $.each( response.removed_log, function( i, file ) {
     77          return;
    7478
    75                             logHtml += file + '\n';
     79        }
    7680
    77                         });
     81        if ( true === response.finished ) {
    7882
    79                         logHtml += '</pre>';
     83          // Cleanup has finished
    8084
    81                         $log.html( logHtml )
     85          var message = 0 === parseInt( response.removed, 10 ) ? oir_plugin.l10n.nothing_to_remove : oir_plugin.l10n.process_finished.replace( '%d', '<a href="#" class="js-oir-show-log">' + response.removed + '</a>' );
    8286
    83                     }
     87          $message
     88            .html( message );
    8489
    85                     return;
     90          if ( record_log && 0 !== parseInt( response.removed, 10 ) && response.removed_log.length ) {
    8691
    87                 }
     92            var logHtml = '<pre>';
    8893
    89                 // Cleanup still in progress
     94            $.each( response.removed_log, function( i, file ) {
    9095
    91                 var completed = ( response.paged * 10 > response.found ) ? response.found : response.paged * 10;
     96              logHtml += file + '\n';
    9297
    93                 $message
    94                     .html( oir_plugin.l10n.cleanup_progress + ' ' + completed + ' / ' + response.found );
     98            });
    9599
    96                 ajax_request( ++response.paged, response.removed, response.removed_log );
     100            logHtml += '</pre>';
    97101
    98             },
    99             'json'
    100         );
     102            $log.html( logHtml )
    101103
    102     }
     104          }
     105
     106          return;
     107
     108        }
     109
     110        // Cleanup still in progress
     111
     112        var completed = ( response.paged * 10 > response.found ) ? response.found : response.paged * 10;
     113
     114        $message
     115          .html( oir_plugin.l10n.cleanup_progress + ' ' + completed + ' / ' + response.found );
     116
     117        ajax_request( ++response.paged, response.removed );
     118
     119      },
     120      'json'
     121    );
     122
     123  }
    103124
    104125});
  • optimize-images-resizing/trunk/optimize-images-resizing.php

    r1410429 r1556512  
    99 * Text Domain: optimize-images-resizing
    1010 * Domain Path: /languages
    11  * Version: 1.3.0
     11 * Version: 1.4.0
    1212 */
    1313
     
    1818if ( ! class_exists( 'OIR_Init' ) ) :
    1919
    20     final class OIR_Init {
     20  final class OIR_Init {
    2121
    22         // Will hold the only instance of our main plugin class
    23         private static $instance;
     22    // Will hold the only instance of our main plugin class
     23    private static $instance;
    2424
    25         // Instantiate the class and set up stuff
    26         public static function instantiate() {
     25    // Instantiate the class and set up stuff
     26    public static function instantiate() {
    2727
    28             if ( ! isset( self::$instance ) && ! ( self::$instance instanceof OIR_Init ) ) {
     28      if ( ! isset( self::$instance ) && ! ( self::$instance instanceof OIR_Init ) ) {
    2929
    30                 self::$instance = new OIR_Init();
    31                 self::$instance->define_constants();
    32                 self::$instance->include_files();
     30        self::$instance = new OIR_Init();
     31        self::$instance->define_constants();
     32        self::$instance->include_files();
    3333
    34                 // load textdomain
    35                 add_action( 'plugins_loaded', array( self::$instance, 'load_textdomain' ) );
     34      }
    3635
    37             }
     36      return self::$instance;
    3837
    39             return self::$instance;
     38    }
    4039
    41         }
     40    public function __construct() {
    4241
    43         // Defines plugin constants
    44         private function define_constants() {
     42      // load textdomain
     43      add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
    4544
    46             // Plugin version
    47             if ( ! defined( 'OIR_VERSION' ) )
    48                 define( 'OIR_VERSION', '1.3.0' );
     45      add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_link_to_plugin_page' ) );
    4946
    50             // Plugin Folder Path
    51             if ( ! defined( 'OIR_PLUGIN_DIR' ) )
    52                 define( 'OIR_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
     47    }
    5348
    54             // Plugin Include Path
    55             if ( ! defined( 'OIR_PLUGIN_DIR_INC' ) )
    56                 define( 'OIR_PLUGIN_DIR_INC', OIR_PLUGIN_DIR . 'inc/' );
     49    // Defines plugin constants
     50    private function define_constants() {
    5751
    58             // Plugin Folder URL
    59             if ( ! defined( 'OIR_PLUGIN_URL' ) )
    60                 define( 'OIR_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
     52      // Plugin version
     53      if ( ! defined( 'OIR_VERSION' ) )
     54        define( 'OIR_VERSION', '1.4.0' );
    6155
    62             // Plugin JS Folder URL
    63             if ( ! defined( 'OIR_JS_URL' ) )
    64                 define( 'OIR_JS_URL', OIR_PLUGIN_URL . 'js/' );
     56      // Plugin Folder Path
     57      if ( ! defined( 'OIR_PLUGIN_DIR' ) )
     58        define( 'OIR_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
    6559
    66             // Plugin CSS Folder URL
    67             if ( ! defined( 'OIR_CSS_URL' ) )
    68                 define( 'OIR_CSS_URL', OIR_PLUGIN_URL . 'css/' );
     60      // Plugin Include Path
     61      if ( ! defined( 'OIR_PLUGIN_DIR_INC' ) )
     62        define( 'OIR_PLUGIN_DIR_INC', OIR_PLUGIN_DIR . 'inc/' );
    6963
    70             // Plugin Root File
    71             if ( ! defined( 'OIR_PLUGIN_FILE' ) )
    72                 define( 'OIR_PLUGIN_FILE', __FILE__ );
     64      // Plugin Folder URL
     65      if ( ! defined( 'OIR_PLUGIN_URL' ) )
     66        define( 'OIR_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
    7367
    74         }
     68      // Plugin JS Folder URL
     69      if ( ! defined( 'OIR_JS_URL' ) )
     70        define( 'OIR_JS_URL', OIR_PLUGIN_URL . 'js/' );
    7571
    76         // Includes necessary files
    77         private function include_files() {
     72      // Plugin CSS Folder URL
     73      if ( ! defined( 'OIR_CSS_URL' ) )
     74        define( 'OIR_CSS_URL', OIR_PLUGIN_URL . 'css/' );
    7875
    79             require_once OIR_PLUGIN_DIR_INC . 'class-resize-image.php';
     76      // Plugin Root File
     77      if ( ! defined( 'OIR_PLUGIN_FILE' ) )
     78        define( 'OIR_PLUGIN_FILE', __FILE__ );
    8079
    81             if ( is_admin() ) {
     80    }
    8281
    83                 require_once OIR_PLUGIN_DIR_INC . 'class-remove-image-sizes.php';
     82    // Includes necessary files
     83    private function include_files() {
    8484
    85             }
     85      require_once OIR_PLUGIN_DIR_INC . 'class-resize-image.php';
    8686
    87         }
     87      if ( is_admin() ) {
    8888
    89         // sets up textdomain
    90         public static function load_textdomain() {
     89        require_once OIR_PLUGIN_DIR_INC . 'class-remove-image-sizes.php';
    9190
    92             $lang_dir = dirname( plugin_basename( OIR_PLUGIN_FILE ) ) . '/languages/';
     91      }
    9392
    94             $lang_dir = trailingslashit( apply_filters( 'oir_textdomain_location', $lang_dir ) );
     93    }
    9594
    96             load_plugin_textdomain( 'optimize-images-resizing', false, $lang_dir );
     95    // adds our own links to the plugins table
     96    public function add_link_to_plugin_page( $links ) {
    9797
    98         }
     98      $links[] = '<a href="'. esc_url( get_admin_url( null, 'tools.php?page=optimize-images-resizing' ) ) .'">' . __( 'Remove image sizes', 'optimize-images-resizing' ) . '</a>';
    9999
    100     }
     100      return $links;
     101
     102    }
     103
     104    // sets up textdomain
     105    public static function load_textdomain() {
     106
     107      $lang_dir = dirname( plugin_basename( OIR_PLUGIN_FILE ) ) . '/languages/';
     108
     109      $lang_dir = trailingslashit( apply_filters( 'oir_textdomain_location', $lang_dir ) );
     110
     111      load_plugin_textdomain( 'optimize-images-resizing', false, $lang_dir );
     112
     113    }
     114
     115  }
    101116
    102117endif;
  • optimize-images-resizing/trunk/readme.txt

    r1410429 r1556512  
    33Tags: images, media, resizing, optimize, cleanup, remove, empty, clean, resize, image
    44Requires at least: 3.8
    5 Tested up to: 4.5
     5Tested up to: 4.7
    66Stable tag: 1.3.0
    77License: GPLv3
     
    1616What this plugin does is it optimizes the image handling in such a way that images are resized only when they are actually needed. What that means is that if your plugins/theme define a lot of image sizes, none of them will be generated on the image upload (like they would be usually), but only if they are actually requested in that size.
    1717
    18 Resizing is done only once and later normally served by WordPress, so there is no performance hit.
     18Resizing is done only once, images are later normally served by WordPress, so there is no performance hit.
    1919
    20 Plugin also includes a method for removing all of the image sizes generated so far (useful when you install this plugin on a site with a lot of existing media).
     20Plugin also includes a method for removing all of the previously generated image sizes (useful when you install this plugin on a site with a lot of existing media).
    2121
    22 **TO REMOVE** image sizes generated prior to activating the plugin, visit the Settings -> Media and use the button under "Remove image sizes" to perform the cleanup.
     22**TO REMOVE** image sizes generated prior to activating the plugin, visit the 'Tools -> Remove image sizes' and use the button to perform the cleanup.
    2323
    2424Other than that, you don't need to do anything, plugin works silently in the background.
     
    35351. Upload `optimize-images-resizing` folder to the `/wp-content/plugins/` directory
    36362. Activate the plugin through the 'Plugins' menu in WordPress
    37 3. (optional) Visit Settings -> Media to clean up your media folder
     373. (optional) Visit 'Tools -> Remove image sizes' to clean up your media folder
    38384. ???
    39395. Profit
     
    4343**I just installed the plugin. Is there anything else I need to do?**
    4444
    45 That depends. Is this a new site with no existing images? If the answer is yes, then there is nothing else you need to do, any uploads that happen after you activated this plugin will be automatically cleaned up. If the answer is no, keep reading.
     45Is this a new site with no existing images? If the answer is yes, then there is nothing else you need to do, any uploads that happen after you activated this plugin will be automatically cleaned up. If the answer is no, keep reading.
    4646
    4747**How to clean up existing images?**
    4848
    49 Images can be cleaned up at any time by visiting Settings -> Media in your WordPress Dashboard. You will see a new piece of UI labeled "Remove image sizes" (check Screenshots tab if you want to see how it looks like). Simply click on the "Start cleanup" button and wait for the process to finish (there is a visual feedback for the duration of the cleanup).
     49Images can be cleaned up at any time by visiting 'Tools -> Remove image sizes' in your WordPress Dashboard. Simply click on the "Start new cleanup" button and wait for the process to finish (there is a visual feedback for the duration of the cleanup).
    5050
    5151**Some image sizes are not cleaned up, which ones and why?**
    5252
    53 Plugin never cleans default image sizes (thumbnail, medium, large), so if your theme/plugins don't define custom image sizes, this plugin will not help you out. Why does it not clean up those  mage sizes? Well the reason for that is that all of those image sizes are used in the Media UI of the WordPress Dashboard. What that means is: if plugin were to clean up all sizes, they would be generated for all of your images as soon as you would visit the Media screen. Since I don't know of anyone that never visits the Media screen, it made sense to exclude those image sizes from the cleaning process and avoid the redundant server load.
     53Plugin never cleans default image sizes (thumbnail, medium, large), so if your theme/plugins don't define custom image sizes, you don't need this plugin. Why does it not clean up those image sizes? Well the reason for that is that all of those image sizes are used in the Media UI of the WordPress Dashboard. What that means is: if plugin were to clean up all sizes, they would be generated for all of your images as soon as you would visit the Media screen. Since I don't know of anyone that never visits the Media screen, it made sense to exclude those image sizes from the cleaning process and avoid the redundant server load.
    5454
    5555**How do I know which files the plugin cleaned up?**
    5656
    57 A list of removed files is available only for the manual cleanup request. Once the request finishes, a message will appear stating how many images it removed. Click on the number to show the list of files that were removed in the process.
     57A list of removed files is available only for the manual cleanup request, by checking the checkbox at the top of the plugin page. Once the request finishes, a message will appear stating how many images it removed. Click on the number to show the list of files that were removed in the process.
    5858
    5959**Are there any drawbacks to using this plugin?**
     
    6767
    6868== Changelog ==
     69
     70= 1.4.0 =
     71* Move plugin to the Tools menu
     72* Add support for resuming image sizes removing
     73* Declare WordPress 4.7 compatibility.
    6974
    7075= 1.3.0 =
Note: See TracChangeset for help on using the changeset viewer.