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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / class-wc-regenerate-images.php
woocommerce / includes Last commit date
abstracts 1 month ago admin 1 week ago blocks 11 months ago cli 1 month ago customizer 4 months ago data-stores 1 month ago emails 1 month ago export 1 year ago gateways 1 month ago import 3 months ago integrations 2 months ago interfaces 4 months ago legacy 4 months ago libraries 1 month ago log-handlers 1 year ago payment-tokens 5 years ago product-usage 1 year ago queue 4 months ago react-admin 1 month ago rest-api 1 month ago shipping 3 months ago shortcodes 1 month ago theme-support 2 years ago tracks 1 month ago traits 5 years ago walkers 5 years ago wccom-site 1 month ago widgets 2 months ago class-wc-ajax.php 1 month ago class-wc-auth.php 1 year ago class-wc-autoloader.php 9 months ago class-wc-background-emailer.php 2 months ago class-wc-background-updater.php 5 years ago class-wc-brands-brand-settings-manager.php 1 year ago class-wc-brands-coupons.php 1 year ago class-wc-brands.php 1 month ago class-wc-breadcrumb.php 4 months ago class-wc-cache-helper.php 1 month ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 3 months ago class-wc-cart-totals.php 11 months ago class-wc-cart.php 1 month ago class-wc-checkout.php 1 month ago class-wc-cli.php 10 months ago class-wc-comments.php 4 months ago class-wc-countries.php 2 months ago class-wc-coupon.php 2 months ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 1 year ago class-wc-customer.php 1 month ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 3 years ago class-wc-datetime.php 4 years ago class-wc-deprecated-action-hooks.php 2 years ago class-wc-deprecated-filter-hooks.php 3 months ago class-wc-discounts.php 11 months ago class-wc-download-handler.php 1 month ago class-wc-emails.php 1 month ago class-wc-embed.php 1 year ago class-wc-form-handler.php 4 months ago class-wc-frontend-scripts.php 2 months ago class-wc-geo-ip.php 9 months ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 2 months ago class-wc-https.php 3 years ago class-wc-install.php 1 week ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 4 months ago class-wc-meta-data.php 1 month ago class-wc-order-factory.php 2 months ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 5 months ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 1 month ago class-wc-order-item-shipping.php 5 months ago class-wc-order-item-tax.php 1 month ago class-wc-order-item.php 1 month ago class-wc-order-query.php 1 month ago class-wc-order-refund.php 1 year ago class-wc-order.php 1 month ago class-wc-payment-gateways.php 1 month ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 2 months ago class-wc-post-types.php 1 month ago class-wc-privacy-background-process.php 1 year ago class-wc-privacy-erasers.php 10 months ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 1 year ago class-wc-product-attribute.php 4 months ago class-wc-product-download.php 4 months ago class-wc-product-external.php 1 year ago class-wc-product-factory.php 3 months ago class-wc-product-grouped.php 3 months ago class-wc-product-query.php 4 months ago class-wc-product-simple.php 11 months ago class-wc-product-variable.php 1 month ago class-wc-product-variation.php 1 month ago class-wc-query.php 1 month ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 3 years ago class-wc-regenerate-images.php 2 years ago class-wc-register-wp-admin-settings.php 5 years ago class-wc-rest-authentication.php 2 years ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 3 months ago class-wc-shipping-rate.php 1 year ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 7 months ago class-wc-shipping.php 2 months ago class-wc-shortcodes.php 1 year ago class-wc-structured-data.php 1 month ago class-wc-tax.php 2 months ago class-wc-template-loader.php 8 months ago class-wc-tracker.php 1 month ago class-wc-validation.php 2 years ago class-wc-webhook.php 1 month ago class-woocommerce.php 1 week ago wc-account-functions.php 1 month ago wc-attribute-functions.php 1 month ago wc-brands-functions.php 1 year ago wc-cart-functions.php 1 month ago wc-conditional-functions.php 11 months ago wc-core-functions.php 2 months ago wc-coupon-functions.php 5 months ago wc-deprecated-functions.php 1 month ago wc-formatting-functions.php 1 month ago wc-interactivity-api-functions.php 2 months ago wc-notice-functions.php 5 months ago wc-order-functions.php 2 months ago wc-order-item-functions.php 3 years ago wc-order-step-logger-functions.php 4 months ago wc-page-functions.php 2 months ago wc-product-functions.php 2 months ago wc-rest-functions.php 7 months ago wc-stock-functions.php 7 months ago wc-template-functions.php 1 month ago wc-template-hooks.php 10 months ago wc-term-functions.php 2 months ago wc-update-functions.php 1 week ago wc-user-functions.php 1 month ago wc-webhook-functions.php 1 month ago wc-widget-functions.php 5 years ago
class-wc-regenerate-images.php
478 lines
1 <?php
2 /**
3 * Regenerate Images Functionality
4 *
5 * All functionality pertaining to regenerating product images in realtime.
6 *
7 * @package WooCommerce\Classes
8 * @version 3.5.0
9 * @since 3.3.0
10 */
11
12 defined( 'ABSPATH' ) || exit;
13
14 /**
15 * Regenerate Images Class
16 */
17 class WC_Regenerate_Images {
18
19 /**
20 * Background process to regenerate all images
21 *
22 * @var WC_Regenerate_Images_Request
23 */
24 protected static $background_process;
25
26 /**
27 * Stores size being generated on the fly.
28 *
29 * @var string
30 */
31 protected static $regenerate_size;
32
33 /**
34 * Init function
35 */
36 public static function init() {
37 add_action( 'image_get_intermediate_size', array( __CLASS__, 'filter_image_get_intermediate_size' ), 10, 3 );
38 add_filter( 'wp_generate_attachment_metadata', array( __CLASS__, 'add_uncropped_metadata' ) );
39 add_filter( 'wp_get_attachment_image_src', array( __CLASS__, 'maybe_resize_image' ), 10, 4 );
40
41 // Not required when Jetpack Photon is in use.
42 if ( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'photon' ) ) {
43 return;
44 }
45
46 if ( apply_filters( 'woocommerce_background_image_regeneration', true ) ) {
47 include_once WC_ABSPATH . 'includes/class-wc-regenerate-images-request.php';
48
49 self::$background_process = new WC_Regenerate_Images_Request();
50
51 add_action( 'admin_init', array( __CLASS__, 'regenerating_notice' ) );
52 add_action( 'woocommerce_hide_regenerating_thumbnails_notice', array( __CLASS__, 'dismiss_regenerating_notice' ) );
53
54 // Regenerate thumbnails in the background after settings changes. Not ran on multisite to avoid multiple simultaneous jobs.
55 if ( ! is_multisite() ) {
56 add_action( 'customize_save_after', array( __CLASS__, 'maybe_regenerate_images' ) );
57 add_action( 'after_switch_theme', array( __CLASS__, 'maybe_regenerate_images' ) );
58 }
59 }
60 }
61
62 /**
63 * If an intermediate size meta differs from the actual image size (settings were changed?) return false so the wrong size is not used.
64 *
65 * @param array $data Size data.
66 * @param int $attachment_id Attachment ID.
67 * @param string $size Size name.
68 * @return array
69 */
70 public static function filter_image_get_intermediate_size( $data, $attachment_id, $size ) {
71 if ( ! is_string( $size ) || ! in_array( $size, apply_filters( 'woocommerce_image_sizes_to_resize', array( 'woocommerce_thumbnail', 'woocommerce_gallery_thumbnail', 'woocommerce_single' ) ), true ) ) {
72 return $data;
73 }
74
75 // If we don't have sizes, we cannot proceed.
76 if ( ! isset( $data['width'], $data['height'] ) ) {
77 return $data;
78 }
79
80 // See if the image size has changed from our settings.
81 if ( ! self::image_size_matches_settings( $data, $size ) ) {
82 // If Photon is running we can just return false and let Jetpack handle regeneration.
83 if ( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'photon' ) ) {
84 return false;
85 } else {
86 // If we get here, Jetpack is not running and we don't have the correct image sized stored. Try to return closest match.
87 $size_data = wc_get_image_size( $size );
88 return image_get_intermediate_size( $attachment_id, array( absint( $size_data['width'] ), absint( $size_data['height'] ) ) );
89 }
90 }
91 return $data;
92 }
93
94 /**
95 * We need to track if uncropped was on or off when generating the images.
96 *
97 * @param array $meta_data Array of meta data.
98 * @return array
99 */
100 public static function add_uncropped_metadata( $meta_data ) {
101 $size_data = wc_get_image_size( 'woocommerce_thumbnail' );
102 if ( isset( $meta_data['sizes'], $meta_data['sizes']['woocommerce_thumbnail'] ) ) {
103 $meta_data['sizes']['woocommerce_thumbnail']['uncropped'] = empty( $size_data['height'] );
104 }
105 return $meta_data;
106 }
107
108 /**
109 * See if an image's dimensions match actual settings.
110 *
111 * @param array $image Image dimensions array.
112 * @param string $size Named size.
113 * @return bool True if they match. False if they do not (may trigger regen).
114 */
115 protected static function image_size_matches_settings( $image, $size ) {
116 $target_size = wc_get_image_size( $size );
117 $uncropped = '' === $target_size['width'] || '' === $target_size['height'];
118
119 if ( ! $uncropped ) {
120 $ratio_match = wp_image_matches_ratio( $image['width'], $image['height'], $target_size['width'], $target_size['height'] );
121
122 // Size is invalid if the widths or crop setting don't match.
123 if ( $ratio_match && $target_size['width'] !== $image['width'] ) {
124 return false;
125 }
126
127 // Size is invalid if the heights don't match.
128 if ( $ratio_match && $target_size['height'] && $target_size['height'] !== $image['height'] ) {
129 return false;
130 }
131 }
132
133 // If cropping mode has changed, regenerate the image.
134 if ( $uncropped && empty( $image['uncropped'] ) ) {
135 return false;
136 }
137
138 return true;
139 }
140
141 /**
142 * Show notice when job is running in background.
143 */
144 public static function regenerating_notice() {
145 if ( ! self::$background_process->is_running() ) {
146 WC_Admin_Notices::add_notice( 'regenerating_thumbnails' );
147 } else {
148 WC_Admin_Notices::remove_notice( 'regenerating_thumbnails' );
149 }
150 }
151
152 /**
153 * Dismiss notice and cancel jobs.
154 */
155 public static function dismiss_regenerating_notice() {
156 if ( self::$background_process ) {
157 self::$background_process->kill_process();
158
159 $log = wc_get_logger();
160 $log->info(
161 __( 'Cancelled product image regeneration job.', 'woocommerce' ),
162 array(
163 'source' => 'wc-image-regeneration',
164 )
165 );
166 }
167 WC_Admin_Notices::remove_notice( 'regenerating_thumbnails' );
168 }
169
170 /**
171 * Regenerate images if the settings have changed since last re-generation.
172 *
173 * @return void
174 */
175 public static function maybe_regenerate_images() {
176 $size_hash = md5(
177 wp_json_encode(
178 array(
179 wc_get_image_size( 'thumbnail' ),
180 wc_get_image_size( 'single' ),
181 wc_get_image_size( 'gallery_thumbnail' ),
182 )
183 )
184 );
185
186 if ( update_option( 'woocommerce_maybe_regenerate_images_hash', $size_hash ) ) {
187 // Size settings have changed. Trigger regen.
188 self::queue_image_regeneration();
189 }
190 }
191
192 /**
193 * Check if we should maybe generate a new image size if not already there.
194 *
195 * @param array $image Properties of the image.
196 * @param int $attachment_id Attachment ID.
197 * @param string|array $size Image size.
198 * @param bool $icon If icon or not.
199 * @return array
200 */
201 public static function maybe_resize_image( $image, $attachment_id, $size, $icon ) {
202 if ( ! apply_filters( 'woocommerce_resize_images', true ) ) {
203 return $image;
204 }
205
206 // List of sizes we want to resize. Ignore others.
207 if ( ! $image || ! in_array( $size, apply_filters( 'woocommerce_image_sizes_to_resize', array( 'woocommerce_thumbnail', 'woocommerce_gallery_thumbnail', 'woocommerce_single' ) ), true ) ) {
208 return $image;
209 }
210
211 $target_size = wc_get_image_size( $size );
212 $image_width = $image[1];
213 $image_height = $image[2];
214 $ratio_match = false;
215 $target_uncropped = '' === $target_size['width'] || '' === $target_size['height'] || ! $target_size['crop'];
216
217 // If '' is passed to either size, we test ratios against the original file. It's uncropped.
218 if ( $target_uncropped ) {
219 $full_size = self::get_full_size_image_dimensions( $attachment_id );
220
221 if ( ! $full_size || ! $full_size['width'] || ! $full_size['height'] ) {
222 return $image;
223 }
224
225 $ratio_match = wp_image_matches_ratio( $image_width, $image_height, $full_size['width'], $full_size['height'] );
226 } else {
227 $ratio_match = wp_image_matches_ratio( $image_width, $image_height, $target_size['width'], $target_size['height'] );
228 }
229
230 if ( ! $ratio_match ) {
231 $full_size = self::get_full_size_image_dimensions( $attachment_id );
232
233 if ( ! $full_size ) {
234 return $image;
235 }
236
237 // Check if the actual image has a larger dimension than the requested image size. Smaller images are not zoom-cropped.
238 if ( $image_width === $target_size['width'] && $full_size['height'] < $target_size['height'] ) {
239 return $image;
240 }
241
242 if ( $image_height === $target_size['height'] && $full_size['width'] < $target_size['width'] ) {
243 return $image;
244 }
245
246 // If the full size image is smaller both ways, don't scale it up.
247 if ( $full_size['height'] < $target_size['height'] && $full_size['width'] < $target_size['width'] ) {
248 return $image;
249 }
250
251 return self::resize_and_return_image( $attachment_id, $image, $size, $icon );
252 }
253
254 return $image;
255 }
256
257 /**
258 * Get full size image dimensions.
259 *
260 * @param int $attachment_id Attachment ID of image.
261 * @return array Width and height. Empty array if the dimensions cannot be found.
262 */
263 private static function get_full_size_image_dimensions( $attachment_id ) {
264 $imagedata = wp_get_attachment_metadata( $attachment_id );
265
266 if ( ! is_array( $imagedata ) ) {
267 return array();
268 }
269
270 if ( ! isset( $imagedata['file'] ) && isset( $imagedata['sizes']['full'] ) ) {
271 $imagedata['height'] = $imagedata['sizes']['full']['height'];
272 $imagedata['width'] = $imagedata['sizes']['full']['width'];
273 }
274
275 // The result of the earlier wp_get_attachment_metadata call is filterable, so we may not have height or
276 // width data at this point.
277 if ( ! isset( $imagedata['height'] ) || ! isset( $imagedata['width'] ) ) {
278 return array();
279 }
280
281 return array(
282 'width' => $imagedata['width'],
283 'height' => $imagedata['height'],
284 );
285 }
286
287 /**
288 * Ensure we are dealing with the correct image attachment
289 *
290 * @param int|WP_Post $attachment Attachment object or ID.
291 * @return boolean
292 */
293 public static function is_regeneratable( $attachment ) {
294 if ( 'site-icon' === get_post_meta( is_object( $attachment ) ? $attachment->ID : $attachment, '_wp_attachment_context', true ) ) {
295 return false;
296 }
297
298 if ( wp_attachment_is_image( $attachment ) ) {
299 return true;
300 }
301
302 return false;
303 }
304
305 /**
306 * Only regenerate images for the requested size.
307 *
308 * @param array $sizes Array of image sizes.
309 * @return array
310 */
311 public static function adjust_intermediate_image_sizes( $sizes ) {
312 return array( self::$regenerate_size );
313 }
314
315 /**
316 * Generate the thumbnail filename and dimensions for a given file.
317 *
318 * @param string $fullsizepath Path to full size image.
319 * @param int $thumbnail_width The width of the thumbnail.
320 * @param int $thumbnail_height The height of the thumbnail.
321 * @param bool $crop Whether to crop or not.
322 * @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.
323 */
324 private static function get_image( $fullsizepath, $thumbnail_width, $thumbnail_height, $crop ) {
325 list( $fullsize_width, $fullsize_height ) = getimagesize( $fullsizepath );
326
327 $dimensions = image_resize_dimensions( $fullsize_width, $fullsize_height, $thumbnail_width, $thumbnail_height, $crop );
328 $editor = wp_get_image_editor( $fullsizepath );
329
330 if ( is_wp_error( $editor ) ) {
331 return false;
332 }
333
334 if ( ! $dimensions || ! is_array( $dimensions ) ) {
335 return false;
336 }
337
338 list( , , , , $dst_w, $dst_h ) = $dimensions;
339 $suffix = "{$dst_w}x{$dst_h}";
340 $file_ext = strtolower( pathinfo( $fullsizepath, PATHINFO_EXTENSION ) );
341
342 return array(
343 'filename' => $editor->generate_filename( $suffix, null, $file_ext ),
344 'width' => $dst_w,
345 'height' => $dst_h,
346 );
347 }
348
349 /**
350 * Regenerate the image according to the required size
351 *
352 * @param int $attachment_id Attachment ID.
353 * @param array $image Original Image.
354 * @param string $size Size to return for new URL.
355 * @param bool $icon If icon or not.
356 * @return string
357 */
358 private static function resize_and_return_image( $attachment_id, $image, $size, $icon ) {
359 if ( ! self::is_regeneratable( $attachment_id ) ) {
360 return $image;
361 }
362
363 $fullsizepath = get_attached_file( $attachment_id );
364
365 if ( false === $fullsizepath || is_wp_error( $fullsizepath ) || ! file_exists( $fullsizepath ) ) {
366 return $image;
367 }
368
369 if ( ! function_exists( 'wp_crop_image' ) ) {
370 include ABSPATH . 'wp-admin/includes/image.php';
371 }
372
373 self::$regenerate_size = is_customize_preview() ? $size . '_preview' : $size;
374
375 if ( is_customize_preview() ) {
376 $image_size = wc_get_image_size( $size );
377
378 // Make sure registered image size matches the size we're requesting.
379 add_image_size( self::$regenerate_size, absint( $image_size['width'] ), absint( $image_size['height'] ), $image_size['crop'] );
380
381 $thumbnail = self::get_image( $fullsizepath, absint( $image_size['width'] ), absint( $image_size['height'] ), $image_size['crop'] );
382
383 // If the file is already there perhaps just load it if we're using the customizer. No need to store in meta data.
384 if ( $thumbnail && file_exists( $thumbnail['filename'] ) ) {
385 $wp_uploads = wp_upload_dir( null, false );
386 $wp_uploads_dir = $wp_uploads['basedir'];
387 $wp_uploads_url = $wp_uploads['baseurl'];
388
389 return array(
390 0 => str_replace( $wp_uploads_dir, $wp_uploads_url, $thumbnail['filename'] ),
391 1 => $thumbnail['width'],
392 2 => $thumbnail['height'],
393 );
394 }
395 }
396
397 $metadata = wp_get_attachment_metadata( $attachment_id );
398
399 // Fix for images with no metadata.
400 if ( ! is_array( $metadata ) ) {
401 $metadata = array();
402 }
403
404 // We only want to regen a specific image size.
405 add_filter( 'intermediate_image_sizes', array( __CLASS__, 'adjust_intermediate_image_sizes' ) );
406
407 // This function will generate the new image sizes.
408 $new_metadata = wp_generate_attachment_metadata( $attachment_id, $fullsizepath );
409
410 // Remove custom filter.
411 remove_filter( 'intermediate_image_sizes', array( __CLASS__, 'adjust_intermediate_image_sizes' ) );
412
413 // If something went wrong lets just return the original image.
414 if ( is_wp_error( $new_metadata ) || empty( $new_metadata ) ) {
415 return $image;
416 }
417
418 if ( isset( $new_metadata['sizes'][ self::$regenerate_size ] ) ) {
419 $metadata['sizes'][ self::$regenerate_size ] = $new_metadata['sizes'][ self::$regenerate_size ];
420 wp_update_attachment_metadata( $attachment_id, $metadata );
421 }
422
423 // Now we've done our regen, attempt to return the new size.
424 $new_image = self::unfiltered_image_downsize( $attachment_id, self::$regenerate_size );
425
426 return $new_image ? $new_image : $image;
427 }
428
429 /**
430 * Image downsize, without this classes filtering on the results.
431 *
432 * @param int $attachment_id Attachment ID.
433 * @param string $size Size to downsize to.
434 * @return string New image URL.
435 */
436 private static function unfiltered_image_downsize( $attachment_id, $size ) {
437 remove_action( 'image_get_intermediate_size', array( __CLASS__, 'filter_image_get_intermediate_size' ), 10, 3 );
438
439 $return = image_downsize( $attachment_id, $size );
440
441 add_action( 'image_get_intermediate_size', array( __CLASS__, 'filter_image_get_intermediate_size' ), 10, 3 );
442
443 return $return;
444 }
445
446 /**
447 * Get list of images and queue them for regeneration
448 *
449 * @return void
450 */
451 public static function queue_image_regeneration() {
452 global $wpdb;
453 // First lets cancel existing running queue to avoid running it more than once.
454 self::$background_process->kill_process();
455
456 // Now lets find all product image attachments IDs and pop them onto the queue.
457 $images = $wpdb->get_results( // @codingStandardsIgnoreLine
458 "SELECT ID
459 FROM $wpdb->posts
460 WHERE post_type = 'attachment'
461 AND post_mime_type LIKE 'image/%'
462 ORDER BY ID DESC"
463 );
464 foreach ( $images as $image ) {
465 self::$background_process->push_to_queue(
466 array(
467 'attachment_id' => $image->ID,
468 )
469 );
470 }
471
472 // Lets dispatch the queue to start processing.
473 self::$background_process->save()->dispatch();
474 }
475 }
476
477 add_action( 'init', array( 'WC_Regenerate_Images', 'init' ) );
478