is_type('variation')) { $parent = $product->get_parent_id(); return $parent ? (int) $parent : $product_id; } return $product_id; } private static function push($product_id, $action = 'upsert') { $id = self::parent_or_self($product_id); if (!$id) { return; } // Avoid duplicate pushes in the same request static $done = []; $key = $action . ':' . $id; if (isset($done[$key])) { return; } $done[$key] = true; AGENTI_API::push_product($id, $action); } public static function on_product_save($product_id) { $product_id = (int) $product_id; if (!$product_id) { return; } $type = get_post_type($product_id); if ($type && !in_array($type, ['product', 'product_variation'], true)) { return; } self::push($product_id, 'upsert'); } public static function on_variation_save($variation_id) { self::push((int) $variation_id, 'upsert'); } public static function on_save_post_product($post_id, $post, $update) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } if (wp_is_post_revision($post_id)) { return; } if (!$post || $post->post_type !== 'product') { return; } if ($post->post_status === 'auto-draft') { return; } if ($post->post_status === 'trash') { self::push((int) $post_id, 'deleted'); return; } self::push((int) $post_id, 'upsert'); } public static function on_save_post_variation($post_id, $post, $update) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } if (!$post || $post->post_type !== 'product_variation') { return; } if ($post->post_status === 'trash') { self::push((int) $post_id, 'deleted'); return; } self::push((int) $post_id, 'upsert'); } public static function on_stock_status($product_id, $stock_status = '', $product = null) { self::push((int) $product_id, 'upsert'); } public static function on_variation_stock_status($variation_id, $stock_status = '', $product = null) { self::push((int) $variation_id, 'upsert'); } public static function on_stock_qty($product) { $id = is_object($product) && method_exists($product, 'get_id') ? $product->get_id() : (int) $product; if ($id) { self::push($id, 'upsert'); } } public static function on_product_trash($post_id) { if (!self::is_product_post($post_id)) { return; } self::push((int) $post_id, 'deleted'); } public static function on_product_untrash($post_id) { if (!self::is_product_post($post_id)) { return; } self::push((int) $post_id, 'upsert'); } public static function on_product_delete($post_id) { if (!self::is_product_post($post_id)) { return; } self::push((int) $post_id, 'deleted'); } public static function on_wc_trash($product_id) { self::push((int) $product_id, 'deleted'); } public static function on_wc_untrash($product_id) { self::push((int) $product_id, 'upsert'); } public static function on_wc_delete($product_id) { self::push((int) $product_id, 'deleted'); } public static function on_status_transition($new_status, $old_status, $post) { if (!$post || !isset($post->post_type)) { return; } if (!in_array($post->post_type, ['product', 'product_variation'], true)) { return; } if ($new_status === $old_status) { return; } if ($new_status === 'trash') { self::push((int) $post->ID, 'deleted'); return; } if ($old_status === 'trash' || in_array($new_status, ['publish', 'draft', 'private', 'pending'], true)) { self::push((int) $post->ID, 'upsert'); } } public static function run_full_sync() { if (function_exists('set_time_limit')) { @set_time_limit(0); } if (function_exists('ignore_user_abort')) { @ignore_user_abort(true); } AGENTI_API::sync_all_products(100); } }