le the woocommerce_attribute_updated hook. * * @param int $id The attribute ID. * @param array $data The attribute data. * * @return void * * @since 10.5.0 * * @internal */ public function handle_woocommerce_attribute_updated( $id, $data ): void { if ( ! is_array( $data ) || ! isset( $data['attribute_name'] ) ) { return; } $taxonomy = wc_attribute_taxonomy_name( $data['attribute_name'] ); $this->invalidate_products_with_attribute( $taxonomy ); } /** * Handle the woocommerce_attribute_deleted hook. * * @param int $id The attribute ID. * @param string $name The attribute name. * @param string $taxonomy The attribute taxonomy. * * @return void * * @since 10.5.0 * * @internal */ public function handle_woocommerce_attribute_deleted( $id, $name, $taxonomy ): void { if ( ! is_string( $taxonomy ) || '' === $taxonomy ) { return; } $this->invalidate_products_with_attribute( $taxonomy ); } /** * Handle the woocommerce_updated_product_attribute_summary hook. * * @param int $variation_id The variation ID. * * @return void * * @since 10.5.0 * * @internal */ public function handle_woocommerce_updated_product_attribute_summary( $variation_id ): void { $this->invalidate_variation_and_parent( (int) $variation_id ); } /** * Handle the edited_term hook. * * @param int $term_id The term ID. * @param int $tt_id The term taxonomy ID. * @param string $taxonomy The taxonomy slug. * * @return void * * @since 10.5.0 * * @internal */ public function handle_edited_term( $term_id, $tt_id, $taxonomy ): void { if ( ! is_string( $taxonomy ) ) { return; } // Only handle product attribute taxonomies. if ( 0 !== strpos( $taxonomy, 'pa_' ) ) { return; } $this->invalidate_products_with_term( (int) $tt_id ); } /** * Get the parent product ID for a variation. * * The result is cached in the object cache to avoid repeated lookups. * * @param int $variation_id The variation ID. * * @return int|null The parent product ID, or null if not found. */ private function get_variation_parent_id( int $variation_id ): ?int { $cache_key = "wc_variation_parent_{$variation_id}"; $cached = wp_cache_get( $cache_key, 'woocommerce' ); if ( false !== $cached ) { // Cast to int: persistent object cache backends (e.g. Redis) can return scalars as strings, // which would violate this method's ?int return type. return $cached ? (int) $cached : null; } if ( $this->is_using_cpt_data_store() ) { $parent_id = wp_get_post_parent_id( $variation_id ); $parent_id = $parent_id ? (int) $parent_id : null; } else { $variation = wc_get_product( $variation_id ); $parent_id = $variation ? (int) $variation->get_parent_id() : null; $parent_id = $parent_id ? $parent_id : null; } // Cache the result (store 0 for null to distinguish from cache miss). wp_cache_set( $cache_key, $parent_id ?? 0, 'woocommerce', HOUR_IN_SECONDS ); return $parent_id; } /** * Invalidate the cached parent ID for a variation. * * @param int $variation_id The variation ID. * * @return void */ private function invalidate_variation_parent_cache( int $variation_id ): void { wp_cache_delete( "wc_variation_parent_{$variation_id}", 'woocommerce' ); } /** * Invalidate a variation and its parent product. * * @param int $variation_id The variation ID. * @param int|null $parent_id Optional parent product ID. If not provided, will be looked up. * * @return void */ private function invalidate_variation_and_parent( int $variation_id, ?int $parent_id = null ): void { $this->invalidate( $variation_id ); if ( is_null( $parent_id ) ) { $parent_id = $this->get_variation_parent_id( $variation_id ); } if ( ! $parent_id ) { return; } $this->invalidate( $parent_id ); } /** * Invalidate all products and variations that have a specific term assigned. * * Uses the indexed wp_term_relationships table for efficient lookups. * The list of entities associated with the term is cached for performance; * the TTL can be customized via the 'woocommerce_version_string_invalidator_taxonomy_lookup_ttl' filter. * * @param int $tt_id The term taxonomy ID. * * @return void */ private function invalidate_products_with_term( int $tt_id ): void { global $wpdb; $cache_key = 'wc_cache_inv_term_' . $tt_id; $entity_ids = wp_cache_get( $cache_key, 'woocommerce' ); if ( false === $entity_ids ) { $entity_ids = $wpdb->get_col( $wpdb->prepare( "SELECT tr.object_id FROM {$wpdb->term_relationships} tr INNER JOIN {$wpdb->posts} p ON tr.object_id = p.ID WHERE tr.term_taxonomy_id = %d AND p.post_type IN ('product', 'product_variation')", $tt_id ) ); /** * Filters the cache TTL for queries that find entities associated with a term or taxonomy. * * These queries are used during cache invalidation to determine which entities * (e.g., products, variations) need their cache cleared when a term or attribute changes. * * @since 10.5.0 * * @param int $ttl Cache TTL in seconds. Default 300 (5 minutes). * @param string $entity_type The type of entity being invalidated ('product'). */ $ttl = apply_filters( 'woocommerce_version_string_invalidator_taxonomy_lookup_ttl', self::DEFAULT_TAXONOMY_LOOKUP_CACHE_TTL, 'product' ); wp_cache_set( $cache_key, $entity_ids, 'woocommerce', $ttl ); } foreach ( $entity_ids as $entity_id ) { $post_type = get_post_type( (int) $entity_id ); if ( 'product_variation' === $post_type ) { $this->invalidate_variation_and_parent( (int) $entity_id ); } else { $this->invalidate( (int) $entity_id ); } } } /** * Invalidate all products using a specific attribute taxonomy. * * The list of entities associated with the taxonomy is cached for performance; * the TTL can be customized via the 'woocommerce_version_string_invalidator_taxonomy_lookup_ttl' filter. * * @param string $taxonomy The attribute taxonomy slug. * * @return void */ private function invalidate_products_with_attribute( string $taxonomy ): void { global $wpdb; $cache_key = 'wc_cache_inv_attr_' . $taxonomy; $cached = wp_cache_get( $cache_key, 'woocommerce' ); if ( false === $cached ) { $product_ids = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_product_attributes' AND meta_value LIKE %s", '%' . $wpdb->esc_like( 's:' . strlen( $taxonomy ) . ':"' . $taxonomy . '"' ) . '%' ) ); $variation_ids = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s", 'attribute_' . $taxonomy ) ); $cached = array( 'product_ids' => $product_ids, 'variation_ids' => $variation_ids, ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment -- Documented above. $ttl = apply_filters( 'woocommerce_version_string_invalidator_taxonomy_lookup_ttl', self::DEFAULT_TAXONOMY_LOOKUP_CACHE_TTL, 'product' ); wp_cache_set( $cache_key, $cached, 'woocommerce', $ttl ); } foreach ( $cached['product_ids'] as $product_id ) { $this->invalidate( (int) $product_id ); } foreach ( $cached['variation_ids'] as $variation_id ) { $this->invalidate_variation_and_parent( (int) $variation_id ); } } /** * Invalidate a product version string. * * @param int $product_id The product ID. * * @return void * * @since 10.5.0 */ public function invalidate( int $product_id ): void { wc_get_container()->get( VersionStringGenerator::class )->delete_version( "product_{$product_id}" ); } /** * Invalidate the product list version string. * * This should be called when products are created, deleted, or change status, * as these operations affect collection/list endpoints. * * @return void */ private function invalidate_products_list(): void { wc_get_container()->get( VersionStringGenerator::class )->delete_version( 'list_products' ); } /** * Invalidate the variations list version string for a specific product. * * This should be called when variations are created, deleted, or change status, * as these operations affect the variations collection/list endpoint for the parent product. * * @param int|null $product_id The parent product ID, or null/0 to skip invalidation. * * @return void */ private function invalidate_variations_list( ?int $product_id ): void { if ( $product_id ) { wc_get_container()->get( VersionStringGenerator::class )->delete_version( "list_product_variations_{$product_id}" ); } } }