data_retriever = new Ovena_ACP_Data_Retriever(); $this->formatter = new Ovena_ACP_Formatter(); $this->api_client = new Ovena_ACP_API_Client(); // Get batch size and ensure it's a valid positive integer $batch_size = get_option( 'ovena_acp_batch_size', 50 ); if ( $batch_size === false || $batch_size === '' ) { $batch_size = 50; } $this->batch_size = max( 1, absint( $batch_size ) ); } /** * Sync all products to Ovena API */ public function sync_products( $force_full_sync = false ) { $this->log_sync_start(); try { // Get products to sync $product_ids = $this->get_products_to_sync( $force_full_sync ); if ( empty( $product_ids ) ) { $this->log_sync_complete( 0, 0, 0 ); return array( 'success' => true, 'message' => __( 'No products to sync', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), 'stats' => array( 'total' => 0, 'processed' => 0, 'errors' => 0 ) ); } // Process products in batches $total_products = count( $product_ids ); $processed = 0; $errors = 0; $error_details = array(); $batches = array_chunk( $product_ids, $this->batch_size ); foreach ( $batches as $batch_index => $batch ) { $batch_result = $this->sync_product_batch( $batch ); $processed += $batch_result['processed']; $errors += $batch_result['errors']; if ( ! empty( $batch_result['error_details'] ) ) { $error_details = array_merge( $error_details, $batch_result['error_details'] ); } // Small delay between batches to avoid overwhelming the API usleep( 100000 ); // 0.1 seconds } $this->log_sync_complete( $total_products, $processed, $errors ); $this->update_last_sync_time(); return array( 'success' => true, /* translators: %1$d: number of processed products, %2$d: total number of products */ 'message' => sprintf( __( 'Sync completed. Processed %1$d/%2$d products.', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $processed, $total_products ), 'stats' => array( 'total' => $total_products, 'processed' => $processed, 'errors' => $errors, 'error_details' => $error_details ) ); } catch ( Exception $e ) { $this->log_sync_error( $e->getMessage() ); return array( 'success' => false, /* translators: %s: error message */ 'message' => sprintf( __( 'Sync failed: %s', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $e->getMessage() ) ); } } /** * Sync a single product */ public function sync_single_product( $product_id ) { try { // Retrieve product data $product_data = $this->data_retriever->get_product_feed_data( $product_id ); if ( ! $product_data ) { return array( 'success' => false, 'error' => __( 'Could not retrieve product data', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ) ); } // Format the data $formatted_data = $this->formatter->format_product_data( $product_data ); if ( ! $formatted_data ) { return array( 'success' => false, 'error' => __( 'Could not format product data', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ) ); } // Validate the data $validation = $this->formatter->validate_product_data( $formatted_data ); if ( ! $validation['valid'] ) { return array( 'success' => false, 'error' => __( 'Product data validation failed', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), 'validation_errors' => $validation['errors'] ); } // Send to API $api_response = $this->api_client->send_product_data( $formatted_data ); if ( $api_response['success'] ) { $this->update_product_sync_status( $product_id, true ); $this->api_client->update_api_stats( true ); } else { $this->update_product_sync_status( $product_id, false, $api_response['error'] ); $this->api_client->update_api_stats( false ); } return $api_response; } catch ( Exception $e ) { return array( 'success' => false, /* translators: %s: error message */ 'error' => sprintf( __( 'Exception during sync: %s', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), $e->getMessage() ) ); } } /** * Sync a batch of products */ private function sync_product_batch( $product_ids ) { $products_data = array(); $processed = 0; $errors = 0; $error_details = array(); // Collect product data foreach ( $product_ids as $product_id ) { $product_data = $this->data_retriever->get_product_feed_data( $product_id ); if ( $product_data ) { $formatted_data = $this->formatter->format_product_data( $product_data ); if ( $formatted_data ) { $validation = $this->formatter->validate_product_data( $formatted_data ); if ( $validation['valid'] ) { $products_data[] = $formatted_data; $processed++; } else { $errors++; $error_details[] = array( 'product_id' => $product_id, 'error' => __( 'Validation failed', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), 'details' => $validation['errors'] ); } } else { $errors++; $error_details[] = array( 'product_id' => $product_id, 'error' => __( 'Could not format product data', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ) ); } } else { $errors++; $error_details[] = array( 'product_id' => $product_id, 'error' => __( 'Could not retrieve product data', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ) ); } } // Send batch to API if we have valid products if ( ! empty( $products_data ) ) { $feed_data = $this->formatter->format_feed( $products_data ); $api_response = $this->api_client->send_feed_data( $feed_data ); if ( $api_response['success'] ) { // Mark all products in batch as successfully synced foreach ( $product_ids as $product_id ) { $this->update_product_sync_status( $product_id, true ); } $this->api_client->update_api_stats( true ); } else { // Mark all products in batch as failed foreach ( $product_ids as $product_id ) { $this->update_product_sync_status( $product_id, false, $api_response['error'] ); } $this->api_client->update_api_stats( false ); $error_details[] = array( 'batch' => $product_ids, 'error' => __( 'API request failed', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), 'details' => $api_response['error'] ); } } return array( 'processed' => $processed, 'errors' => $errors, 'error_details' => $error_details ); } /** * Get products that need to be synced */ private function get_products_to_sync( $force_full_sync = false ) { if ( $force_full_sync ) { $products = $this->data_retriever->get_all_products(); return $products; } // Get products modified since last sync $last_sync = get_option( 'ovena_acp_last_sync' ); if ( ! $last_sync ) { // First sync - get all products $products = $this->data_retriever->get_all_products(); return $products; } // Get products modified since last sync $args = array( 'status' => 'publish', 'limit' => -1, 'date_modified' => '>' . $last_sync, 'return' => 'ids' ); $products = wc_get_products( $args ); return $products; } /** * Update product sync status */ private function update_product_sync_status( $product_id, $success, $error_message = '' ) { $sync_status = get_option( 'ovena_acp_product_sync_status', array() ); $sync_status[ $product_id ] = array( 'last_sync' => current_time( 'mysql' ), 'success' => $success, 'error' => $error_message ); // Keep only recent entries to prevent option from growing too large if ( count( $sync_status ) > 1000 ) { $sync_status = array_slice( $sync_status, -500, null, true ); } update_option( 'ovena_acp_product_sync_status', $sync_status ); } /** * Update last sync time */ private function update_last_sync_time() { update_option( 'ovena_acp_last_sync', current_time( 'mysql' ) ); } /** * Log sync start */ private function log_sync_start() { $this->write_sync_log( 'Sync process started' ); } /** * Log sync completion */ private function log_sync_complete( $total, $processed, $errors ) { $this->write_sync_log( "Sync completed - Total: {$total}, Processed: {$processed}, Errors: {$errors}" ); } /** * Log sync error */ private function log_sync_error( $error_message ) { $this->write_sync_log( "Sync error: {$error_message}" ); } /** * Write to sync log */ private function write_sync_log( $message ) { $log_entry = array( 'timestamp' => current_time( 'mysql' ), 'message' => $message ); $sync_logs = get_option( 'ovena_acp_sync_logs', array() ); // Keep only last 50 entries if ( count( $sync_logs ) >= 50 ) { $sync_logs = array_slice( $sync_logs, -25 ); } $sync_logs[] = $log_entry; update_option( 'ovena_acp_sync_logs', $sync_logs ); } /** * Get sync statistics */ public function get_sync_stats() { $stats = array( 'last_sync' => get_option( 'ovena_acp_last_sync' ), 'total_products' => count( $this->data_retriever->get_all_products() ), 'api_stats' => $this->api_client->get_api_stats(), 'recent_logs' => get_option( 'ovena_acp_sync_logs', array() ) ); return $stats; } /** * Test sync process */ public function test_sync() { try { // Test API connection via health endpoint $api_test = $this->api_client->test_connection(); if ( ! $api_test['success'] ) { return array( 'success' => false, 'error' => isset( $api_test['error'] ) ? $api_test['error'] : __( 'API connection test failed', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ) ); } // Return health check results return array( 'success' => true, 'message' => isset( $api_test['message'] ) ? $api_test['message'] : __( 'API connection successful', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ), 'status' => isset( $api_test['status'] ) ? $api_test['status'] : 'Unknown', 'timestamp' => isset( $api_test['timestamp'] ) ? $api_test['timestamp'] : 'N/A', 'status_code' => isset( $api_test['status_code'] ) ? $api_test['status_code'] : 200 ); } catch ( Exception $e ) { return array( 'success' => false, 'error' => __( 'Test connection exception: ', 'agentic-commerce-protocol-chatgpt-product-feed-ovena-acp-for-woocommerce' ) . $e->getMessage() ); } } }