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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 2 years ago data-stores 1 year ago emails 1 year ago export 1 year ago gateways 1 year ago import 1 year ago integrations 2 years ago interfaces 1 year ago legacy 1 year ago libraries 1 year ago log-handlers 2 years ago payment-tokens 5 years ago product-usage 1 year ago queue 4 years ago react-admin 1 year ago rest-api 1 year ago shipping 1 year ago shortcodes 1 year 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 1 year 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 1 year 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 1 year ago class-wc-cart-totals.php 2 years 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 3 years ago class-wc-countries.php 1 year ago class-wc-coupon.php 1 year ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 1 year ago class-wc-customer.php 2 years 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 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 6 years ago class-wc-geolocation.php 1 year ago class-wc-https.php 2 years ago class-wc-install.php 1 year 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 1 year ago class-wc-payment-gateways.php 1 year 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 5 years ago class-wc-privacy-erasers.php 1 year ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 1 year 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 2 years ago class-wc-shipping-rate.php 1 year ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 5 years ago class-wc-shipping.php 4 years 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 2 years ago class-wc-tracker.php 1 year ago class-wc-validation.php 2 years 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-brands-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 3 years ago wc-deprecated-functions.php 2 years ago wc-formatting-functions.php 1 year ago wc-notice-functions.php 2 years ago wc-order-functions.php 1 year ago wc-order-item-functions.php 3 years 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 3 years 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 5 years ago
class-wc-post-data.php
613 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' ) );
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 * @param mixed $id ID of post being deleted.
309 */
310 public static function delete_post( $id ) {
311 $container = wc_get_container();
312 if ( ! $container->get( LegacyProxy::class )->call_function( 'current_user_can', 'delete_posts' ) || ! $id ) {
313 return;
314 }
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 * Trash post.
354 *
355 * @param mixed $id Post ID.
356 */
357 public static function trash_post( $id ) {
358 if ( ! $id ) {
359 return;
360 }
361
362 $post_type = self::get_post_type( $id );
363
364 // If this is an order, trash any refunds too.
365 if ( in_array( $post_type, wc_get_order_types( 'order-count' ), true ) ) {
366 global $wpdb;
367
368 $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
369
370 foreach ( $refunds as $refund ) {
371 $wpdb->update( $wpdb->posts, array( 'post_status' => OrderStatus::TRASH ), array( 'ID' => $refund->ID ) );
372 }
373
374 wc_delete_shop_order_transients( $id );
375
376 // If this is a product, trash children variations.
377 } elseif ( 'product' === $post_type ) {
378 $data_store = WC_Data_Store::load( 'product-variable' );
379 $data_store->delete_variations( $id, false );
380 wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_deleted( $id );
381 } elseif ( 'product_variation' === $post_type ) {
382 wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_deleted( $id );
383 }
384 }
385
386 /**
387 * Untrash post.
388 *
389 * @param mixed $id Post ID.
390 */
391 public static function untrash_post( $id ) {
392 if ( ! $id ) {
393 return;
394 }
395
396 $post_type = self::get_post_type( $id );
397
398 if ( in_array( $post_type, wc_get_order_types( 'order-count' ), true ) ) {
399 global $wpdb;
400
401 $refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
402
403 foreach ( $refunds as $refund ) {
404 $wpdb->update( $wpdb->posts, array( 'post_status' => OrderInternalStatus::COMPLETED ), array( 'ID' => $refund->ID ) );
405 }
406
407 wc_delete_shop_order_transients( $id );
408
409 } elseif ( 'product' === $post_type ) {
410 $data_store = WC_Data_Store::load( 'product-variable' );
411 $data_store->untrash_variations( $id );
412
413 wc_product_force_unique_sku( $id );
414 self::clear_global_unique_id_if_necessary( $id );
415
416 wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_changed( $id );
417 } elseif ( 'product_variation' === $post_type ) {
418 wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_changed( $id );
419 }
420 }
421
422 /**
423 * Clear global unique id if it's not unique.
424 *
425 * @param mixed $id Post ID.
426 */
427 private static function clear_global_unique_id_if_necessary( $id ) {
428 $product = wc_get_product( $id );
429 if ( $product && ! wc_product_has_global_unique_id( $id, $product->get_global_unique_id() ) ) {
430 $product->set_global_unique_id( '' );
431 $product->save();
432 }
433 }
434
435 /**
436 * Get the post type for a given post.
437 *
438 * @param int $id The post id.
439 * @return string The post type.
440 */
441 private static function get_post_type( $id ) {
442 return wc_get_container()->get( LegacyProxy::class )->call_function( 'get_post_type', $id );
443 }
444
445 /**
446 * Before deleting an order, do some cleanup.
447 *
448 * @since 3.2.0
449 * @param int $order_id Order ID.
450 */
451 public static function before_delete_order( $order_id ) {
452 if ( OrderUtil::is_order( $order_id, wc_get_order_types() ) ) {
453 // Clean up user.
454 $order = wc_get_order( $order_id );
455
456 // Check for `get_customer_id`, since this may be e.g. a refund order (which doesn't implement it).
457 $customer_id = is_callable( array( $order, 'get_customer_id' ) ) ? $order->get_customer_id() : 0;
458
459 if ( $customer_id > 0 && 'shop_order' === $order->get_type() ) {
460 $customer = new WC_Customer( $customer_id );
461 $order_count = $customer->get_order_count();
462 $order_count --;
463
464 if ( 0 === $order_count ) {
465 $customer->set_is_paying_customer( false );
466 $customer->save();
467 }
468
469 // Delete order count and last order meta.
470 delete_user_meta( $customer_id, '_order_count' );
471 delete_user_meta( $customer_id, '_last_order' );
472 }
473
474 // Clean up items.
475 self::delete_order_items( $order_id );
476 self::delete_order_downloadable_permissions( $order_id );
477 }
478 }
479
480 /**
481 * Remove item meta on permanent deletion.
482 *
483 * @param int $postid Post ID.
484 */
485 public static function delete_order_items( $postid ) {
486 global $wpdb;
487
488 if ( OrderUtil::is_order( $postid, wc_get_order_types() ) ) {
489 do_action( 'woocommerce_delete_order_items', $postid );
490
491 $wpdb->query(
492 "
493 DELETE {$wpdb->prefix}woocommerce_order_items, {$wpdb->prefix}woocommerce_order_itemmeta
494 FROM {$wpdb->prefix}woocommerce_order_items
495 JOIN {$wpdb->prefix}woocommerce_order_itemmeta ON {$wpdb->prefix}woocommerce_order_items.order_item_id = {$wpdb->prefix}woocommerce_order_itemmeta.order_item_id
496 WHERE {$wpdb->prefix}woocommerce_order_items.order_id = '{$postid}';
497 "
498 ); // WPCS: unprepared SQL ok.
499
500 do_action( 'woocommerce_deleted_order_items', $postid );
501 }
502 }
503
504 /**
505 * Remove downloadable permissions on permanent order deletion.
506 *
507 * @param int $postid Post ID.
508 */
509 public static function delete_order_downloadable_permissions( $postid ) {
510 if ( OrderUtil::is_order( $postid, wc_get_order_types() ) ) {
511 do_action( 'woocommerce_delete_order_downloadable_permissions', $postid );
512
513 $data_store = WC_Data_Store::load( 'customer-download' );
514 $data_store->delete_by_order_id( $postid );
515
516 do_action( 'woocommerce_deleted_order_downloadable_permissions', $postid );
517 }
518 }
519
520 /**
521 * Flush meta cache for CRUD objects on direct update.
522 *
523 * @param int $meta_id Meta ID.
524 * @param int $object_id Object ID.
525 * @param string $meta_key Meta key.
526 * @param mixed $meta_value Meta value.
527 */
528 public static function flush_object_meta_cache( $meta_id, $object_id, $meta_key, $meta_value ) {
529 WC_Cache_Helper::invalidate_cache_group( 'object_' . $object_id );
530 }
531
532 /**
533 * Ensure default category gets set.
534 *
535 * @since 3.3.0
536 * @param int $object_id Product ID.
537 * @param array $terms Terms array.
538 * @param array $tt_ids Term ids array.
539 * @param string $taxonomy Taxonomy name.
540 * @param bool $append Are we appending or setting terms.
541 */
542 public static function force_default_term( $object_id, $terms, $tt_ids, $taxonomy, $append ) {
543 if ( ! $append && 'product_cat' === $taxonomy && empty( $tt_ids ) && 'product' === get_post_type( $object_id ) ) {
544 $default_term = absint( get_option( 'default_product_cat', 0 ) );
545 $tt_ids = array_map( 'absint', $tt_ids );
546
547 if ( $default_term && ! in_array( $default_term, $tt_ids, true ) ) {
548 wp_set_post_terms( $object_id, array( $default_term ), 'product_cat', true );
549 }
550 }
551 }
552
553 /**
554 * Ensure statuses are correctly reassigned when restoring orders and products.
555 *
556 * @param string $new_status The new status of the post being restored.
557 * @param int $post_id The ID of the post being restored.
558 * @param string $previous_status The status of the post at the point where it was trashed.
559 * @return string
560 */
561 public static function wp_untrash_post_status( $new_status, $post_id, $previous_status ) {
562 $post_types = array( 'shop_order', 'shop_coupon', 'product', 'product_variation' );
563
564 if ( in_array( get_post_type( $post_id ), $post_types, true ) ) {
565 $new_status = $previous_status;
566 }
567
568 return $new_status;
569 }
570
571 /**
572 * When setting stock level, ensure the stock status is kept in sync.
573 *
574 * @param int $meta_id Meta ID.
575 * @param int $object_id Object ID.
576 * @param string $meta_key Meta key.
577 * @param mixed $meta_value Meta value.
578 * @deprecated 3.3
579 */
580 public static function sync_product_stock_status( $meta_id, $object_id, $meta_key, $meta_value ) {}
581
582 /**
583 * Update changed downloads.
584 *
585 * @deprecated 3.3.0 No action is necessary on changes to download paths since download_id is no longer based on file hash.
586 * @param int $product_id Product ID.
587 * @param int $variation_id Variation ID. Optional product variation identifier.
588 * @param array $downloads Newly set files.
589 */
590 public static function process_product_file_download_paths( $product_id, $variation_id, $downloads ) {
591 wc_deprecated_function( __FUNCTION__, '3.3' );
592 }
593
594 /**
595 * Delete transients when terms are set.
596 *
597 * @deprecated 3.6
598 * @param int $object_id Object ID.
599 * @param mixed $terms An array of object terms.
600 * @param array $tt_ids An array of term taxonomy IDs.
601 * @param string $taxonomy Taxonomy slug.
602 * @param mixed $append Whether to append new terms to the old terms.
603 * @param array $old_tt_ids Old array of term taxonomy IDs.
604 */
605 public static function set_object_terms( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
606 if ( in_array( get_post_type( $object_id ), array( 'product', 'product_variation' ), true ) ) {
607 self::delete_product_query_transients();
608 }
609 }
610 }
611
612 WC_Post_Data::init();
613