get_parent_id()); if ($parent instanceof WC_Product) { $base_product = $parent; } } // Use the same unfiltered storage membership as the product DTO so binary // owner uploads cannot diverge from canonical featured/gallery ownership. if (function_exists('vasp_wc_catalog_ordered_image_ids') && function_exists('get_post_meta')) { $vasp_product_id = (int) $product->get_id(); $vasp_base_id = (int) $base_product->get_id(); $vasp_meta = []; foreach (array_unique([$vasp_product_id, $vasp_base_id]) as $vasp_id) { $vasp_meta[$vasp_id] = [ '_thumbnail_id' => get_post_meta($vasp_id, '_thumbnail_id', true), '_product_image_gallery' => get_post_meta($vasp_id, '_product_image_gallery', true), ]; } return vasp_wc_catalog_ordered_image_ids($vasp_product_id, $vasp_base_id, $vasp_meta); } $primary_id = (int) $product->get_image_id(); if ($primary_id <= 0 && $product->get_id() !== $base_product->get_id()) { $primary_id = (int) $base_product->get_image_id(); } $vasp_image_ids = []; if ($primary_id > 0) { $vasp_image_ids[] = $primary_id; } foreach ((array) $base_product->get_gallery_image_ids() as $vasp_gallery_id) { $vasp_gallery_id = (int) $vasp_gallery_id; if ($vasp_gallery_id > 0 && !in_array($vasp_gallery_id, $vasp_image_ids, true)) { $vasp_image_ids[] = $vasp_gallery_id; } } // Preserve WooCommerce source order: featured image first, then gallery. return $vasp_image_ids; } /** Read the registered WooCommerce thumbnail when WordPress has generated it. */ function vasp_wc_image_sync_thumbnail_bytes(int $vasp_attachment, string $vasp_source_path): ?array { if (!function_exists('image_get_intermediate_size')) { return null; } $vasp_thumbnail = image_get_intermediate_size($vasp_attachment, 'woocommerce_thumbnail'); if (!is_array($vasp_thumbnail) || empty($vasp_thumbnail['file'])) { return null; } $vasp_path = $vasp_source_path !== '' ? trailingslashit(dirname($vasp_source_path)) . basename((string) $vasp_thumbnail['file']) : ''; $vasp_max = 10 * 1024 * 1024; if ($vasp_path !== '' && is_file($vasp_path) && is_readable($vasp_path)) { $vasp_bytes = file_get_contents($vasp_path); $vasp_type = function_exists('wp_check_filetype') ? (string) (wp_check_filetype($vasp_path)['type'] ?? '') : ''; } else { $vasp_url = (string) ($vasp_thumbnail['url'] ?? ''); if ($vasp_url === '') { return null; } $vasp_response = wp_safe_remote_get($vasp_url, [ 'timeout' => 30, 'redirection' => 3, 'limit_response_size' => $vasp_max + 1, ]); if (is_wp_error($vasp_response) || (int) wp_remote_retrieve_response_code($vasp_response) !== 200) { return null; } $vasp_bytes = wp_remote_retrieve_body($vasp_response); $vasp_type = (string) wp_remote_retrieve_header($vasp_response, 'content-type'); } if (!is_string($vasp_bytes) || $vasp_bytes === '' || strlen($vasp_bytes) > $vasp_max) { return null; } $vasp_type = trim(explode(';', $vasp_type, 2)[0]); return [ 'bytes' => $vasp_bytes, 'content_type' => strpos($vasp_type, 'image/') === 0 ? $vasp_type : '', 'path' => $vasp_path !== '' && is_file($vasp_path) ? $vasp_path : '', ]; } /** Reject thumbnail payloads that cannot describe a useful rendered image. */ function vasp_wc_image_sync_bytes_are_usable(string $vasp_bytes, string $vasp_path = ''): bool { $vasp_dimensions = $vasp_path !== '' && function_exists('wp_getimagesize') ? wp_getimagesize($vasp_path) : @getimagesizefromstring($vasp_bytes); if ((!is_array($vasp_dimensions) || (int) ($vasp_dimensions[0] ?? 0) <= 1 || (int) ($vasp_dimensions[1] ?? 0) <= 1) && $vasp_path === '' && function_exists('wp_getimagesize') && function_exists('wp_tempnam')) { // WordPress recognizes modern formats (notably AVIF) on PHP versions // where getimagesizefromstring() reports 0x0 or cannot decode them. $vasp_temporary_image = wp_tempnam('valuespace-image-validation'); if (is_string($vasp_temporary_image) && $vasp_temporary_image !== '') { if (file_put_contents($vasp_temporary_image, $vasp_bytes) !== false) { $vasp_dimensions = wp_getimagesize($vasp_temporary_image); } wp_delete_file($vasp_temporary_image); } } return is_array($vasp_dimensions) && (int) ($vasp_dimensions[0] ?? 0) > 1 && (int) ($vasp_dimensions[1] ?? 0) > 1; } /** Preserve the source format while producing a WooCommerce thumbnail for transport. */ function vasp_wc_image_sync_prepare_upload_bytes( int $vasp_attachment, string $vasp_source_path, string $vasp_original_bytes, string $vasp_content_type, string $vasp_source_file = '' ) { $vasp_thumbnail = vasp_wc_image_sync_thumbnail_bytes($vasp_attachment, $vasp_source_path); if (is_array($vasp_thumbnail)) { if (vasp_wc_image_sync_bytes_are_usable( $vasp_thumbnail['bytes'], (string) ($vasp_thumbnail['path'] ?? '') )) { return [ 'bytes' => $vasp_thumbnail['bytes'], 'content_type' => $vasp_thumbnail['content_type'] !== '' ? $vasp_thumbnail['content_type'] : $vasp_content_type, ]; } if (vasp_wc_image_sync_bytes_are_usable($vasp_original_bytes)) { return [ 'bytes' => $vasp_original_bytes, 'content_type' => $vasp_content_type, ]; } return vasp_wc_catalog_storage_error('image_source_read_failed', [ 'error_code' => 'image_source_read_failed', 'step' => 'image_download', 'image_file' => $vasp_source_file, ]); } $vasp_size = function_exists('wc_get_image_size') ? wc_get_image_size('thumbnail') : []; $vasp_width = max(1, (int) ($vasp_size['width'] ?? 300)); $vasp_height = max(1, (int) ($vasp_size['height'] ?? 300)); $vasp_crop = !empty($vasp_size['crop']); $vasp_dimensions = @getimagesizefromstring($vasp_original_bytes); if (!is_array($vasp_dimensions) || ((int) $vasp_dimensions[0] <= $vasp_width && (int) $vasp_dimensions[1] <= $vasp_height)) { return ['bytes' => $vasp_original_bytes, 'content_type' => $vasp_content_type]; } $vasp_temporary_source = ''; if ($vasp_source_path === '') { $vasp_temporary_source = wp_tempnam('valuespace-image-' . $vasp_attachment); if (!is_string($vasp_temporary_source) || $vasp_temporary_source === '' || file_put_contents($vasp_temporary_source, $vasp_original_bytes) === false) { return ['bytes' => $vasp_original_bytes, 'content_type' => $vasp_content_type]; } $vasp_source_path = $vasp_temporary_source; } $vasp_editor = wp_get_image_editor($vasp_source_path); if (is_wp_error($vasp_editor)) { if ($vasp_temporary_source !== '') { wp_delete_file($vasp_temporary_source); } return ['bytes' => $vasp_original_bytes, 'content_type' => $vasp_content_type]; } $vasp_resized = $vasp_editor->resize($vasp_width, $vasp_height, $vasp_crop); $vasp_output_path = $vasp_editor->generate_filename('valuespace'); $vasp_saved = is_wp_error($vasp_resized) ? $vasp_resized : $vasp_editor->save($vasp_output_path, $vasp_content_type); if (is_wp_error($vasp_saved) || !is_array($vasp_saved) || empty($vasp_saved['path'])) { if ($vasp_temporary_source !== '') { wp_delete_file($vasp_temporary_source); } if (is_string($vasp_output_path) && is_file($vasp_output_path)) { wp_delete_file($vasp_output_path); } return ['bytes' => $vasp_original_bytes, 'content_type' => $vasp_content_type]; } $vasp_upload_bytes = file_get_contents($vasp_saved['path']); $vasp_upload_usable = is_string($vasp_upload_bytes) && vasp_wc_image_sync_bytes_are_usable($vasp_upload_bytes, $vasp_saved['path']); wp_delete_file($vasp_saved['path']); if ($vasp_temporary_source !== '') { wp_delete_file($vasp_temporary_source); } if (!$vasp_upload_usable) { if (!vasp_wc_image_sync_bytes_are_usable($vasp_original_bytes)) { return vasp_wc_catalog_storage_error('image_source_read_failed', [ 'error_code' => 'image_source_read_failed', 'step' => 'image_download', 'image_file' => $vasp_source_file, ]); } return ['bytes' => $vasp_original_bytes, 'content_type' => $vasp_content_type]; } if (strlen($vasp_upload_bytes) >= strlen($vasp_original_bytes) && vasp_wc_image_sync_bytes_are_usable($vasp_original_bytes)) { return ['bytes' => $vasp_original_bytes, 'content_type' => $vasp_content_type]; } $vasp_upload_type = is_string($vasp_saved['mime-type'] ?? null) ? sanitize_mime_type($vasp_saved['mime-type']) : $vasp_content_type; vasp_log('WC Catalog Sync: image prepared for upload', 'debug', [ 'image_id' => $vasp_attachment, 'source_bytes' => strlen($vasp_original_bytes), 'upload_bytes' => strlen($vasp_upload_bytes), ]); return ['bytes' => $vasp_upload_bytes, 'content_type' => $vasp_upload_type]; } /** Local bytes are authoritative; offloaded media uses the safe bounded URL reader. */ function vasp_wc_image_sync_source_file(string $vasp_path, string $vasp_url = ''): string { $vasp_candidate = $vasp_path !== '' ? $vasp_path : (string) wp_parse_url($vasp_url, PHP_URL_PATH); return sanitize_text_field(substr(rawurldecode(basename(str_replace('\\', '/', $vasp_candidate))), 0, 180)); } function vasp_wc_image_sync_prepare_source( string $vasp_entity_type, string $vasp_entity_id, string $vasp_image_id ) { $vasp_attachment = absint($vasp_image_id); $vasp_path = get_attached_file($vasp_attachment); $vasp_url = function_exists('vasp_wc_catalog_image_url') ? vasp_wc_catalog_image_url($vasp_attachment) : wp_get_attachment_url($vasp_attachment); $vasp_content_type = get_post_mime_type($vasp_attachment); $vasp_max = 10 * 1024 * 1024; $vasp_source_max = max($vasp_max, function_exists('wp_max_upload_size') ? (int) wp_max_upload_size() : $vasp_max); $vasp_local_path = ''; $vasp_source_file = vasp_wc_image_sync_source_file( is_string($vasp_path) ? $vasp_path : '', is_string($vasp_url) ? $vasp_url : '' ); if (!is_string($vasp_url) || $vasp_url === '') { if ($vasp_source_file === '') { /* translators: %d: WordPress Media Library attachment ID. */ $vasp_source_file = sprintf(__('Media item %d', 'valuespace'), $vasp_attachment); } return vasp_wc_catalog_storage_error('image_source_missing', [ 'error_code' => 'image_source_missing', 'step' => 'image_download', 'image_file' => $vasp_source_file, ]); } if (is_string($vasp_path) && $vasp_path !== '' && is_file($vasp_path) && is_readable($vasp_path)) { if (filesize($vasp_path) > $vasp_source_max) { return vasp_wc_catalog_storage_error('image_source_too_large', [ 'error_code' => 'image_source_too_large', 'step' => 'image_download', 'image_file' => $vasp_source_file, ]); } $vasp_bytes = file_get_contents($vasp_path); if ($vasp_bytes === false || $vasp_bytes === '') { return vasp_wc_catalog_storage_error('image_source_read_failed', [ 'error_code' => 'image_source_read_failed', 'step' => 'image_download', 'image_file' => $vasp_source_file, ]); } $vasp_local_path = $vasp_path; } else { if ($vasp_source_file === '') { $vasp_source_file = vasp_wc_image_sync_source_file('', $vasp_url); } $vasp_response = wp_safe_remote_get($vasp_url, [ 'timeout' => 30, 'redirection' => 3, 'limit_response_size' => $vasp_source_max + 1, ]); if (is_wp_error($vasp_response) || (int) wp_remote_retrieve_response_code($vasp_response) !== 200) { // A source429 is not destination capacity and requires explicit retry. return vasp_wc_catalog_storage_error('image_source_download_failed', [ 'error_code' => 'image_source_download_failed', 'status' => is_wp_error($vasp_response) ? 0 : (int) wp_remote_retrieve_response_code($vasp_response), 'step' => 'image_download', 'image_file' => $vasp_source_file, ]); } $vasp_bytes = wp_remote_retrieve_body($vasp_response); if ($vasp_bytes === '' || strlen($vasp_bytes) > $vasp_source_max) { return vasp_wc_catalog_storage_error('image_source_too_large', [ 'error_code' => 'image_source_too_large', 'step' => 'image_download', 'image_file' => $vasp_source_file, ]); } $vasp_response_content_type = wp_remote_retrieve_header($vasp_response, 'content-type'); if (is_string($vasp_response_content_type) && $vasp_response_content_type !== '') { $vasp_content_type = $vasp_response_content_type; } } $vasp_content_type = is_string($vasp_content_type) ? trim(explode(';', $vasp_content_type, 2)[0]) : ''; $vasp_content_type = strpos(strtolower($vasp_content_type), 'image/') === 0 ? sanitize_mime_type($vasp_content_type) : 'application/octet-stream'; // Inspection and reception must describe the exact bounded bytes selected // for transport so thumbnail repair is visible even when the original is unchanged. $vasp_upload = vasp_wc_image_sync_prepare_upload_bytes( $vasp_attachment, $vasp_local_path, $vasp_bytes, $vasp_content_type, $vasp_source_file ); if (is_wp_error($vasp_upload)) { return $vasp_upload; } if (strlen($vasp_upload['bytes']) > $vasp_max) { return vasp_wc_catalog_storage_error('image_source_too_large', [ 'error_code' => 'image_source_too_large', 'step' => 'images', 'image_file' => $vasp_source_file, ]); } return [ 'bytes' => $vasp_upload['bytes'], 'hash' => hash('sha256', $vasp_upload['bytes']), 'content_type' => $vasp_upload['content_type'], ]; } /** Prepare one bounded wave; unreadable WordPress files are reported and skipped. */ function vasp_wc_image_sync_prepare_records( array $vasp_records, array &$vasp_source_failures = [] ): array { $vasp_prepared = []; $vasp_sources = []; $vasp_source_failures = []; foreach ($vasp_records as $vasp_record) { $vasp_image_id = (string) $vasp_record['imageId']; if (!isset($vasp_sources[$vasp_image_id])) { $vasp_sources[$vasp_image_id] = vasp_wc_image_sync_prepare_source( $vasp_record['entityType'], $vasp_record['entityId'], $vasp_image_id ); } if (is_wp_error($vasp_sources[$vasp_image_id])) { $vasp_error = $vasp_sources[$vasp_image_id]; $vasp_source_failures[] = [ 'record' => $vasp_record, 'error' => $vasp_error, ]; continue; } $vasp_prepared[] = ['record' => $vasp_record] + $vasp_sources[$vasp_image_id]; } return $vasp_prepared; } /** Every submitted binary requires direct canonical integration acknowledgement. */ function vasp_wc_image_sync_upload_prepared( array $vasp_prepared, array &$vasp_acknowledgements = [], array &$vasp_rejections = [] ) { $vasp_acknowledgements = []; $vasp_rejections = []; $vasp_offset = 0; foreach (array_chunk($vasp_prepared, VASP_WC_IMAGE_SYNC_CONCURRENCY) as $vasp_wave) { $vasp_requests = []; foreach ($vasp_wave as $vasp_item) { $vasp_requests[] = [ 'endpoint' => 'ecommerce_catalog_images_endpoint', 'method' => 'POST', 'data' => $vasp_item['bytes'], 'params' => $vasp_item['record'], 'content_type' => $vasp_item['content_type'], ]; } vasp_log('WC Catalog Sync: sending image wave', 'debug', [ 'item_count' => count($vasp_requests), 'concurrency' => min(count($vasp_requests), VASP_WC_IMAGE_SYNC_CONCURRENCY), ]); $vasp_acknowledged_before = count($vasp_acknowledgements); $vasp_responses = vasp_api_request_parallel($vasp_requests, VASP_WC_IMAGE_SYNC_CONCURRENCY); $vasp_system_failure = null; $vasp_capacity_failure = null; foreach ($vasp_wave as $vasp_index => $vasp_item) { $vasp_received = vasp_wc_catalog_received_at( $vasp_responses[$vasp_index] ?? null, 1, 'ecommerce_catalog_images_endpoint' ); if (is_wp_error($vasp_received)) { if (vasp_wc_catalog_failure_is_retryable_delivery($vasp_received)) { $vasp_capacity_failure = $vasp_capacity_failure ?? $vasp_received; } else { $vasp_data = $vasp_received->get_error_data(); $vasp_status = is_array($vasp_data) ? (int) ($vasp_data['status'] ?? 0) : 0; if ($vasp_status >= 400 && $vasp_status < 500 && !in_array($vasp_status, [401, 403, 408], true)) { $vasp_rejections[$vasp_offset + $vasp_index] = true; vasp_log('WC Catalog Sync: product image was not integrated; remaining images continue', 'warning', [ 'entity_type' => $vasp_item['record']['entityType'], 'entity_id' => $vasp_item['record']['entityId'], 'image_id' => $vasp_item['record']['imageId'], 'http_status' => $vasp_status, ]); } else { $vasp_system_failure = $vasp_system_failure ?? $vasp_received; } } } else { $vasp_acknowledgements[$vasp_offset + $vasp_index] = $vasp_received; } } vasp_log('WC Catalog Sync: image wave received', 'debug', [ 'accepted_count' => count($vasp_acknowledgements) - $vasp_acknowledged_before, 'item_count' => count($vasp_requests), 'capacity_limited' => $vasp_capacity_failure !== null, ]); if ($vasp_capacity_failure !== null) { return $vasp_capacity_failure; } if ($vasp_system_failure !== null) { return $vasp_system_failure; } $vasp_offset += count($vasp_wave); } return true; }