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 5.2.0-rc.1 10.0.3 5.2.0-rc.2 10.0.4 5.2.1 10.0.5 5.2.2 10.0.6 5.2.3 10.1.0 5.2.4 10.1.0-rc.1 5.2.5 10.1.0-rc.2 5.3.0 10.1.0-rc.3 5.3.0-beta.1 10.1.0-rc.4 5.3.0-rc.1 10.1.1 5.3.0-rc.2 10.1.2 5.3.1 10.1.3 5.3.2 10.1.4 5.3.3 10.2.0 5.4.0 10.2.0-beta.1 5.4.0-beta.1 10.2.0-beta.2 5.4.0-rc.1 10.2.0-rc.1 5.4.1 10.2.1 5.4.2 10.2.2 5.4.3 10.2.3 5.4.4 10.2.4 5.4.5 10.3.0 5.5.0 10.3.0-beta.1 5.5.0-beta.1 10.3.0-beta.2 5.5.0-rc.1 10.3.0-rc.1 5.5.0-rc.2 10.3.0-rc.2 5.5.1 10.3.1 5.5.2 10.3.2 5.5.3 10.3.3 5.5.4 10.3.4 5.5.5 10.3.5 5.6.0 10.3.6 5.6.0-beta.1 10.3.7 5.6.0-rc.1 10.3.8 5.6.0-rc.2 10.4.0 5.6.1 10.4.0-beta.1 5.6.2 10.4.0-beta.2 5.6.3 10.4.0-rc.1 5.7.0 10.4.1 5.7.0-beta.1 10.4.2 5.7.0-rc.1 10.4.3 5.7.1 10.4.4 5.7.2 10.5.0 5.7.3 10.5.0-beta.1 5.8.0 10.5.0-beta.2 5.8.0-beta.1 10.5.0-rc.1 5.8.0-beta.2 10.5.0-rc.2 5.8.0-rc.1 10.5.0-rc.3 5.8.1 10.5.1 5.8.2 10.5.2 5.9.0 10.5.3 5.9.0-beta.1 10.6.0 5.9.0-rc.1 10.6.0-beta.1 5.9.0-rc.2 10.6.0-beta.2 5.9.1 10.6.0-rc.1 5.9.2 10.6.1 6.0.0 10.6.2 6.0.0-beta.1 10.7.0 6.0.0-rc.1 10.7.0-beta.1 6.0.1 10.7.0-beta.2 6.0.2 10.7.0-rc.1 6.1.0 3.0.0 6.1.0-beta.1 3.0.1 6.1.0-rc.1 3.0.2 6.1.0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7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / class-wc-post-data.php
woocommerce / includes Last commit date
abstracts 1 month ago admin 1 month ago blocks 1 month ago cli 2 months ago customizer 6 months ago data-stores 1 month ago emails 1 month ago export 1 month ago gateways 2 months ago import 1 month ago integrations 1 month ago interfaces 6 months ago legacy 1 month ago libraries 2 months ago log-handlers 1 year ago payment-tokens 6 years ago product-usage 1 year ago queue 6 months ago react-admin 1 month ago rest-api 1 month ago shipping 5 months ago shortcodes 1 month ago theme-support 2 years ago tracks 2 months ago traits 5 years ago walkers 5 years ago wccom-site 2 months ago widgets 1 month ago class-wc-ajax.php 1 month ago class-wc-auth.php 2 years ago class-wc-autoloader.php 10 months ago class-wc-background-emailer.php 3 months ago class-wc-background-updater.php 6 years ago class-wc-brands-brand-settings-manager.php 1 year ago class-wc-brands-coupons.php 1 year ago class-wc-brands.php 1 month ago class-wc-breadcrumb.php 6 months ago class-wc-cache-helper.php 2 months ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 5 months ago class-wc-cart-totals.php 1 year ago class-wc-cart.php 1 month ago class-wc-checkout.php 1 month ago class-wc-cli.php 1 year ago class-wc-comments.php 6 months ago class-wc-countries.php 3 months ago class-wc-coupon.php 3 months ago class-wc-customer-download-log.php 6 years ago class-wc-customer-download.php 2 years ago class-wc-customer.php 1 month ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 3 years ago class-wc-datetime.php 4 years ago class-wc-deprecated-action-hooks.php 2 years ago class-wc-deprecated-filter-hooks.php 5 months ago class-wc-discounts.php 1 year ago class-wc-download-handler.php 1 month ago class-wc-emails.php 1 month ago class-wc-embed.php 1 year ago class-wc-form-handler.php 1 month ago class-wc-frontend-scripts.php 4 weeks ago class-wc-geo-ip.php 10 months ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 1 month ago class-wc-https.php 3 years ago class-wc-install.php 1 month ago class-wc-integrations.php 6 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 6 months ago class-wc-meta-data.php 2 months ago class-wc-order-factory.php 3 months ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 1 month ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 2 months ago class-wc-order-item-shipping.php 7 months ago class-wc-order-item-tax.php 2 months ago class-wc-order-item.php 1 month ago class-wc-order-query.php 2 months ago class-wc-order-refund.php 1 year ago class-wc-order.php 1 month ago class-wc-payment-gateways.php 1 month ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 1 month ago class-wc-post-types.php 1 month ago class-wc-privacy-background-process.php 1 year ago class-wc-privacy-erasers.php 1 year ago class-wc-privacy-exporters.php 5 years ago class-wc-privacy.php 1 year ago class-wc-product-attribute.php 6 months ago class-wc-product-download.php 1 month ago class-wc-product-external.php 1 year ago class-wc-product-factory.php 5 months ago class-wc-product-grouped.php 1 month ago class-wc-product-query.php 6 months ago class-wc-product-simple.php 1 year ago class-wc-product-variable.php 2 months ago class-wc-product-variation.php 2 months ago class-wc-query.php 1 month ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 3 years ago class-wc-regenerate-images.php 1 month ago class-wc-register-wp-admin-settings.php 5 years ago class-wc-rest-authentication.php 2 years ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 5 months ago class-wc-shipping-rate.php 1 year ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 9 months ago class-wc-shipping.php 1 month ago class-wc-shortcodes.php 1 year ago class-wc-structured-data.php 1 month ago class-wc-tax.php 1 month ago class-wc-template-loader.php 9 months ago class-wc-tracker.php 1 month ago class-wc-validation.php 1 month ago class-wc-webhook.php 2 months ago class-woocommerce.php 4 weeks ago wc-account-functions.php 2 months ago wc-attribute-functions.php 1 month ago wc-brands-functions.php 1 year ago wc-cart-functions.php 2 months ago wc-conditional-functions.php 1 year ago wc-core-functions.php 3 months ago wc-coupon-functions.php 7 months ago wc-deprecated-functions.php 2 months ago wc-formatting-functions.php 1 month ago wc-interactivity-api-functions.php 3 months ago wc-notice-functions.php 7 months ago wc-order-functions.php 3 months ago wc-order-item-functions.php 3 years ago wc-order-step-logger-functions.php 6 months ago wc-page-functions.php 1 month ago wc-product-functions.php 1 month ago wc-rest-functions.php 9 months ago wc-stock-functions.php 1 month ago wc-template-functions.php 2 months ago wc-template-hooks.php 1 year ago wc-term-functions.php 1 month ago wc-update-functions.php 1 month ago wc-user-functions.php 1 month ago wc-webhook-functions.php 2 months ago wc-widget-functions.php 6 years ago
class-wc-post-data.php
1188 lines
1 <?php
2 /**
3 * Post Data
4 *
5 * Standardises certain post data on save.
6 *
7 * @package WooCommerce\Classes\Data
8 * @version 2.2.0
9 */
10
11 use Automattic\WooCommerce\Enums\OrderStatus;
12 use Automattic\WooCommerce\Enums\OrderInternalStatus;
13 use Automattic\WooCommerce\Enums\ProductStatus;
14 use Automattic\WooCommerce\Enums\ProductType;
15 use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer;
16 use Automattic\WooCommerce\Internal\ProductAttributesLookup\LookupDataStore as ProductAttributesLookupDataStore;
17 use Automattic\WooCommerce\Proxies\LegacyProxy;
18 use Automattic\WooCommerce\Utilities\OrderUtil;
19
20 defined( 'ABSPATH' ) || exit;
21
22 /**
23 * Post data class.
24 */
25 class WC_Post_Data {
26
27 /**
28 * Editing term.
29 *
30 * @var object
31 */
32 private static $editing_term = null;
33
34 /**
35 * Hook in methods.
36 *
37 * @return void
38 */
39 public static function init() {
40 add_action( 'clean_post_cache', array( __CLASS__, 'invalidate_products_last_modified' ), 10, 2 );
41 add_action( 'clean_post_cache', array( __CLASS__, 'invalidate_db_block_templates_cache' ), 10, 2 );
42 add_filter( 'post_type_link', array( __CLASS__, 'variation_post_link' ), 10, 2 );
43 add_action( 'shutdown', array( __CLASS__, 'do_deferred_product_sync' ), 10 );
44 add_action( 'set_object_terms', array( __CLASS__, 'force_default_term' ), 10, 5 );
45 add_action( 'set_object_terms', array( __CLASS__, 'delete_product_query_transients' ) );
46 add_action( 'set_object_terms', array( __CLASS__, 'recount_terms_for_product_visibility_change' ), 10, 6 );
47 add_action( 'deleted_term_relationships', array( __CLASS__, 'delete_product_query_transients' ) );
48 add_action( 'woocommerce_product_set_stock_status', array( __CLASS__, 'delete_product_query_transients' ) );
49 add_action( 'woocommerce_product_set_visibility', array( __CLASS__, 'delete_product_query_transients' ) );
50 add_action( 'woocommerce_product_type_changed', array( __CLASS__, 'product_type_changed' ), 10, 3 );
51
52 add_action( 'edit_term', array( __CLASS__, 'edit_term' ), 10, 3 );
53 add_action( 'edited_term', array( __CLASS__, 'edited_term' ), 10, 3 );
54 add_filter( 'update_order_item_metadata', array( __CLASS__, 'update_order_item_metadata' ), 10, 5 );
55 add_filter( 'update_post_metadata', array( __CLASS__, 'update_post_metadata' ), 10, 5 );
56 add_filter( 'wp_insert_post_data', array( __CLASS__, 'wp_insert_post_data' ) );
57 add_filter( 'oembed_response_data', array( __CLASS__, 'filter_oembed_response_data' ), 10, 2 );
58 add_filter( 'wp_untrash_post_status', array( __CLASS__, 'wp_untrash_post_status' ), 10, 3 );
59
60 // Status transitions.
61 add_action( 'transition_post_status', array( __CLASS__, 'transition_post_status' ), 10, 3 );
62 add_action( 'delete_post', array( __CLASS__, 'delete_post_data' ) );
63 add_action( 'wp_trash_post', array( __CLASS__, 'trash_post' ) );
64 add_action( 'untrashed_post', array( __CLASS__, 'untrash_post' ) );
65 add_action( 'before_delete_post', array( __CLASS__, 'before_delete_order' ) );
66 add_action( 'woocommerce_before_delete_order', array( __CLASS__, 'before_delete_order' ) );
67
68 // Meta cache flushing.
69 add_action( 'updated_post_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
70 add_action( 'added_post_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
71 add_action( 'deleted_post_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
72 add_action( 'updated_order_item_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
73
74 // Product Variations - Attributes.
75 // Priority 50 to make sure this runs after WooCommerce attribute migrations.
76 add_action( 'woocommerce_attribute_updated', array( __CLASS__, 'handle_global_attribute_updated' ), 50, 3 );
77 add_action( 'woocommerce_attribute_deleted', array( __CLASS__, 'handle_global_attribute_updated' ), 10, 3 );
78 // Product Variations - Terms.
79 add_action( 'edited_term', array( __CLASS__, 'handle_attribute_term_updated' ), 10, 3 );
80 add_action( 'delete_term', array( __CLASS__, 'handle_attribute_term_deleted' ), 10, 4 );
81 // Product Variations - Parent Product Updates Attributes.
82 add_action( 'woocommerce_product_attributes_updated', array( __CLASS__, 'on_product_attributes_updated' ), 10, 1 );
83 // Product Variations - Action Scheduler.
84 add_action( 'wc_regenerate_product_variation_summaries', array( __CLASS__, 'regenerate_product_variation_summaries' ), 10, 1 );
85 add_action( 'wc_regenerate_attribute_variation_summaries', array( __CLASS__, 'regenerate_attribute_variation_summaries' ), 10, 1 );
86 add_action( 'wc_regenerate_term_variation_summaries', array( __CLASS__, 'regenerate_term_variation_summaries' ), 10, 2 );
87 }
88
89 /**
90 * Link to parent products when getting permalink for variation.
91 *
92 * @param string $permalink Permalink.
93 * @param WP_Post $post Post data.
94 *
95 * @return string
96 */
97 public static function variation_post_link( $permalink, $post ) {
98 if ( isset( $post->ID, $post->post_type ) && 'product_variation' === $post->post_type ) {
99 $variation = wc_get_product( $post->ID );
100
101 if ( $variation && $variation->get_parent_id() ) {
102 return $variation->get_permalink();
103 }
104 }
105 return $permalink;
106 }
107
108 /**
109 * Sync products queued to sync.
110 *
111 * @return void
112 */
113 public static function do_deferred_product_sync() {
114 global $wc_deferred_product_sync;
115
116 // Syncing a product may defer more products (e.g. from hooks fired while saving it),
117 // so the queue is drained in rounds, syncing each product at most once.
118 $processed = array();
119 while ( ! empty( $wc_deferred_product_sync ) ) {
120 $product_ids = array_diff( wp_parse_id_list( $wc_deferred_product_sync ), $processed );
121 $wc_deferred_product_sync = array();
122
123 foreach ( $product_ids as $product_id ) {
124 self::deferred_product_sync( $product_id );
125 }
126
127 $processed = array_merge( $processed, $product_ids );
128 }
129 }
130
131 /**
132 * Sync a product.
133 *
134 * @param int $product_id Product ID.
135 *
136 * @return void
137 */
138 public static function deferred_product_sync( $product_id ) {
139 $product = wc_get_product( $product_id );
140
141 if ( is_callable( array( $product, 'sync' ) ) ) {
142 $product->sync( $product );
143 }
144 }
145
146 /**
147 * When a post status changes.
148 *
149 * @param string $new_status New status.
150 * @param string $old_status Old status.
151 * @param WP_Post $post Post data.
152 *
153 * @return void
154 */
155 public static function transition_post_status( $new_status, $old_status, $post ) {
156 if ( ( ProductStatus::PUBLISH === $new_status || ProductStatus::PUBLISH === $old_status ) && in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) {
157 self::delete_product_query_transients();
158 }
159
160 if ( ProductStatus::PUBLISH === $new_status && ProductStatus::PUBLISH !== $old_status && in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) {
161 /**
162 * Fires when a product or product variation transitions to published status.
163 *
164 * @since 10.8.0
165 * @param int $product_id The product or variation ID.
166 */
167 do_action( 'woocommerce_product_published', $post->ID );
168 }
169 }
170
171 /**
172 * Delete product view transients when needed e.g. when post status changes, or visibility/stock status is modified.
173 *
174 * @return void
175 */
176 public static function delete_product_query_transients() {
177 WC_Cache_Helper::get_transient_version( 'product_query', true );
178 }
179
180 /**
181 * Invalidate the cached products last modified timestamp when a product post cache is cleaned.
182 *
183 * This does not use wp_cache_set_last_changed() because the cached value is exposed to
184 * clients via the Last-Modified HTTP header for collection cache invalidation. WordPress
185 * core's last_changed pattern auto-seeds with the current time on cache miss, which is
186 * acceptable for opaque cache-key salts but would force all clients to unnecessarily
187 * invalidate their local caches. Instead, invalidating the cache here allows the read side
188 * in ProductQuery::get_last_modified() to fall back to the DB and re-seed with the real
189 * last modification time.
190 *
191 * @since 10.6.0
192 *
193 * @param int $post_id Post ID.
194 * @param WP_Post $post Post object.
195 */
196 public static function invalidate_products_last_modified( $post_id, $post ): void {
197 if ( $post instanceof WP_Post && in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) {
198 wp_cache_delete( 'last_modified', 'wc_products' );
199 }
200 }
201
202 /**
203 * Invalidates cache entries related to fetching block templates from DB. Please reference to
204 * `Utils\BlockTemplateUtils::get_block_templates_from_db` for further details.
205 *
206 * @param int $post_id Post ID.
207 * @param \WP_Post $post Post object.
208 *
209 * @internal
210 * @since 10.7.0
211 *
212 * @return void
213 */
214 public static function invalidate_db_block_templates_cache( $post_id, $post ): void {
215 if ( $post instanceof \WP_Post && in_array( $post->post_type, array( 'wp_template_part', 'wp_template' ), true ) ) {
216 wp_cache_delete( $post->post_type . '-ids', 'woocommerce_blocks' );
217 }
218 }
219
220 /**
221 * Handle type changes.
222 *
223 * @since 3.0.0
224 *
225 * @param WC_Product $product Product data.
226 * @param string $from Origin type.
227 * @param string $to New type.
228 *
229 * @return void
230 */
231 public static function product_type_changed( $product, $from, $to ) {
232 /**
233 * Filter to prevent variations from being deleted while switching from a variable product type to a variable product type.
234 *
235 * @since 5.0.0
236 *
237 * @param bool A boolean value of true will delete the variations.
238 * @param WC_Product $product Product data.
239 * @return string $from Origin type.
240 * @param string $to New type.
241 */
242 if ( apply_filters( 'woocommerce_delete_variations_on_product_type_change', ProductType::VARIABLE === $from && ProductType::VARIABLE !== $to, $product, $from, $to ) ) {
243 // If the product is no longer variable, we should ensure all variations are removed.
244 $data_store = WC_Data_Store::load( 'product-variable' );
245 $data_store->delete_variations( $product->get_id(), true );
246 }
247 }
248
249 /**
250 * When editing a term, check for product attributes.
251 *
252 * @param int $term_id Term ID.
253 * @param int $tt_id Term taxonomy ID.
254 * @param string $taxonomy Taxonomy slug.
255 *
256 * @return void
257 */
258 public static function edit_term( $term_id, $tt_id, $taxonomy ) {
259 if ( strpos( $taxonomy, 'pa_' ) === 0 ) {
260 self::$editing_term = get_term_by( 'id', $term_id, $taxonomy );
261 } else {
262 self::$editing_term = null;
263 }
264 }
265
266 /**
267 * When a term is edited, check for product attributes and update variations.
268 *
269 * @param int $term_id Term ID.
270 * @param int $tt_id Term taxonomy ID.
271 * @param string $taxonomy Taxonomy slug.
272 *
273 * @return void
274 */
275 public static function edited_term( $term_id, $tt_id, $taxonomy ) {
276 if ( ! is_null( self::$editing_term ) && strpos( $taxonomy, 'pa_' ) === 0 ) {
277 $edited_term = get_term_by( 'id', $term_id, $taxonomy );
278
279 if ( $edited_term->slug !== self::$editing_term->slug ) {
280 global $wpdb;
281
282 $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = %s WHERE meta_key = %s AND meta_value = %s;", $edited_term->slug, 'attribute_' . sanitize_title( $taxonomy ), self::$editing_term->slug ) );
283
284 $wpdb->query(
285 $wpdb->prepare(
286 "UPDATE {$wpdb->postmeta} SET meta_value = REPLACE( meta_value, %s, %s ) WHERE meta_key = '_default_attributes'",
287 serialize( self::$editing_term->taxonomy ) . serialize( self::$editing_term->slug ),
288 serialize( $edited_term->taxonomy ) . serialize( $edited_term->slug )
289 )
290 );
291 }
292 } else {
293 self::$editing_term = null;
294 }
295 }
296
297 /**
298 * Ensure floats are correctly converted to strings based on PHP locale.
299 *
300 * @param null $check Whether to allow updating metadata for the given type.
301 * @param int $object_id Object ID.
302 * @param string $meta_key Meta key.
303 * @param mixed $meta_value Meta value. Must be serializable if non-scalar.
304 * @param mixed $prev_value If specified, only update existing metadata entries with the specified value. Otherwise, update all entries.
305 * @return null|bool
306 */
307 public static function update_order_item_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
308 if ( ! empty( $meta_value ) && is_float( $meta_value ) ) {
309
310 // Convert float to string.
311 $meta_value = wc_float_to_string( $meta_value );
312
313 // Update meta value with new string.
314 update_metadata( 'order_item', $object_id, $meta_key, $meta_value, $prev_value );
315
316 return true;
317 }
318 return $check;
319 }
320
321 /**
322 * Ensure floats are correctly converted to strings based on PHP locale.
323 *
324 * @param null $check Whether to allow updating metadata for the given type.
325 * @param int $object_id Object ID.
326 * @param string $meta_key Meta key.
327 * @param mixed $meta_value Meta value. Must be serializable if non-scalar.
328 * @param mixed $prev_value If specified, only update existing metadata entries with the specified value. Otherwise, update all entries.
329 * @return null|bool
330 */
331 public static function update_post_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
332 // Delete product cache if someone uses meta directly.
333 if ( in_array( get_post_type( $object_id ), array( 'product', 'product_variation' ), true ) ) {
334 wp_cache_delete( 'product-' . $object_id, 'products' );
335 }
336
337 if ( ! empty( $meta_value ) && is_float( $meta_value ) && ! registered_meta_key_exists( 'post', $meta_key ) && in_array( get_post_type( $object_id ), array_merge( wc_get_order_types(), array( 'shop_coupon', 'product', 'product_variation' ) ), true ) ) {
338
339 // Convert float to string.
340 $meta_value = wc_float_to_string( $meta_value );
341
342 // Update meta value with new string.
343 update_metadata( 'post', $object_id, $meta_key, $meta_value, $prev_value );
344
345 return true;
346 }
347 return $check;
348 }
349
350 /**
351 * Forces the order posts to have a title in a certain format (containing the date).
352 * Forces certain product data based on the product's type, e.g. grouped products cannot have a parent.
353 *
354 * @param array $data An array of slashed post data.
355 * @return array
356 */
357 public static function wp_insert_post_data( $data ) {
358 if ( 'shop_order' === $data['post_type'] && isset( $data['post_date'] ) ) {
359 $order_title = 'Order';
360 if ( $data['post_date'] ) {
361 $order_title .= ' &ndash; ' . date_i18n( 'F j, Y @ h:i A', strtotime( $data['post_date'] ) );
362 }
363 $data['post_title'] = $order_title;
364 } elseif ( 'product' === $data['post_type'] && isset( $_POST['product-type'] ) ) { // WPCS: input var ok, CSRF ok.
365 $product_type = wc_clean( wp_unslash( $_POST['product-type'] ) ); // WPCS: input var ok, CSRF ok.
366 switch ( $product_type ) {
367 case ProductType::GROUPED:
368 case ProductType::VARIABLE:
369 $data['post_parent'] = 0;
370 break;
371 }
372 } elseif ( 'product' === $data['post_type'] && ProductStatus::AUTO_DRAFT === $data['post_status'] ) {
373 $data['post_title'] = 'AUTO-DRAFT';
374 } elseif ( 'shop_coupon' === $data['post_type'] ) {
375 // Coupons should never allow unfiltered HTML.
376 $data['post_title'] = wp_filter_kses( $data['post_title'] );
377 }
378
379 return $data;
380 }
381
382 /**
383 * Change embed data for certain post types.
384 *
385 * @since 3.2.0
386 * @param array $data The response data.
387 * @param WP_Post $post The post object.
388 * @return array
389 */
390 public static function filter_oembed_response_data( $data, $post ) {
391 if ( in_array( $post->post_type, array( 'shop_order', 'shop_coupon' ), true ) ) {
392 return array();
393 }
394 return $data;
395 }
396
397 /**
398 * Removes variations etc. belonging to a deleted post, and clears transients.
399 *
400 * @internal Use the delete_post function instead.
401 * @since 9.8.0
402 *
403 * @param mixed $id ID of post being deleted.
404 *
405 * @return void
406 */
407 public static function delete_post_data( $id ) {
408 $container = wc_get_container();
409
410 $post_type = self::get_post_type( $id );
411 switch ( $post_type ) {
412 case 'product':
413 $data_store = WC_Data_Store::load( 'product-variable' );
414 $data_store->delete_variations( $id, true );
415 $data_store->delete_from_lookup_table( $id, 'wc_product_meta_lookup' );
416 $container->get( ProductAttributesLookupDataStore::class )->on_product_deleted( $id );
417
418 $parent_id = wp_get_post_parent_id( $id );
419 if ( $parent_id ) {
420 wc_delete_product_transients( $parent_id );
421 }
422
423 break;
424 case 'product_variation':
425 $data_store = WC_Data_Store::load( 'product' );
426 $data_store->delete_from_lookup_table( $id, 'wc_product_meta_lookup' );
427 wc_delete_product_transients( wp_get_post_parent_id( $id ) );
428 $container->get( ProductAttributesLookupDataStore::class )->on_product_deleted( $id );
429
430 break;
431 case 'shop_order':
432 case DataSynchronizer::PLACEHOLDER_ORDER_POST_TYPE:
433 global $wpdb;
434
435 $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
436
437 if ( ! is_null( $refunds ) ) {
438 foreach ( $refunds as $refund ) {
439 wp_delete_post( $refund->ID, true );
440 }
441 }
442 break;
443 }
444 }
445
446 /**
447 * Removes variations etc. belonging to a deleted post, and clears transients, if the user has permission.
448 *
449 * @param mixed $id ID of post being deleted.
450 *
451 * @return void
452 */
453 public static function delete_post( $id ) {
454 $container = wc_get_container();
455 if ( ! $container->get( LegacyProxy::class )->call_function( 'current_user_can', 'delete_posts' ) || ! $id ) {
456 return;
457 }
458
459 self::delete_post_data( $id );
460 }
461
462 /**
463 * Trash post.
464 *
465 * @param mixed $id Post ID.
466 *
467 * @return void
468 */
469 public static function trash_post( $id ) {
470 if ( ! $id ) {
471 return;
472 }
473
474 $post_type = self::get_post_type( $id );
475
476 // If this is an order, trash any refunds too.
477 if ( in_array( $post_type, wc_get_order_types( 'order-count' ), true ) ) {
478 global $wpdb;
479
480 $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
481
482 foreach ( $refunds as $refund ) {
483 $wpdb->update( $wpdb->posts, array( 'post_status' => OrderStatus::TRASH ), array( 'ID' => $refund->ID ) );
484 }
485
486 wc_delete_shop_order_transients( $id );
487
488 // If this is a product, trash children variations.
489 } elseif ( 'product' === $post_type ) {
490 $data_store = WC_Data_Store::load( 'product-variable' );
491 $data_store->delete_variations( $id, false );
492 wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_deleted( $id );
493 } elseif ( 'product_variation' === $post_type ) {
494 wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_deleted( $id );
495 }
496 }
497
498 /**
499 * Untrash post.
500 *
501 * @param mixed $id Post ID.
502 *
503 * @return void
504 */
505 public static function untrash_post( $id ) {
506 if ( ! $id ) {
507 return;
508 }
509
510 $post_type = self::get_post_type( $id );
511
512 if ( in_array( $post_type, wc_get_order_types( 'order-count' ), true ) ) {
513 global $wpdb;
514
515 $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
516
517 foreach ( $refunds as $refund ) {
518 $wpdb->update( $wpdb->posts, array( 'post_status' => OrderInternalStatus::COMPLETED ), array( 'ID' => $refund->ID ) );
519 }
520
521 wc_delete_shop_order_transients( $id );
522
523 } elseif ( 'product' === $post_type ) {
524 $data_store = WC_Data_Store::load( 'product-variable' );
525 $data_store->untrash_variations( $id );
526
527 wc_product_force_unique_sku( $id );
528 self::clear_global_unique_id_if_necessary( $id );
529
530 wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_changed( $id );
531 } elseif ( 'product_variation' === $post_type ) {
532 wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_changed( $id );
533 }
534 }
535
536 /**
537 * Clear global unique id if it's not unique.
538 *
539 * @param mixed $id Post ID.
540 *
541 * @return void
542 */
543 private static function clear_global_unique_id_if_necessary( $id ) {
544 $product = wc_get_product( $id );
545 if ( $product && ! wc_product_has_global_unique_id( $id, $product->get_global_unique_id() ) ) {
546 $product->set_global_unique_id( '' );
547 $product->save();
548 }
549 }
550
551 /**
552 * Get the post type for a given post.
553 *
554 * @param int $id The post id.
555 * @return string The post type.
556 */
557 private static function get_post_type( $id ) {
558 return wc_get_container()->get( LegacyProxy::class )->call_function( 'get_post_type', $id );
559 }
560
561 /**
562 * Before deleting an order, do some cleanup.
563 *
564 * @since 3.2.0
565 * @param int $order_id Order ID.
566 *
567 * @return void
568 */
569 public static function before_delete_order( $order_id ) {
570 if ( OrderUtil::is_order( $order_id, wc_get_order_types() ) ) {
571 // Clean up user.
572 $order = wc_get_order( $order_id );
573
574 // Check for `get_customer_id`, since this may be e.g. a refund order (which doesn't implement it).
575 $customer_id = is_callable( array( $order, 'get_customer_id' ) ) ? $order->get_customer_id() : 0;
576
577 if ( $customer_id > 0 && 'shop_order' === $order->get_type() ) {
578 $customer = new WC_Customer( $customer_id );
579 $order_count = $customer->get_order_count();
580 $order_count --;
581
582 if ( 0 === $order_count ) {
583 $customer->set_is_paying_customer( false );
584 $customer->save();
585 }
586
587 // Delete order count and last order meta.
588 delete_user_meta( $customer_id, '_order_count' );
589 delete_user_meta( $customer_id, '_last_order' );
590 }
591
592 // Clean up items.
593 self::delete_order_items( $order_id );
594 self::delete_order_downloadable_permissions( $order_id );
595 }
596 }
597
598 /**
599 * Remove item meta on permanent deletion.
600 *
601 * @param int $postid Post ID.
602 *
603 * @return void
604 */
605 public static function delete_order_items( $postid ) {
606 global $wpdb;
607
608 if ( OrderUtil::is_order( $postid, wc_get_order_types() ) ) {
609 do_action( 'woocommerce_delete_order_items', $postid );
610
611 $wpdb->query(
612 "
613 DELETE {$wpdb->prefix}woocommerce_order_items, {$wpdb->prefix}woocommerce_order_itemmeta
614 FROM {$wpdb->prefix}woocommerce_order_items
615 JOIN {$wpdb->prefix}woocommerce_order_itemmeta ON {$wpdb->prefix}woocommerce_order_items.order_item_id = {$wpdb->prefix}woocommerce_order_itemmeta.order_item_id
616 WHERE {$wpdb->prefix}woocommerce_order_items.order_id = '{$postid}';
617 "
618 ); // WPCS: unprepared SQL ok.
619
620 do_action( 'woocommerce_deleted_order_items', $postid );
621 }
622 }
623
624 /**
625 * Remove downloadable permissions on permanent order deletion.
626 *
627 * @param int $postid Post ID.
628 *
629 * @return void
630 */
631 public static function delete_order_downloadable_permissions( $postid ) {
632 if ( OrderUtil::is_order( $postid, wc_get_order_types() ) ) {
633 do_action( 'woocommerce_delete_order_downloadable_permissions', $postid );
634
635 $data_store = WC_Data_Store::load( 'customer-download' );
636 $data_store->delete_by_order_id( $postid );
637
638 do_action( 'woocommerce_deleted_order_downloadable_permissions', $postid );
639 }
640 }
641
642 /**
643 * Flush meta cache for CRUD objects on direct update.
644 *
645 * @param int $meta_id Meta ID.
646 * @param int $object_id Object ID.
647 * @param string $meta_key Meta key.
648 * @param mixed $meta_value Meta value.
649 *
650 * @return void
651 */
652 public static function flush_object_meta_cache( $meta_id, $object_id, $meta_key, $meta_value ) {
653 WC_Cache_Helper::invalidate_cache_group( 'object_' . $object_id );
654 }
655
656 /**
657 * Ensure default category gets set.
658 *
659 * @since 3.3.0
660 * @param int $object_id Product ID.
661 * @param array $terms Terms array.
662 * @param array $tt_ids Term ids array.
663 * @param string $taxonomy Taxonomy name.
664 * @param bool $append Are we appending or setting terms.
665 *
666 * @return void
667 */
668 public static function force_default_term( $object_id, $terms, $tt_ids, $taxonomy, $append ) {
669 if ( ! $append && 'product_cat' === $taxonomy && empty( $tt_ids ) && 'product' === get_post_type( $object_id ) ) {
670 $default_term = absint( get_option( 'default_product_cat', 0 ) );
671 $tt_ids = array_map( 'absint', $tt_ids );
672
673 if ( $default_term && ! in_array( $default_term, $tt_ids, true ) ) {
674 wp_set_post_terms( $object_id, array( $default_term ), 'product_cat', true );
675 }
676 }
677 }
678
679 /**
680 * Recounts product terms when product visibility changes affect catalog display.
681 *
682 * @param int $object_id The object ID.
683 * @param array $terms An array of object terms.
684 * @param array $tt_ids An array of term taxonomy IDs.
685 * @param string $taxonomy Taxonomy slug.
686 * @param bool $append Whether to append new terms to the old terms.
687 * @param array $old_tt_ids The old array of term taxonomy IDs.
688 *
689 * @since 10.4.0
690 *
691 * @return void
692 */
693 public static function recount_terms_for_product_visibility_change( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
694 if ( 'product_visibility' !== $taxonomy ) {
695 return;
696 }
697
698 if ( $append ) {
699 $modified_tt_ids = $tt_ids;
700 } else {
701 $modified_tt_ids = array_merge( array_diff( $tt_ids, $old_tt_ids ), array_diff( $old_tt_ids, $tt_ids ) );
702 }
703
704 if ( empty( $modified_tt_ids ) ) {
705 return;
706 }
707
708 // Despite the name, wc_get_product_visibility_term_ids() actually returns an associative array with term_taxonomy_ids.
709 $visibility_tt_ids = wc_get_product_visibility_term_ids();
710
711 $tt_ids_modifying_term_counts = array();
712 if ( ! empty( $visibility_tt_ids['exclude-from-catalog'] ) ) {
713 $tt_ids_modifying_term_counts[] = $visibility_tt_ids['exclude-from-catalog'];
714 }
715
716 if ( ! empty( $visibility_tt_ids['outofstock'] ) && 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
717 $tt_ids_modifying_term_counts[] = $visibility_tt_ids['outofstock'];
718 }
719
720 if ( ! empty( array_intersect( $modified_tt_ids, $tt_ids_modifying_term_counts ) ) ) {
721 _wc_recount_terms_by_product( $object_id );
722 }
723 }
724
725 /**
726 * Ensure statuses are correctly reassigned when restoring orders and products.
727 *
728 * @param string $new_status The new status of the post being restored.
729 * @param int $post_id The ID of the post being restored.
730 * @param string $previous_status The status of the post at the point where it was trashed.
731 * @return string
732 */
733 public static function wp_untrash_post_status( $new_status, $post_id, $previous_status ) {
734 $post_types = array( 'shop_order', 'shop_coupon', 'product', 'product_variation' );
735
736 if ( in_array( get_post_type( $post_id ), $post_types, true ) ) {
737 $new_status = $previous_status;
738 }
739
740 return $new_status;
741 }
742
743 /**
744 * When setting stock level, ensure the stock status is kept in sync.
745 *
746 * @param int $meta_id Meta ID.
747 * @param int $object_id Object ID.
748 * @param string $meta_key Meta key.
749 * @param mixed $meta_value Meta value.
750 * @deprecated 3.3
751 *
752 * @return void
753 */
754 public static function sync_product_stock_status( $meta_id, $object_id, $meta_key, $meta_value ) {}
755
756 /**
757 * Update changed downloads.
758 *
759 * @deprecated 3.3.0 No action is necessary on changes to download paths since download_id is no longer based on file hash.
760 * @param int $product_id Product ID.
761 * @param int $variation_id Variation ID. Optional product variation identifier.
762 * @param array $downloads Newly set files.
763 *
764 * @return void
765 */
766 public static function process_product_file_download_paths( $product_id, $variation_id, $downloads ) {
767 wc_deprecated_function( __FUNCTION__, '3.3' );
768 }
769
770 /**
771 * Delete transients when terms are set.
772 *
773 * @deprecated 3.6
774 * @param int $object_id Object ID.
775 * @param mixed $terms An array of object terms.
776 * @param array $tt_ids An array of term taxonomy IDs.
777 * @param string $taxonomy Taxonomy slug.
778 * @param mixed $append Whether to append new terms to the old terms.
779 * @param array $old_tt_ids Old array of term taxonomy IDs.
780 *
781 * @return void
782 */
783 public static function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
784 if ( in_array( get_post_type( $object_id ), array( 'product', 'product_variation' ), true ) ) {
785 self::delete_product_query_transients();
786 }
787 }
788
789 /**
790 * Regenerates attribute summaries for a list of variations.
791 *
792 * @since 10.2.0
793 * @param array $variation_ids Array of variation IDs.
794 *
795 * @return void
796 */
797 private static function regenerate_variation_summaries( $variation_ids ) {
798 if ( empty( $variation_ids ) ) {
799 return;
800 }
801
802 $variation_ids = array_unique( array_filter( array_map( 'intval', $variation_ids ) ) );
803
804 foreach ( $variation_ids as $variation_id ) {
805 self::regenerate_variation_attribute_summary( $variation_id );
806 }
807 }
808
809 /**
810 * Regenerates the attribute summary for a single variation.
811 *
812 * @since 10.2.0
813 * @param int $variation_id Variation ID.
814 *
815 * @return void
816 */
817 public static function regenerate_variation_attribute_summary( $variation_id ) {
818 global $wpdb;
819
820 $product = wc_get_product( $variation_id );
821 if ( ! $product || ! $product->is_type( 'variation' ) ) {
822 return;
823 }
824
825 $data_store = WC_Data_Store::load( 'product-variation' );
826 if ( $data_store->has_callable( 'get_attribute_summary' ) ) {
827 $new_summary = $data_store->get_attribute_summary( $product );
828 $current_excerpt = get_post_field( 'post_excerpt', $variation_id );
829 if ( $new_summary === $current_excerpt ) {
830 return;
831 }
832
833 /**
834 * Update directly via $wpdb for performance: Avoid firing save_post hooks, loading full post objects,
835 * and creating revisions. This is safe here as we're only updating post_excerpt.
836 */
837 $wpdb->update(
838 $wpdb->posts,
839 array( 'post_excerpt' => $new_summary ),
840 array( 'ID' => $variation_id )
841 );
842 clean_post_cache( $variation_id );
843 /**
844 * Fires after the attribute summary of a product variation has been updated.
845 *
846 * @since 10.2.0
847 * @param int $variation_id The ID of the product variation.
848 */
849 do_action( 'woocommerce_updated_product_attribute_summary', $variation_id );
850 }
851 }
852
853 /**
854 * Gets the threshold for synchronous regeneration of variation summaries.
855 *
856 * @since 10.2.0
857 * @return int
858 */
859 public static function get_variation_summaries_sync_threshold() {
860 /**
861 * Filters the threshold for synchronous regeneration of variation attribute summaries.
862 * If the number of variations affected by an update is below this threshold, the summaries
863 * are regenerated synchronously. Otherwise, the regeneration is scheduled asynchronously.
864 *
865 * @since 10.2.0
866 * @param int $threshold The default threshold value (50).
867 * @return int The filtered threshold value.
868 */
869 return absint( apply_filters( 'woocommerce_regenerate_variation_summaries_sync_threshold', 50 ) );
870 }
871
872 /**
873 * Handles updates to a global attribute by triggering variation summary regeneration.
874 *
875 * @since 10.2.0
876 * @param int $attribute_id Attribute ID.
877 * @param string $attribute Attribute name.
878 * @param string $old_slug Old attribute slug.
879 *
880 * @return void
881 */
882 public static function handle_global_attribute_updated( $attribute_id, $attribute, $old_slug ) {
883 // We use this trigger for both updates and deletions of global attributes.
884 // They pass different parameters to $old_slug - deleted attributes include the "pa_" prefix, while updated attributes do not.
885 // Remove it if existing for consistency.
886 if ( strpos( $old_slug, 'pa_' ) === 0 ) {
887 $old_slug = substr( $old_slug, 3 );
888 }
889 $taxonomy = 'pa_' . $old_slug;
890 $threshold = self::get_variation_summaries_sync_threshold();
891 // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query
892 $args = array(
893 'post_type' => 'product_variation',
894 'post_status' => 'any',
895 'posts_per_page' => $threshold + 1,
896 'fields' => 'ids',
897 'meta_query' => array(
898 array(
899 'key' => 'attribute_' . $taxonomy,
900 'compare' => 'EXISTS',
901 ),
902 ),
903 );
904 // phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_meta_query
905
906 $variation_ids = get_posts( $args );
907
908 if ( empty( $variation_ids ) ) {
909 return;
910 }
911
912 if ( count( $variation_ids ) <= $threshold ) {
913 // Update variation summaries that used this product attribute, but
914 // wait until shutdown. This will allow WooC to carry out post_meta migrations
915 // if the slug of the attribute changed.
916 add_action(
917 'shutdown',
918 function () use ( $variation_ids ) {
919 self::regenerate_variation_summaries( $variation_ids );
920 }
921 );
922 } else {
923 $new_slug = ! empty( $attribute['attribute_name'] ) ? $attribute['attribute_name'] : $old_slug;
924 $new_taxonomy = 'pa_' . $new_slug;
925
926 self::schedule_variation_summary_regeneration(
927 'wc_regenerate_attribute_variation_summaries',
928 array( $new_taxonomy ),
929 'Taxonomy: ' . $taxonomy . ', Attribute ID: ' . $attribute_id
930 );
931 }
932 }
933
934 /**
935 * Regenerates variation summaries for all variations using a specific attribute taxonomy.
936 *
937 * @since 10.2.0
938 * @param string $taxonomy Attribute taxonomy.
939 *
940 * @return void
941 */
942 public static function regenerate_attribute_variation_summaries( $taxonomy ) {
943 // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query
944 $variation_ids = get_posts(
945 array(
946 'post_type' => 'product_variation',
947 'numberposts' => -1,
948 'fields' => 'ids',
949 'meta_query' => array(
950 array(
951 'key' => 'attribute_' . $taxonomy,
952 'compare' => 'EXISTS',
953 ),
954 ),
955 )
956 );
957 // phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_meta_query
958 self::regenerate_variation_summaries( $variation_ids );
959 }
960
961 /**
962 * Handles regeneration of variation summaries when a variable product's attributes are updated.
963 *
964 * @since 10.2.0
965 * @param WC_Product $product The variable product whose attributes were updated.
966 *
967 * @return void
968 */
969 public static function on_product_attributes_updated( $product ) {
970 if ( $product->is_type( 'variable' ) ) {
971 global $wpdb;
972 $threshold = self::get_variation_summaries_sync_threshold();
973 $variation_ids = $wpdb->get_col(
974 $wpdb->prepare(
975 "SELECT DISTINCT ID
976 FROM {$wpdb->posts}
977 WHERE post_parent = %d
978 AND post_type = %s
979 LIMIT %d
980 ",
981 $product->get_id(),
982 'product_variation',
983 $threshold + 1
984 )
985 );
986
987 if ( empty( $variation_ids ) ) {
988 return;
989 }
990
991 if ( count( $variation_ids ) <= $threshold ) {
992 // If the number of variations is below the threshold, regenerate summaries synchronously.
993 $variation_ids = $product->get_children();
994 self::regenerate_variation_summaries( $variation_ids );
995 } else {
996 self::schedule_variation_summary_regeneration(
997 'wc_regenerate_product_variation_summaries',
998 array( $product->get_id() ),
999 'Product ID: ' . $product->get_id()
1000 );
1001 }
1002 }
1003 }
1004
1005 /**
1006 * Regenerates variation summaries for all variations of a variable product.
1007 *
1008 * @since 10.2.0
1009 * @param int $product_id Variable product ID.
1010 *
1011 * @return void
1012 */
1013 public static function regenerate_product_variation_summaries( $product_id ) {
1014 $product = wc_get_product( $product_id );
1015 if ( ! $product || ! $product->is_type( 'variable' ) ) {
1016 return;
1017 }
1018
1019 $variation_ids = $product->get_children();
1020 self::regenerate_variation_summaries( $variation_ids );
1021 }
1022
1023 /**
1024 * Hook called after a term is updated to handle updates for product variations.
1025 *
1026 * @param int $term_id Term ID.
1027 * @param int $tt_id Term taxonomy ID.
1028 * @param string $taxonomy Taxonomy slug.
1029 *
1030 * @return void
1031 */
1032 public static function handle_attribute_term_updated( $term_id, $tt_id, $taxonomy ) {
1033 if ( strpos( $taxonomy, 'pa_' ) !== 0 ) {
1034 return;
1035 }
1036
1037 $new_term = get_term( $term_id, $taxonomy );
1038 if ( is_wp_error( $new_term ) || ! $new_term ) {
1039 return;
1040 }
1041
1042 $meta_key = 'attribute_' . $taxonomy;
1043 global $wpdb;
1044
1045 $threshold = self::get_variation_summaries_sync_threshold();
1046 $variation_ids = $wpdb->get_col(
1047 $wpdb->prepare(
1048 "SELECT DISTINCT pm.post_id
1049 FROM $wpdb->postmeta pm
1050 INNER JOIN $wpdb->posts p ON pm.post_id = p.ID
1051 WHERE pm.meta_key = %s
1052 AND pm.meta_value = %s
1053 AND p.post_type = 'product_variation'
1054 LIMIT %d
1055 ",
1056 $meta_key,
1057 $new_term->slug,
1058 $threshold + 1
1059 )
1060 );
1061 if ( empty( $variation_ids ) ) {
1062 return;
1063 }
1064
1065 if ( count( $variation_ids ) <= $threshold ) {
1066 // If the number of variations is below the threshold, regenerate summaries synchronously.
1067 self::regenerate_variation_summaries( $variation_ids );
1068 } else {
1069 self::schedule_variation_summary_regeneration(
1070 'wc_regenerate_term_variation_summaries',
1071 array( $taxonomy, $new_term->slug ),
1072 'Taxonomy: ' . $taxonomy . ', Term ID: ' . $term_id
1073 );
1074 }
1075 }
1076
1077 /**
1078 * Hook called after a term is deleted to handle updates for product variations.
1079 *
1080 * @param int $term_id Term ID.
1081 * @param int $tt_id Term taxonomy ID.
1082 * @param string $taxonomy Taxonomy slug.
1083 * @param WP_Term $deleted_term Copy of the already-deleted term.
1084 *
1085 * @return void
1086 */
1087 public static function handle_attribute_term_deleted( $term_id, $tt_id, $taxonomy, $deleted_term ) {
1088 if ( strpos( $taxonomy, 'pa_' ) !== 0 ) {
1089 return;
1090 }
1091
1092 $meta_key = 'attribute_' . $taxonomy;
1093 global $wpdb;
1094 $threshold = self::get_variation_summaries_sync_threshold();
1095 $variation_ids = $wpdb->get_col(
1096 $wpdb->prepare(
1097 "SELECT DISTINCT pm.post_id
1098 FROM $wpdb->postmeta pm
1099 INNER JOIN $wpdb->posts p ON pm.post_id = p.ID
1100 WHERE pm.meta_key = %s
1101 AND pm.meta_value = %s
1102 AND p.post_type = 'product_variation'
1103 LIMIT %d
1104 ",
1105 $meta_key,
1106 $deleted_term->slug,
1107 $threshold + 1
1108 )
1109 );
1110
1111 if ( empty( $variation_ids ) ) {
1112 return;
1113 }
1114
1115 if ( count( $variation_ids ) <= $threshold ) {
1116 // If the number of variations is below the threshold, regenerate summaries synchronously.
1117 self::regenerate_variation_summaries( $variation_ids );
1118 } else {
1119 self::schedule_variation_summary_regeneration(
1120 'wc_regenerate_term_variation_summaries',
1121 array( $taxonomy, $deleted_term->slug ),
1122 'Taxonomy: ' . $taxonomy . ', Term ID: ' . $term_id
1123 );
1124 }
1125 }
1126
1127 /**
1128 * Schedule an asynchronous action to regenerate product variation summaries.
1129 *
1130 * This method uses the WooCommerce Action Scheduler to queue a single regeneration action
1131 * for product variation summaries. It first checks whether an identical action with the
1132 * given arguments is already scheduled to avoid duplicate jobs. If the Action Scheduler
1133 * is not available, a warning is logged instead.
1134 *
1135 * @param string $action_name The name/identifier of the scheduled action (hook name).
1136 * @param array $args Arguments to pass to the scheduled action callback.
1137 * @param string $warning_message Message to log when the Action Scheduler is unavailable.
1138 * @param string $group Optional. The Action Scheduler group to associate with
1139 * the scheduled action. Default 'woocommerce'.
1140 *
1141 * @return void
1142 */
1143 private static function schedule_variation_summary_regeneration( $action_name, $args, $warning_message, $group = 'woocommerce' ) {
1144 if ( function_exists( 'as_schedule_single_action' ) ) {
1145 // Prevent duplicate scheduling of the action.
1146 $when = as_next_scheduled_action( $action_name, $args, $group );
1147 if ( ! $when ) {
1148 as_schedule_single_action( time() + 1, $action_name, $args, $group );
1149 }
1150 } else {
1151 wc_get_logger()->warning(
1152 'Action Scheduler unavailable for product variation summary regeneration. ' . $warning_message,
1153 array( 'source' => 'woocommerce-variations' )
1154 );
1155 }
1156 }
1157
1158 /**
1159 * Regenerates variation summaries for all variations using a specific term.
1160 *
1161 * @since 10.2.0
1162 * @param string $taxonomy Taxonomy slug.
1163 * @param string $term_slug Term slug.
1164 *
1165 * @return void
1166 */
1167 public static function regenerate_term_variation_summaries( $taxonomy, $term_slug ) {
1168 global $wpdb;
1169
1170 $variation_ids = $wpdb->get_col(
1171 $wpdb->prepare(
1172 "SELECT pm.post_id FROM {$wpdb->postmeta} pm
1173 INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID
1174 WHERE pm.meta_key = %s
1175 AND pm.meta_value = %s
1176 AND p.post_type = %s",
1177 'attribute_' . $taxonomy,
1178 $term_slug,
1179 'product_variation'
1180 )
1181 );
1182
1183 self::regenerate_variation_summaries( $variation_ids );
1184 }
1185 }
1186
1187 WC_Post_Data::init();
1188