should_skip_operation($product_id, $current_action)) return; if (!$product) return; $product_data = $this->get_product_data($product); $this->send_to_external_system(array( 'operation' => 'updateOrCreate', 'product' => $product_data, )); } public function handle_product_deletion($post_id) { if (get_post_type($post_id) !== 'product') return; if ($this->should_skip_operation($post_id, 'delete')) return; $product_data = array( 'id' => (string) $post_id ); $this->send_to_external_system(array( 'operation' => 'delete', 'product' => $product_data, )); } public function handle_product_trash($post_id) { if (get_post_type($post_id) !== 'product') return; if ($this->should_skip_operation($post_id, 'trash')) return; $product = wc_get_product($post_id); if (!$product) return; $product_data = $this->get_product_data($product); $product_data['visible'] = false; $this->send_to_external_system(array( 'operation' => 'updateOrCreate', 'product' => $product_data, )); } public function handle_product_untrash($post_id) { if (get_post_type($post_id) !== 'product') return; if ($this->should_skip_operation($post_id, 'untrash')) return; $product = wc_get_product($post_id); if (!$product) return; $product_data = $this->get_product_data($product); $product_data['visible'] = $product->get_catalog_visibility() !== 'hidden'; $this->send_to_external_system(array( 'operation' => 'updateOrCreate', 'product' => $product_data, )); } private function should_skip_operation($product_id, $action) { if ($this->last_product_id === $product_id && $this->last_action === $action) { return true; } $this->last_product_id = $product_id; $this->last_action = $action; return false; } private function get_product_data($product) { $image_id = $product->get_image_id(); $image_src = wp_get_attachment_image_src($image_id, 'full'); $image_url = $image_src ? $image_src[0] : null; $is_published = $product->get_status() === 'publish'; return array( 'id' => (string) $product->get_id(), 'title' => $product->get_name(), 'price' => (float) $product->get_price(), 'imageUrl' => $image_url, 'inStock' => $product->is_in_stock(), 'visible' => $is_published && $product->get_catalog_visibility() !== 'hidden' ); } private function send_to_external_system($data) { $activation_key = get_option('a2cai_activation_key'); if (!$activation_key) { return; } $endpoint = ACAI_API_BASE_URL . '/products'; $response = wp_remote_post($endpoint, array( 'body' => json_encode($data), 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $activation_key ), )); if (is_wp_error($response)) { // Connection error - silently fail. } } private function verify_token($token) { if (empty($token)) { return false; } $stored_key = get_option('a2cai_activation_key'); return hash_equals($stored_key, $token); } } new ACAI_Product_Sync();