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