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