woocommerce/includes/class-wc-regenerate-images.php

206 lines
6.7 KiB
PHP
Raw Normal View History

2017-11-14 12:39:28 +00:00
<?php
/**
* Regenerate Images Functionality
*
* All functionality pertaining to regenerating product images in realtime.
*
* @package WooCommerce/Classes
* @version 3.3.0
* @since 3.3.0
*/
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 {
/**
* 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-14 12:39:28 +00:00
/**
* Init function
*/
public static function init() {
2018-01-25 17:03:54 +00:00
include_once WC_ABSPATH . 'includes/class-wc-regenerate-images-request.php';
2017-11-15 08:03:59 +00:00
self::$background_process = new WC_Regenerate_Images_Request();
if ( apply_filters( 'woocommerce_resize_images', true ) && ! is_admin() ) {
2017-12-12 03:01:25 +00:00
// Handle on-the-fly image resizing.
add_filter( 'wp_get_attachment_image_src', array( __CLASS__, 'maybe_resize_image' ), 10, 4 );
}
if ( apply_filters( 'woocommerce_background_image_regeneration', true ) ) {
// Actions to handle image generation when settings change.
add_action( 'update_option_woocommerce_thumbnail_cropping', array( __CLASS__, 'maybe_regenerate_images_option_update' ), 10, 3 );
add_action( 'update_option_woocommerce_thumbnail_image_width', array( __CLASS__, 'maybe_regenerate_images_option_update' ), 10, 3 );
add_action( 'update_option_woocommerce_single_image_width', array( __CLASS__, 'maybe_regenerate_images_option_update' ), 10, 3 );
add_action( 'after_switch_theme', array( __CLASS__, 'maybe_regenerate_image_theme_switch' ) );
}
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 ) {
2017-12-12 17:24:52 +00:00
// Use a whitelist of sizes we want to resize. Ignore others.
if ( ! in_array( $size, array( 'woocommerce_thumbnail', 'woocommerce_single', 'shop_thumbnail', 'shop_catalog', 'shop_single' ), true ) ) {
return $image;
}
// Get image metadata - we need it to proceed.
2017-11-14 12:39:28 +00:00
$imagemeta = wp_get_attachment_metadata( $attachment_id );
if ( false === $imagemeta || empty( $imagemeta ) ) {
return $image;
}
$size_settings = wc_get_image_size( $size );
2017-12-12 17:24:52 +00:00
// If size differs from image meta, regen.
2018-01-31 17:43:39 +00:00
if ( ! isset( $imagemeta['sizes'], $imagemeta['sizes'][ $size ] ) || $imagemeta['sizes'][ $size ]['width'] !== $size_settings['width'] || $imagemeta['sizes'][ $size ]['height'] !== $size_settings['height'] ) {
2017-11-14 12:39:28 +00:00
$image = self::resize_and_return_image( $attachment_id, $image, $size, $icon );
}
2017-11-14 12:39:28 +00:00
return $image;
}
/**
* Regenerate the image according to the required size
2017-11-14 12:39:28 +00:00
*
* @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 ) {
$attachment = get_post( $attachment_id );
if ( ! $attachment || 'attachment' !== $attachment->post_type || 'image/' !== substr( $attachment->post_mime_type, 0, 6 ) ) {
2017-11-14 12:39:28 +00:00
return $image;
}
if ( ! function_exists( 'wp_crop_image' ) ) {
2018-01-25 17:03:54 +00:00
include ABSPATH . 'wp-admin/includes/image.php';
2017-11-14 12:39:28 +00:00
}
$wp_uploads = wp_upload_dir( null, false );
$wp_uploads_dir = $wp_uploads['basedir'];
$wp_uploads_url = $wp_uploads['baseurl'];
2018-01-25 17:03:54 +00:00
$original_image_file_path = get_attached_file( $attachment->ID );
2017-11-14 12:39:28 +00:00
if ( ! file_exists( $original_image_file_path ) || ! getimagesize( $original_image_file_path ) ) {
return $image;
}
$info = pathinfo( $original_image_file_path );
2018-01-25 17:03:54 +00:00
$ext = $info['extension'];
2017-11-14 12:39:28 +00:00
list( $orig_w, $orig_h ) = getimagesize( $original_image_file_path );
// Get image size after cropping.
$image_size = wc_get_image_size( $size );
$dimensions = image_resize_dimensions( $orig_w, $orig_h, $image_size['width'], $image_size['height'], $image_size['crop'] );
if ( ! $dimensions || ! is_array( $dimensions ) ) {
return $image;
}
$dst_w = $dimensions[4];
$dst_h = $dimensions[5];
$suffix = "{$dst_w}x{$dst_h}";
$dst_rel_path = str_replace( '.' . $ext, '', $original_image_file_path );
$destfilename = "{$dst_rel_path}-{$suffix}.{$ext}";
// If the file is already there perhaps just load it.
if ( file_exists( $destfilename ) ) {
return array(
0 => str_replace( $wp_uploads_dir, $wp_uploads_url, $destfilename ),
1 => $image_size['width'],
2 => $image_size['height'],
);
}
// Lets resize the image if it does not exist yet.
$editor = wp_get_image_editor( $original_image_file_path );
if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $image_size['width'], $image_size['height'], $image_size['crop'] ) ) ) {
return $image;
}
$resized_file = $editor->save();
if ( ! is_wp_error( $resized_file ) ) {
$img_url = str_replace( $wp_uploads_dir, $wp_uploads_url, $resized_file['path'] );
return array(
0 => $img_url,
1 => $image_size['width'],
2 => $image_size['height'],
);
}
// Lets just add this here as a fallback.
return $image;
}
/**
* Check if we should regenerate the product images when options change.
*
* @param mixed $old_value Old option value.
* @param mixed $new_value New option value.
* @param string $option Option name.
2017-11-14 12:39:28 +00:00
*/
public static function maybe_regenerate_images_option_update( $old_value, $new_value, $option ) {
if ( $new_value === $old_value ) {
return;
}
self::queue_image_regeneration();
}
/**
* Check if we should generate images when new themes declares custom sizes.
*/
public static function maybe_regenerate_image_theme_switch() {
2018-01-25 15:06:12 +00:00
if ( wc_get_theme_support( 'single_image_width' ) || wc_get_theme_support( 'thumbnail_image_width' ) ) {
self::queue_image_regeneration();
}
}
/**
* Get list of images and queue them for regeneration
*
* @return void
*/
private static function queue_image_regeneration() {
global $wpdb;
// First lets cancel existing running queue to avoid running it more than once.
2017-11-15 08:03:59 +00:00
self::$background_process->cancel_process();
// 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 ) {
2017-11-15 08:03:59 +00:00
self::$background_process->push_to_queue( array(
'attachment_id' => $image->ID,
) );
}
// 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
}
}
WC_Regenerate_Images::init();