Changeset 1556512
- Timestamp:
- 12/17/2016 02:01:02 AM (9 years ago)
- Location:
- optimize-images-resizing
- Files:
-
- 7 edited
-
assets/screenshot-1.png (modified) (previous)
-
trunk/css/remove-image-sizes.css (modified) (1 diff)
-
trunk/inc/class-remove-image-sizes.php (modified) (2 diffs)
-
trunk/inc/class-resize-image.php (modified) (1 diff)
-
trunk/js/remove-image-sizes.js (modified) (1 diff)
-
trunk/optimize-images-resizing.php (modified) (2 diffs)
-
trunk/readme.txt (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
optimize-images-resizing/trunk/css/remove-image-sizes.css
r1410429 r1556512 1 1 #oir-status-message, 2 2 #oir-log { 3 display: none;3 display: none; 4 4 } 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 7 7 if ( ! class_exists( 'OIR_Remove_Image_Sizes' ) ) : 8 8 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 } 209 283 210 284 endif; … … 217 291 if ( defined( 'WP_CLI' ) && WP_CLI ) { 218 292 219 require_once 'inc/class-cli-command.php';293 require_once 'inc/class-cli-command.php'; 220 294 221 295 } -
optimize-images-resizing/trunk/inc/class-resize-image.php
r1270791 r1556512 7 7 if ( ! class_exists( 'OIR_Resize_Image' ) ) : 8 8 9 final class OIR_Resize_Image {9 final class OIR_Resize_Image { 10 10 11 // Will hold the only instance of our main plugin class12 private static $instance;11 // Will hold the only instance of our main plugin class 12 private static $instance; 13 13 14 // Instantiate the class and set up stuff15 public static function instantiate() {14 // Instantiate the class and set up stuff 15 public static function instantiate() { 16 16 17 if ( ! isset( self::$instance ) && ! ( self::$instance instanceof OIR_Resize_Image ) ) {17 if ( ! isset( self::$instance ) && ! ( self::$instance instanceof OIR_Resize_Image ) ) { 18 18 19 self::$instance = new OIR_Resize_Image();19 self::$instance = new OIR_Resize_Image(); 20 20 21 add_filter( 'image_downsize', array( self::$instance, 'image_downsize' ), 10, 3 ); 21 } 22 22 23 } 23 return self::$instance; 24 24 25 return self::$instance; 25 } 26 26 27 } 27 public function __construct() { 28 28 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 ); 31 30 32 // we don't handle this 33 if ( is_array( $size ) ) return false; 31 } 34 32 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 ) { 37 35 38 if ( empty( $meta['file'] ) ) return false; 36 // we don't handle this 37 if ( is_array( $size ) ) return false; 39 38 40 // get $size dimensions 41 global $_wp_additional_image_sizes;39 $meta = wp_get_attachment_metadata( $id ); 40 $wanted_width = $wanted_height = 0; 42 41 43 if ( isset( $_wp_additional_image_sizes ) && isset( $_wp_additional_image_sizes[ $size ] ) ) { 42 if ( empty( $meta['file'] ) ) return false; 44 43 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; 48 46 49 } else if ( in_array( $size, array( 'thumbnail', 'medium', 'large' )) ) {47 if ( isset( $_wp_additional_image_sizes ) && isset( $_wp_additional_image_sizes[ $size ] ) ) { 50 48 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; 54 52 55 } else{53 } else if ( in_array( $size, array( 'thumbnail', 'medium', 'large' ) ) ) { 56 54 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; 59 58 60 } 59 } else { 61 60 62 if ( 0 === absint( $wanted_width ) && 0 === absint( $wanted_height ) ) { 61 // unknown size, bail out 62 return false; 63 63 64 return false; 64 } 65 65 66 } 66 if ( 0 === absint( $wanted_width ) && 0 === absint( $wanted_height ) ) { 67 67 68 if ( $intermediate = image_get_intermediate_size( $id, $size ) ) { 68 return false; 69 69 70 $img_url = wp_get_attachment_url( $id ); 71 $img_url_basename = wp_basename( $img_url ); 70 } 72 71 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 ) ) { 76 73 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 ); 83 76 84 } else { 77 $img_url = str_replace( $img_url_basename, $intermediate['file'], $img_url ); 78 $result_width = $intermediate['width']; 79 $result_height = $intermediate['height']; 85 80 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 ); 88 87 89 $image_editor = wp_get_image_editor( $attachment_path ); 88 } else { 90 89 91 if ( ! is_wp_error( $image_editor ) ) { 90 // image size not found, create it 91 $attachment_path = get_attached_file( $id ); 92 92 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 ); 95 94 96 $result_width = $result_image_size['width']; 97 $result_height = $result_image_size['height']; 95 if ( ! is_wp_error( $image_editor ) ) { 98 96 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(); 101 99 102 $image_editor->save( $filename ); 100 $result_width = $result_image_size['width']; 101 $result_height = $result_image_size['height']; 103 102 104 $result_filename = wp_basename( $filename ); 103 $suffix = $result_width . 'x' . $result_height; 104 $filename = $image_editor->generate_filename( $suffix ); 105 105 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 ); 112 107 113 wp_update_attachment_metadata( $id, $meta);108 $result_filename = wp_basename( $filename ); 114 109 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 ); 117 116 118 $img_url = str_replace( $img_url_basename, $result_filename, $img_url);117 wp_update_attachment_metadata( $id, $meta ); 119 118 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 ); 126 121 127 } else { 122 $img_url = str_replace( $img_url_basename, $result_filename, $img_url ); 128 123 129 return false; 124 return array( 125 $img_url, 126 $result_width, 127 $result_height, 128 true, 129 ); 130 130 131 } 131 } else { 132 132 133 } 133 return false; 134 134 135 return false; 135 } 136 136 137 }137 } 138 138 139 } 139 return false; 140 141 } 142 143 } 140 144 141 145 endif; -
optimize-images-resizing/trunk/js/remove-image-sizes.js
r1410429 r1556512 1 1 jQuery( function( $ ) { 2 2 3 "use strict";3 "use strict"; 4 4 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; 9 12 10 $button.on( 'click', function( e ) {13 $button_new_cleanup.on( 'click', function( e ) { 11 14 12 e.preventDefault();15 e.preventDefault(); 13 16 14 $button.hide();15 $message16 .html( oir_plugin.l10n.cleanup_progress )17 .show();17 $buttons.hide(); 18 $message 19 .html( oir_plugin.l10n.cleanup_progress ) 20 .show(); 18 21 19 ajax_request();22 record_log = $( '#oir-keep-the-log' ).is( ':checked' ); 20 23 21 });24 ajax_request(); 22 25 23 $body.on( 'click', '.js-oir-show-log', function( e ) { 26 }); 24 27 25 e.preventDefault(); 28 $button_old_cleanup.on( 'click', function( e ) { 26 29 27 $log.stop().slideToggle();30 e.preventDefault(); 28 31 29 }); 32 $buttons.hide(); 33 $message 34 .html( oir_plugin.l10n.cleanup_progress ) 35 .show(); 30 36 31 function ajax_request( paged, removed, removed_log ) { 37 record_log = $( '#oir-keep-the-log' ).is( ':checked' ); 32 38 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 ); 36 40 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 ); 47 42 48 if ( true !== response.success ) { 43 }); 49 44 50 // Looks like something went wrong 45 $body.on( 'click', '.js-oir-show-log', function( e ) { 51 46 52 $message 53 .html( oir_plugin.l10n.something_wrong ) 54 .show(); 47 e.preventDefault(); 55 48 56 return;49 $log.stop().slideToggle(); 57 50 58 } 51 }); 59 52 60 if ( true === response.finished ) {53 function ajax_request( paged, removed ) { 61 54 62 // Cleanup has finished 55 paged = 'undefined' == typeof paged ? 1 : parseInt( paged, 10 ); 56 removed = 'undefined' == typeof removed ? 0 : parseInt( removed, 10 ); 63 57 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 ) { 65 68 66 $message 67 .html( message ); 69 if ( true !== response.success ) { 68 70 69 if ( 0 !== parseInt( response.removed, 10 ) && response.removed_log.length ) { 71 // Looks like something went wrong 70 72 71 var logHtml = '<pre>'; 73 $message 74 .html( oir_plugin.l10n.something_wrong ) 75 .show(); 72 76 73 $.each( response.removed_log, function( i, file ) { 77 return; 74 78 75 logHtml += file + '\n'; 79 } 76 80 77 }); 81 if ( true === response.finished ) { 78 82 79 logHtml += '</pre>'; 83 // Cleanup has finished 80 84 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>' ); 82 86 83 } 87 $message 88 .html( message ); 84 89 85 return; 90 if ( record_log && 0 !== parseInt( response.removed, 10 ) && response.removed_log.length ) { 86 91 87 } 92 var logHtml = '<pre>'; 88 93 89 // Cleanup still in progress 94 $.each( response.removed_log, function( i, file ) { 90 95 91 var completed = ( response.paged * 10 > response.found ) ? response.found : response.paged * 10;96 logHtml += file + '\n'; 92 97 93 $message 94 .html( oir_plugin.l10n.cleanup_progress + ' ' + completed + ' / ' + response.found ); 98 }); 95 99 96 ajax_request( ++response.paged, response.removed, response.removed_log );100 logHtml += '</pre>'; 97 101 98 }, 99 'json' 100 ); 102 $log.html( logHtml ) 101 103 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 } 103 124 104 125 }); -
optimize-images-resizing/trunk/optimize-images-resizing.php
r1410429 r1556512 9 9 * Text Domain: optimize-images-resizing 10 10 * Domain Path: /languages 11 * Version: 1. 3.011 * Version: 1.4.0 12 12 */ 13 13 … … 18 18 if ( ! class_exists( 'OIR_Init' ) ) : 19 19 20 final class OIR_Init {20 final class OIR_Init { 21 21 22 // Will hold the only instance of our main plugin class23 private static $instance;22 // Will hold the only instance of our main plugin class 23 private static $instance; 24 24 25 // Instantiate the class and set up stuff26 public static function instantiate() {25 // Instantiate the class and set up stuff 26 public static function instantiate() { 27 27 28 if ( ! isset( self::$instance ) && ! ( self::$instance instanceof OIR_Init ) ) {28 if ( ! isset( self::$instance ) && ! ( self::$instance instanceof OIR_Init ) ) { 29 29 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(); 33 33 34 // load textdomain 35 add_action( 'plugins_loaded', array( self::$instance, 'load_textdomain' ) ); 34 } 36 35 37 } 36 return self::$instance; 38 37 39 return self::$instance; 38 } 40 39 41 } 40 public function __construct() { 42 41 43 // Defines plugin constants 44 private function define_constants() { 42 // load textdomain 43 add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) ); 45 44 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' ) ); 49 46 50 // Plugin Folder Path 51 if ( ! defined( 'OIR_PLUGIN_DIR' ) ) 52 define( 'OIR_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); 47 } 53 48 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() { 57 51 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' ); 61 55 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__ ) ); 65 59 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/' ); 69 63 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__ ) ); 73 67 74 } 68 // Plugin JS Folder URL 69 if ( ! defined( 'OIR_JS_URL' ) ) 70 define( 'OIR_JS_URL', OIR_PLUGIN_URL . 'js/' ); 75 71 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/' ); 78 75 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__ ); 80 79 81 if ( is_admin() ) { 80 } 82 81 83 require_once OIR_PLUGIN_DIR_INC . 'class-remove-image-sizes.php'; 82 // Includes necessary files 83 private function include_files() { 84 84 85 } 85 require_once OIR_PLUGIN_DIR_INC . 'class-resize-image.php'; 86 86 87 } 87 if ( is_admin() ) { 88 88 89 // sets up textdomain 90 public static function load_textdomain() { 89 require_once OIR_PLUGIN_DIR_INC . 'class-remove-image-sizes.php'; 91 90 92 $lang_dir = dirname( plugin_basename( OIR_PLUGIN_FILE ) ) . '/languages/'; 91 } 93 92 94 $lang_dir = trailingslashit( apply_filters( 'oir_textdomain_location', $lang_dir ) ); 93 } 95 94 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 ) { 97 97 98 } 98 $links[] = '<a href="'. esc_url( get_admin_url( null, 'tools.php?page=optimize-images-resizing' ) ) .'">' . __( 'Remove image sizes', 'optimize-images-resizing' ) . '</a>'; 99 99 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 } 101 116 102 117 endif; -
optimize-images-resizing/trunk/readme.txt
r1410429 r1556512 3 3 Tags: images, media, resizing, optimize, cleanup, remove, empty, clean, resize, image 4 4 Requires at least: 3.8 5 Tested up to: 4. 55 Tested up to: 4.7 6 6 Stable tag: 1.3.0 7 7 License: GPLv3 … … 16 16 What 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. 17 17 18 Resizing is done only once andlater normally served by WordPress, so there is no performance hit.18 Resizing is done only once, images are later normally served by WordPress, so there is no performance hit. 19 19 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).20 Plugin 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). 21 21 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. 23 23 24 24 Other than that, you don't need to do anything, plugin works silently in the background. … … 35 35 1. Upload `optimize-images-resizing` folder to the `/wp-content/plugins/` directory 36 36 2. Activate the plugin through the 'Plugins' menu in WordPress 37 3. (optional) Visit Settings -> Mediato clean up your media folder37 3. (optional) Visit 'Tools -> Remove image sizes' to clean up your media folder 38 38 4. ??? 39 39 5. Profit … … 43 43 **I just installed the plugin. Is there anything else I need to do?** 44 44 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.45 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. 46 46 47 47 **How to clean up existing images?** 48 48 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 "Startcleanup" button and wait for the process to finish (there is a visual feedback for the duration of the cleanup).49 Images 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). 50 50 51 51 **Some image sizes are not cleaned up, which ones and why?** 52 52 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 thosemage 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.53 Plugin 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. 54 54 55 55 **How do I know which files the plugin cleaned up?** 56 56 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.57 A 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. 58 58 59 59 **Are there any drawbacks to using this plugin?** … … 67 67 68 68 == 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. 69 74 70 75 = 1.3.0 =
Note: See TracChangeset
for help on using the changeset viewer.