. **************************************************************************/ class RegenerateThumbnails { public $menu_id; // Plugin initialization public function __construct() { // Load up the localization file if we're using WordPress in a different language load_plugin_textdomain( 'regenerate-thumbnails' ); add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueues' ) ); add_action( 'wp_ajax_regeneratethumbnail', array( $this, 'ajax_process_image' ) ); add_filter( 'media_row_actions', array( $this, 'add_media_row_action' ), 10, 2 ); //add_filter( 'bulk_actions-upload', array( $this, 'add_bulk_actions' ), 99 ); // A last minute change to 3.1 makes this no longer work add_action( 'admin_head-upload.php', array( $this, 'add_bulk_actions_via_javascript' ) ); add_action( 'admin_action_bulk_regenerate_thumbnails', array( $this, 'bulk_action_handler' ) ); // Top drowndown add_action( 'admin_action_-1', array( $this, 'bulk_action_handler' ) ); // Bottom dropdown (assumes top dropdown = default value) // Allow people to change what capability is required to use this plugin $this->capability = apply_filters( 'regenerate_thumbs_cap', 'manage_options' ); } // Register the management page public function add_admin_menu() { $this->menu_id = add_management_page( __( 'Regenerate Thumbnails', 'regenerate-thumbnails' ), __( 'Regen. Thumbnails', 'regenerate-thumbnails' ), $this->capability, 'regenerate-thumbnails', array($this, 'regenerate_interface') ); } // Enqueue the needed Javascript and CSS public function admin_enqueues( $hook_suffix ) { if ( $hook_suffix != $this->menu_id ) return; // WordPress 3.1 vs older version compatibility if ( wp_script_is( 'jquery-ui-widget', 'registered' ) ) 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' ); else 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' ); wp_enqueue_style( 'jquery-ui-regenthumbs', plugins_url( 'jquery-ui/redmond/jquery-ui-1.7.2.custom.css', __FILE__ ), array(), '1.7.2' ); } // Add a "Regenerate Thumbnails" link to the media row actions public function add_media_row_action( $actions, $post ) { if ( 'image/' != substr( $post->post_mime_type, 0, 6 ) || ! current_user_can( $this->capability ) ) return $actions; $url = wp_nonce_url( admin_url( 'tools.php?page=regenerate-thumbnails&goback=1&ids=' . $post->ID ), 'regenerate-thumbnails' ); $actions['regenerate_thumbnails'] = '' . __( 'Regenerate Thumbnails', 'regenerate-thumbnails' ) . ''; return $actions; } // Add "Regenerate Thumbnails" to the Bulk Actions media dropdown public function add_bulk_actions( $actions ) { $delete = false; if ( ! empty( $actions['delete'] ) ) { $delete = $actions['delete']; unset( $actions['delete'] ); } $actions['bulk_regenerate_thumbnails'] = __( 'Regenerate Thumbnails', 'regenerate-thumbnails' ); if ( $delete ) $actions['delete'] = $delete; return $actions; } // Add new items to the Bulk Actions using Javascript // A last minute change to the "bulk_actions-xxxxx" filter in 3.1 made it not possible to add items using that public function add_bulk_actions_via_javascript() { if ( ! current_user_can( $this->capability ) ) return; ?>
' . sprintf( __( "Unable to find any images. Are you sure some exist?", 'regenerate-thumbnails' ), admin_url( 'upload.php?post_mime_type=image' ) ) . "
' . __( "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' ) . '
'; $count = count( $images ); $text_goback = ( ! empty( $_GET['goback'] ) ) ? sprintf( __( 'To go back to the previous page, click here.', 'regenerate-thumbnails' ), 'javascript:history.go(-1)' ) : ''; $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, click here. %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 ); $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 ); ?>
0' ); ?>
0' ); ?>
' . esc_html( $fullsizepath ) . '
' ) );
@set_time_limit( 900 ); // 5 minutes per image should be PLENTY
$metadata = wp_generate_attachment_metadata( $image->ID, $fullsizepath );
if ( is_wp_error( $metadata ) )
$this->die_json_error_msg( $image->ID, $metadata->get_error_message() );
if ( empty( $metadata ) )
$this->die_json_error_msg( $image->ID, __( 'Unknown failure reason.', 'regenerate-thumbnails' ) );
// If this fails, then it just means that nothing was changed (old value == new value)
wp_update_attachment_metadata( $image->ID, $metadata );
die( json_encode( array( 'success' => sprintf( __( '"%1$s" (ID %2$s) was successfully resized in %3$s seconds.', 'regenerate-thumbnails' ), esc_html( get_the_title( $image->ID ) ), $image->ID, timer_stop() ) ) ) );
}
// Helper to make a JSON error message
public function die_json_error_msg( $id, $message ) {
die( json_encode( array( 'error' => sprintf( __( '"%1$s" (ID %2$s) failed to resize. The error message was: %3$s', 'regenerate-thumbnails' ), esc_html( get_the_title( $id ) ), $id, $message ) ) ) );
}
// Helper function to escape quotes in strings for use in Javascript
public function esc_quotes( $string ) {
return str_replace( '"', '\"', $string );
}
}
// Start up this plugin
add_action( 'init', 'RegenerateThumbnails' );
function RegenerateThumbnails() {
global $RegenerateThumbnails;
$RegenerateThumbnails = new RegenerateThumbnails();
}
?>