WC_Regenerate_Images_Request::task()protectedWC 1.0

Code to execute for each item in the queue

Метод класса: WC_Regenerate_Images_Request{}

Хуков нет.

Возвращает

true|false.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->task( $item );
$item(разное) (обязательный)
Queue item to iterate over.

Код WC_Regenerate_Images_Request::task() WC 8.7.0

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 ];
			}
		}
	}

	// 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;
}