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