get_col($wpdb->prepare( "SELECT DISTINCT posts.ID FROM %i posts INNER JOIN %i meta ON meta.post_id = posts.ID WHERE posts.post_type IN ('product', 'product_variation') AND posts.post_status = 'publish' AND ((meta.meta_key = '_thumbnail_id' AND meta.meta_value = %s) OR (meta.meta_key = '_product_image_gallery' AND FIND_IN_SET(%s, meta.meta_value) > 0))", $wpdb->posts, $wpdb->postmeta, $image_id, $image_id )); $ids = array_values(array_unique(array_filter(array_map('intval', (array) $ids)))); // Variation payloads inherit their parent's featured/gallery images. Include // published children so a same-URL binary replacement refreshes each // entity-scoped image record, not only the parent product. $parent_ids = array_values(array_filter($ids, static function (int $product_id): bool { return get_post_type($product_id) === 'product'; })); if (!empty($parent_ids)) { $placeholders = implode(',', array_fill(0, count($parent_ids), '%d')); $variation_ids = $wpdb->get_col($wpdb->prepare( "SELECT DISTINCT ID FROM %i WHERE post_type = 'product_variation' AND post_status = 'publish' AND post_parent IN ({$placeholders})", $wpdb->posts, ...$parent_ids )); $ids = array_merge($ids, array_map('intval', (array) $variation_ids)); } $product_ids[$attachment_id] = array_values(array_unique(array_filter($ids))); return $product_ids[$attachment_id]; } /** Return every product/variation with a direct image-meta reference to an attachment. */ function vasp_wc_catalog_direct_product_ids_for_attachment(int $attachment_id): array { if ($attachment_id <= 0) { return []; } global $wpdb; $image_id = (string) $attachment_id; $ids = $wpdb->get_col($wpdb->prepare( "SELECT DISTINCT posts.ID FROM %i posts INNER JOIN %i meta ON meta.post_id = posts.ID WHERE posts.post_type IN ('product', 'product_variation') AND ((meta.meta_key = '_thumbnail_id' AND meta.meta_value = %s) OR (meta.meta_key = '_product_image_gallery' AND FIND_IN_SET(%s, meta.meta_value) > 0))", $wpdb->posts, $wpdb->postmeta, $image_id, $image_id )); return array_values(array_unique(array_filter(array_map('intval', (array) $ids)))); } /** Return category/brand terms whose single image association uses an attachment. */ function vasp_wc_catalog_taxonomy_image_owners_for_attachment(int $attachment_id): array { if ($attachment_id <= 0) { return []; } global $wpdb; $rows = $wpdb->get_results($wpdb->prepare( "SELECT DISTINCT tm.term_id, tt.taxonomy FROM %i tm INNER JOIN %i tt ON tt.term_id = tm.term_id WHERE tm.meta_key = 'thumbnail_id' AND tm.meta_value = %s", $wpdb->termmeta, $wpdb->term_taxonomy, (string) $attachment_id ), ARRAY_A); if ($wpdb->last_error !== '' || !is_array($rows)) { return []; } return array_values(array_filter($rows, static function ($row): bool { $taxonomy = (string) ($row['taxonomy'] ?? ''); return $taxonomy === 'product_cat' || vasp_wc_catalog_is_brand_taxonomy($taxonomy); })); } /** Collect catalog and optional binary refresh work for one changed attachment. */ function vasp_wc_catalog_collect_attachment_image_change(int $attachment_id, bool $binary_changed): void { if ($attachment_id <= 0 || !vasp_wc_catalog_can_push_delta()) { return; } $product_ids = vasp_wc_catalog_product_ids_for_attachment($attachment_id); if (!empty($product_ids)) { vasp_wc_catalog_realtime_collect_products_changed($product_ids); } if ($binary_changed) { vasp_wc_image_sync_request_products($product_ids); } foreach (vasp_wc_catalog_taxonomy_image_owners_for_attachment($attachment_id) as $owner) { $taxonomy = (string) ($owner['taxonomy'] ?? ''); vasp_wc_catalog_realtime_collect_taxonomy_snapshot( $taxonomy === 'product_cat' ? 'categories' : 'brands' ); } } /** Collect attachment title/file/alt changes without performing Gateway I/O. */ function vasp_wc_catalog_attachment_hook_sync(int $attachment_id): void { vasp_wc_catalog_collect_attachment_image_change($attachment_id, false); } /** * Remove an explicitly deleted Media Library item from every WooCommerce owner. * * Product image-set changes still travel through the product API. Category and * brand image changes travel through their existing snapshot APIs. The server * can then remove only owner references and let its shared orphan reconciler * decide whether any canonical image/blob is truly unowned. */ function vasp_wc_catalog_attachment_delete_hook_sync(int $attachment_id): void { if ($attachment_id <= 0) { return; } $product_ids = vasp_wc_catalog_direct_product_ids_for_attachment($attachment_id); if (!empty($product_ids)) { global $wpdb; $image_id = (string) $attachment_id; foreach (array_chunk($product_ids, 500) as $product_chunk) { $placeholders = implode(',', array_fill(0, count($product_chunk), '%d')); $wpdb->query($wpdb->prepare( "DELETE FROM %i WHERE post_id IN ($placeholders) AND meta_key='_thumbnail_id' AND meta_value=%s", array_merge([$wpdb->postmeta], $product_chunk, [$image_id]) )); $wpdb->query($wpdb->prepare( "UPDATE %i SET meta_value=TRIM(BOTH ',' FROM REPLACE( CONCAT(',',REPLACE(meta_value,' ',''),','),CONCAT(',',%s,','),',')) WHERE post_id IN ($placeholders) AND meta_key='_product_image_gallery' AND FIND_IN_SET(%s,REPLACE(meta_value,' ',''))>0", array_merge([$wpdb->postmeta, $image_id], $product_chunk, [$image_id]) )); $wpdb->query($wpdb->prepare( "DELETE FROM %i WHERE post_id IN ($placeholders) AND meta_key='_product_image_gallery' AND meta_value=''", array_merge([$wpdb->postmeta], $product_chunk) )); } foreach ($product_ids as $product_id) { wp_cache_delete($product_id, 'post_meta'); } } $taxonomy_owners = vasp_wc_catalog_taxonomy_image_owners_for_attachment($attachment_id); foreach ($taxonomy_owners as $owner) { delete_term_meta((int) $owner['term_id'], 'thumbnail_id', (string) $attachment_id); } if (!vasp_wc_catalog_can_push_delta()) { return; } if (!empty($product_ids)) { vasp_wc_catalog_realtime_collect_products_changed($product_ids); } foreach ($taxonomy_owners as $owner) { $taxonomy = (string) ($owner['taxonomy'] ?? ''); vasp_wc_catalog_realtime_collect_taxonomy_snapshot( $taxonomy === 'product_cat' ? 'categories' : 'brands' ); } } /** Collect completed WooCommerce CRUD saves, including metadata-only updates. */ function vasp_wc_catalog_product_crud_hook_sync($product_id): void { $update = strpos(current_filter(), 'woocommerce_new_') !== 0; vasp_wc_catalog_product_hook_sync((int) $product_id, get_post((int) $product_id), $update); } /** * Collect a realtime product delta after product save hooks. * * @param int $post_id Product post ID. * @param WP_Post $post Post object. * @param bool $update Whether this is an update. * @return void */ function vasp_wc_catalog_product_hook_sync($post_id, $post, $update): void { if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id) || !vasp_wc_catalog_can_push_delta()) { return; } if (!$post instanceof WP_Post || !in_array($post->post_type, ['product', 'product_variation'], true)) { return; } // Status transitions run before save/CRUD callbacks. An unpublished save // must therefore leave the delete collected by transition_post_status // intact instead of replacing it with an upsert that produces no payload. if ($post->post_status !== 'publish') { return; } if ($post->post_type === 'product_variation') { $parent_id = (int) wp_get_post_parent_id((int) $post_id); if ($parent_id <= 0 || get_post_status($parent_id) !== 'publish') { return; } } vasp_wc_catalog_realtime_collect_product_changed((int) $post_id); } /** * Collect deletes when a product leaves published state. * * @param string $new_status New post status. * @param string $old_status Old post status. * @param WP_Post $post Post object. * @return void */ function vasp_wc_catalog_product_status_hook_sync(string $new_status, string $old_status, WP_Post $post): void { if ($old_status === $new_status || !in_array($post->post_type, ['product', 'product_variation'], true) || !vasp_wc_catalog_can_push_delta()) { return; } if ($new_status === 'publish') { vasp_wc_catalog_realtime_collect_product_changed((int) $post->ID); vasp_wc_image_sync_request_products([(int) $post->ID]); return; } if ($old_status === 'publish') { vasp_wc_catalog_realtime_collect_product_removed((int) $post->ID); } } /** * Collect deletes before a product is removed. * * @param int $post_id Post ID. * @return void */ function vasp_wc_catalog_product_delete_hook_sync(int $post_id): void { $post_type = get_post_type($post_id); if (!in_array($post_type, ['product', 'product_variation'], true) || !vasp_wc_catalog_can_push_delta()) { return; } $GLOBALS['vasp_wc_catalog_deleting_products'][$post_id] = true; vasp_wc_catalog_realtime_collect_product_removed($post_id); } /** Return whether WordPress is currently removing this product in this request. */ function vasp_wc_catalog_is_product_being_deleted(int $product_id): bool { return !empty($GLOBALS['vasp_wc_catalog_deleting_products'][$product_id]); } /** * Collect a product when taxonomy assignments change. * * @param int $object_id Product/post ID. * @param array|string $terms New terms. * @param array $tt_ids New term taxonomy IDs. * @param string $taxonomy Taxonomy slug. * @param bool $append Whether terms were appended. * @param array $old_tt_ids Previous term taxonomy IDs. * @return void */ function vasp_wc_catalog_object_terms_hook_sync($object_id, $terms, $tt_ids, string $taxonomy, bool $append, array $old_tt_ids): void { unset($terms, $tt_ids, $append, $old_tt_ids); if (!vasp_wc_catalog_can_push_delta() || vasp_wc_catalog_is_product_being_deleted((int) $object_id) || !vasp_wc_catalog_is_catalog_taxonomy($taxonomy) ) { return; } $product = wc_get_product((int) $object_id); if (!$product instanceof WC_Product) { return; } vasp_wc_catalog_collect_taxonomy_product_changes([(int) $object_id], $taxonomy); } /** * Return whether a taxonomy can affect catalog payloads. * * @param string $taxonomy Taxonomy slug. * @return bool */ function vasp_wc_catalog_is_catalog_taxonomy(string $taxonomy): bool { return in_array($taxonomy, ['product_cat', 'product_tag'], true) || vasp_wc_catalog_is_brand_taxonomy($taxonomy) || strpos($taxonomy, 'pa_') === 0; } /** * Collect every product currently related to one term. * * Term names are denormalized into product payloads, so editing a category, * tag, brand, or attribute term must refresh every affected product. Category * and brand snapshots are delivered first by the same realtime feed. * * @param int $term_id Term ID. * @param string $taxonomy Taxonomy slug. * @return void */ function vasp_wc_catalog_collect_products_for_term_relationship(int $term_id, string $taxonomy): void { $product_ids = get_objects_in_term($term_id, $taxonomy); if (is_wp_error($product_ids) || !is_array($product_ids)) { return; } vasp_wc_catalog_collect_taxonomy_product_changes($product_ids, $taxonomy); } /** Collect dependent products and request the owning realtime taxonomy snapshots. */ function vasp_wc_catalog_collect_taxonomy_product_changes(array $product_ids, string $taxonomy): void { if (empty($product_ids)) { return; } vasp_wc_catalog_realtime_collect_products_changed($product_ids); $has_published_product = false; foreach ($product_ids as $product_id) { if (vasp_wc_catalog_product_id_is_published((int) $product_id)) { $has_published_product = true; break; } } if ($has_published_product && ($taxonomy === 'product_cat' || vasp_wc_catalog_is_brand_taxonomy($taxonomy)) ) { vasp_wc_catalog_realtime_collect_taxonomy_snapshot( $taxonomy === 'product_cat' ? 'categories' : 'brands' ); } } /** * Collect every product using any term in an attribute taxonomy. * * @param string $taxonomy Attribute taxonomy slug. * @return void */ function vasp_wc_catalog_collect_products_for_attribute_taxonomy(string $taxonomy): void { $term_ids = get_terms([ 'taxonomy' => $taxonomy, 'hide_empty' => false, 'fields' => 'ids', ]); if (is_wp_error($term_ids) || !is_array($term_ids) || empty($term_ids)) { return; } $product_ids = get_objects_in_term(array_map('intval', $term_ids), $taxonomy); if (is_wp_error($product_ids) || !is_array($product_ids)) { return; } vasp_wc_catalog_realtime_collect_products_changed($product_ids); } /** * Collect term deltas for categories, tags, brands, and attribute terms. * * @param int $term_id Term ID. * @param int $term_taxonomy_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. * @return void */ function vasp_wc_catalog_term_hook_sync(int $term_id, int $term_taxonomy_id, string $taxonomy): void { unset($term_taxonomy_id); if (!vasp_wc_catalog_can_push_delta()) { return; } $term = get_term($term_id, $taxonomy); if (!$term instanceof WP_Term || is_wp_error($term)) { return; } if (in_array($taxonomy, ['product_cat', 'product_tag'], true) || vasp_wc_catalog_is_brand_taxonomy($taxonomy)) { vasp_wc_catalog_collect_products_for_term_relationship($term_id, $taxonomy); return; } if (strpos($taxonomy, 'pa_') === 0) { vasp_wc_catalog_collect_products_for_term_relationship($term_id, $taxonomy); } } /** * Remember products affected by a term delete before WordPress removes relations. * * @param int $term_id Term ID. * @param string $taxonomy Taxonomy slug. * @return void */ function vasp_wc_catalog_term_pre_delete_hook_sync(int $term_id, string $taxonomy): void { if (!vasp_wc_catalog_can_push_delta() || !vasp_wc_catalog_is_catalog_taxonomy($taxonomy)) { return; } $product_ids = get_objects_in_term($term_id, $taxonomy); if (is_wp_error($product_ids) || !is_array($product_ids)) { return; } $key = $taxonomy . ':' . $term_id; vasp_wc_catalog_deleted_term_products($key, array_values(array_unique(array_filter(array_map('intval', $product_ids))))); } /** * Collect term delete events. * * @param int $term_id Term ID. * @param int $term_taxonomy_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. * @param mixed $deleted_term Deleted term object. * @return void */ function vasp_wc_catalog_term_delete_hook_sync(int $term_id, int $term_taxonomy_id, string $taxonomy, $deleted_term): void { unset($term_taxonomy_id, $deleted_term); if (!vasp_wc_catalog_can_push_delta()) { return; } if (in_array($taxonomy, ['product_cat', 'product_tag'], true) || vasp_wc_catalog_is_brand_taxonomy($taxonomy)) { vasp_wc_catalog_collect_products_for_deleted_term($term_id, $taxonomy); return; } if (strpos($taxonomy, 'pa_') === 0) { vasp_wc_catalog_collect_products_for_deleted_term($term_id, $taxonomy); } } /** * Collect products captured before a term was deleted. * * @param int $term_id Term ID. * @param string $taxonomy Taxonomy slug. * @return void */ function vasp_wc_catalog_collect_products_for_deleted_term(int $term_id, string $taxonomy): void { $key = $taxonomy . ':' . $term_id; $product_ids = vasp_wc_catalog_deleted_term_products($key, null, true); vasp_wc_catalog_collect_taxonomy_product_changes($product_ids, $taxonomy); } /** * Store product IDs captured before a term relationship is deleted. * * @param string $key Taxonomy and term key. * @param array|null $product_ids Product IDs to store, or null to read. * @param bool $forget Whether to forget the stored IDs after reading. * @return array */ function vasp_wc_catalog_deleted_term_products(string $key, ?array $product_ids = null, bool $forget = false): array { static $store = []; if ($product_ids !== null) { $store[$key] = $product_ids; } $stored = is_array($store[$key] ?? null) ? $store[$key] : []; if ($forget) { unset($store[$key]); } return $stored; } /** * Collect attribute schema changes. * * @param int $attribute_id Attribute ID. * @param array|mixed $attribute Attribute data from WooCommerce. * @return void */ function vasp_wc_catalog_attribute_schema_hook_sync(int $attribute_id = 0, $attribute = null): void { if (!vasp_wc_catalog_can_push_delta()) { return; } $attribute_name = ''; if (is_array($attribute)) { $attribute_name = (string) ($attribute['attribute_name'] ?? $attribute['name'] ?? ''); } elseif (is_object($attribute)) { $attribute_name = (string) ($attribute->attribute_name ?? $attribute->name ?? ''); } if ($attribute_name === '' && $attribute_id > 0 && function_exists('wc_get_attribute')) { $attribute_object = wc_get_attribute($attribute_id); if (is_object($attribute_object)) { $attribute_name = (string) ($attribute_object->slug ?? $attribute_object->name ?? ''); } } if ($attribute_name !== '') { $taxonomy = strpos($attribute_name, 'pa_') === 0 ? $attribute_name : wc_attribute_taxonomy_name($attribute_name); vasp_wc_catalog_collect_products_for_attribute_taxonomy($taxonomy); return; } if (!function_exists('wc_get_attribute_taxonomies')) { return; } $attribute_taxonomies = wc_get_attribute_taxonomies(); if (!is_array($attribute_taxonomies)) { return; } foreach ($attribute_taxonomies as $attribute_taxonomy) { vasp_wc_catalog_collect_products_for_attribute_taxonomy(wc_attribute_taxonomy_name($attribute_taxonomy->attribute_name)); } } /** * Collect attribute schema deletion. * * @param int $attribute_id Attribute ID. * @param string $attribute_name Attribute name. * @param string $taxonomy Taxonomy slug. * @return void */ function vasp_wc_catalog_attribute_schema_delete_hook_sync(int $attribute_id, string $attribute_name, string $taxonomy): void { unset($attribute_id, $attribute_name); if (!vasp_wc_catalog_can_push_delta()) { return; } vasp_wc_catalog_collect_products_for_attribute_taxonomy($taxonomy); }