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