From 5af93f68ad0a9796ab442e989b7f97914352213b Mon Sep 17 00:00:00 2001 From: Gerhard Potgieter Date: Wed, 15 Nov 2017 08:36:57 +0200 Subject: [PATCH] Add basic background queue processing framework for image regeneration --- .../class-wc-regenerate-images-request.php | 94 +++++++++++++++++++ includes/class-wc-regenerate-images.php | 55 ++++------- 2 files changed, 111 insertions(+), 38 deletions(-) create mode 100644 includes/class-wc-regenerate-images-request.php diff --git a/includes/class-wc-regenerate-images-request.php b/includes/class-wc-regenerate-images-request.php new file mode 100644 index 00000000000..70d32fc8a3b --- /dev/null +++ b/includes/class-wc-regenerate-images-request.php @@ -0,0 +1,94 @@ +info( __( 'Starting product image regeneration job.', 'woocommerce' ) ); + parent::dispatch(); + } + + /** + * Code to execute for each item in the queue + * + * @param mixed $item Queue item to iterate over. + * @return bool + */ + protected function task( $item ) { + $message = $this->get_message( $item ); + $this->really_long_running_task(); + + if ( ! is_array( $message ) && ! isset( $message['attachment_id'] ) ) { + return false; + } + + $attachment_id = absint( $message['attachment_id'] ); + + $attachment = get_post( $attachment_id ); + if ( ! $attachment || 'attachment' !== $attachment->post_type || 'image/' != substr( $attachment->post_mime_type, 0, 6 ) ) { + return false; + } + + $fullsizepath = get_attached_file( $attachment->ID ); + + // Check if the file exists, if not just remove item from queue. + if ( false === $fullsizepath || ! file_exists( $fullsizepath ) ) { + return false; + } + + // This function will generate the new image sizes. + $metadata = wp_generate_attachment_metadata( $attachment->ID, $fullsizepath ); + + // If something went wrong lets just remove the item from the queue. + if ( is_wp_error( $metadata ) || empty( $metadata ) ) { + return false; + } + + // Update the meta data with the new size values. + wp_update_attachment_metadata( $attachment->ID, $metadata ); + + // We made it till the end, now lets remove the item from the queue. + return false; + } + + /** + * This runs once the job has completed all items on the queue. + * + * @return void + */ + protected function complete() { + parent::complete(); + $log = wc_get_logger(); + $log->info( __( 'Completed product image regeneration job.', 'woocommerce' ) ); + } + + +} diff --git a/includes/class-wc-regenerate-images.php b/includes/class-wc-regenerate-images.php index 9f9de828be8..eb5cf17b1cf 100644 --- a/includes/class-wc-regenerate-images.php +++ b/includes/class-wc-regenerate-images.php @@ -20,10 +20,21 @@ if ( ! defined( 'ABSPATH' ) ) { */ class WC_Regenerate_Images { + /** + * Background process to regenerate all images + * + * @var WC_Regenerate_Images_Request + */ + protected $background_process; + /** * Init function */ public static function init() { + include_once( WC_ABSPATH . 'includes/class-wc-regenerate-images-request.php' ); + $this->background_process = new WC_Regenerate_Images_Request(); + + // Action to handle on-the-fly image resizing. add_action( 'wp_get_attachment_image_src', array( __CLASS__, 'maybe_resize_image' ), 10, 4 ); } @@ -37,7 +48,7 @@ class WC_Regenerate_Images { * @return array */ public static function maybe_resize_image( $image, $attachment_id, $size, $icon ) { - if ( ! apply_filters( 'woocommerce_resize_image', true ) ) { + if ( ! apply_filters( 'woocommerce_resize_images', true ) ) { return $image; } @@ -60,7 +71,7 @@ class WC_Regenerate_Images { } /** - * Regenerate the image sizes + * Regenerate the image according to the required size * * @param int $attachment_id Attachment ID. * @param array $image Original Image. @@ -135,43 +146,11 @@ class WC_Regenerate_Images { } /** - * Regenerate the image sizes - * - * @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 + * Regenerate the product images using a job queue in the background. */ - private static function resize_and_return_image_background( $attachment_id, $image, $size, $icon ) { - $attachment = get_post( $attachment_id ); - if ( ! $attachment || 'attachment' !== $attachment->post_type || 'image/' != substr( $attachment->post_mime_type, 0, 6 ) ) { - return $image; - } - - $fullsizepath = get_attached_file( $attachment->ID ); - - // Check if the file exists, if not just return the original image. - if ( false === $fullsizepath || ! file_exists( $fullsizepath ) ) { - return $image; - } - - // This function will generate the new image sizes. - $metadata = wp_generate_attachment_metadata( $attachment->ID, $fullsizepath ); - - // If something went wrong lets just return the original image. - if ( is_wp_error( $metadata ) || empty( $metadata ) ) { - return $image; - } - - // Update the meta data with the new size values. - wp_update_attachment_metadata( $attachment->ID, $metadata ); - - $new_image = wp_get_attachment_image_src( $attachment->ID, $size, $icon ); - if ( false === $new_image ) { - return $image; - } - return $new_image; + private static function regenerate_images_background() { + $this->background_process->push_to_queue( $item ); + $this->background_process->save()->dispatch(); } } WC_Regenerate_Images::init();