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