Regenerate Thumbnails - Version 2.1.0

Version Description

Download this release

Release Info

Developer Viper007Bond
Plugin Icon 128x128 Regenerate Thumbnails
Version 2.1.0
Comparing to
See all releases

Code changes from version 2.0.0 to 2.1.0

jquery-ui/{ui.progressbar.js → jquery.ui.progressbar.min.1.7.2.js} RENAMED
File without changes
jquery-ui/jquery.ui.progressbar.min.js ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * jQuery UI Progressbar 1.8.6
3
+ *
4
+ * Copyright 2010, AUTHORS.txt (http://jqueryui.com/about)
5
+ * Dual licensed under the MIT or GPL Version 2 licenses.
6
+ * http://jquery.org/license
7
+ *
8
+ * http://docs.jquery.com/UI/Progressbar
9
+ *
10
+ * Depends:
11
+ * jquery.ui.core.js
12
+ * jquery.ui.widget.js
13
+ */
14
+ (function(b,c){b.widget("ui.progressbar",{options:{value:0},min:0,max:100,_create:function(){this.element.addClass("ui-progressbar ui-widget ui-widget-content ui-corner-all").attr({role:"progressbar","aria-valuemin":this.min,"aria-valuemax":this.max,"aria-valuenow":this._value()});this.valueDiv=b("<div class='ui-progressbar-value ui-widget-header ui-corner-left'></div>").appendTo(this.element);this._refreshValue()},destroy:function(){this.element.removeClass("ui-progressbar ui-widget ui-widget-content ui-corner-all").removeAttr("role").removeAttr("aria-valuemin").removeAttr("aria-valuemax").removeAttr("aria-valuenow");
15
+ this.valueDiv.remove();b.Widget.prototype.destroy.apply(this,arguments)},value:function(a){if(a===c)return this._value();this._setOption("value",a);return this},_setOption:function(a,d){if(a==="value"){this.options.value=d;this._refreshValue();this._trigger("change");this._value()===this.max&&this._trigger("complete")}b.Widget.prototype._setOption.apply(this,arguments)},_value:function(){var a=this.options.value;if(typeof a!=="number")a=0;return Math.min(this.max,Math.max(this.min,a))},_refreshValue:function(){var a=
16
+ this.value();this.valueDiv.toggleClass("ui-corner-right",a===this.max).width(a+"%");this.element.attr("aria-valuenow",a)}});b.extend(b.ui.progressbar,{version:"1.8.6"})})(jQuery);
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: Viper007Bond
3
  Donate link: http://www.viper007bond.com/donate/
4
  Tags: thumbnail, thumbnails
5
- Requires at least: 2.6
6
- Tested up to: 2.9
7
  Stable tag: trunk
8
 
9
  Allows you to regenerate all thumbnails after changing the thumbnail sizes.
@@ -30,16 +30,44 @@ Visit Tools -> Regen. Thumbnails to get started.
30
 
31
  **See Also:** ["Installing Plugins" article on the WP Codex](http://codex.wordpress.org/Managing_Plugins#Installing_Plugins)
32
 
 
 
 
 
33
  == ChangeLog ==
34
 
35
- **Version 2.0.0**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  * Recoded from scratch. Now uses an AJAX request per attachment to do the resizing. No more PHP maximum execution time errors or anything like that. Also features a pretty progress bar to let the user know how it's going.
38
 
39
- **Version 1.1.0**
40
 
41
  * WordPress 2.7 updates -- code + UI. Thanks to jdub and Patrick F.
42
 
43
- **Version 1.0.0**
44
 
45
  * Initial release.
2
  Contributors: Viper007Bond
3
  Donate link: http://www.viper007bond.com/donate/
4
  Tags: thumbnail, thumbnails
5
+ Requires at least: 2.8
6
+ Tested up to: 3.0
7
  Stable tag: trunk
8
 
9
  Allows you to regenerate all thumbnails after changing the thumbnail sizes.
30
 
31
  **See Also:** ["Installing Plugins" article on the WP Codex](http://codex.wordpress.org/Managing_Plugins#Installing_Plugins)
32
 
33
+ == Screenshots ==
34
+
35
+ 1. The plugin at work.
36
+
37
  == ChangeLog ==
38
 
39
+ = Version 2.1.0 =
40
+
41
+ Lots of new features!
42
+
43
+ * Thanks to a lot of jQuery help from [Boris Schapira](http://borisschapira.com/), a failed image regeneration will no longer stop the whole process.
44
+ * The results of each image regeneration is now outputted. You can easily see which images were successfully regenerated and which failed. Was inspired by a concept by Boris.
45
+ * There is now a button on the regeneration page that will allow you to abort resizing images for any reason. Based on code by Boris.
46
+ * You can now regenerate single images from the Media page. The link to do so will show up in the actions list when you hover over the row.
47
+ * You can now bulk regenerate multiple from the Media page. Check the boxes and then select "Regenerate Thumbnails" form the "Bulk Actions" dropdown. WordPress 3.1+ only.
48
+ * The total time that the regeneration process took is now displayed in the final status message.
49
+ * jQuery UI Progressbar version upgraded.
50
+
51
+ = Version 2.0.3 =
52
+
53
+ * Switch out deprecated function call.
54
+
55
+ = Version 2.0.2 =
56
+
57
+ * Directly query the database to only fetch what the plugin needs (the attachment ID). This will reduce the memory required as it's not storing the whole row for each attachment.
58
+
59
+ = Version 2.0.1 =
60
+
61
+ * I accidentally left a `check_admin_referer()` (nonce check) commented out.
62
+
63
+ = Version 2.0.0 =
64
 
65
  * Recoded from scratch. Now uses an AJAX request per attachment to do the resizing. No more PHP maximum execution time errors or anything like that. Also features a pretty progress bar to let the user know how it's going.
66
 
67
+ = Version 1.1.0 =
68
 
69
  * WordPress 2.7 updates -- code + UI. Thanks to jdub and Patrick F.
70
 
71
+ = Version 1.0.0 =
72
 
73
  * Initial release.
regenerate-thumbnails.php CHANGED
@@ -5,13 +5,13 @@
5
  Plugin Name: Regenerate Thumbnails
6
  Plugin URI: http://www.viper007bond.com/wordpress-plugins/regenerate-thumbnails/
7
  Description: Allows you to regenerate all thumbnails after changing the thumbnail sizes.
8
- Version: 2.0.0
9
  Author: Viper007Bond
10
  Author URI: http://www.viper007bond.com/
11
 
12
  **************************************************************************
13
 
14
- Copyright (C) 2008 Viper007Bond
15
 
16
  This program is free software: you can redistribute it and/or modify
17
  it under the terms of the GNU General Public License as published by
@@ -29,40 +29,96 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
29
  **************************************************************************/
30
 
31
  class RegenerateThumbnails {
 
32
 
33
  // Plugin initialization
34
  function RegenerateThumbnails() {
35
- if ( !function_exists('admin_url') )
36
  return false;
37
 
38
  // Load up the localization file if we're using WordPress in a different language
39
  // Place it in this plugin's "localization" folder and name it "regenerate-thumbnails-[value in wp-config].mo"
40
  load_plugin_textdomain( 'regenerate-thumbnails', false, '/regenerate-thumbnails/localization' );
41
 
42
- add_action( 'admin_menu', array(&$this, 'add_admin_menu') );
43
- add_action( 'admin_enqueue_scripts', array(&$this, 'admin_enqueues') );
44
- add_action( 'wp_ajax_regeneratethumbnail', array(&$this, 'ajax_process_image') );
 
 
 
45
  }
46
 
47
 
48
  // Register the management page
49
  function add_admin_menu() {
50
- add_management_page( __( 'Regenerate Thumbnails', 'regenerate-thumbnails' ), __( 'Regen. Thumbnails', 'regenerate-thumbnails' ), 'manage_options', 'regenerate-thumbnails', array(&$this, 'regenerate_interface') );
51
  }
52
 
53
 
54
  // Enqueue the needed Javascript and CSS
55
  function admin_enqueues( $hook_suffix ) {
56
- if ( 'tools_page_regenerate-thumbnails' != $hook_suffix )
57
  return;
58
 
59
- wp_enqueue_script( 'jquery-ui-progressbar', plugins_url( 'jquery-ui/ui.progressbar.js', __FILE__ ), array('jquery-ui-core'), '1.7.2' );
 
 
 
 
 
60
  wp_enqueue_style( 'jquery-ui-regenthumbs', plugins_url( 'jquery-ui/redmond/jquery-ui-1.7.2.custom.css', __FILE__ ), array(), '1.7.2' );
61
  }
62
 
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  // The user interface plus thumbnail regenerator
65
- function regenerate_interface() { ?>
 
 
 
66
 
67
  <div id="message" class="updated fade" style="display:none"></div>
68
 
@@ -72,49 +128,64 @@ class RegenerateThumbnails {
72
  <?php
73
 
74
  // If the button was clicked
75
- if ( !empty($_POST['regenerate-thumbnails']) ) {
76
  // Capability check
77
- if ( !current_user_can('manage_options') )
78
- wp_die( __('Cheatin&#8217; uh?') );
79
 
80
  // Form nonce check
81
- //check_admin_referer( 'regenerate-thumbnails' );
82
-
83
- // Get all image attachments
84
- $images =& get_children( array(
85
- 'post_type' => 'attachment',
86
- 'post_mime_type' => 'image',
87
- 'numberposts' => -1,
88
- 'post_status' => 'inherit',
89
- 'post_parent' => null, // any parent
90
- 'output' => 'object',
91
- ) );
92
-
93
- // Make sure there are images to process
94
- if ( !$images ) {
95
- echo ' <p>' . sprintf( __( "Unable to find any images. Are you sure <a href='%s'>some exist</a>?", 'regenerate-thumbnails' ), admin_url('upload.php?post_mime_type=image') ) . "</p>\n\n";
96
- }
97
-
98
- // Valid results
99
- else {
100
- echo ' <p>' . __( "Please be patient while all thumbnails are regenerated. This can take a while if your server is slow (cheap hosting) or if you have many images. Do not navigate away from this page until this script is done or all thumbnails won't be resized. You will be notified via this page when all regenerating is completed.", 'regenerate-thumbnails' ) . '</p>';
101
 
102
  // Generate the list of IDs
103
  $ids = array();
104
  foreach ( $images as $image )
105
  $ids[] = $image->ID;
106
  $ids = implode( ',', $ids );
 
107
 
108
- $count = count( $images );
 
 
 
 
 
 
109
  ?>
110
 
111
 
112
  <noscript><p><em><?php _e( 'You must enable Javascript in order to proceed!', 'regenerate-thumbnails' ) ?></em></p></noscript>
113
 
114
- <div id="regenthumbsbar" style="position:relative;height:25px;">
115
- <div id="regenthumbsbar-percent" style="position:absolute;left:50%;top:50%;width:50px;margin-left:-25px;height:25px;margin-top:-9px;font-weight:bold;text-align:center;"></div>
116
  </div>
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  <script type="text/javascript">
119
  // <![CDATA[
120
  jQuery(document).ready(function($){
@@ -123,24 +194,95 @@ class RegenerateThumbnails {
123
  var rt_total = rt_images.length;
124
  var rt_count = 1;
125
  var rt_percent = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
- $("#regenthumbsbar").progressbar();
128
- $("#regenthumbsbar-percent").html( "0%" );
 
129
 
 
130
  function RegenThumbs( id ) {
131
- $.post( "admin-ajax.php", { action: "regeneratethumbnail", id: id }, function() {
132
- rt_percent = ( rt_count / rt_total ) * 100;
133
- $("#regenthumbsbar").progressbar( "value", rt_percent );
134
- $("#regenthumbsbar-percent").html( Math.round(rt_percent) + "%" );
135
- rt_count = rt_count + 1;
136
-
137
- if ( rt_images.length ) {
138
- RegenThumbs( rt_images.shift() );
139
- } else {
140
- $("#message").html("<p><strong><?php echo js_escape( sprintf( __( 'All done! Processed %d images.', 'regenerate-thumbnails' ), $count ) ); ?></strong></p>");
141
- $("#message").show();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  }
143
-
144
  });
145
  }
146
 
@@ -149,21 +291,19 @@ class RegenerateThumbnails {
149
  // ]]>
150
  </script>
151
  <?php
152
- }
153
  }
154
 
155
  // No button click? Display the form.
156
  else {
157
  ?>
158
- <p><?php printf( __( "Use this tool to regenerate thumbnails for all images that you have uploaded to your blog. This is useful if you've changed any of the thumbnail dimensions on the <a href='%s'>media settings page</a>. Old thumbnails will be kept to avoid any broken images due to hard-coded URLs.", 'regenerate-thumbnails'), admin_url('options-media.php') ); ?></p>
159
-
160
- <p><?php _e( "This process is not reversible, although you can just change your thumbnail dimensions back to the old values and click the button again if you don't like the results.", 'regenerate-thumbnails'); ?></p>
161
-
162
- <p><?php _e( "To begin, just press the button below.", 'regenerate-thumbnails'); ?></p>
163
-
164
  <form method="post" action="">
165
  <?php wp_nonce_field('regenerate-thumbnails') ?>
166
 
 
 
 
 
 
167
 
168
  <p><input type="submit" class="button hide-if-no-js" name="regenerate-thumbnails" id="regenerate-thumbnails" value="<?php _e( 'Regenerate All Thumbnails', 'regenerate-thumbnails' ) ?>" /></p>
169
 
@@ -181,25 +321,44 @@ class RegenerateThumbnails {
181
 
182
  // Process a single image ID (this is an AJAX handler)
183
  function ajax_process_image() {
184
- if ( !current_user_can( 'manage_options' ) )
185
- die('-1');
186
 
187
  $id = (int) $_REQUEST['id'];
 
188
 
189
- if ( empty($id) )
190
- die('-1');
191
 
192
- $fullsizepath = get_attached_file( $id );
 
193
 
194
- if ( false === $fullsizepath || !file_exists($fullsizepath) )
195
- die('-1');
196
 
197
- set_time_limit( 60 );
 
198
 
199
- if ( wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $fullsizepath ) ) )
200
- die('1');
201
- else
202
- die('-1');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  }
204
  }
205
 
5
  Plugin Name: Regenerate Thumbnails
6
  Plugin URI: http://www.viper007bond.com/wordpress-plugins/regenerate-thumbnails/
7
  Description: Allows you to regenerate all thumbnails after changing the thumbnail sizes.
8
+ Version: 2.1.0
9
  Author: Viper007Bond
10
  Author URI: http://www.viper007bond.com/
11
 
12
  **************************************************************************
13
 
14
+ Copyright (C) 2008-2010 Viper007Bond
15
 
16
  This program is free software: you can redistribute it and/or modify
17
  it under the terms of the GNU General Public License as published by
29
  **************************************************************************/
30
 
31
  class RegenerateThumbnails {
32
+ var $menu_id;
33
 
34
  // Plugin initialization
35
  function RegenerateThumbnails() {
36
+ if ( ! function_exists( 'admin_url' ) )
37
  return false;
38
 
39
  // Load up the localization file if we're using WordPress in a different language
40
  // Place it in this plugin's "localization" folder and name it "regenerate-thumbnails-[value in wp-config].mo"
41
  load_plugin_textdomain( 'regenerate-thumbnails', false, '/regenerate-thumbnails/localization' );
42
 
43
+ add_action( 'admin_menu', array( &$this, 'add_admin_menu' ) );
44
+ add_action( 'admin_enqueue_scripts', array( &$this, 'admin_enqueues' ) );
45
+ add_action( 'wp_ajax_regeneratethumbnail', array( &$this, 'ajax_process_image' ) );
46
+ add_filter( 'media_row_actions', array( &$this, 'add_media_row_action' ), 10, 2 );
47
+ add_filter( 'bulk_actions-upload', array( &$this, 'add_bulk_actions' ), 99 );
48
+ add_action( 'admin_action_bulk_regenerate_thumbnails', array( &$this, 'bulk_action_handler' ) );
49
  }
50
 
51
 
52
  // Register the management page
53
  function add_admin_menu() {
54
+ $this->menu_id = add_management_page( __( 'Regenerate Thumbnails', 'regenerate-thumbnails' ), __( 'Regen. Thumbnails', 'regenerate-thumbnails' ), 'manage_options', 'regenerate-thumbnails', array(&$this, 'regenerate_interface') );
55
  }
56
 
57
 
58
  // Enqueue the needed Javascript and CSS
59
  function admin_enqueues( $hook_suffix ) {
60
+ if ( $hook_suffix != $this->menu_id )
61
  return;
62
 
63
+ // WordPress 3.1 vs older version compatibility
64
+ if ( wp_script_is( 'jquery-ui-widget', 'registered' ) )
65
+ wp_enqueue_script( 'jquery-ui-progressbar', plugins_url( 'jquery-ui/jquery.ui.progressbar.min.js', __FILE__ ), array( 'jquery-ui-core', 'jquery-ui-widget' ), '1.8.6' );
66
+ else
67
+ wp_enqueue_script( 'jquery-ui-progressbar', plugins_url( 'jquery-ui/jquery.ui.progressbar.min.1.7.2.js', __FILE__ ), array( 'jquery-ui-core' ), '1.7.2' );
68
+
69
  wp_enqueue_style( 'jquery-ui-regenthumbs', plugins_url( 'jquery-ui/redmond/jquery-ui-1.7.2.custom.css', __FILE__ ), array(), '1.7.2' );
70
  }
71
 
72
 
73
+ // Add a "Regenerate Thumbnails" link to the media row actions
74
+ function add_media_row_action( $actions, $post ) {
75
+ if ( 'image/' != substr( $post->post_mime_type, 0, 6 ) )
76
+ return $actions;
77
+
78
+ $url = wp_nonce_url( admin_url( 'tools.php?page=regenerate-thumbnails&goback=1&ids=' . $post->ID ), 'regenerate-thumbnails' );
79
+ $actions['regenerate_thumbnails'] = '<a href="' . esc_url( $url ) . '" title="' . esc_attr( __( "Regenerate the thumbnails for this single image", 'regenerate-thumbnails' ) ) . '">' . __( 'Regenerate Thumbnails', 'regenerate-thumbnails' ) . '</a>';
80
+
81
+ return $actions;
82
+ }
83
+
84
+
85
+ // Add "Regenerate Thumbnails" to the Bulk Actions media dropdown
86
+ function add_bulk_actions( $actions ) {
87
+ $delete = false;
88
+ if ( ! empty( $actions['delete'] ) ) {
89
+ $delete = $actions['delete'];
90
+ unset( $actions['delete'] );
91
+ }
92
+
93
+ $actions['bulk_regenerate_thumbnails'] = __( 'Regenerate Thumbnails', 'regenerate-thumbnails' );
94
+
95
+ if ( $delete )
96
+ $actions['delete'] = $delete;
97
+
98
+ return $actions;
99
+ }
100
+
101
+
102
+ // Handles the bulk actions POST
103
+ function bulk_action_handler() {
104
+ check_admin_referer( 'bulk-media' );
105
+
106
+ if ( empty( $_POST['media'] ) && is_array( $_POST['media'] ) )
107
+ return;
108
+
109
+ $ids = implode( ',', array_map( 'intval', $_POST['media'] ) );
110
+
111
+ // Can't use wp_nonce_url() as it escapes HTML entities
112
+ wp_redirect( add_query_arg( '_wpnonce', wp_create_nonce( 'regenerate-thumbnails' ), admin_url( 'tools.php?page=regenerate-thumbnails&goback=1&ids=' . $ids ) ) );
113
+ exit();
114
+ }
115
+
116
+
117
  // The user interface plus thumbnail regenerator
118
+ function regenerate_interface() {
119
+ global $wpdb;
120
+
121
+ ?>
122
 
123
  <div id="message" class="updated fade" style="display:none"></div>
124
 
128
  <?php
129
 
130
  // If the button was clicked
131
+ if ( ! empty( $_POST['regenerate-thumbnails'] ) || ! empty( $_REQUEST['ids'] ) ) {
132
  // Capability check
133
+ if ( !current_user_can( 'manage_options' ) )
134
+ wp_die( __( 'Cheatin&#8217; uh?' ) );
135
 
136
  // Form nonce check
137
+ check_admin_referer( 'regenerate-thumbnails' );
138
+
139
+ // Create the list of image IDs
140
+ if ( ! empty( $_REQUEST['ids'] ) ) {
141
+ $images = array_map( 'intval', explode( ',', trim( $_REQUEST['ids'], ',' ) ) );
142
+ $ids = implode( ',', $images );
143
+ } else {
144
+ // Directly querying the database is normally frowned upon, but all
145
+ // of the API functions will return the full post objects which will
146
+ // suck up lots of memory. This is best, just not as future proof.
147
+ if ( ! $images = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' ORDER BY ID" ) ) {
148
+ echo ' <p>' . sprintf( __( "Unable to find any images. Are you sure <a href='%s'>some exist</a>?", 'regenerate-thumbnails' ), admin_url( 'upload.php?post_mime_type=image' ) ) . "</p></div>";
149
+ return;
150
+ }
 
 
 
 
 
 
151
 
152
  // Generate the list of IDs
153
  $ids = array();
154
  foreach ( $images as $image )
155
  $ids[] = $image->ID;
156
  $ids = implode( ',', $ids );
157
+ }
158
 
159
+ echo ' <p>' . __( "Please be patient while the thumbnails are regenerated. This can take a while if your server is slow (inexpensive hosting) or if you have many images. Do not navigate away from this page until this script is done or the thumbnails will not be resized. You will be notified via this page when the regenerating is completed.", 'regenerate-thumbnails' ) . '</p>';
160
+
161
+ $count = count( $images );
162
+
163
+ $text_goback = ( ! empty( $_GET['goback'] ) ) ? sprintf( __( 'To go back to the previous page, <a href="%s">click here</a>.', 'regenerate-thumbnails' ), 'javascript:history.go(-1)' ) : '';
164
+ $text_failures = sprintf( __( 'All done! %1$s image(s) were successfully resized in %2$s seconds and there were %3$s failure(s). To try regenerating the failed images again, <a href="%4$s">click here</a>. %5$s', 'regenerate-thumbnails' ), "' + rt_successes + '", "' + rt_totaltime + '", "' + rt_errors + '", esc_url( wp_nonce_url( admin_url( 'tools.php?page=regenerate-thumbnails&goback=1' ), 'regenerate-thumbnails' ) . '&ids=' ) . "' + rt_failedlist + '", $text_goback );
165
+ $text_nofailures = sprintf( __( 'All done! %1$s image(s) were successfully resized in %2$s seconds and there were 0 failures. %3$s', 'regenerate-thumbnails' ), "' + rt_successes + '", "' + rt_totaltime + '", $text_goback );
166
  ?>
167
 
168
 
169
  <noscript><p><em><?php _e( 'You must enable Javascript in order to proceed!', 'regenerate-thumbnails' ) ?></em></p></noscript>
170
 
171
+ <div id="regenthumbs-bar" style="position:relative;height:25px;">
172
+ <div id="regenthumbs-bar-percent" style="position:absolute;left:50%;top:50%;width:300px;margin-left:-150px;height:25px;margin-top:-9px;font-weight:bold;text-align:center;"></div>
173
  </div>
174
 
175
+ <p><input type="button" class="button hide-if-no-js" name="regenthumbs-stop" id="regenthumbs-stop" value="<?php _e( 'Abort Resizing Images', 'regenerate-thumbnails' ) ?>" /></p>
176
+
177
+ <h3 class="title"><?php _e( 'Debugging Information', 'regenerate-thumbnails' ) ?></h3>
178
+
179
+ <p>
180
+ <?php printf( __( 'Total Images: %s', 'regenerate-thumbnails' ), $count ); ?><br />
181
+ <?php printf( __( 'Images Resized: %s', 'regenerate-thumbnails' ), '<span id="regenthumbs-debug-successcount">0</span>' ); ?><br />
182
+ <?php printf( __( 'Resize Failures: %s', 'regenerate-thumbnails' ), '<span id="regenthumbs-debug-failurecount">0</span>' ); ?>
183
+ </p>
184
+
185
+ <ol id="regenthumbs-debuglist">
186
+ <li style="display:none"></li>
187
+ </ol>
188
+
189
  <script type="text/javascript">
190
  // <![CDATA[
191
  jQuery(document).ready(function($){
194
  var rt_total = rt_images.length;
195
  var rt_count = 1;
196
  var rt_percent = 0;
197
+ var rt_successes = 0;
198
+ var rt_errors = 0;
199
+ var rt_failedlist = '';
200
+ var rt_resulttext = '';
201
+ var rt_timestart = new Date().getTime();
202
+ var rt_timeend = 0;
203
+ var rt_totaltime = 0;
204
+ var rt_continue = true;
205
+
206
+ // Create the progress bar
207
+ $("#regenthumbs-bar").progressbar();
208
+ $("#regenthumbs-bar-percent").html( "0%" );
209
+
210
+ // Stop button
211
+ $("#regenthumbs-stop").click(function() {
212
+ rt_continue = false;
213
+ $('#regenthumbs-stop').val("<?php echo $this->esc_quotes( __( 'Stopping...', 'regenerate-thumbnails' ) ); ?>");
214
+ });
215
+
216
+ // Clear out the empty list element that's there for HTML validation purposes
217
+ $("#regenthumbs-debuglist li").remove();
218
+
219
+ // Called after each resize. Updates debug information and the progress bar.
220
+ function RegenThumbsUpdateStatus( id, success, response ) {
221
+ $("#regenthumbs-bar").progressbar( "value", ( rt_count / rt_total ) * 100 );
222
+ $("#regenthumbs-bar-percent").html( Math.round( ( rt_count / rt_total ) * 1000 ) / 10 + "%" );
223
+ rt_count = rt_count + 1;
224
+
225
+ if ( success ) {
226
+ rt_successes = rt_successes + 1;
227
+ $("#regenthumbs-debug-successcount").html(rt_successes);
228
+ $("#regenthumbs-debuglist").append("<li>" + response.success + "</li>");
229
+ }
230
+ else {
231
+ rt_errors = rt_errors + 1;
232
+ rt_failedlist = rt_failedlist + ',' + id;
233
+ $("#regenthumbs-debug-failurecount").html(rt_errors);
234
+ $("#regenthumbs-debuglist").append("<li>" + response.error + "</li>");
235
+ }
236
+ }
237
+
238
+ // Called when all images have been processed. Shows the results and cleans up.
239
+ function RegenThumbsFinishUp() {
240
+ rt_timeend = new Date().getTime();
241
+ rt_totaltime = Math.round( ( rt_timeend - rt_timestart ) / 1000 );
242
+
243
+ $('#regenthumbs-stop').hide();
244
+
245
+ if ( rt_errors > 0 ) {
246
+ rt_resulttext = '<?php echo $text_failures; ?>';
247
+ } else {
248
+ rt_resulttext = '<?php echo $text_nofailures; ?>';
249
+ }
250
 
251
+ $("#message").html("<p><strong>" + rt_resulttext + "</strong></p>");
252
+ $("#message").show();
253
+ }
254
 
255
+ // Regenerate a specified image via AJAX
256
  function RegenThumbs( id ) {
257
+ $.ajax({
258
+ type: 'POST',
259
+ url: ajaxurl,
260
+ data: { action: "regeneratethumbnail", id: id },
261
+ success: function( response ) {
262
+ if ( response.success ) {
263
+ RegenThumbsUpdateStatus( id, true, response );
264
+ }
265
+ else {
266
+ RegenThumbsUpdateStatus( id, false, response );
267
+ }
268
+
269
+ if ( rt_images.length && rt_continue ) {
270
+ RegenThumbs( rt_images.shift() );
271
+ }
272
+ else {
273
+ RegenThumbsFinishUp();
274
+ }
275
+ },
276
+ error: function() {
277
+ RegenThumbsUpdateStatus( id, false, response );
278
+
279
+ if ( rt_images.length && rt_continue ) {
280
+ RegenThumbs( rt_images.shift() );
281
+ }
282
+ else {
283
+ RegenThumbsFinishUp();
284
+ }
285
  }
 
286
  });
287
  }
288
 
291
  // ]]>
292
  </script>
293
  <?php
 
294
  }
295
 
296
  // No button click? Display the form.
297
  else {
298
  ?>
 
 
 
 
 
 
299
  <form method="post" action="">
300
  <?php wp_nonce_field('regenerate-thumbnails') ?>
301
 
302
+ <p><?php printf( __( "Use this tool to regenerate thumbnails for all images that you have uploaded to your blog. This is useful if you've changed any of the thumbnail dimensions on the <a href='%s'>media settings page</a>. Old thumbnails will be kept to avoid any broken images due to hard-coded URLs.", 'regenerate-thumbnails' ), admin_url('options-media.php') ); ?></p>
303
+
304
+ <p><?php _e( "This process is not reversible, although you can just change your thumbnail dimensions back to the old values and click the button again if you don't like the results.", 'regenerate-thumbnails' ); ?></p>
305
+
306
+ <p><?php _e( 'To begin, just press the button below.', 'regenerate-thumbnails '); ?></p>
307
 
308
  <p><input type="submit" class="button hide-if-no-js" name="regenerate-thumbnails" id="regenerate-thumbnails" value="<?php _e( 'Regenerate All Thumbnails', 'regenerate-thumbnails' ) ?>" /></p>
309
 
321
 
322
  // Process a single image ID (this is an AJAX handler)
323
  function ajax_process_image() {
324
+ header( 'Content-type: application/json' );
 
325
 
326
  $id = (int) $_REQUEST['id'];
327
+ $image = get_post( $id );
328
 
329
+ if ( ! $image || 'attachment' != $image->post_type || 'image/' != substr( $image->post_mime_type, 0, 6 ) )
330
+ die( json_encode( array( 'error' => sprintf( __( 'Failed resize: %s is an invalid image ID.', 'regenerate-thumbnails' ), esc_html( $_REQUEST['id'] ) ) ) ) );
331
 
332
+ if ( !current_user_can( 'manage_options' ) )
333
+ $this->die_json_error_msg( $image->ID, __( "Your user account doesn't have permission to resize images", 'regenerate-thumbnails' ) );
334
 
335
+ $fullsizepath = get_attached_file( $image->ID );
 
336
 
337
+ if ( false === $fullsizepath || ! file_exists( $fullsizepath ) )
338
+ $this->die_json_error_msg( $image->ID, sprintf( __( 'The originally uploaded image file cannot be found at %s', 'regenerate-thumbnails' ), '<code>' . esc_html( $fullsizepath ) . '</code>' ) );
339
 
340
+ set_time_limit( 900 );
341
+
342
+ $metadata = wp_generate_attachment_metadata( $image->ID, $fullsizepath );
343
+ if ( empty( $metadata ) )
344
+ $this->die_json_error_msg( $image->ID, __( 'Unknown failure reason.', 'regenerate-thumbnails' ) );
345
+
346
+ // If this fails, then it just means that nothing was changed (old value == new value)
347
+ wp_update_attachment_metadata( $image->ID, $metadata );
348
+
349
+ die( json_encode( array( 'success' => sprintf( __( '&quot;%1$s&quot; (ID %2$s) was successfully resized in %3$s seconds.', 'regenerate-thumbnails' ), esc_html( get_the_title( $image->ID ) ), $image->ID, timer_stop() ) ) ) );
350
+ }
351
+
352
+
353
+ // Helper to make a JSON error message
354
+ function die_json_error_msg( $id, $message ) {
355
+ die( json_encode( array( 'error' => sprintf( __( '&quot;%1$s&quot; (ID %2$s) failed to resize. The error message was: %3$s', 'regenerate-thumbnails' ), esc_html( get_the_title( $id ) ), $id, $message ) ) ) );
356
+ }
357
+
358
+
359
+ // Helper function to escape quotes in strings for use in Javascript
360
+ function esc_quotes( $string ) {
361
+ return str_replace( '"', '\"', $string );
362
  }
363
  }
364
 
screenshot-1.png CHANGED
Binary file