Geen omschrijving

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. $log->info(
  87. sprintf(
  88. // translators: %s: ID of the attachment.
  89. __( 'Regenerating images for attachment ID: %s', 'woocommerce' ),
  90. $this->attachment_id
  91. ),
  92. array(
  93. 'source' => 'wc-image-regeneration',
  94. )
  95. );
  96. $fullsizepath = get_attached_file( $this->attachment_id );
  97. // Check if the file exists, if not just remove item from queue.
  98. if ( false === $fullsizepath || is_wp_error( $fullsizepath ) || ! file_exists( $fullsizepath ) ) {
  99. return false;
  100. }
  101. $old_metadata = wp_get_attachment_metadata( $this->attachment_id );
  102. // We only want to regen WC images.
  103. add_filter( 'intermediate_image_sizes', array( $this, 'adjust_intermediate_image_sizes' ) );
  104. // We only want to resize images if they do not already exist.
  105. add_filter( 'intermediate_image_sizes_advanced', array( $this, 'filter_image_sizes_to_only_missing_thumbnails' ), 10, 3 );
  106. // This function will generate the new image sizes.
  107. $new_metadata = wp_generate_attachment_metadata( $this->attachment_id, $fullsizepath );
  108. // Remove custom filters.
  109. remove_filter( 'intermediate_image_sizes', array( $this, 'adjust_intermediate_image_sizes' ) );
  110. remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'filter_image_sizes_to_only_missing_thumbnails' ), 10, 3 );
  111. // If something went wrong lets just remove the item from the queue.
  112. if ( is_wp_error( $new_metadata ) || empty( $new_metadata ) ) {
  113. return false;
  114. }
  115. if ( ! empty( $old_metadata ) && ! empty( $old_metadata['sizes'] ) && is_array( $old_metadata['sizes'] ) ) {
  116. foreach ( $old_metadata['sizes'] as $old_size => $old_size_data ) {
  117. if ( empty( $new_metadata['sizes'][ $old_size ] ) ) {
  118. $new_metadata['sizes'][ $old_size ] = $old_metadata['sizes'][ $old_size ];
  119. }
  120. }
  121. // Handle legacy sizes.
  122. if ( isset( $new_metadata['sizes']['shop_thumbnail'], $new_metadata['sizes']['woocommerce_gallery_thumbnail'] ) ) {
  123. $new_metadata['sizes']['shop_thumbnail'] = $new_metadata['sizes']['woocommerce_gallery_thumbnail'];
  124. }
  125. if ( isset( $new_metadata['sizes']['shop_catalog'], $new_metadata['sizes']['woocommerce_thumbnail'] ) ) {
  126. $new_metadata['sizes']['shop_catalog'] = $new_metadata['sizes']['woocommerce_thumbnail'];
  127. }
  128. if ( isset( $new_metadata['sizes']['shop_single'], $new_metadata['sizes']['woocommerce_single'] ) ) {
  129. $new_metadata['sizes']['shop_single'] = $new_metadata['sizes']['woocommerce_single'];
  130. }
  131. }
  132. // Update the meta data with the new size values.
  133. wp_update_attachment_metadata( $this->attachment_id, $new_metadata );
  134. // We made it till the end, now lets remove the item from the queue.
  135. return false;
  136. }
  137. /**
  138. * Filters the list of thumbnail sizes to only include those which have missing files.
  139. *
  140. * @param array $sizes An associative array of registered thumbnail image sizes.
  141. * @param array $metadata An associative array of fullsize image metadata: width, height, file.
  142. * @param int $attachment_id Attachment ID. Only passed from WP 5.0+.
  143. * @return array An associative array of image sizes.
  144. */
  145. public function filter_image_sizes_to_only_missing_thumbnails( $sizes, $metadata, $attachment_id = null ) {
  146. $attachment_id = is_null( $attachment_id ) ? $this->attachment_id : $attachment_id;
  147. if ( ! $sizes || ! $attachment_id ) {
  148. return $sizes;
  149. }
  150. $fullsizepath = get_attached_file( $attachment_id );
  151. $editor = wp_get_image_editor( $fullsizepath );
  152. if ( is_wp_error( $editor ) ) {
  153. return $sizes;
  154. }
  155. $metadata = wp_get_attachment_metadata( $attachment_id );
  156. // This is based on WP_Image_Editor_GD::multi_resize() and others.
  157. foreach ( $sizes as $size => $size_data ) {
  158. if ( empty( $metadata['sizes'][ $size ] ) ) {
  159. continue;
  160. }
  161. if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
  162. continue;
  163. }
  164. if ( ! isset( $size_data['width'] ) ) {
  165. $size_data['width'] = null;
  166. }
  167. if ( ! isset( $size_data['height'] ) ) {
  168. $size_data['height'] = null;
  169. }
  170. if ( ! isset( $size_data['crop'] ) ) {
  171. $size_data['crop'] = false;
  172. }
  173. $image_sizes = getimagesize( $fullsizepath );
  174. if ( false === $image_sizes ) {
  175. continue;
  176. }
  177. list( $orig_w, $orig_h ) = $image_sizes;
  178. $dimensions = image_resize_dimensions( $orig_w, $orig_h, $size_data['width'], $size_data['height'], $size_data['crop'] );
  179. if ( ! $dimensions || ! is_array( $dimensions ) ) {
  180. continue;
  181. }
  182. $info = pathinfo( $fullsizepath );
  183. $ext = $info['extension'];
  184. $dst_w = $dimensions[4];
  185. $dst_h = $dimensions[5];
  186. $suffix = "{$dst_w}x{$dst_h}";
  187. $dst_rel_path = str_replace( '.' . $ext, '', $fullsizepath );
  188. $thumbnail = "{$dst_rel_path}-{$suffix}.{$ext}";
  189. if ( $dst_w === $metadata['sizes'][ $size ]['width'] && $dst_h === $metadata['sizes'][ $size ]['height'] && file_exists( $thumbnail ) ) {
  190. unset( $sizes[ $size ] );
  191. }
  192. }
  193. return $sizes;
  194. }
  195. /**
  196. * Returns the sizes we want to regenerate.
  197. *
  198. * @param array $sizes Sizes to generate.
  199. * @return array
  200. */
  201. public function adjust_intermediate_image_sizes( $sizes ) {
  202. // Prevent a filter loop.
  203. $unfiltered_sizes = array( 'woocommerce_thumbnail', 'woocommerce_gallery_thumbnail', 'woocommerce_single' );
  204. static $in_filter = false;
  205. if ( $in_filter ) {
  206. return $unfiltered_sizes;
  207. }
  208. $in_filter = true;
  209. $filtered_sizes = apply_filters( 'woocommerce_regenerate_images_intermediate_image_sizes', $unfiltered_sizes );
  210. $in_filter = false;
  211. return $filtered_sizes;
  212. }
  213. /**
  214. * This runs once the job has completed all items on the queue.
  215. *
  216. * @return void
  217. */
  218. protected function complete() {
  219. parent::complete();
  220. $log = wc_get_logger();
  221. $log->info(
  222. __( 'Completed product image regeneration job.', 'woocommerce' ),
  223. array(
  224. 'source' => 'wc-image-regeneration',
  225. )
  226. );
  227. }
  228. }