Do not auto generate images on setting changes or theme switches. Introduce a regenerate button under tools. Only resize images on the fly when using the customizer to preview changes.

This commit is contained in:
Gerhard Potgieter 2018-02-13 09:55:50 +02:00
parent 7339511495
commit 794272a732
3 changed files with 13 additions and 37 deletions

View File

@ -2,8 +2,6 @@
/**
* Debug/Status page
*
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin/System Status
* @version 2.2.0
*/

View File

@ -179,6 +179,11 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller {
'button' => __( 'Reset', 'woocommerce' ),
'desc' => __( 'This will reset your usage tracking settings, causing it to show the opt-in banner again and not sending any data.', 'woocommerce' ),
),
'regenerate_thumbnails' => array(
'name' => __( 'Regenerate shop thumbnails', 'woocommerce' ),
'button' => __( 'Regenerate', 'woocommerce' ),
'desc' => __( 'This will regenerate all shop thumbnails to match your theme and/or image setting.', 'woocommerce' ),
)
);
return apply_filters( 'woocommerce_debug_tools', $tools );
@ -468,6 +473,11 @@ class WC_REST_System_Status_Tools_Controller extends WC_REST_Controller {
$message = __( 'Usage tracking settings successfully reset.', 'woocommerce' );
break;
case 'regenerate_thumbnails':
WC_Regenerate_Images::queue_image_regeneration();
$message = __( 'Thumbnail regeneration is running in the background. Depending on the amount of images in your store this might take a while, please check the logs for when it is complete.', 'woocommerce' );
break;
default:
$tools = $this->get_tools();
if ( isset( $tools[ $tool ]['callback'] ) ) {

View File

@ -39,18 +39,10 @@ class WC_Regenerate_Images {
add_filter( 'wp_generate_attachment_metadata', array( __CLASS__, 'add_uncropped_metadata' ) );
if ( ! is_admin() ) {
// Handle on-the-fly image resizing.
// Resize WooCommerce images on the fly when browsing site through customizer as to showcase image setting changes in real time.
if ( is_customize_preview() ) {
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' ) );
}
}
/**
@ -261,36 +253,12 @@ class WC_Regenerate_Images {
return $new_image ? $new_image : $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.
*/
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() {
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() {
public static function queue_image_regeneration() {
global $wpdb;
// First lets cancel existing running queue to avoid running it more than once.
self::$background_process->kill_process();