2017-11-14 12:39:28 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Regenerate Images Functionality
|
|
|
|
*
|
|
|
|
* All functionality pertaining to regenerating product images in realtime.
|
|
|
|
*
|
|
|
|
* @package WooCommerce/Classes
|
2018-08-08 07:27:54 +00:00
|
|
|
* @version 3.5.0
|
2018-03-21 03:14:07 +00:00
|
|
|
* @since 3.3.0
|
2017-11-14 12:39:28 +00:00
|
|
|
*/
|
|
|
|
|
2018-01-25 17:03:54 +00:00
|
|
|
defined( 'ABSPATH' ) || exit;
|
2017-11-14 12:39:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Regenerate Images Class
|
|
|
|
*/
|
|
|
|
class WC_Regenerate_Images {
|
|
|
|
|
2017-11-15 06:36:57 +00:00
|
|
|
/**
|
|
|
|
* Background process to regenerate all images
|
|
|
|
*
|
|
|
|
* @var WC_Regenerate_Images_Request
|
|
|
|
*/
|
2017-11-15 08:03:59 +00:00
|
|
|
protected static $background_process;
|
2017-11-15 06:36:57 +00:00
|
|
|
|
2018-02-09 14:06:00 +00:00
|
|
|
/**
|
|
|
|
* Stores size being generated on the fly.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-02-14 13:20:34 +00:00
|
|
|
protected static $regenerate_size;
|
2018-02-09 14:06:00 +00:00
|
|
|
|
2017-11-14 12:39:28 +00:00
|
|
|
/**
|
|
|
|
* Init function
|
|
|
|
*/
|
|
|
|
public static function init() {
|
2018-02-14 13:20:34 +00:00
|
|
|
add_action( 'image_get_intermediate_size', array( __CLASS__, 'filter_image_get_intermediate_size' ), 10, 3 );
|
2018-02-09 14:06:00 +00:00
|
|
|
add_filter( 'wp_generate_attachment_metadata', array( __CLASS__, 'add_uncropped_metadata' ) );
|
2018-05-08 21:23:45 +00:00
|
|
|
add_filter( 'wp_get_attachment_image_src', array( __CLASS__, 'maybe_resize_image' ), 10, 4 );
|
2018-02-13 12:02:24 +00:00
|
|
|
|
2018-02-14 13:20:34 +00:00
|
|
|
// Not required when Jetpack Photon is in use.
|
2018-03-07 11:18:42 +00:00
|
|
|
if ( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'photon' ) ) {
|
2018-02-14 13:20:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-14 17:25:52 +00:00
|
|
|
if ( apply_filters( 'woocommerce_background_image_regeneration', true ) ) {
|
2018-02-13 12:02:24 +00:00
|
|
|
include_once WC_ABSPATH . 'includes/class-wc-regenerate-images-request.php';
|
|
|
|
|
|
|
|
self::$background_process = new WC_Regenerate_Images_Request();
|
|
|
|
|
|
|
|
add_action( 'admin_init', array( __CLASS__, 'regenerating_notice' ) );
|
|
|
|
add_action( 'woocommerce_hide_regenerating_thumbnails_notice', array( __CLASS__, 'dismiss_regenerating_notice' ) );
|
2018-02-14 17:25:52 +00:00
|
|
|
|
|
|
|
// Regenerate thumbnails in the background after settings changes. Not ran on multisite to avoid multiple simultanious jobs.
|
|
|
|
if ( ! is_multisite() ) {
|
|
|
|
add_action( 'customize_save_after', array( __CLASS__, 'maybe_regenerate_images' ) );
|
|
|
|
add_action( 'after_switch_theme', array( __CLASS__, 'maybe_regenerate_images' ) );
|
|
|
|
}
|
2018-02-13 12:02:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-14 13:20:34 +00:00
|
|
|
/**
|
|
|
|
* If an intermediate size meta differs from the actual image size (settings were changed?) return false so the wrong size is not used.
|
|
|
|
*
|
|
|
|
* @param array $data Size data.
|
|
|
|
* @param int $attachment_id Attachment ID.
|
|
|
|
* @param string $size Size name.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function filter_image_get_intermediate_size( $data, $attachment_id, $size ) {
|
|
|
|
if ( ! is_string( $size ) || ! in_array( $size, apply_filters( 'woocommerce_image_sizes_to_resize', array( 'woocommerce_thumbnail', 'woocommerce_gallery_thumbnail', 'woocommerce_single', 'shop_thumbnail', 'shop_catalog', 'shop_single' ) ), true ) ) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have sizes, we cannot proceed.
|
|
|
|
if ( ! isset( $data['width'], $data['height'] ) ) {
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2018-02-14 13:43:52 +00:00
|
|
|
// See if the image size has changed from our settings.
|
2018-02-14 13:20:34 +00:00
|
|
|
if ( ! self::image_size_matches_settings( $data, $size ) ) {
|
2018-02-14 13:43:52 +00:00
|
|
|
// If Photon is running we can just return false and let Jetpack handle regeneration.
|
2018-03-07 11:18:42 +00:00
|
|
|
if ( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'photon' ) ) {
|
2018-02-14 13:43:52 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// If we get here, Jetpack is not running and we don't have the correct image sized stored. Try to return closest match.
|
|
|
|
$size_data = wc_get_image_size( $size );
|
|
|
|
return image_get_intermediate_size( $attachment_id, array( absint( $size_data['width'] ), absint( $size_data['height'] ) ) );
|
|
|
|
}
|
2018-02-14 13:20:34 +00:00
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We need to track if uncropped was on or off when generating the images.
|
|
|
|
*
|
|
|
|
* @param array $meta_data Array of meta data.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function add_uncropped_metadata( $meta_data ) {
|
|
|
|
$size_data = wc_get_image_size( 'woocommerce_thumbnail' );
|
|
|
|
if ( isset( $meta_data['sizes'], $meta_data['sizes']['woocommerce_thumbnail'] ) ) {
|
2019-02-02 09:54:33 +00:00
|
|
|
$meta_data['sizes']['woocommerce_thumbnail']['uncropped'] = empty( $size_data['height'] );
|
2018-02-14 13:20:34 +00:00
|
|
|
}
|
|
|
|
return $meta_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See if an image's dimensions match actual settings.
|
|
|
|
*
|
|
|
|
* @param array $image Image dimensions array.
|
|
|
|
* @param string $size Named size.
|
2019-02-22 12:10:54 +00:00
|
|
|
* @return bool True if they match. False if they do not (may trigger regen).
|
2018-02-14 13:20:34 +00:00
|
|
|
*/
|
|
|
|
protected static function image_size_matches_settings( $image, $size ) {
|
2019-02-22 12:10:54 +00:00
|
|
|
$target_size = wc_get_image_size( $size );
|
|
|
|
$uncropped = '' === $target_size['width'] || '' === $target_size['height'];
|
2018-02-14 13:20:34 +00:00
|
|
|
|
2019-02-22 12:10:54 +00:00
|
|
|
if ( ! $uncropped ) {
|
|
|
|
$ratio_match = wp_image_matches_ratio( $image['width'], $image['height'], $target_size['width'], $target_size['height'] );
|
2018-02-14 13:20:34 +00:00
|
|
|
|
2019-02-22 12:10:54 +00:00
|
|
|
// Size is invalid if the widths or crop setting don't match.
|
|
|
|
if ( $ratio_match && $target_size['width'] !== $image['width'] ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size is invalid if the heights don't match.
|
|
|
|
if ( $ratio_match && $target_size['height'] && $target_size['height'] !== $image['height'] ) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-14 13:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If cropping mode has changed, regenerate the image.
|
2019-02-22 12:10:54 +00:00
|
|
|
if ( $uncropped && empty( $image['uncropped'] ) ) {
|
2018-02-14 13:20:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-13 12:02:24 +00:00
|
|
|
/**
|
|
|
|
* Show notice when job is running in background.
|
|
|
|
*/
|
|
|
|
public static function regenerating_notice() {
|
|
|
|
if ( ! self::$background_process->is_running() ) {
|
|
|
|
WC_Admin_Notices::add_notice( 'regenerating_thumbnails' );
|
|
|
|
} else {
|
|
|
|
WC_Admin_Notices::remove_notice( 'regenerating_thumbnails' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dismiss notice and cancel jobs.
|
|
|
|
*/
|
|
|
|
public static function dismiss_regenerating_notice() {
|
|
|
|
if ( self::$background_process ) {
|
|
|
|
self::$background_process->kill_process();
|
2018-02-13 19:22:01 +00:00
|
|
|
|
|
|
|
$log = wc_get_logger();
|
2018-03-21 03:14:07 +00:00
|
|
|
$log->info(
|
|
|
|
__( 'Cancelled product image regeneration job.', 'woocommerce' ),
|
2018-02-13 19:22:01 +00:00
|
|
|
array(
|
|
|
|
'source' => 'wc-image-regeneration',
|
|
|
|
)
|
|
|
|
);
|
2018-02-13 12:02:24 +00:00
|
|
|
}
|
|
|
|
WC_Admin_Notices::remove_notice( 'regenerating_thumbnails' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regenerate images if the settings have changed since last re-generation.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-02-13 17:57:45 +00:00
|
|
|
public static function maybe_regenerate_images() {
|
2018-03-21 03:14:07 +00:00
|
|
|
$size_hash = md5(
|
|
|
|
wp_json_encode(
|
|
|
|
array(
|
|
|
|
wc_get_image_size( 'thumbnail' ),
|
|
|
|
wc_get_image_size( 'single' ),
|
|
|
|
wc_get_image_size( 'gallery_thumbnail' ),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2018-02-13 12:02:24 +00:00
|
|
|
|
|
|
|
if ( update_option( 'woocommerce_maybe_regenerate_images_hash', $size_hash ) ) {
|
|
|
|
// Size settings have changed. Trigger regen.
|
|
|
|
self::queue_image_regeneration();
|
|
|
|
}
|
2017-11-14 12:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if we should maybe generate a new image size if not already there.
|
|
|
|
*
|
|
|
|
* @param array $image Properties of the image.
|
|
|
|
* @param int $attachment_id Attachment ID.
|
|
|
|
* @param string|array $size Image size.
|
|
|
|
* @param bool $icon If icon or not.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function maybe_resize_image( $image, $attachment_id, $size, $icon ) {
|
2018-01-31 21:10:56 +00:00
|
|
|
if ( ! apply_filters( 'woocommerce_resize_images', true ) ) {
|
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
2017-12-12 17:24:52 +00:00
|
|
|
// Use a whitelist of sizes we want to resize. Ignore others.
|
2018-05-08 21:23:45 +00:00
|
|
|
if ( ! $image || ! in_array( $size, apply_filters( 'woocommerce_image_sizes_to_resize', array( 'woocommerce_thumbnail', 'woocommerce_gallery_thumbnail', 'woocommerce_single', 'shop_thumbnail', 'shop_catalog', 'shop_single' ) ), true ) ) {
|
2017-12-12 17:24:52 +00:00
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:59:43 +00:00
|
|
|
$target_size = wc_get_image_size( $size );
|
|
|
|
$image_width = $image[1];
|
|
|
|
$image_height = $image[2];
|
|
|
|
$ratio_match = false;
|
|
|
|
$target_uncropped = '' === $target_size['width'] || '' === $target_size['height'] || ! $target_size['crop'];
|
2017-11-14 12:39:28 +00:00
|
|
|
|
2018-05-08 21:23:45 +00:00
|
|
|
// If '' is passed to either size, we test ratios against the original file. It's uncropped.
|
2019-03-18 10:59:43 +00:00
|
|
|
if ( $target_uncropped ) {
|
2019-02-22 12:10:54 +00:00
|
|
|
$full_size = self::get_full_size_image_dimensions( $attachment_id );
|
2018-05-08 21:23:45 +00:00
|
|
|
|
2019-03-01 10:19:40 +00:00
|
|
|
if ( ! $full_size || ! $full_size['width'] || ! $full_size['height'] ) {
|
2018-05-08 21:23:45 +00:00
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
2019-02-22 12:10:54 +00:00
|
|
|
$ratio_match = wp_image_matches_ratio( $image_width, $image_height, $full_size['width'], $full_size['height'] );
|
2018-05-08 21:23:45 +00:00
|
|
|
} else {
|
2019-02-22 12:10:54 +00:00
|
|
|
$ratio_match = wp_image_matches_ratio( $image_width, $image_height, $target_size['width'], $target_size['height'] );
|
2017-11-14 12:39:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 21:23:45 +00:00
|
|
|
if ( ! $ratio_match ) {
|
2019-02-22 12:10:54 +00:00
|
|
|
$full_size = self::get_full_size_image_dimensions( $attachment_id );
|
|
|
|
|
|
|
|
if ( ! $full_size ) {
|
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the actual image has a larger dimension than the requested image size. Smaller images are not zoom-cropped.
|
|
|
|
if ( $image_width === $target_size['width'] && $full_size['height'] < $target_size['height'] ) {
|
|
|
|
return $image;
|
2019-02-21 16:58:40 +00:00
|
|
|
}
|
2019-02-22 12:10:54 +00:00
|
|
|
|
|
|
|
if ( $image_height === $target_size['height'] && $full_size['width'] < $target_size['width'] ) {
|
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
2019-03-01 10:15:01 +00:00
|
|
|
// If the full size image is smaller both ways, don't scale it up.
|
|
|
|
if ( $full_size['height'] < $target_size['height'] && $full_size['width'] < $target_size['width'] ) {
|
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
2019-02-22 12:10:54 +00:00
|
|
|
return self::resize_and_return_image( $attachment_id, $image, $size, $icon );
|
2018-02-09 14:06:00 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 12:39:28 +00:00
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
2019-02-22 12:10:28 +00:00
|
|
|
/**
|
|
|
|
* Get full size image dimensions.
|
|
|
|
*
|
|
|
|
* @param int $attachment_id Attachment ID of image.
|
|
|
|
* @return array Width and height. Empty array if the dimensions cannot be found.
|
|
|
|
*/
|
|
|
|
private static function get_full_size_image_dimensions( $attachment_id ) {
|
|
|
|
$imagedata = wp_get_attachment_metadata( $attachment_id );
|
|
|
|
|
|
|
|
if ( ! $imagedata ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isset( $imagedata['file'] ) && isset( $imagedata['sizes']['full'] ) ) {
|
|
|
|
$imagedata['height'] = $imagedata['sizes']['full']['height'];
|
|
|
|
$imagedata['width'] = $imagedata['sizes']['full']['width'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'width' => $imagedata['width'],
|
|
|
|
'height' => $imagedata['height'],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-14 12:39:28 +00:00
|
|
|
/**
|
2018-02-08 13:36:50 +00:00
|
|
|
* Ensure we are dealing with the correct image attachment
|
2017-11-14 12:39:28 +00:00
|
|
|
*
|
2019-02-21 17:15:37 +00:00
|
|
|
* @param int|WP_Post $attachment Attachment object or ID.
|
2018-02-08 13:36:50 +00:00
|
|
|
* @return boolean
|
2017-11-14 12:39:28 +00:00
|
|
|
*/
|
2018-02-08 13:36:50 +00:00
|
|
|
public static function is_regeneratable( $attachment ) {
|
2019-02-21 17:15:24 +00:00
|
|
|
if ( 'site-icon' === get_post_meta( is_object( $attachment ) ? $attachment->ID : $attachment, '_wp_attachment_context', true ) ) {
|
2018-02-08 13:36:50 +00:00
|
|
|
return false;
|
2017-11-14 12:39:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 13:36:50 +00:00
|
|
|
if ( wp_attachment_is_image( $attachment ) ) {
|
|
|
|
return true;
|
2017-11-14 12:39:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 13:36:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-11-14 12:39:28 +00:00
|
|
|
|
2018-02-08 13:36:50 +00:00
|
|
|
/**
|
2018-02-09 14:06:00 +00:00
|
|
|
* Only regenerate images for the requested size.
|
2018-02-08 13:36:50 +00:00
|
|
|
*
|
|
|
|
* @param array $sizes Array of image sizes.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function adjust_intermediate_image_sizes( $sizes ) {
|
2018-02-14 13:20:34 +00:00
|
|
|
return array( self::$regenerate_size );
|
2018-02-08 13:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-09 14:06:00 +00:00
|
|
|
* Generate the thumbnail filename and dimensions for a given file.
|
2018-02-08 13:36:50 +00:00
|
|
|
*
|
2018-02-09 14:06:00 +00:00
|
|
|
* @param string $fullsizepath Path to full size image.
|
|
|
|
* @param int $thumbnail_width The width of the thumbnail.
|
|
|
|
* @param int $thumbnail_height The height of the thumbnail.
|
|
|
|
* @param bool $crop Whether to crop or not.
|
|
|
|
* @return array|false An array of the filename, thumbnail width, and thumbnail height, or false on failure to resize such as the thumbnail being larger than the fullsize image.
|
2018-02-08 13:36:50 +00:00
|
|
|
*/
|
2018-02-09 14:06:00 +00:00
|
|
|
private static function get_image( $fullsizepath, $thumbnail_width, $thumbnail_height, $crop ) {
|
|
|
|
list( $fullsize_width, $fullsize_height ) = getimagesize( $fullsizepath );
|
2018-02-08 13:36:50 +00:00
|
|
|
|
2018-02-09 14:06:00 +00:00
|
|
|
$dimensions = image_resize_dimensions( $fullsize_width, $fullsize_height, $thumbnail_width, $thumbnail_height, $crop );
|
|
|
|
$editor = wp_get_image_editor( $fullsizepath );
|
2017-11-14 12:39:28 +00:00
|
|
|
|
2018-02-09 14:06:00 +00:00
|
|
|
if ( is_wp_error( $editor ) ) {
|
2018-02-08 13:36:50 +00:00
|
|
|
return false;
|
2017-11-14 12:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $dimensions || ! is_array( $dimensions ) ) {
|
2018-02-08 13:36:50 +00:00
|
|
|
return false;
|
2017-11-14 12:39:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 14:06:00 +00:00
|
|
|
list( , , , , $dst_w, $dst_h ) = $dimensions;
|
2018-03-21 03:14:07 +00:00
|
|
|
$suffix = "{$dst_w}x{$dst_h}";
|
|
|
|
$file_ext = strtolower( pathinfo( $fullsizepath, PATHINFO_EXTENSION ) );
|
2017-11-14 12:39:28 +00:00
|
|
|
|
2018-02-09 14:06:00 +00:00
|
|
|
return array(
|
|
|
|
'filename' => $editor->generate_filename( $suffix, null, $file_ext ),
|
|
|
|
'width' => $dst_w,
|
|
|
|
'height' => $dst_h,
|
|
|
|
);
|
2018-02-08 13:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regenerate the image according to the required size
|
|
|
|
*
|
|
|
|
* @param int $attachment_id Attachment ID.
|
|
|
|
* @param array $image Original Image.
|
|
|
|
* @param string $size Size to return for new URL.
|
|
|
|
* @param bool $icon If icon or not.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function resize_and_return_image( $attachment_id, $image, $size, $icon ) {
|
2019-02-21 17:15:24 +00:00
|
|
|
if ( ! self::is_regeneratable( $attachment_id ) ) {
|
2018-02-08 13:36:50 +00:00
|
|
|
return $image;
|
2017-11-14 12:39:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 14:06:00 +00:00
|
|
|
$fullsizepath = get_attached_file( $attachment_id );
|
|
|
|
|
|
|
|
if ( false === $fullsizepath || is_wp_error( $fullsizepath ) || ! file_exists( $fullsizepath ) ) {
|
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
2018-02-08 13:36:50 +00:00
|
|
|
if ( ! function_exists( 'wp_crop_image' ) ) {
|
|
|
|
include ABSPATH . 'wp-admin/includes/image.php';
|
|
|
|
}
|
|
|
|
|
2018-05-08 21:23:45 +00:00
|
|
|
self::$regenerate_size = is_customize_preview() ? $size . '_preview' : $size;
|
2018-02-09 14:06:00 +00:00
|
|
|
|
2018-05-08 21:23:45 +00:00
|
|
|
if ( is_customize_preview() ) {
|
2019-02-21 17:37:16 +00:00
|
|
|
$image_size = wc_get_image_size( $size );
|
|
|
|
|
2018-05-08 21:23:45 +00:00
|
|
|
// Make sure registered image size matches the size we're requesting.
|
|
|
|
add_image_size( self::$regenerate_size, absint( $image_size['width'] ), absint( $image_size['height'] ), $image_size['crop'] );
|
|
|
|
|
|
|
|
$thumbnail = self::get_image( $fullsizepath, absint( $image_size['width'] ), absint( $image_size['height'] ), $image_size['crop'] );
|
|
|
|
|
|
|
|
// If the file is already there perhaps just load it if we're using the customizer. No need to store in meta data.
|
|
|
|
if ( $thumbnail && file_exists( $thumbnail['filename'] ) ) {
|
|
|
|
$wp_uploads = wp_upload_dir( null, false );
|
|
|
|
$wp_uploads_dir = $wp_uploads['basedir'];
|
|
|
|
$wp_uploads_url = $wp_uploads['baseurl'];
|
|
|
|
|
|
|
|
return array(
|
|
|
|
0 => str_replace( $wp_uploads_dir, $wp_uploads_url, $thumbnail['filename'] ),
|
|
|
|
1 => $thumbnail['width'],
|
|
|
|
2 => $thumbnail['height'],
|
|
|
|
);
|
|
|
|
}
|
2017-11-14 12:39:28 +00:00
|
|
|
}
|
2018-02-08 13:36:50 +00:00
|
|
|
|
2018-02-13 14:33:45 +00:00
|
|
|
$metadata = wp_get_attachment_metadata( $attachment_id );
|
2018-02-08 13:36:50 +00:00
|
|
|
|
2019-01-31 13:01:59 +00:00
|
|
|
// Fix for images with no metadata.
|
|
|
|
if ( ! is_array( $metadata ) ) {
|
|
|
|
$metadata = array();
|
|
|
|
}
|
|
|
|
|
2018-02-14 13:20:34 +00:00
|
|
|
// We only want to regen a specific image size.
|
2018-02-08 13:36:50 +00:00
|
|
|
add_filter( 'intermediate_image_sizes', array( __CLASS__, 'adjust_intermediate_image_sizes' ) );
|
|
|
|
|
|
|
|
// This function will generate the new image sizes.
|
|
|
|
$new_metadata = wp_generate_attachment_metadata( $attachment_id, $fullsizepath );
|
|
|
|
|
|
|
|
// Remove custom filter.
|
|
|
|
remove_filter( 'intermediate_image_sizes', array( __CLASS__, 'adjust_intermediate_image_sizes' ) );
|
|
|
|
|
|
|
|
// If something went wrong lets just return the original image.
|
|
|
|
if ( is_wp_error( $new_metadata ) || empty( $new_metadata ) ) {
|
|
|
|
return $image;
|
2017-11-14 12:39:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 21:23:45 +00:00
|
|
|
if ( isset( $new_metadata['sizes'][ self::$regenerate_size ] ) ) {
|
|
|
|
$metadata['sizes'][ self::$regenerate_size ] = $new_metadata['sizes'][ self::$regenerate_size ];
|
2018-02-13 14:33:45 +00:00
|
|
|
wp_update_attachment_metadata( $attachment_id, $metadata );
|
2018-02-08 13:36:50 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 14:06:00 +00:00
|
|
|
// Now we've done our regen, attempt to return the new size.
|
2019-02-22 12:10:16 +00:00
|
|
|
$new_image = self::unfiltered_image_downsize( $attachment_id, self::$regenerate_size );
|
2018-02-09 14:06:00 +00:00
|
|
|
|
|
|
|
return $new_image ? $new_image : $image;
|
2017-11-14 12:39:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 12:09:59 +00:00
|
|
|
/**
|
|
|
|
* Image downsize, without this classes filtering on the results.
|
|
|
|
*
|
|
|
|
* @param int $attachment_id Attachment ID.
|
|
|
|
* @param string $size Size to downsize to.
|
|
|
|
* @return string New image URL.
|
|
|
|
*/
|
|
|
|
private static function unfiltered_image_downsize( $attachment_id, $size ) {
|
|
|
|
remove_action( 'image_get_intermediate_size', array( __CLASS__, 'filter_image_get_intermediate_size' ), 10, 3 );
|
|
|
|
|
|
|
|
$return = image_downsize( $attachment_id, $size );
|
|
|
|
|
|
|
|
add_action( 'image_get_intermediate_size', array( __CLASS__, 'filter_image_get_intermediate_size' ), 10, 3 );
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2017-11-16 12:37:33 +00:00
|
|
|
/**
|
|
|
|
* Get list of images and queue them for regeneration
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-02-13 07:55:50 +00:00
|
|
|
public static function queue_image_regeneration() {
|
2017-11-16 12:37:33 +00:00
|
|
|
global $wpdb;
|
2017-11-15 07:23:36 +00:00
|
|
|
// First lets cancel existing running queue to avoid running it more than once.
|
2018-02-01 14:03:28 +00:00
|
|
|
self::$background_process->kill_process();
|
2017-11-15 07:23:36 +00:00
|
|
|
|
|
|
|
// Now lets find all product image attachments IDs and pop them onto the queue.
|
|
|
|
$images = $wpdb->get_results( // @codingStandardsIgnoreLine
|
|
|
|
"SELECT ID
|
|
|
|
FROM $wpdb->posts
|
|
|
|
WHERE post_type = 'attachment'
|
|
|
|
AND post_mime_type LIKE 'image/%'
|
|
|
|
ORDER BY ID DESC"
|
|
|
|
);
|
|
|
|
foreach ( $images as $image ) {
|
2018-03-21 03:14:07 +00:00
|
|
|
self::$background_process->push_to_queue(
|
|
|
|
array(
|
|
|
|
'attachment_id' => $image->ID,
|
|
|
|
)
|
|
|
|
);
|
2017-11-15 07:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lets dispatch the queue to start processing.
|
2017-11-15 08:03:59 +00:00
|
|
|
self::$background_process->save()->dispatch();
|
2017-11-14 12:39:28 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-13 12:51:55 +00:00
|
|
|
|
2018-02-14 13:20:34 +00:00
|
|
|
add_action( 'init', array( 'WC_Regenerate_Images', 'init' ) );
|