query($wpdb->prepare( "INSERT INTO %i (option_name,option_value,autoload) VALUES (%s,%s,'no') ON DUPLICATE KEY UPDATE option_value=CASE WHEN (option_value='realtime_categories' AND VALUES(option_value)='realtime_brands') OR (option_value='realtime_brands' AND VALUES(option_value)='realtime_categories') THEN 'realtime_taxonomies' WHEN FIELD(VALUES(option_value),'realtime','realtime_categories','realtime_brands','realtime_taxonomies','manual','nightly')> FIELD(option_value,'realtime','realtime_categories','realtime_brands','realtime_taxonomies','manual','nightly') THEN VALUES(option_value) ELSE option_value END", $wpdb->options, VASP_OPTION_WC_CATALOG_PENDING_MODE, $vasp_mode )); wp_cache_delete(VASP_OPTION_WC_CATALOG_PENDING_MODE, 'options'); wp_cache_delete('notoptions', 'options'); return $vasp_saved !== false; } /** Queue only operation metadata; product work lives in the coalescing queue. */ function vasp_wc_catalog_queue(string $vasp_mode = 'manual', bool $vasp_restart = false) { if (!vasp_wc_catalog_storage_ready()) { return vasp_wc_catalog_fail(vasp_wc_catalog_storage_error('storage_not_ready')); } $vasp_mode = in_array($vasp_mode, [ 'manual', 'nightly', 'realtime', 'realtime_categories', 'realtime_brands', 'realtime_taxonomies', ], true) ? $vasp_mode : 'manual'; $vasp_new_run = $vasp_restart || !vasp_wc_catalog_active(); // Promote at the unique option row, never from a stale request-local read. if (!vasp_wc_catalog_promote_pending_mode($vasp_mode)) { return vasp_wc_catalog_fail(vasp_wc_catalog_storage_error('pending_mode_not_saved')); } if ($vasp_new_run) { $vasp_pending_mode = (string) get_option(VASP_OPTION_WC_CATALOG_PENDING_MODE, $vasp_mode); // A new run owns a new projection even if the previous worker still holds // the service lease. Full starts announce now; realtime waits for its worker. update_option(VASP_OPTION_WC_CATALOG_STEP, vasp_wc_catalog_step_for_mode($vasp_pending_mode), false); update_option(VASP_OPTION_WC_CATALOG_STATUS, 'pending', false); update_option(VASP_OPTION_WC_CATALOG_PROGRESS, [ 'processed' => 0, 'trigger' => $vasp_pending_mode === 'nightly' ? 'nightly' : 'realtime', 'productScanProcessed' => 0, 'productScanTotal' => 0, 'imageScanProcessed' => 0, 'imageScanTotal' => 0, 'imageSourceIssueCount' => 0, 'fullInspectionMode' => vasp_wc_catalog_mode_is_full_inspection($vasp_pending_mode), ], false); } if (!vasp_scheduler_request_service('woocommerce_catalog', true)) { return vasp_wc_catalog_fail(vasp_wc_catalog_storage_error('catalog_schedule_failed')); } return vasp_wc_catalog_push_in_progress_response(); } /** Consume only the observed value; a concurrent promotion remains pending. */ function vasp_wc_catalog_take_pending_mode() { global $wpdb; $vasp_mode = $wpdb->get_var($wpdb->prepare( "SELECT option_value FROM %i WHERE option_name=%s", $wpdb->options, VASP_OPTION_WC_CATALOG_PENDING_MODE )); if ($wpdb->last_error !== '') { return vasp_wc_catalog_storage_error('pending_mode_read_failed'); } if ($vasp_mode === null) { return ''; } $vasp_deleted = $wpdb->query($wpdb->prepare( "DELETE FROM %i WHERE option_name=%s AND option_value=%s", $wpdb->options, VASP_OPTION_WC_CATALOG_PENDING_MODE, $vasp_mode )); wp_cache_delete(VASP_OPTION_WC_CATALOG_PENDING_MODE, 'options'); wp_cache_delete('notoptions', 'options'); if ($vasp_deleted === false) { return vasp_wc_catalog_storage_error('pending_mode_not_consumed'); } if ($vasp_deleted !== 1) { return ''; } vasp_wc_catalog_set_active_trigger((string) $vasp_mode); return (string) $vasp_mode; } function vasp_wc_catalog_sync_start() { if (!vasp_woocommerce_sync_is_available()) { return vasp_create_error(VASP_ERROR_DEPENDENCY_NOT_MET, [ 'user_message' => __('Activate WooCommerce before starting a catalog update.', 'valuespace'), ]); } vasp_wc_catalog_enable_push_mode(); $vasp_result = vasp_wc_catalog_queue('manual', true); vasp_reset_vasp_status_request_cache(); return $vasp_result; } function vasp_handle_restart_wc_catalog_sync(): void { if (!current_user_can('manage_options')) { wp_die(esc_html__('Insufficient permissions', 'valuespace')); } check_admin_referer('vasp_restart_wc_catalog_sync_nonce'); vasp_log('WC Catalog Sync: manual start requested from System Info', 'debug'); $result = vasp_wc_catalog_sync_start(); if (is_wp_error($result)) { vasp_queue_admin_notice($result->get_error_message(), 'warning'); } $redirect_url = isset($_GET['from']) && $_GET['from'] === 'info' ? admin_url('admin.php?page=valuespace-info') : admin_url('admin.php?page=valuespace'); wp_safe_redirect($redirect_url); exit; } function vasp_wc_catalog_disable_push_mode() { // WordPress owns the setting, so local delivery stops without waiting for // Gateway. A later status read retries the server update if needed. vasp_wc_catalog_shutdown('data_sync_disable'); $vasp_updated = vasp_update_catalog_data_sync_configuration(false); $vasp_result = [ 'status' => 'disabled', 'pipeline' => 'push', 'message' => __('WooCommerce catalog updates are off.', 'valuespace'), ]; if (!$vasp_updated) { $vasp_result['warningMessage'] = __('WooCommerce catalog updates are off.', 'valuespace'); $vasp_result['warningDescription'] = __('The ValueSpace setting could not be updated and will be aligned automatically.', 'valuespace'); } return $vasp_result; } function vasp_wc_catalog_shutdown(string $vasp_context): void { delete_option(VASP_OPTION_WC_CATALOG_PUSH_ENABLED); update_option(VASP_OPTION_WC_CATALOG_STATUS, 'disabled', false); delete_option(VASP_OPTION_WC_CATALOG_PENDING_MODE); delete_option(VASP_OPTION_WC_CATALOG_STEP); delete_option(VASP_OPTION_WC_CATALOG_LAST_ERROR); vasp_scheduler_cancel_service('woocommerce_catalog'); vasp_scheduler_cancel_service('woocommerce_images'); vasp_wc_catalog_unregister_realtime_hooks(); vasp_wc_catalog_realtime_collector_store(null, true); // Accepted baselines and pending facts survive ordinary Data Sync disable. vasp_log('WC Catalog Sync: local delivery disabled', 'info', ['context' => $vasp_context]); } function vasp_wc_catalog_handle_vasp_disconnect(): void { vasp_wc_catalog_shutdown('valuespace_disconnect'); } /** Return the actionable item-level image issue represented by an error. */ function vasp_wc_catalog_image_source_issue_code(WP_Error $vasp_error): string { $vasp_data = $vasp_error->get_error_data(); $vasp_data = is_array($vasp_data) ? $vasp_data : []; $vasp_code = sanitize_key((string) ($vasp_data['error_code'] ?? $vasp_data['reason'] ?? '')); if ($vasp_code === 'image_source_download_failed' && in_array((int) ($vasp_data['status'] ?? 0), [404, 410], true)) { return 'image_source_missing'; } return vasp_is_catalog_image_source_error($vasp_code) ? $vasp_code : ''; } /** Retain bounded file facts without failing the image feed. */ function vasp_wc_catalog_warn_image_sources(array $vasp_failures): void { $vasp_existing_raw = get_option(VASP_OPTION_WC_CATALOG_LAST_ERROR, ''); $vasp_existing = is_string($vasp_existing_raw) ? json_decode($vasp_existing_raw, true) : null; $vasp_existing_code = is_array($vasp_existing) ? sanitize_key((string) ($vasp_existing['technical_code'] ?? '')) : ''; if ($vasp_existing_code !== '' && !vasp_is_catalog_image_source_error($vasp_existing_code)) { return; } $vasp_code = ''; $vasp_details = []; $vasp_existing_details = is_array($vasp_existing['error_details'] ?? null) ? $vasp_existing['error_details'] : []; $vasp_files = array_merge( [(string) ($vasp_existing_details['image_file'] ?? '')], is_array($vasp_existing_details['image_files'] ?? null) ? $vasp_existing_details['image_files'] : [] ); $vasp_product_ids = is_array($vasp_existing_details['product_ids'] ?? null) ? $vasp_existing_details['product_ids'] : []; $vasp_image_ids = is_array($vasp_existing_details['image_ids'] ?? null) ? $vasp_existing_details['image_ids'] : []; foreach ($vasp_failures as $vasp_failure) { $vasp_error = $vasp_failure['error'] ?? null; if (!$vasp_error instanceof WP_Error) { continue; } $vasp_failure_code = vasp_wc_catalog_image_source_issue_code($vasp_error); if ($vasp_failure_code === '') { continue; } if ($vasp_code === '') { $vasp_code = $vasp_failure_code; } if ($vasp_failure_code !== $vasp_code) { continue; } $vasp_item_details = function_exists('vasp_scheduler_error_details') ? vasp_scheduler_error_details($vasp_error) : (is_array($vasp_error->get_error_data()) ? $vasp_error->get_error_data() : []); if (!$vasp_details) { $vasp_details = $vasp_item_details; } if (!empty($vasp_item_details['image_file'])) { $vasp_files[] = $vasp_item_details['image_file']; } $vasp_product_id = (string) ($vasp_failure['record']['entityId'] ?? ($vasp_item_details['entity_id'] ?? '')); if ($vasp_product_id !== '') { $vasp_product_ids[] = $vasp_product_id; } $vasp_image_id = absint($vasp_failure['record']['imageId'] ?? 0); if ($vasp_image_id > 0) { $vasp_image_ids[] = $vasp_image_id; } } if ($vasp_code === '') { return; } vasp_log('WC Catalog Sync: product image sources skipped; remaining images continue', 'warning', [ 'error_code' => $vasp_code, 'affected_count' => count($vasp_failures), ]); $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); $vasp_progress = is_array($vasp_progress) ? $vasp_progress : []; $vasp_progress['imageSourceIssueCount'] = max(0, (int) ($vasp_progress['imageSourceIssueCount'] ?? 0)) + 1; update_option(VASP_OPTION_WC_CATALOG_PROGRESS, $vasp_progress, false); if ($vasp_existing_code !== '') { $vasp_code = $vasp_existing_code; } $vasp_details['step'] = 'image_download'; $vasp_details['image_files'] = array_slice(array_values(array_unique(array_filter($vasp_files))), 0, 10); $vasp_details['entity_type'] = 'product'; $vasp_details['product_ids'] = array_slice(array_values(array_unique(array_filter($vasp_product_ids))), 0, 10); $vasp_details['image_ids'] = array_slice(array_values(array_unique(array_filter(array_map('absint', $vasp_image_ids)))), 0, 10); unset($vasp_details['image_file']); $vasp_record = vasp_build_error_record($vasp_code, '', 'catalog_sync', 'image_download', $vasp_details); if ($vasp_existing_code === $vasp_code && !empty($vasp_existing['timestamp'])) { $vasp_record['timestamp'] = max(0, (int) $vasp_existing['timestamp']); } vasp_store_error_record($vasp_record); } /** Retain one product-payload image warning while the remaining products continue. */ function vasp_wc_catalog_warn_product_payload_image(WP_Error $vasp_error, int $vasp_product_id): void { $vasp_code = vasp_wc_catalog_image_source_issue_code($vasp_error); if ($vasp_code === '' || $vasp_product_id <= 0) { return; } $vasp_existing_raw = get_option(VASP_OPTION_WC_CATALOG_LAST_ERROR, ''); $vasp_existing = is_string($vasp_existing_raw) ? json_decode($vasp_existing_raw, true) : null; $vasp_existing_code = is_array($vasp_existing) ? sanitize_key((string) ($vasp_existing['technical_code'] ?? '')) : ''; if ($vasp_existing_code !== '' && !vasp_is_catalog_image_source_error($vasp_existing_code)) { return; } $vasp_details = function_exists('vasp_scheduler_error_details') ? vasp_scheduler_error_details($vasp_error) : (is_array($vasp_error->get_error_data()) ? $vasp_error->get_error_data() : []); $vasp_existing_details = is_array($vasp_existing['error_details'] ?? null) ? $vasp_existing['error_details'] : []; $vasp_details['step'] = 'catalog_payload'; $vasp_details['product_ids'] = array_slice(array_values(array_unique(array_filter(array_merge( is_array($vasp_existing_details['product_ids'] ?? null) ? array_map('absint', $vasp_existing_details['product_ids']) : [], [$vasp_product_id] )))), 0, 10); $vasp_details['image_ids'] = array_slice(array_values(array_unique(array_filter(array_merge( is_array($vasp_existing_details['image_ids'] ?? null) ? array_map('absint', $vasp_existing_details['image_ids']) : [], is_array($vasp_details['image_ids'] ?? null) ? array_map('absint', $vasp_details['image_ids']) : [] )))), 0, 10); $vasp_details['image_files'] = array_slice(array_values(array_unique(array_filter(array_merge( is_array($vasp_existing_details['image_files'] ?? null) ? $vasp_existing_details['image_files'] : [], is_array($vasp_details['image_files'] ?? null) ? $vasp_details['image_files'] : [], [(string) ($vasp_details['image_file'] ?? '')] )))), 0, 10); unset($vasp_details['image_file']); vasp_log('WC Catalog Sync: product skipped after image source warning; remaining products continue', 'warning', [ 'error_code' => $vasp_code, 'product_ids' => [$vasp_product_id], 'image_ids' => $vasp_details['image_ids'], ]); $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); $vasp_progress = is_array($vasp_progress) ? $vasp_progress : []; $vasp_progress['imageSourceIssueCount'] = max(0, (int) ($vasp_progress['imageSourceIssueCount'] ?? 0)) + 1; update_option(VASP_OPTION_WC_CATALOG_PROGRESS, $vasp_progress, false); $vasp_record = vasp_build_error_record($vasp_code, '', 'catalog_sync', 'catalog_payload', $vasp_details); if ($vasp_existing_code === $vasp_code && !empty($vasp_existing['timestamp'])) { $vasp_record['timestamp'] = max(0, (int) $vasp_existing['timestamp']); } vasp_store_error_record($vasp_record); } /** Retain one bounded warning for owners rejected by canonical integration. */ function vasp_wc_catalog_warn_image_rejections( array $vasp_product_ids, int $vasp_affected_count = 0 ): void { $vasp_product_ids = array_slice(array_values(array_unique(array_filter(array_map( 'absint', $vasp_product_ids )))), 0, 10); $vasp_affected_count = max($vasp_affected_count, count($vasp_product_ids)); if ($vasp_affected_count <= 0) { return; } $vasp_existing_raw = get_option(VASP_OPTION_WC_CATALOG_LAST_ERROR, ''); $vasp_existing = is_string($vasp_existing_raw) ? json_decode($vasp_existing_raw, true) : null; $vasp_existing_code = is_array($vasp_existing) ? sanitize_key((string) ($vasp_existing['technical_code'] ?? '')) : ''; if ($vasp_existing_code !== '' && $vasp_existing_code !== 'catalog_image_integration_failed' && !vasp_is_catalog_image_source_error($vasp_existing_code)) { return; } $vasp_existing_details = is_array($vasp_existing['error_details'] ?? null) ? $vasp_existing['error_details'] : []; $vasp_product_ids = array_slice(array_values(array_unique(array_merge( is_array($vasp_existing_details['product_ids'] ?? null) ? array_map('absint', $vasp_existing_details['product_ids']) : [], $vasp_product_ids ))), 0, 10); vasp_log('WC Catalog Sync: rejected product image owners skipped; remaining images continue', 'warning', [ 'error_code' => 'catalog_image_integration_failed', 'affected_count' => $vasp_affected_count, ]); $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); $vasp_progress = is_array($vasp_progress) ? $vasp_progress : []; $vasp_progress['imageSourceIssueCount'] = max(0, (int) ($vasp_progress['imageSourceIssueCount'] ?? 0)) + 1; update_option(VASP_OPTION_WC_CATALOG_PROGRESS, $vasp_progress, false); $vasp_record = vasp_build_error_record( 'catalog_image_integration_failed', '', 'catalog_sync', 'images', ['entity_type' => 'product', 'product_ids' => $vasp_product_ids] ); if (is_array($vasp_existing) && !empty($vasp_existing['timestamp'])) { $vasp_record['timestamp'] = max(0, (int) $vasp_existing['timestamp']); } vasp_store_error_record($vasp_record); } /** Stop both local feeds; table rows survive a failed operation. */ function vasp_wc_catalog_record_failure(WP_Error $vasp_error, string $vasp_step): void { $vasp_data = $vasp_error->get_error_data(); $vasp_data = is_array($vasp_data) ? $vasp_data : []; $vasp_code = vasp_error_technical_code($vasp_error); if ($vasp_code === '') { $vasp_code = VASP_ERROR_WC_CATALOG_SCHEDULE_FAILED; } $vasp_details = function_exists('vasp_scheduler_error_details') ? vasp_scheduler_error_details($vasp_error) : []; vasp_store_user_friendly_error($vasp_code, $vasp_error->get_error_message(), 'catalog_sync', $vasp_step, $vasp_details); } function vasp_wc_catalog_store_failure(string $vasp_message): void { update_option(VASP_OPTION_WC_CATALOG_STATUS, 'failed', false); delete_option(vasp_scheduler_pending_option('woocommerce_catalog')); delete_option(vasp_scheduler_pending_option('woocommerce_images')); if (get_option(VASP_OPTION_WC_CATALOG_LAST_ERROR, '') === '') { vasp_record_user_friendly_error(VASP_ERROR_WC_CATALOG_SCHEDULE_FAILED, $vasp_message, 'catalog_sync'); } } function vasp_wc_catalog_fail(WP_Error $vasp_error): WP_Error { $vasp_data = $vasp_error->get_error_data(); $vasp_data = is_array($vasp_data) ? $vasp_data : []; $vasp_step = (string) get_option(VASP_OPTION_WC_CATALOG_STEP, ''); $vasp_stage = function_exists('vasp_wc_catalog_phase_for_step') ? vasp_wc_catalog_phase_for_step($vasp_step) : ''; $vasp_failure_area = function_exists('vasp_wc_catalog_failure_area_for_step') ? vasp_wc_catalog_failure_area_for_step($vasp_step) : ''; vasp_wc_catalog_record_failure($vasp_error, $vasp_failure_area !== '' ? $vasp_failure_area : $vasp_step); if ($vasp_stage !== '' && function_exists('vasp_wc_catalog_signal_failure')) { // Reporting is secondary; the original local failure is already durable. vasp_wc_catalog_signal_failure($vasp_stage, $vasp_failure_area); } $vasp_data['manual_retry_required'] = true; $vasp_error->add_data($vasp_data); vasp_wc_catalog_store_failure( __('The catalog update could not be completed. Select Sync to try again.', 'valuespace') ); return $vasp_error; } function vasp_wc_catalog_add_progress(int $vasp_received): void { $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); $vasp_progress = is_array($vasp_progress) ? $vasp_progress : []; $vasp_progress['processed'] = (int) ($vasp_progress['processed'] ?? 0) + $vasp_received; update_option(VASP_OPTION_WC_CATALOG_PROGRESS, $vasp_progress, false); } function vasp_wc_catalog_begin_product_progress(): void { $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); $vasp_progress = is_array($vasp_progress) ? $vasp_progress : []; $vasp_progress['processed'] = 0; update_option(VASP_OPTION_WC_CATALOG_PROGRESS, $vasp_progress, false); } function vasp_wc_catalog_begin_product_scan(int $vasp_total): void { $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); $vasp_progress = is_array($vasp_progress) ? $vasp_progress : []; $vasp_progress['productScanProcessed'] = 0; $vasp_progress['productScanTotal'] = max(0, $vasp_total); update_option(VASP_OPTION_WC_CATALOG_PROGRESS, $vasp_progress, false); } function vasp_wc_catalog_add_product_scan_progress(int $vasp_processed): int { $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); $vasp_progress = is_array($vasp_progress) ? $vasp_progress : []; $vasp_total = max(0, (int) ($vasp_progress['productScanTotal'] ?? 0)); $vasp_progress['productScanProcessed'] = min( $vasp_total, max(0, (int) ($vasp_progress['productScanProcessed'] ?? 0)) + max(0, $vasp_processed) ); update_option(VASP_OPTION_WC_CATALOG_PROGRESS, $vasp_progress, false); return $vasp_progress['productScanProcessed']; } function vasp_wc_catalog_full_inspection_mode(): bool { $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); return is_array($vasp_progress) && !empty($vasp_progress['fullInspectionMode']); } function vasp_wc_catalog_complete_full_inspection(): void { $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); $vasp_progress = is_array($vasp_progress) ? $vasp_progress : []; $vasp_progress['fullInspectionMode'] = false; update_option(VASP_OPTION_WC_CATALOG_PROGRESS, $vasp_progress, false); } function vasp_wc_catalog_begin_image_scan(int $vasp_total): void { $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); $vasp_progress = is_array($vasp_progress) ? $vasp_progress : []; $vasp_progress['imageScanProcessed'] = 0; $vasp_progress['imageScanTotal'] = max(0, $vasp_total); update_option(VASP_OPTION_WC_CATALOG_PROGRESS, $vasp_progress, false); } function vasp_wc_catalog_add_image_scan_progress(int $vasp_processed): int { $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); $vasp_progress = is_array($vasp_progress) ? $vasp_progress : []; $vasp_total = max(0, (int) ($vasp_progress['imageScanTotal'] ?? 0)); $vasp_progress['imageScanProcessed'] = min( $vasp_total, max(0, (int) ($vasp_progress['imageScanProcessed'] ?? 0)) + max(0, $vasp_processed) ); update_option(VASP_OPTION_WC_CATALOG_PROGRESS, $vasp_progress, false); return $vasp_progress['imageScanProcessed']; } function vasp_wc_catalog_begin_image_progress(int $vasp_total): void { $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); $vasp_progress = is_array($vasp_progress) ? $vasp_progress : []; $vasp_progress['imagesProcessed'] = 0; $vasp_progress['imagesTotal'] = max(0, $vasp_total); update_option(VASP_OPTION_WC_CATALOG_PROGRESS, $vasp_progress, false); } function vasp_wc_catalog_extend_image_progress(int $vasp_remaining): int { $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); $vasp_progress = is_array($vasp_progress) ? $vasp_progress : []; $vasp_processed = max(0, (int) ($vasp_progress['imagesProcessed'] ?? 0)); $vasp_total = max( max(0, (int) ($vasp_progress['imagesTotal'] ?? 0)), $vasp_processed + max(0, $vasp_remaining) ); $vasp_progress['imagesProcessed'] = $vasp_processed; $vasp_progress['imagesTotal'] = $vasp_total; update_option(VASP_OPTION_WC_CATALOG_PROGRESS, $vasp_progress, false); return $vasp_total; } function vasp_wc_catalog_add_image_progress(int $vasp_received): int { $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); $vasp_progress = is_array($vasp_progress) ? $vasp_progress : []; $vasp_total = max(0, (int) ($vasp_progress['imagesTotal'] ?? 0)); $vasp_progress['imagesProcessed'] = min( $vasp_total, max(0, (int) ($vasp_progress['imagesProcessed'] ?? 0)) + max(0, $vasp_received) ); update_option(VASP_OPTION_WC_CATALOG_PROGRESS, $vasp_progress, false); return $vasp_progress['imagesProcessed']; } function vasp_wc_catalog_local_progress_metrics(): array { $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); $vasp_processed = is_array($vasp_progress) ? (int) ($vasp_progress['processed'] ?? 0) : 0; $vasp_remaining = vasp_wc_catalog_storage_ready() ? vasp_wc_catalog_pending_count() : 0; $vasp_image_remaining = vasp_wc_catalog_storage_ready() ? vasp_wc_catalog_pending_count(true) : 0; $vasp_scan_processed = is_array($vasp_progress) ? max(0, (int) ($vasp_progress['productScanProcessed'] ?? 0)) : 0; $vasp_scan_total = is_array($vasp_progress) ? max(0, (int) ($vasp_progress['productScanTotal'] ?? 0)) : 0; return [ 'pipeline' => 'push', 'stage' => (string) get_option(VASP_OPTION_WC_CATALOG_STEP, ''), 'processed' => $vasp_processed, 'total' => $vasp_processed + $vasp_remaining, 'productsProcessed' => $vasp_processed, 'productsRemaining' => $vasp_remaining, 'productsQueued' => $vasp_processed + $vasp_remaining, 'changesWaiting' => $vasp_remaining > 0 || $vasp_image_remaining > 0 || get_option(VASP_OPTION_WC_CATALOG_PENDING_MODE, '') !== '', 'productScanProcessed' => min($vasp_scan_processed, $vasp_scan_total), 'productScanTotal' => $vasp_scan_total, 'imageScanProcessed' => is_array($vasp_progress) ? max(0, (int) ($vasp_progress['imageScanProcessed'] ?? 0)) : 0, 'imageScanTotal' => is_array($vasp_progress) ? max(0, (int) ($vasp_progress['imageScanTotal'] ?? 0)) : 0, 'imagesProcessed' => is_array($vasp_progress) ? max(0, (int) ($vasp_progress['imagesProcessed'] ?? 0)) : 0, 'imagesTotal' => is_array($vasp_progress) ? max(0, (int) ($vasp_progress['imagesTotal'] ?? 0)) : 0, 'progress' => $vasp_remaining > 0 ? min(99, (int) floor(100 * $vasp_processed / max(1, $vasp_processed + $vasp_remaining))) : 0, ]; } function vasp_wc_catalog_push_in_progress_response(): array { return ['status' => 'in_progress'] + vasp_wc_catalog_local_progress_metrics(); } function vasp_wc_catalog_sync_progress(bool $vasp_include_gateway = true) { $vasp_status = (string) get_option(VASP_OPTION_WC_CATALOG_STATUS, 'not_started'); $vasp_completed = (int) get_option(VASP_OPTION_WC_CATALOG_SYNC_COMPLETED_TIME, 0); $vasp_result = [ 'status' => in_array($vasp_status, ['pending', 'started'], true) ? 'in_progress' : $vasp_status, 'error' => '', 'error_hint' => '', 'error_code' => '', 'can_retry' => false, 'pushEnabled' => vasp_wc_catalog_push_enabled(), 'completedAt' => $vasp_status === 'completed' && $vasp_completed > 0 ? gmdate('c', $vasp_completed) : null, 'deliveryCompletedAt' => $vasp_completed > 0 ? gmdate('c', $vasp_completed) : null, ] + vasp_wc_catalog_local_progress_metrics(); if ($vasp_status === 'failed') { $vasp_error_raw = get_option(VASP_OPTION_WC_CATALOG_LAST_ERROR, ''); $vasp_error_record = is_string($vasp_error_raw) ? json_decode($vasp_error_raw, true) : null; $vasp_error_display = is_array($vasp_error_record) ? vasp_format_error_for_display($vasp_error_record, 'alert') : null; if ($vasp_error_display) { $vasp_result['error'] = $vasp_error_display['description']; $vasp_result['error_hint'] = $vasp_error_display['hint']; $vasp_result['error_code'] = $vasp_error_display['technical_code']; $vasp_result['can_retry'] = !empty($vasp_error_display['can_retry']); $vasp_result['affected_product'] = (array) ($vasp_error_display['affected_product'] ?? []); } } if (!$vasp_include_gateway || $vasp_status === 'failed' || !vasp_wc_catalog_push_enabled()) { return $vasp_result; } $vasp_server = vasp_wc_catalog_read_progress(); if (is_wp_error($vasp_server)) { return $vasp_server; } $vasp_projected = vasp_wc_catalog_project_server_progress($vasp_result, $vasp_server); if (($vasp_projected['status'] ?? '') === 'failed') { $vasp_error = is_array($vasp_server['error'] ?? null) ? $vasp_server['error'] : []; $vasp_code = substr(sanitize_key((string) ($vasp_error['code'] ?? 'catalog_synchronization_failed')), 0, 64); $vasp_step = substr(sanitize_key((string) ($vasp_server['step'] ?? 'catalog_processing')), 0, 64); $vasp_existing_raw = get_option(VASP_OPTION_WC_CATALOG_LAST_ERROR, ''); $vasp_existing = is_string($vasp_existing_raw) ? json_decode($vasp_existing_raw, true) : null; if (!is_array($vasp_existing) || ($vasp_existing['technical_code'] ?? '') !== $vasp_code || ($vasp_existing['step'] ?? '') !== $vasp_step) { $vasp_failed_items = is_numeric($vasp_server['failedItems'] ?? null) ? max(0, (int) $vasp_server['failedItems']) : null; $vasp_details = ['step' => $vasp_step]; if ($vasp_failed_items !== null) { $vasp_details['failed_items'] = $vasp_failed_items; } vasp_record_user_friendly_error($vasp_code, '', 'catalog_sync', $vasp_step, $vasp_details); } update_option(VASP_OPTION_WC_CATALOG_STATUS, 'failed', false); update_option(VASP_OPTION_WC_CATALOG_STEP, $vasp_step, false); } if (($vasp_projected['status'] ?? '') === 'completed') { $vasp_terminal_time = strtotime((string) ($vasp_projected['completedAt'] ?? '')) ?: 0; if ($vasp_terminal_time > 0) { update_option(VASP_OPTION_WC_CATALOG_SYNC_COMPLETED_TIME, $vasp_terminal_time, false); } $vasp_progress = get_option(VASP_OPTION_WC_CATALOG_PROGRESS, []); if (empty($vasp_progress['imageSourceIssueCount'])) { delete_option(VASP_OPTION_WC_CATALOG_LAST_ERROR); } } return $vasp_projected; }