WC_Regenerate_Images_Request::task( mixed $item )

Code to execute for each item in the queue


Description Description


Parameters Parameters

$item

(Required) Queue item to iterate over.


Top ↑

Return Return

(bool)


Top ↑

Source Source

File: includes/class-wc-regenerate-images-request.php

	protected function task( $item ) {
		if ( ! is_array( $item ) && ! isset( $item['attachment_id'] ) ) {
			return false;
		}

		$this->attachment_id = absint( $item['attachment_id'] );
		$attachment          = get_post( $this->attachment_id );

		if ( ! $attachment || 'attachment' !== $attachment->post_type || ! $this->is_regeneratable( $attachment ) ) {
			return false;
		}

		if ( ! function_exists( 'wp_crop_image' ) ) {
			include ABSPATH . 'wp-admin/includes/image.php';
		}

		$log = wc_get_logger();

		$log->info(
			sprintf(
				// translators: %s: ID of the attachment.
				__( 'Regenerating images for attachment ID: %s', 'woocommerce' ),
				$this->attachment_id
			),
			array(
				'source' => 'wc-image-regeneration',
			)
		);

		$fullsizepath = get_attached_file( $this->attachment_id );

		// Check if the file exists, if not just remove item from queue.
		if ( false === $fullsizepath || is_wp_error( $fullsizepath ) || ! file_exists( $fullsizepath ) ) {
			return false;
		}

		$old_metadata = wp_get_attachment_metadata( $this->attachment_id );

		// We only want to regen WC images.
		add_filter( 'intermediate_image_sizes', array( $this, 'adjust_intermediate_image_sizes' ) );

		// We only want to resize images if they do not already exist.
		add_filter( 'intermediate_image_sizes_advanced', array( $this, 'filter_image_sizes_to_only_missing_thumbnails' ), 10, 3 );

		// This function will generate the new image sizes.
		$new_metadata = wp_generate_attachment_metadata( $this->attachment_id, $fullsizepath );

		// Remove custom filters.
		remove_filter( 'intermediate_image_sizes', array( $this, 'adjust_intermediate_image_sizes' ) );
		remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'filter_image_sizes_to_only_missing_thumbnails' ), 10, 3 );

		// If something went wrong lets just remove the item from the queue.
		if ( is_wp_error( $new_metadata ) || empty( $new_metadata ) ) {
			return false;
		}

		if ( ! empty( $old_metadata ) && ! empty( $old_metadata['sizes'] ) && is_array( $old_metadata['sizes'] ) ) {
			foreach ( $old_metadata['sizes'] as $old_size => $old_size_data ) {
				if ( empty( $new_metadata['sizes'][ $old_size ] ) ) {
					$new_metadata['sizes'][ $old_size ] = $old_metadata['sizes'][ $old_size ];
				}
			}
			// Handle legacy sizes.
			if ( isset( $new_metadata['sizes']['shop_thumbnail'], $new_metadata['sizes']['woocommerce_gallery_thumbnail'] ) ) {
				$new_metadata['sizes']['shop_thumbnail'] = $new_metadata['sizes']['woocommerce_gallery_thumbnail'];
			}
			if ( isset( $new_metadata['sizes']['shop_catalog'], $new_metadata['sizes']['woocommerce_thumbnail'] ) ) {
				$new_metadata['sizes']['shop_catalog'] = $new_metadata['sizes']['woocommerce_thumbnail'];
			}
			if ( isset( $new_metadata['sizes']['shop_single'], $new_metadata['sizes']['woocommerce_single'] ) ) {
				$new_metadata['sizes']['shop_single'] = $new_metadata['sizes']['woocommerce_single'];
			}
		}

		// Update the meta data with the new size values.
		wp_update_attachment_metadata( $this->attachment_id, $new_metadata );

		// We made it till the end, now lets remove the item from the queue.
		return false;
	}


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.