class-wc-regenerate-images-request.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * All functionality to regenerate images in the background when settings change.
  4. *
  5. * @package WooCommerce/Classes
  6. * @version 3.3.0
  7. * @since 3.3.0
  8. */
  9. defined( 'ABSPATH' ) || exit;
  10. if ( ! class_exists( 'WC_Background_Process', false ) ) {
  11. include_once dirname( __FILE__ ) . '/abstracts/class-wc-background-process.php';
  12. }
  13. /**
  14. * Class that extends WC_Background_Process to process image regeneration in the background.
  15. */
  16. class WC_Regenerate_Images_Request extends WC_Background_Process {
  17. /**
  18. * Stores the attachment ID being processed.
  19. *
  20. * @var integer
  21. */
  22. protected $attachment_id = 0;
  23. /**
  24. * Initiate new background process.
  25. */
  26. public function __construct() {
  27. // Uses unique prefix per blog so each blog has separate queue.
  28. $this->prefix = 'wp_' . get_current_blog_id();
  29. $this->action = 'wc_regenerate_images';
  30. // This is needed to prevent timeouts due to threading. See https://core.trac.wordpress.org/ticket/36534.
  31. @putenv( 'MAGICK_THREAD_LIMIT=1' ); // @codingStandardsIgnoreLine.
  32. parent::__construct();
  33. }
  34. /**
  35. * Is job running?
  36. *
  37. * @return boolean
  38. */
  39. public function is_running() {
  40. return $this->is_queue_empty();
  41. }
  42. /**
  43. * Limit each task ran per batch to 1 for image regen.
  44. *
  45. * @return bool
  46. */
  47. protected function batch_limit_exceeded() {
  48. return true;
  49. }
  50. /**
  51. * Determines whether an attachment can have its thumbnails regenerated.
  52. *
  53. * Adapted from Regenerate Thumbnails by Alex Mills.
  54. *
  55. * @param WP_Post $attachment An attachment's post object.
  56. * @return bool Whether the given attachment can have its thumbnails regenerated.
  57. */
  58. protected function is_regeneratable( $attachment ) {
  59. if ( 'site-icon' === get_post_meta( $attachment->ID, '_wp_attachment_context', true ) ) {
  60. return false;
  61. }
  62. if ( wp_attachment_is_image( $attachment ) ) {
  63. return true;
  64. }
  65. return false;
  66. }
  67. /**
  68. * Code to execute for each item in the queue
  69. *
  70. * @param mixed $item Queue item to iterate over.
  71. * @return bool
  72. */
  73. protected function task( $item ) {
  74. if ( ! is_array( $item ) && ! isset( $item['attachment_id'] ) ) {
  75. return false;
  76. }
  77. $this->attachment_id = absint( $item['attachment_id'] );
  78. $attachment = get_post( $this->attachment_id );
  79. if ( ! $attachment || 'attachment' !== $attachment->post_type || ! $this->is_regeneratable( $attachment ) ) {
  80. return false;
  81. }
  82. if ( ! function_exists( 'wp_crop_image' ) ) {
  83. include ABSPATH . 'wp-admin/includes/image.php';
  84. }
  85. $log = wc_get_logger();
  86. // translators: %s: ID of the attachment.
  87. $log->info( sprintf( __( 'Regenerating images for attachment ID: %s', 'woocommerce' ), $this->attachment_id ),
  88. array(
  89. 'source' => 'wc-image-regeneration',
  90. )
  91. );
  92. $fullsizepath = get_attached_file( $this->attachment_id );
  93. // Check if the file exists, if not just remove item from queue.
  94. if ( false === $fullsizepath || is_wp_error( $fullsizepath ) || ! file_exists( $fullsizepath ) ) {
  95. return false;
  96. }
  97. $old_metadata = wp_get_attachment_metadata( $this->attachment_id );
  98. // We only want to regen WC images.
  99. add_filter( 'intermediate_image_sizes', array( $this, 'adjust_intermediate_image_sizes' ) );
  100. // We only want to resize images if they do not already exist.
  101. add_filter( 'intermediate_image_sizes_advanced', array( $this, 'filter_image_sizes_to_only_missing_thumbnails' ), 10, 3 );
  102. // This function will generate the new image sizes.
  103. $new_metadata = wp_generate_attachment_metadata( $this->attachment_id, $fullsizepath );
  104. // Remove custom filters.
  105. remove_filter( 'intermediate_image_sizes', array( $this, 'adjust_intermediate_image_sizes' ) );
  106. remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'filter_image_sizes_to_only_missing_thumbnails' ), 10, 3 );
  107. // If something went wrong lets just remove the item from the queue.
  108. if ( is_wp_error( $new_metadata ) || empty( $new_metadata ) ) {
  109. return false;
  110. }
  111. if ( ! empty( $old_metadata ) && ! empty( $old_metadata['sizes'] ) && is_array( $old_metadata['sizes'] ) ) {
  112. foreach ( $old_metadata['sizes'] as $old_size => $old_size_data ) {
  113. if ( empty( $new_metadata['sizes'][ $old_size ] ) ) {
  114. $new_metadata['sizes'][ $old_size ] = $old_metadata['sizes'][ $old_size ];
  115. }
  116. }
  117. // Handle legacy sizes.
  118. if ( isset( $new_metadata['sizes']['shop_thumbnail'], $new_metadata['sizes']['woocommerce_gallery_thumbnail'] ) ) {
  119. $new_metadata['sizes']['shop_thumbnail'] = $new_metadata['sizes']['woocommerce_gallery_thumbnail'];
  120. }
  121. if ( isset( $new_metadata['sizes']['shop_catalog'], $new_metadata['sizes']['woocommerce_thumbnail'] ) ) {
  122. $new_metadata['sizes']['shop_catalog'] = $new_metadata['sizes']['woocommerce_thumbnail'];
  123. }
  124. if ( isset( $new_metadata['sizes']['shop_single'], $new_metadata['sizes']['woocommerce_single'] ) ) {
  125. $new_metadata['sizes']['shop_single'] = $new_metadata['sizes']['woocommerce_single'];
  126. }
  127. }
  128. // Update the meta data with the new size values.
  129. wp_update_attachment_metadata( $this->attachment_id, $new_metadata );
  130. // We made it till the end, now lets remove the item from the queue.
  131. return false;
  132. }
  133. /**
  134. * Filters the list of thumbnail sizes to only include those which have missing files.
  135. *
  136. * @param array $sizes An associative array of registered thumbnail image sizes.
  137. * @param array $metadata An associative array of fullsize image metadata: width, height, file.
  138. * @param int $attachment_id Attachment ID. Only passed from WP 5.0+.
  139. * @return array An associative array of image sizes.
  140. */
  141. public function filter_image_sizes_to_only_missing_thumbnails( $sizes, $metadata, $attachment_id = null ) {
  142. $attachment_id = is_null( $attachment_id ) ? $this->attachment_id : $attachment_id;
  143. if ( ! $sizes || ! $attachment_id ) {
  144. return $sizes;
  145. }
  146. $fullsizepath = get_attached_file( $attachment_id );
  147. $editor = wp_get_image_editor( $fullsizepath );
  148. if ( is_wp_error( $editor ) ) {
  149. return $sizes;
  150. }
  151. $metadata = wp_get_attachment_metadata( $attachment_id );
  152. // This is based on WP_Image_Editor_GD::multi_resize() and others.
  153. foreach ( $sizes as $size => $size_data ) {
  154. if ( empty( $metadata['sizes'][ $size ] ) ) {
  155. continue;
  156. }
  157. if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
  158. continue;
  159. }
  160. if ( ! isset( $size_data['width'] ) ) {
  161. $size_data['width'] = null;
  162. }
  163. if ( ! isset( $size_data['height'] ) ) {
  164. $size_data['height'] = null;
  165. }
  166. if ( ! isset( $size_data['crop'] ) ) {
  167. $size_data['crop'] = false;
  168. }
  169. list( $orig_w, $orig_h ) = getimagesize( $fullsizepath );
  170. $dimensions = image_resize_dimensions( $orig_w, $orig_h, $size_data['width'], $size_data['height'], $size_data['crop'] );
  171. if ( ! $dimensions || ! is_array( $dimensions ) ) {
  172. continue;
  173. }
  174. $info = pathinfo( $fullsizepath );
  175. $ext = $info['extension'];
  176. $dst_w = $dimensions[4];
  177. $dst_h = $dimensions[5];
  178. $suffix = "{$dst_w}x{$dst_h}";
  179. $dst_rel_path = str_replace( '.' . $ext, '', $fullsizepath );
  180. $thumbnail = "{$dst_rel_path}-{$suffix}.{$ext}";
  181. if ( $dst_w === $metadata['sizes'][ $size ]['width'] && $dst_h === $metadata['sizes'][ $size ]['height'] && file_exists( $thumbnail ) ) {
  182. unset( $sizes[ $size ] );
  183. }
  184. }
  185. return $sizes;
  186. }
  187. /**
  188. * Returns the sizes we want to regenerate.
  189. *
  190. * @param array $sizes Sizes to generate.
  191. * @return array
  192. */
  193. public function adjust_intermediate_image_sizes( $sizes ) {
  194. return apply_filters( 'woocommerce_regenerate_images_intermediate_image_sizes', array( 'woocommerce_thumbnail', 'woocommerce_gallery_thumbnail', 'woocommerce_single' ) );
  195. }
  196. /**
  197. * This runs once the job has completed all items on the queue.
  198. *
  199. * @return void
  200. */
  201. protected function complete() {
  202. parent::complete();
  203. $log = wc_get_logger();
  204. $log->info( __( 'Completed product image regeneration job.', 'woocommerce' ),
  205. array(
  206. 'source' => 'wc-image-regeneration',
  207. )
  208. );
  209. }
  210. }