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3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / class-wc-post-data.php
woocommerce / includes Last commit date
abstracts 5 years ago admin 5 years ago cli 5 years ago customizer 5 years ago data-stores 5 years ago emails 5 years ago export 5 years ago gateways 5 years ago import 5 years ago integrations 5 years ago interfaces 5 years ago legacy 5 years ago libraries 6 years ago log-handlers 5 years ago payment-tokens 5 years ago queue 5 years ago rest-api 5 years ago shipping 5 years ago shortcodes 5 years ago theme-support 5 years ago tracks 5 years ago traits 5 years ago walkers 5 years ago wccom-site 5 years ago widgets 5 years ago class-wc-ajax.php 5 years ago class-wc-api.php 5 years ago class-wc-auth.php 5 years ago class-wc-autoloader.php 5 years ago class-wc-background-emailer.php 5 years ago class-wc-background-updater.php 5 years ago class-wc-breadcrumb.php 5 years ago class-wc-cache-helper.php 5 years ago class-wc-cart-fees.php 5 years ago class-wc-cart-session.php 5 years ago class-wc-cart-totals.php 5 years ago class-wc-cart.php 5 years ago class-wc-checkout.php 5 years ago class-wc-cli.php 8 years ago class-wc-comments.php 5 years ago class-wc-countries.php 5 years ago class-wc-coupon.php 5 years ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 5 years ago class-wc-customer.php 5 years ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 6 years ago class-wc-datetime.php 5 years ago class-wc-deprecated-action-hooks.php 8 years ago class-wc-deprecated-filter-hooks.php 7 years ago class-wc-discounts.php 5 years ago class-wc-download-handler.php 5 years ago class-wc-emails.php 5 years ago class-wc-embed.php 5 years ago class-wc-form-handler.php 5 years ago class-wc-frontend-scripts.php 5 years ago class-wc-geo-ip.php 5 years ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 5 years ago class-wc-https.php 5 years ago class-wc-install.php 5 years ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 5 years ago class-wc-logger.php 5 years ago class-wc-meta-data.php 7 years ago class-wc-order-factory.php 5 years ago class-wc-order-item-coupon.php 5 years ago class-wc-order-item-fee.php 5 years ago class-wc-order-item-meta.php 5 years ago class-wc-order-item-product.php 5 years ago class-wc-order-item-shipping.php 5 years ago class-wc-order-item-tax.php 5 years ago class-wc-order-item.php 5 years ago class-wc-order-query.php 5 years ago class-wc-order-refund.php 5 years ago class-wc-order.php 5 years ago class-wc-payment-gateways.php 5 years ago class-wc-payment-tokens.php 5 years ago class-wc-post-data.php 5 years ago class-wc-post-types.php 5 years ago class-wc-privacy-background-process.php 5 years ago class-wc-privacy-erasers.php 6 years ago class-wc-privacy-exporters.php 6 years ago class-wc-privacy.php 5 years ago class-wc-product-attribute.php 5 years ago class-wc-product-download.php 5 years ago class-wc-product-external.php 5 years ago class-wc-product-factory.php 5 years ago class-wc-product-grouped.php 8 years ago class-wc-product-query.php 5 years ago class-wc-product-simple.php 5 years ago class-wc-product-variable.php 5 years ago class-wc-product-variation.php 5 years ago class-wc-query.php 5 years ago class-wc-rate-limiter.php 5 years ago class-wc-regenerate-images-request.php 5 years ago class-wc-regenerate-images.php 5 years ago class-wc-register-wp-admin-settings.php 5 years ago class-wc-rest-authentication.php 5 years ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 5 years ago class-wc-shipping-rate.php 5 years ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 5 years ago class-wc-shipping.php 5 years ago class-wc-shortcodes.php 5 years ago class-wc-structured-data.php 5 years ago class-wc-tax.php 5 years ago class-wc-template-loader.php 5 years ago class-wc-tracker.php 5 years ago class-wc-validation.php 5 years ago class-wc-webhook.php 5 years ago class-woocommerce.php 5 years ago wc-account-functions.php 5 years ago wc-attribute-functions.php 5 years ago wc-cart-functions.php 5 years ago wc-conditional-functions.php 5 years ago wc-core-functions.php 5 years ago wc-coupon-functions.php 5 years ago wc-deprecated-functions.php 5 years ago wc-formatting-functions.php 5 years ago wc-notice-functions.php 5 years ago wc-order-functions.php 5 years ago wc-order-item-functions.php 5 years ago wc-page-functions.php 5 years ago wc-product-functions.php 5 years ago wc-rest-functions.php 5 years ago wc-stock-functions.php 5 years ago wc-template-functions.php 5 years ago wc-template-hooks.php 5 years ago wc-term-functions.php 5 years ago wc-update-functions.php 5 years ago wc-user-functions.php 5 years ago wc-webhook-functions.php 5 years ago wc-widget-functions.php 5 years ago
class-wc-post-data.php
565 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 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Post data class.
15 */
16 class WC_Post_Data {
17
18 /**
19 * Editing term.
20 *
21 * @var object
22 */
23 private static $editing_term = null;
24
25 /**
26 * Hook in methods.
27 */
28 public static function init() {
29 add_filter( 'post_type_link', array( __CLASS__, 'variation_post_link' ), 10, 2 );
30 add_action( 'shutdown', array( __CLASS__, 'do_deferred_product_sync' ), 10 );
31 add_action( 'set_object_terms', array( __CLASS__, 'force_default_term' ), 10, 5 );
32 add_action( 'set_object_terms', array( __CLASS__, 'delete_product_query_transients' ) );
33 add_action( 'deleted_term_relationships', array( __CLASS__, 'delete_product_query_transients' ) );
34 add_action( 'woocommerce_product_set_stock_status', array( __CLASS__, 'delete_product_query_transients' ) );
35 add_action( 'woocommerce_product_set_visibility', array( __CLASS__, 'delete_product_query_transients' ) );
36 add_action( 'woocommerce_product_type_changed', array( __CLASS__, 'product_type_changed' ), 10, 3 );
37
38 add_action( 'edit_term', array( __CLASS__, 'edit_term' ), 10, 3 );
39 add_action( 'edited_term', array( __CLASS__, 'edited_term' ), 10, 3 );
40 add_filter( 'update_order_item_metadata', array( __CLASS__, 'update_order_item_metadata' ), 10, 5 );
41 add_filter( 'update_post_metadata', array( __CLASS__, 'update_post_metadata' ), 10, 5 );
42 add_filter( 'wp_insert_post_data', array( __CLASS__, 'wp_insert_post_data' ) );
43 add_filter( 'oembed_response_data', array( __CLASS__, 'filter_oembed_response_data' ), 10, 2 );
44 add_filter( 'wp_untrash_post_status', array( __CLASS__, 'wp_untrash_post_status' ), 10, 3 );
45
46 // Status transitions.
47 add_action( 'transition_post_status', array( __CLASS__, 'transition_post_status' ), 10, 3 );
48 add_action( 'delete_post', array( __CLASS__, 'delete_post' ) );
49 add_action( 'wp_trash_post', array( __CLASS__, 'trash_post' ) );
50 add_action( 'untrashed_post', array( __CLASS__, 'untrash_post' ) );
51 add_action( 'before_delete_post', array( __CLASS__, 'before_delete_order' ) );
52
53 // Meta cache flushing.
54 add_action( 'updated_post_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
55 add_action( 'updated_order_item_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
56 }
57
58 /**
59 * Link to parent products when getting permalink for variation.
60 *
61 * @param string $permalink Permalink.
62 * @param WP_Post $post Post data.
63 *
64 * @return string
65 */
66 public static function variation_post_link( $permalink, $post ) {
67 if ( isset( $post->ID, $post->post_type ) && 'product_variation' === $post->post_type ) {
68 $variation = wc_get_product( $post->ID );
69
70 if ( $variation && $variation->get_parent_id() ) {
71 return $variation->get_permalink();
72 }
73 }
74 return $permalink;
75 }
76
77 /**
78 * Sync products queued to sync.
79 */
80 public static function do_deferred_product_sync() {
81 global $wc_deferred_product_sync;
82
83 if ( ! empty( $wc_deferred_product_sync ) ) {
84 $wc_deferred_product_sync = wp_parse_id_list( $wc_deferred_product_sync );
85 array_walk( $wc_deferred_product_sync, array( __CLASS__, 'deferred_product_sync' ) );
86 }
87 }
88
89 /**
90 * Sync a product.
91 *
92 * @param int $product_id Product ID.
93 */
94 public static function deferred_product_sync( $product_id ) {
95 $product = wc_get_product( $product_id );
96
97 if ( is_callable( array( $product, 'sync' ) ) ) {
98 $product->sync( $product );
99 }
100 }
101
102 /**
103 * When a post status changes.
104 *
105 * @param string $new_status New status.
106 * @param string $old_status Old status.
107 * @param WP_Post $post Post data.
108 */
109 public static function transition_post_status( $new_status, $old_status, $post ) {
110 if ( ( 'publish' === $new_status || 'publish' === $old_status ) && in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) {
111 self::delete_product_query_transients();
112 }
113 }
114
115 /**
116 * Delete product view transients when needed e.g. when post status changes, or visibility/stock status is modified.
117 */
118 public static function delete_product_query_transients() {
119 WC_Cache_Helper::get_transient_version( 'product_query', true );
120 }
121
122 /**
123 * Handle type changes.
124 *
125 * @since 3.0.0
126 *
127 * @param WC_Product $product Product data.
128 * @param string $from Origin type.
129 * @param string $to New type.
130 */
131 public static function product_type_changed( $product, $from, $to ) {
132 /**
133 * Filter to prevent variations from being deleted while switching from a variable product type to a variable product type.
134 *
135 * @since 5.0.0
136 *
137 * @param bool A boolean value of true will delete the variations.
138 * @param WC_Product $product Product data.
139 * @return string $from Origin type.
140 * @param string $to New type.
141 */
142 if ( apply_filters( 'woocommerce_delete_variations_on_product_type_change', 'variable' === $from && 'variable' !== $to, $product, $from, $to ) ) {
143 // If the product is no longer variable, we should ensure all variations are removed.
144 $data_store = WC_Data_Store::load( 'product-variable' );
145 $data_store->delete_variations( $product->get_id(), true );
146 }
147 }
148
149 /**
150 * When editing a term, check for product attributes.
151 *
152 * @param int $term_id Term ID.
153 * @param int $tt_id Term taxonomy ID.
154 * @param string $taxonomy Taxonomy slug.
155 */
156 public static function edit_term( $term_id, $tt_id, $taxonomy ) {
157 if ( strpos( $taxonomy, 'pa_' ) === 0 ) {
158 self::$editing_term = get_term_by( 'id', $term_id, $taxonomy );
159 } else {
160 self::$editing_term = null;
161 }
162 }
163
164 /**
165 * When a term is edited, check for product attributes and update variations.
166 *
167 * @param int $term_id Term ID.
168 * @param int $tt_id Term taxonomy ID.
169 * @param string $taxonomy Taxonomy slug.
170 */
171 public static function edited_term( $term_id, $tt_id, $taxonomy ) {
172 if ( ! is_null( self::$editing_term ) && strpos( $taxonomy, 'pa_' ) === 0 ) {
173 $edited_term = get_term_by( 'id', $term_id, $taxonomy );
174
175 if ( $edited_term->slug !== self::$editing_term->slug ) {
176 global $wpdb;
177
178 $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 ) );
179
180 $wpdb->query(
181 $wpdb->prepare(
182 "UPDATE {$wpdb->postmeta} SET meta_value = REPLACE( meta_value, %s, %s ) WHERE meta_key = '_default_attributes'",
183 serialize( self::$editing_term->taxonomy ) . serialize( self::$editing_term->slug ),
184 serialize( $edited_term->taxonomy ) . serialize( $edited_term->slug )
185 )
186 );
187 }
188 } else {
189 self::$editing_term = null;
190 }
191 }
192
193 /**
194 * Ensure floats are correctly converted to strings based on PHP locale.
195 *
196 * @param null $check Whether to allow updating metadata for the given type.
197 * @param int $object_id Object ID.
198 * @param string $meta_key Meta key.
199 * @param mixed $meta_value Meta value. Must be serializable if non-scalar.
200 * @param mixed $prev_value If specified, only update existing metadata entries with the specified value. Otherwise, update all entries.
201 * @return null|bool
202 */
203 public static function update_order_item_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
204 if ( ! empty( $meta_value ) && is_float( $meta_value ) ) {
205
206 // Convert float to string.
207 $meta_value = wc_float_to_string( $meta_value );
208
209 // Update meta value with new string.
210 update_metadata( 'order_item', $object_id, $meta_key, $meta_value, $prev_value );
211
212 return true;
213 }
214 return $check;
215 }
216
217 /**
218 * Ensure floats are correctly converted to strings based on PHP locale.
219 *
220 * @param null $check Whether to allow updating metadata for the given type.
221 * @param int $object_id Object ID.
222 * @param string $meta_key Meta key.
223 * @param mixed $meta_value Meta value. Must be serializable if non-scalar.
224 * @param mixed $prev_value If specified, only update existing metadata entries with the specified value. Otherwise, update all entries.
225 * @return null|bool
226 */
227 public static function update_post_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
228 // Delete product cache if someone uses meta directly.
229 if ( in_array( get_post_type( $object_id ), array( 'product', 'product_variation' ), true ) ) {
230 wp_cache_delete( 'product-' . $object_id, 'products' );
231 }
232
233 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 ) ) {
234
235 // Convert float to string.
236 $meta_value = wc_float_to_string( $meta_value );
237
238 // Update meta value with new string.
239 update_metadata( 'post', $object_id, $meta_key, $meta_value, $prev_value );
240
241 return true;
242 }
243 return $check;
244 }
245
246 /**
247 * Forces the order posts to have a title in a certain format (containing the date).
248 * Forces certain product data based on the product's type, e.g. grouped products cannot have a parent.
249 *
250 * @param array $data An array of slashed post data.
251 * @return array
252 */
253 public static function wp_insert_post_data( $data ) {
254 if ( 'shop_order' === $data['post_type'] && isset( $data['post_date'] ) ) {
255 $order_title = 'Order';
256 if ( $data['post_date'] ) {
257 $order_title .= ' &ndash; ' . date_i18n( 'F j, Y @ h:i A', strtotime( $data['post_date'] ) );
258 }
259 $data['post_title'] = $order_title;
260 } elseif ( 'product' === $data['post_type'] && isset( $_POST['product-type'] ) ) { // WPCS: input var ok, CSRF ok.
261 $product_type = wc_clean( wp_unslash( $_POST['product-type'] ) ); // WPCS: input var ok, CSRF ok.
262 switch ( $product_type ) {
263 case 'grouped':
264 case 'variable':
265 $data['post_parent'] = 0;
266 break;
267 }
268 } elseif ( 'product' === $data['post_type'] && 'auto-draft' === $data['post_status'] ) {
269 $data['post_title'] = 'AUTO-DRAFT';
270 } elseif ( 'shop_coupon' === $data['post_type'] ) {
271 // Coupons should never allow unfiltered HTML.
272 $data['post_title'] = wp_filter_kses( $data['post_title'] );
273 }
274
275 return $data;
276 }
277
278 /**
279 * Change embed data for certain post types.
280 *
281 * @since 3.2.0
282 * @param array $data The response data.
283 * @param WP_Post $post The post object.
284 * @return array
285 */
286 public static function filter_oembed_response_data( $data, $post ) {
287 if ( in_array( $post->post_type, array( 'shop_order', 'shop_coupon' ), true ) ) {
288 return array();
289 }
290 return $data;
291 }
292
293 /**
294 * Removes variations etc belonging to a deleted post, and clears transients.
295 *
296 * @param mixed $id ID of post being deleted.
297 */
298 public static function delete_post( $id ) {
299 if ( ! current_user_can( 'delete_posts' ) || ! $id ) {
300 return;
301 }
302
303 $post_type = get_post_type( $id );
304
305 switch ( $post_type ) {
306 case 'product':
307 $data_store = WC_Data_Store::load( 'product-variable' );
308 $data_store->delete_variations( $id, true );
309 $data_store->delete_from_lookup_table( $id, 'wc_product_meta_lookup' );
310 $parent_id = wp_get_post_parent_id( $id );
311
312 if ( $parent_id ) {
313 wc_delete_product_transients( $parent_id );
314 }
315 break;
316 case 'product_variation':
317 $data_store = WC_Data_Store::load( 'product' );
318 $data_store->delete_from_lookup_table( $id, 'wc_product_meta_lookup' );
319 wc_delete_product_transients( wp_get_post_parent_id( $id ) );
320 break;
321 case 'shop_order':
322 global $wpdb;
323
324 $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
325
326 if ( ! is_null( $refunds ) ) {
327 foreach ( $refunds as $refund ) {
328 wp_delete_post( $refund->ID, true );
329 }
330 }
331 break;
332 }
333 }
334
335 /**
336 * Trash post.
337 *
338 * @param mixed $id Post ID.
339 */
340 public static function trash_post( $id ) {
341 if ( ! $id ) {
342 return;
343 }
344
345 $post_type = get_post_type( $id );
346
347 // If this is an order, trash any refunds too.
348 if ( in_array( $post_type, wc_get_order_types( 'order-count' ), true ) ) {
349 global $wpdb;
350
351 $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
352
353 foreach ( $refunds as $refund ) {
354 $wpdb->update( $wpdb->posts, array( 'post_status' => 'trash' ), array( 'ID' => $refund->ID ) );
355 }
356
357 wc_delete_shop_order_transients( $id );
358
359 // If this is a product, trash children variations.
360 } elseif ( 'product' === $post_type ) {
361 $data_store = WC_Data_Store::load( 'product-variable' );
362 $data_store->delete_variations( $id, false );
363 }
364 }
365
366 /**
367 * Untrash post.
368 *
369 * @param mixed $id Post ID.
370 */
371 public static function untrash_post( $id ) {
372 if ( ! $id ) {
373 return;
374 }
375
376 $post_type = get_post_type( $id );
377
378 if ( in_array( $post_type, wc_get_order_types( 'order-count' ), true ) ) {
379 global $wpdb;
380
381 $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
382
383 foreach ( $refunds as $refund ) {
384 $wpdb->update( $wpdb->posts, array( 'post_status' => 'wc-completed' ), array( 'ID' => $refund->ID ) );
385 }
386
387 wc_delete_shop_order_transients( $id );
388
389 } elseif ( 'product' === $post_type ) {
390 $data_store = WC_Data_Store::load( 'product-variable' );
391 $data_store->untrash_variations( $id );
392
393 wc_product_force_unique_sku( $id );
394 }
395 }
396
397 /**
398 * Before deleting an order, do some cleanup.
399 *
400 * @since 3.2.0
401 * @param int $order_id Order ID.
402 */
403 public static function before_delete_order( $order_id ) {
404 if ( in_array( get_post_type( $order_id ), wc_get_order_types(), true ) ) {
405 // Clean up user.
406 $order = wc_get_order( $order_id );
407
408 // Check for `get_customer_id`, since this may be e.g. a refund order (which doesn't implement it).
409 $customer_id = is_callable( array( $order, 'get_customer_id' ) ) ? $order->get_customer_id() : 0;
410
411 if ( $customer_id > 0 && 'shop_order' === $order->get_type() ) {
412 $customer = new WC_Customer( $customer_id );
413 $order_count = $customer->get_order_count();
414 $order_count --;
415
416 if ( 0 === $order_count ) {
417 $customer->set_is_paying_customer( false );
418 $customer->save();
419 }
420
421 // Delete order count and last order meta.
422 delete_user_meta( $customer_id, '_order_count' );
423 delete_user_meta( $customer_id, '_last_order' );
424 }
425
426 // Clean up items.
427 self::delete_order_items( $order_id );
428 self::delete_order_downloadable_permissions( $order_id );
429 }
430 }
431
432 /**
433 * Remove item meta on permanent deletion.
434 *
435 * @param int $postid Post ID.
436 */
437 public static function delete_order_items( $postid ) {
438 global $wpdb;
439
440 if ( in_array( get_post_type( $postid ), wc_get_order_types(), true ) ) {
441 do_action( 'woocommerce_delete_order_items', $postid );
442
443 $wpdb->query(
444 "
445 DELETE {$wpdb->prefix}woocommerce_order_items, {$wpdb->prefix}woocommerce_order_itemmeta
446 FROM {$wpdb->prefix}woocommerce_order_items
447 JOIN {$wpdb->prefix}woocommerce_order_itemmeta ON {$wpdb->prefix}woocommerce_order_items.order_item_id = {$wpdb->prefix}woocommerce_order_itemmeta.order_item_id
448 WHERE {$wpdb->prefix}woocommerce_order_items.order_id = '{$postid}';
449 "
450 ); // WPCS: unprepared SQL ok.
451
452 do_action( 'woocommerce_deleted_order_items', $postid );
453 }
454 }
455
456 /**
457 * Remove downloadable permissions on permanent order deletion.
458 *
459 * @param int $postid Post ID.
460 */
461 public static function delete_order_downloadable_permissions( $postid ) {
462 if ( in_array( get_post_type( $postid ), wc_get_order_types(), true ) ) {
463 do_action( 'woocommerce_delete_order_downloadable_permissions', $postid );
464
465 $data_store = WC_Data_Store::load( 'customer-download' );
466 $data_store->delete_by_order_id( $postid );
467
468 do_action( 'woocommerce_deleted_order_downloadable_permissions', $postid );
469 }
470 }
471
472 /**
473 * Flush meta cache for CRUD objects on direct update.
474 *
475 * @param int $meta_id Meta ID.
476 * @param int $object_id Object ID.
477 * @param string $meta_key Meta key.
478 * @param string $meta_value Meta value.
479 */
480 public static function flush_object_meta_cache( $meta_id, $object_id, $meta_key, $meta_value ) {
481 WC_Cache_Helper::invalidate_cache_group( 'object_' . $object_id );
482 }
483
484 /**
485 * Ensure default category gets set.
486 *
487 * @since 3.3.0
488 * @param int $object_id Product ID.
489 * @param array $terms Terms array.
490 * @param array $tt_ids Term ids array.
491 * @param string $taxonomy Taxonomy name.
492 * @param bool $append Are we appending or setting terms.
493 */
494 public static function force_default_term( $object_id, $terms, $tt_ids, $taxonomy, $append ) {
495 if ( ! $append && 'product_cat' === $taxonomy && empty( $tt_ids ) && 'product' === get_post_type( $object_id ) ) {
496 $default_term = absint( get_option( 'default_product_cat', 0 ) );
497 $tt_ids = array_map( 'absint', $tt_ids );
498
499 if ( $default_term && ! in_array( $default_term, $tt_ids, true ) ) {
500 wp_set_post_terms( $object_id, array( $default_term ), 'product_cat', true );
501 }
502 }
503 }
504
505 /**
506 * Ensure statuses are correctly reassigned when restoring orders and products.
507 *
508 * @param string $new_status The new status of the post being restored.
509 * @param int $post_id The ID of the post being restored.
510 * @param string $previous_status The status of the post at the point where it was trashed.
511 * @return string
512 */
513 public static function wp_untrash_post_status( $new_status, $post_id, $previous_status ) {
514 $post_types = array( 'shop_order', 'shop_coupon', 'product', 'product_variation' );
515
516 if ( in_array( get_post_type( $post_id ), $post_types, true ) ) {
517 $new_status = $previous_status;
518 }
519
520 return $new_status;
521 }
522
523 /**
524 * When setting stock level, ensure the stock status is kept in sync.
525 *
526 * @param int $meta_id Meta ID.
527 * @param int $object_id Object ID.
528 * @param string $meta_key Meta key.
529 * @param mixed $meta_value Meta value.
530 * @deprecated 3.3
531 */
532 public static function sync_product_stock_status( $meta_id, $object_id, $meta_key, $meta_value ) {}
533
534 /**
535 * Update changed downloads.
536 *
537 * @deprecated 3.3.0 No action is necessary on changes to download paths since download_id is no longer based on file hash.
538 * @param int $product_id Product ID.
539 * @param int $variation_id Variation ID. Optional product variation identifier.
540 * @param array $downloads Newly set files.
541 */
542 public static function process_product_file_download_paths( $product_id, $variation_id, $downloads ) {
543 wc_deprecated_function( __FUNCTION__, '3.3' );
544 }
545
546 /**
547 * Delete transients when terms are set.
548 *
549 * @deprecated 3.6
550 * @param int $object_id Object ID.
551 * @param mixed $terms An array of object terms.
552 * @param array $tt_ids An array of term taxonomy IDs.
553 * @param string $taxonomy Taxonomy slug.
554 * @param mixed $append Whether to append new terms to the old terms.
555 * @param array $old_tt_ids Old array of term taxonomy IDs.
556 */
557 public static function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
558 if ( in_array( get_post_type( $object_id ), array( 'product', 'product_variation' ), true ) ) {
559 self::delete_product_query_transients();
560 }
561 }
562 }
563
564 WC_Post_Data::init();
565