';
echo '
' . esc_html_x( 'Regenerate Thumbnails', 'admin page title', 'regenerate-thumbnails' ) . '
';
if ( version_compare( $wp_version, '4.7', '<' ) ) {
echo '
' . sprintf(
__( 'This plugin requires WordPress 4.7 or newer. You are on version %1$s. Please upgrade.', 'regenerate-thumbnails' ),
esc_html( $wp_version ),
esc_url( admin_url( 'update-core.php' ) )
) . '
';
} else {
?>
';
}
/**
* If the image editor doesn't support image resizing (thumbnailing), then add an admin notice
* warning the user of this.
*/
public function add_admin_notice_if_resizing_not_supported() {
if ( ! wp_image_editor_supports( array( 'methods' => array( 'resize' ) ) ) ) {
add_action( 'admin_notices', array( $this, 'admin_notices_resizing_not_supported' ) );
}
}
/**
* Outputs an admin notice stating that image resizing (thumbnailing) is not supported.
*/
public function admin_notices_resizing_not_supported() {
?>
ID, '_wp_attachment_context', true ) ) {
return false;
}
if ( wp_attachment_is_image( $post ) ) {
return true;
}
if ( function_exists( 'wp_get_original_image_path' ) ) {
$fullsize = wp_get_original_image_path( $post->ID );
} else {
$fullsize = get_attached_file( $post->ID );
}
if ( ! $fullsize || ! file_exists( $fullsize ) ) {
return false;
}
$image_editor_args = array(
'path' => $fullsize,
'methods' => array( 'resize' )
);
$file_info = wp_check_filetype( $image_editor_args['path'] );
// If $file_info['type'] is false, then we let the editor attempt to
// figure out the file type, rather than forcing a failure based on extension.
if ( isset( $file_info ) && $file_info['type'] ) {
$image_editor_args['mime_type'] = $file_info['type'];
}
return (bool) _wp_image_editor_choose( $image_editor_args );
}
/**
* Adds "Regenerate Thumbnails" below each image in the media library list view.
*
* @param array $actions An array of current actions.
* @param WP_Post $post The current attachment's post object.
*
* @return array The new list of actions.
*/
public function add_regenerate_link_to_media_list_view( $actions, $post ) {
if ( ! current_user_can( $this->capability ) || ! $this->is_regeneratable( $post ) ) {
return $actions;
}
$actions['regenerate_thumbnails'] = '
' . _x( 'Regenerate Thumbnails', 'action for a single image', 'regenerate-thumbnails' ) . '';
return $actions;
}
/**
* Add a "Regenerate Thumbnails" button to the submit box on the non-modal "Edit Media" screen for an image attachment.
*/
public function add_button_to_media_edit_page() {
global $post;
if ( ! current_user_can( $this->capability ) || ! $this->is_regeneratable( $post ) ) {
return;
}
echo '
';
}
/**
* Adds a "Regenerate Thumbnails" button to the edit media modal view.
*
* Ideally it would be down with the actions but I'm not good enough at JavaScript
* in order to be able to do it, so instead I'm adding it to the bottom of the list
* of media fields. Pull requests to improve this are welcome!
*
* @param array $form_fields An array of existing form fields.
* @param WP_Post $post The current media item, as a post object.
*
* @return array The new array of form fields.
*/
public function add_button_to_edit_media_modal_fields_area( $form_fields, $post ) {
if ( ! current_user_can( $this->capability ) || ! $this->is_regeneratable( $post ) ) {
return $form_fields;
}
$form_fields['regenerate_thumbnails'] = array(
'label' => '',
'input' => 'html',
'html' => '
' . _x( 'Regenerate Thumbnails', 'action for a single image', 'regenerate-thumbnails' ) . '',
'show_in_modal' => true,
'show_in_edit' => false,
);
return $form_fields;
}
/**
* Add "Regenerate Thumbnails" to the bulk actions dropdown on the media list using Javascript.
*/
public function add_bulk_actions_via_javascript() {
if ( ! current_user_can( $this->capability ) ) {
return;
}
?>
'regenerate-thumbnails',
'ids' => rawurlencode( implode( ',', array_map( 'intval', $_REQUEST['media'] ) ) ),
),
admin_url( 'tools.php' )
)
);
exit();
}
/**
* Returns an array of all thumbnail sizes, including their label, size, and crop setting.
*
* @return array An array, with the thumbnail label as the key and an array of thumbnail properties (width, height, crop).
*/
public function get_thumbnail_sizes() {
global $_wp_additional_image_sizes;
$thumbnail_sizes = array();
foreach ( get_intermediate_image_sizes() as $size ) {
$thumbnail_sizes[ $size ]['label'] = $size;
if ( in_array( $size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) {
$thumbnail_sizes[ $size ]['width'] = (int) get_option( $size . '_size_w' );
$thumbnail_sizes[ $size ]['height'] = (int) get_option( $size . '_size_h' );
$thumbnail_sizes[ $size ]['crop'] = ( 'thumbnail' == $size ) ? (bool) get_option( 'thumbnail_crop' ) : false;
} elseif ( ! empty( $_wp_additional_image_sizes ) && ! empty( $_wp_additional_image_sizes[ $size ] ) ) {
$thumbnail_sizes[ $size ]['width'] = (int) $_wp_additional_image_sizes[ $size ]['width'];
$thumbnail_sizes[ $size ]['height'] = (int) $_wp_additional_image_sizes[ $size ]['height'];
$thumbnail_sizes[ $size ]['crop'] = (bool) $_wp_additional_image_sizes[ $size ]['crop'];
}
}
return $thumbnail_sizes;
}
}
/**
* Returns the single instance of this plugin, creating one if needed.
*
* @return RegenerateThumbnails
*/
function RegenerateThumbnails() {
return RegenerateThumbnails::instance();
}
/**
* Initialize this plugin once all other plugins have finished loading.
*/
add_action( 'init', 'RegenerateThumbnails' );