Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../includes
File: class-wc-post-data.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Post Data
[2] Fix | Delete
*
[3] Fix | Delete
* Standardises certain post data on save.
[4] Fix | Delete
*
[5] Fix | Delete
* @package WooCommerce\Classes\Data
[6] Fix | Delete
* @version 2.2.0
[7] Fix | Delete
*/
[8] Fix | Delete
[9] Fix | Delete
use Automattic\WooCommerce\Enums\OrderStatus;
[10] Fix | Delete
use Automattic\WooCommerce\Enums\OrderInternalStatus;
[11] Fix | Delete
use Automattic\WooCommerce\Enums\ProductStatus;
[12] Fix | Delete
use Automattic\WooCommerce\Enums\ProductType;
[13] Fix | Delete
use Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer;
[14] Fix | Delete
use Automattic\WooCommerce\Internal\ProductAttributesLookup\LookupDataStore as ProductAttributesLookupDataStore;
[15] Fix | Delete
use Automattic\WooCommerce\Proxies\LegacyProxy;
[16] Fix | Delete
use Automattic\WooCommerce\Utilities\OrderUtil;
[17] Fix | Delete
[18] Fix | Delete
defined( 'ABSPATH' ) || exit;
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Post data class.
[22] Fix | Delete
*/
[23] Fix | Delete
class WC_Post_Data {
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Editing term.
[27] Fix | Delete
*
[28] Fix | Delete
* @var object
[29] Fix | Delete
*/
[30] Fix | Delete
private static $editing_term = null;
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* Hook in methods.
[34] Fix | Delete
*/
[35] Fix | Delete
public static function init() {
[36] Fix | Delete
add_filter( 'post_type_link', array( __CLASS__, 'variation_post_link' ), 10, 2 );
[37] Fix | Delete
add_action( 'shutdown', array( __CLASS__, 'do_deferred_product_sync' ), 10 );
[38] Fix | Delete
add_action( 'set_object_terms', array( __CLASS__, 'force_default_term' ), 10, 5 );
[39] Fix | Delete
add_action( 'set_object_terms', array( __CLASS__, 'delete_product_query_transients' ) );
[40] Fix | Delete
add_action( 'deleted_term_relationships', array( __CLASS__, 'delete_product_query_transients' ) );
[41] Fix | Delete
add_action( 'woocommerce_product_set_stock_status', array( __CLASS__, 'delete_product_query_transients' ) );
[42] Fix | Delete
add_action( 'woocommerce_product_set_visibility', array( __CLASS__, 'delete_product_query_transients' ) );
[43] Fix | Delete
add_action( 'woocommerce_product_type_changed', array( __CLASS__, 'product_type_changed' ), 10, 3 );
[44] Fix | Delete
[45] Fix | Delete
add_action( 'edit_term', array( __CLASS__, 'edit_term' ), 10, 3 );
[46] Fix | Delete
add_action( 'edited_term', array( __CLASS__, 'edited_term' ), 10, 3 );
[47] Fix | Delete
add_filter( 'update_order_item_metadata', array( __CLASS__, 'update_order_item_metadata' ), 10, 5 );
[48] Fix | Delete
add_filter( 'update_post_metadata', array( __CLASS__, 'update_post_metadata' ), 10, 5 );
[49] Fix | Delete
add_filter( 'wp_insert_post_data', array( __CLASS__, 'wp_insert_post_data' ) );
[50] Fix | Delete
add_filter( 'oembed_response_data', array( __CLASS__, 'filter_oembed_response_data' ), 10, 2 );
[51] Fix | Delete
add_filter( 'wp_untrash_post_status', array( __CLASS__, 'wp_untrash_post_status' ), 10, 3 );
[52] Fix | Delete
[53] Fix | Delete
// Status transitions.
[54] Fix | Delete
add_action( 'transition_post_status', array( __CLASS__, 'transition_post_status' ), 10, 3 );
[55] Fix | Delete
add_action( 'delete_post', array( __CLASS__, 'delete_post_data' ) );
[56] Fix | Delete
add_action( 'wp_trash_post', array( __CLASS__, 'trash_post' ) );
[57] Fix | Delete
add_action( 'untrashed_post', array( __CLASS__, 'untrash_post' ) );
[58] Fix | Delete
add_action( 'before_delete_post', array( __CLASS__, 'before_delete_order' ) );
[59] Fix | Delete
add_action( 'woocommerce_before_delete_order', array( __CLASS__, 'before_delete_order' ) );
[60] Fix | Delete
[61] Fix | Delete
// Meta cache flushing.
[62] Fix | Delete
add_action( 'updated_post_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
[63] Fix | Delete
add_action( 'added_post_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
[64] Fix | Delete
add_action( 'deleted_post_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
[65] Fix | Delete
add_action( 'updated_order_item_meta', array( __CLASS__, 'flush_object_meta_cache' ), 10, 4 );
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* Link to parent products when getting permalink for variation.
[70] Fix | Delete
*
[71] Fix | Delete
* @param string $permalink Permalink.
[72] Fix | Delete
* @param WP_Post $post Post data.
[73] Fix | Delete
*
[74] Fix | Delete
* @return string
[75] Fix | Delete
*/
[76] Fix | Delete
public static function variation_post_link( $permalink, $post ) {
[77] Fix | Delete
if ( isset( $post->ID, $post->post_type ) && 'product_variation' === $post->post_type ) {
[78] Fix | Delete
$variation = wc_get_product( $post->ID );
[79] Fix | Delete
[80] Fix | Delete
if ( $variation && $variation->get_parent_id() ) {
[81] Fix | Delete
return $variation->get_permalink();
[82] Fix | Delete
}
[83] Fix | Delete
}
[84] Fix | Delete
return $permalink;
[85] Fix | Delete
}
[86] Fix | Delete
[87] Fix | Delete
/**
[88] Fix | Delete
* Sync products queued to sync.
[89] Fix | Delete
*/
[90] Fix | Delete
public static function do_deferred_product_sync() {
[91] Fix | Delete
global $wc_deferred_product_sync;
[92] Fix | Delete
[93] Fix | Delete
if ( ! empty( $wc_deferred_product_sync ) ) {
[94] Fix | Delete
$wc_deferred_product_sync = wp_parse_id_list( $wc_deferred_product_sync );
[95] Fix | Delete
array_walk( $wc_deferred_product_sync, array( __CLASS__, 'deferred_product_sync' ) );
[96] Fix | Delete
}
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
/**
[100] Fix | Delete
* Sync a product.
[101] Fix | Delete
*
[102] Fix | Delete
* @param int $product_id Product ID.
[103] Fix | Delete
*/
[104] Fix | Delete
public static function deferred_product_sync( $product_id ) {
[105] Fix | Delete
$product = wc_get_product( $product_id );
[106] Fix | Delete
[107] Fix | Delete
if ( is_callable( array( $product, 'sync' ) ) ) {
[108] Fix | Delete
$product->sync( $product );
[109] Fix | Delete
}
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
/**
[113] Fix | Delete
* When a post status changes.
[114] Fix | Delete
*
[115] Fix | Delete
* @param string $new_status New status.
[116] Fix | Delete
* @param string $old_status Old status.
[117] Fix | Delete
* @param WP_Post $post Post data.
[118] Fix | Delete
*/
[119] Fix | Delete
public static function transition_post_status( $new_status, $old_status, $post ) {
[120] Fix | Delete
if ( ( ProductStatus::PUBLISH === $new_status || ProductStatus::PUBLISH === $old_status ) && in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) {
[121] Fix | Delete
self::delete_product_query_transients();
[122] Fix | Delete
}
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
/**
[126] Fix | Delete
* Delete product view transients when needed e.g. when post status changes, or visibility/stock status is modified.
[127] Fix | Delete
*/
[128] Fix | Delete
public static function delete_product_query_transients() {
[129] Fix | Delete
WC_Cache_Helper::get_transient_version( 'product_query', true );
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
/**
[133] Fix | Delete
* Handle type changes.
[134] Fix | Delete
*
[135] Fix | Delete
* @since 3.0.0
[136] Fix | Delete
*
[137] Fix | Delete
* @param WC_Product $product Product data.
[138] Fix | Delete
* @param string $from Origin type.
[139] Fix | Delete
* @param string $to New type.
[140] Fix | Delete
*/
[141] Fix | Delete
public static function product_type_changed( $product, $from, $to ) {
[142] Fix | Delete
/**
[143] Fix | Delete
* Filter to prevent variations from being deleted while switching from a variable product type to a variable product type.
[144] Fix | Delete
*
[145] Fix | Delete
* @since 5.0.0
[146] Fix | Delete
*
[147] Fix | Delete
* @param bool A boolean value of true will delete the variations.
[148] Fix | Delete
* @param WC_Product $product Product data.
[149] Fix | Delete
* @return string $from Origin type.
[150] Fix | Delete
* @param string $to New type.
[151] Fix | Delete
*/
[152] Fix | Delete
if ( apply_filters( 'woocommerce_delete_variations_on_product_type_change', ProductType::VARIABLE === $from && ProductType::VARIABLE !== $to, $product, $from, $to ) ) {
[153] Fix | Delete
// If the product is no longer variable, we should ensure all variations are removed.
[154] Fix | Delete
$data_store = WC_Data_Store::load( 'product-variable' );
[155] Fix | Delete
$data_store->delete_variations( $product->get_id(), true );
[156] Fix | Delete
}
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
/**
[160] Fix | Delete
* When editing a term, check for product attributes.
[161] Fix | Delete
*
[162] Fix | Delete
* @param int $term_id Term ID.
[163] Fix | Delete
* @param int $tt_id Term taxonomy ID.
[164] Fix | Delete
* @param string $taxonomy Taxonomy slug.
[165] Fix | Delete
*/
[166] Fix | Delete
public static function edit_term( $term_id, $tt_id, $taxonomy ) {
[167] Fix | Delete
if ( strpos( $taxonomy, 'pa_' ) === 0 ) {
[168] Fix | Delete
self::$editing_term = get_term_by( 'id', $term_id, $taxonomy );
[169] Fix | Delete
} else {
[170] Fix | Delete
self::$editing_term = null;
[171] Fix | Delete
}
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
/**
[175] Fix | Delete
* When a term is edited, check for product attributes and update variations.
[176] Fix | Delete
*
[177] Fix | Delete
* @param int $term_id Term ID.
[178] Fix | Delete
* @param int $tt_id Term taxonomy ID.
[179] Fix | Delete
* @param string $taxonomy Taxonomy slug.
[180] Fix | Delete
*/
[181] Fix | Delete
public static function edited_term( $term_id, $tt_id, $taxonomy ) {
[182] Fix | Delete
if ( ! is_null( self::$editing_term ) && strpos( $taxonomy, 'pa_' ) === 0 ) {
[183] Fix | Delete
$edited_term = get_term_by( 'id', $term_id, $taxonomy );
[184] Fix | Delete
[185] Fix | Delete
if ( $edited_term->slug !== self::$editing_term->slug ) {
[186] Fix | Delete
global $wpdb;
[187] Fix | Delete
[188] Fix | Delete
$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 ) );
[189] Fix | Delete
[190] Fix | Delete
$wpdb->query(
[191] Fix | Delete
$wpdb->prepare(
[192] Fix | Delete
"UPDATE {$wpdb->postmeta} SET meta_value = REPLACE( meta_value, %s, %s ) WHERE meta_key = '_default_attributes'",
[193] Fix | Delete
serialize( self::$editing_term->taxonomy ) . serialize( self::$editing_term->slug ),
[194] Fix | Delete
serialize( $edited_term->taxonomy ) . serialize( $edited_term->slug )
[195] Fix | Delete
)
[196] Fix | Delete
);
[197] Fix | Delete
}
[198] Fix | Delete
} else {
[199] Fix | Delete
self::$editing_term = null;
[200] Fix | Delete
}
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* Ensure floats are correctly converted to strings based on PHP locale.
[205] Fix | Delete
*
[206] Fix | Delete
* @param null $check Whether to allow updating metadata for the given type.
[207] Fix | Delete
* @param int $object_id Object ID.
[208] Fix | Delete
* @param string $meta_key Meta key.
[209] Fix | Delete
* @param mixed $meta_value Meta value. Must be serializable if non-scalar.
[210] Fix | Delete
* @param mixed $prev_value If specified, only update existing metadata entries with the specified value. Otherwise, update all entries.
[211] Fix | Delete
* @return null|bool
[212] Fix | Delete
*/
[213] Fix | Delete
public static function update_order_item_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
[214] Fix | Delete
if ( ! empty( $meta_value ) && is_float( $meta_value ) ) {
[215] Fix | Delete
[216] Fix | Delete
// Convert float to string.
[217] Fix | Delete
$meta_value = wc_float_to_string( $meta_value );
[218] Fix | Delete
[219] Fix | Delete
// Update meta value with new string.
[220] Fix | Delete
update_metadata( 'order_item', $object_id, $meta_key, $meta_value, $prev_value );
[221] Fix | Delete
[222] Fix | Delete
return true;
[223] Fix | Delete
}
[224] Fix | Delete
return $check;
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
/**
[228] Fix | Delete
* Ensure floats are correctly converted to strings based on PHP locale.
[229] Fix | Delete
*
[230] Fix | Delete
* @param null $check Whether to allow updating metadata for the given type.
[231] Fix | Delete
* @param int $object_id Object ID.
[232] Fix | Delete
* @param string $meta_key Meta key.
[233] Fix | Delete
* @param mixed $meta_value Meta value. Must be serializable if non-scalar.
[234] Fix | Delete
* @param mixed $prev_value If specified, only update existing metadata entries with the specified value. Otherwise, update all entries.
[235] Fix | Delete
* @return null|bool
[236] Fix | Delete
*/
[237] Fix | Delete
public static function update_post_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) {
[238] Fix | Delete
// Delete product cache if someone uses meta directly.
[239] Fix | Delete
if ( in_array( get_post_type( $object_id ), array( 'product', 'product_variation' ), true ) ) {
[240] Fix | Delete
wp_cache_delete( 'product-' . $object_id, 'products' );
[241] Fix | Delete
}
[242] Fix | Delete
[243] Fix | Delete
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 ) ) {
[244] Fix | Delete
[245] Fix | Delete
// Convert float to string.
[246] Fix | Delete
$meta_value = wc_float_to_string( $meta_value );
[247] Fix | Delete
[248] Fix | Delete
// Update meta value with new string.
[249] Fix | Delete
update_metadata( 'post', $object_id, $meta_key, $meta_value, $prev_value );
[250] Fix | Delete
[251] Fix | Delete
return true;
[252] Fix | Delete
}
[253] Fix | Delete
return $check;
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
/**
[257] Fix | Delete
* Forces the order posts to have a title in a certain format (containing the date).
[258] Fix | Delete
* Forces certain product data based on the product's type, e.g. grouped products cannot have a parent.
[259] Fix | Delete
*
[260] Fix | Delete
* @param array $data An array of slashed post data.
[261] Fix | Delete
* @return array
[262] Fix | Delete
*/
[263] Fix | Delete
public static function wp_insert_post_data( $data ) {
[264] Fix | Delete
if ( 'shop_order' === $data['post_type'] && isset( $data['post_date'] ) ) {
[265] Fix | Delete
$order_title = 'Order';
[266] Fix | Delete
if ( $data['post_date'] ) {
[267] Fix | Delete
$order_title .= ' &ndash; ' . date_i18n( 'F j, Y @ h:i A', strtotime( $data['post_date'] ) );
[268] Fix | Delete
}
[269] Fix | Delete
$data['post_title'] = $order_title;
[270] Fix | Delete
} elseif ( 'product' === $data['post_type'] && isset( $_POST['product-type'] ) ) { // WPCS: input var ok, CSRF ok.
[271] Fix | Delete
$product_type = wc_clean( wp_unslash( $_POST['product-type'] ) ); // WPCS: input var ok, CSRF ok.
[272] Fix | Delete
switch ( $product_type ) {
[273] Fix | Delete
case ProductType::GROUPED:
[274] Fix | Delete
case ProductType::VARIABLE:
[275] Fix | Delete
$data['post_parent'] = 0;
[276] Fix | Delete
break;
[277] Fix | Delete
}
[278] Fix | Delete
} elseif ( 'product' === $data['post_type'] && ProductStatus::AUTO_DRAFT === $data['post_status'] ) {
[279] Fix | Delete
$data['post_title'] = 'AUTO-DRAFT';
[280] Fix | Delete
} elseif ( 'shop_coupon' === $data['post_type'] ) {
[281] Fix | Delete
// Coupons should never allow unfiltered HTML.
[282] Fix | Delete
$data['post_title'] = wp_filter_kses( $data['post_title'] );
[283] Fix | Delete
}
[284] Fix | Delete
[285] Fix | Delete
return $data;
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
/**
[289] Fix | Delete
* Change embed data for certain post types.
[290] Fix | Delete
*
[291] Fix | Delete
* @since 3.2.0
[292] Fix | Delete
* @param array $data The response data.
[293] Fix | Delete
* @param WP_Post $post The post object.
[294] Fix | Delete
* @return array
[295] Fix | Delete
*/
[296] Fix | Delete
public static function filter_oembed_response_data( $data, $post ) {
[297] Fix | Delete
if ( in_array( $post->post_type, array( 'shop_order', 'shop_coupon' ), true ) ) {
[298] Fix | Delete
return array();
[299] Fix | Delete
}
[300] Fix | Delete
return $data;
[301] Fix | Delete
}
[302] Fix | Delete
[303] Fix | Delete
/**
[304] Fix | Delete
* Removes variations etc. belonging to a deleted post, and clears transients.
[305] Fix | Delete
*
[306] Fix | Delete
* @internal Use the delete_post function instead.
[307] Fix | Delete
* @since 9.8.0
[308] Fix | Delete
*
[309] Fix | Delete
* @param mixed $id ID of post being deleted.
[310] Fix | Delete
*/
[311] Fix | Delete
public static function delete_post_data( $id ) {
[312] Fix | Delete
$container = wc_get_container();
[313] Fix | Delete
[314] Fix | Delete
$post_type = self::get_post_type( $id );
[315] Fix | Delete
switch ( $post_type ) {
[316] Fix | Delete
case 'product':
[317] Fix | Delete
$data_store = WC_Data_Store::load( 'product-variable' );
[318] Fix | Delete
$data_store->delete_variations( $id, true );
[319] Fix | Delete
$data_store->delete_from_lookup_table( $id, 'wc_product_meta_lookup' );
[320] Fix | Delete
$container->get( ProductAttributesLookupDataStore::class )->on_product_deleted( $id );
[321] Fix | Delete
[322] Fix | Delete
$parent_id = wp_get_post_parent_id( $id );
[323] Fix | Delete
if ( $parent_id ) {
[324] Fix | Delete
wc_delete_product_transients( $parent_id );
[325] Fix | Delete
}
[326] Fix | Delete
[327] Fix | Delete
break;
[328] Fix | Delete
case 'product_variation':
[329] Fix | Delete
$data_store = WC_Data_Store::load( 'product' );
[330] Fix | Delete
$data_store->delete_from_lookup_table( $id, 'wc_product_meta_lookup' );
[331] Fix | Delete
wc_delete_product_transients( wp_get_post_parent_id( $id ) );
[332] Fix | Delete
$container->get( ProductAttributesLookupDataStore::class )->on_product_deleted( $id );
[333] Fix | Delete
[334] Fix | Delete
break;
[335] Fix | Delete
case 'shop_order':
[336] Fix | Delete
case DataSynchronizer::PLACEHOLDER_ORDER_POST_TYPE:
[337] Fix | Delete
global $wpdb;
[338] Fix | Delete
[339] Fix | Delete
$refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
[340] Fix | Delete
[341] Fix | Delete
if ( ! is_null( $refunds ) ) {
[342] Fix | Delete
foreach ( $refunds as $refund ) {
[343] Fix | Delete
wp_delete_post( $refund->ID, true );
[344] Fix | Delete
}
[345] Fix | Delete
}
[346] Fix | Delete
break;
[347] Fix | Delete
}
[348] Fix | Delete
}
[349] Fix | Delete
[350] Fix | Delete
/**
[351] Fix | Delete
* Removes variations etc. belonging to a deleted post, and clears transients, if the user has permission.
[352] Fix | Delete
*
[353] Fix | Delete
* @param mixed $id ID of post being deleted.
[354] Fix | Delete
*/
[355] Fix | Delete
public static function delete_post( $id ) {
[356] Fix | Delete
$container = wc_get_container();
[357] Fix | Delete
if ( ! $container->get( LegacyProxy::class )->call_function( 'current_user_can', 'delete_posts' ) || ! $id ) {
[358] Fix | Delete
return;
[359] Fix | Delete
}
[360] Fix | Delete
[361] Fix | Delete
self::delete_post_data( $id );
[362] Fix | Delete
}
[363] Fix | Delete
[364] Fix | Delete
/**
[365] Fix | Delete
* Trash post.
[366] Fix | Delete
*
[367] Fix | Delete
* @param mixed $id Post ID.
[368] Fix | Delete
*/
[369] Fix | Delete
public static function trash_post( $id ) {
[370] Fix | Delete
if ( ! $id ) {
[371] Fix | Delete
return;
[372] Fix | Delete
}
[373] Fix | Delete
[374] Fix | Delete
$post_type = self::get_post_type( $id );
[375] Fix | Delete
[376] Fix | Delete
// If this is an order, trash any refunds too.
[377] Fix | Delete
if ( in_array( $post_type, wc_get_order_types( 'order-count' ), true ) ) {
[378] Fix | Delete
global $wpdb;
[379] Fix | Delete
[380] Fix | Delete
$refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
[381] Fix | Delete
[382] Fix | Delete
foreach ( $refunds as $refund ) {
[383] Fix | Delete
$wpdb->update( $wpdb->posts, array( 'post_status' => OrderStatus::TRASH ), array( 'ID' => $refund->ID ) );
[384] Fix | Delete
}
[385] Fix | Delete
[386] Fix | Delete
wc_delete_shop_order_transients( $id );
[387] Fix | Delete
[388] Fix | Delete
// If this is a product, trash children variations.
[389] Fix | Delete
} elseif ( 'product' === $post_type ) {
[390] Fix | Delete
$data_store = WC_Data_Store::load( 'product-variable' );
[391] Fix | Delete
$data_store->delete_variations( $id, false );
[392] Fix | Delete
wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_deleted( $id );
[393] Fix | Delete
} elseif ( 'product_variation' === $post_type ) {
[394] Fix | Delete
wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_deleted( $id );
[395] Fix | Delete
}
[396] Fix | Delete
}
[397] Fix | Delete
[398] Fix | Delete
/**
[399] Fix | Delete
* Untrash post.
[400] Fix | Delete
*
[401] Fix | Delete
* @param mixed $id Post ID.
[402] Fix | Delete
*/
[403] Fix | Delete
public static function untrash_post( $id ) {
[404] Fix | Delete
if ( ! $id ) {
[405] Fix | Delete
return;
[406] Fix | Delete
}
[407] Fix | Delete
[408] Fix | Delete
$post_type = self::get_post_type( $id );
[409] Fix | Delete
[410] Fix | Delete
if ( in_array( $post_type, wc_get_order_types( 'order-count' ), true ) ) {
[411] Fix | Delete
global $wpdb;
[412] Fix | Delete
[413] Fix | Delete
$refunds = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'shop_order_refund' AND post_parent = %d", $id ) );
[414] Fix | Delete
[415] Fix | Delete
foreach ( $refunds as $refund ) {
[416] Fix | Delete
$wpdb->update( $wpdb->posts, array( 'post_status' => OrderInternalStatus::COMPLETED ), array( 'ID' => $refund->ID ) );
[417] Fix | Delete
}
[418] Fix | Delete
[419] Fix | Delete
wc_delete_shop_order_transients( $id );
[420] Fix | Delete
[421] Fix | Delete
} elseif ( 'product' === $post_type ) {
[422] Fix | Delete
$data_store = WC_Data_Store::load( 'product-variable' );
[423] Fix | Delete
$data_store->untrash_variations( $id );
[424] Fix | Delete
[425] Fix | Delete
wc_product_force_unique_sku( $id );
[426] Fix | Delete
self::clear_global_unique_id_if_necessary( $id );
[427] Fix | Delete
[428] Fix | Delete
wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_changed( $id );
[429] Fix | Delete
} elseif ( 'product_variation' === $post_type ) {
[430] Fix | Delete
wc_get_container()->get( ProductAttributesLookupDataStore::class )->on_product_changed( $id );
[431] Fix | Delete
}
[432] Fix | Delete
}
[433] Fix | Delete
[434] Fix | Delete
/**
[435] Fix | Delete
* Clear global unique id if it's not unique.
[436] Fix | Delete
*
[437] Fix | Delete
* @param mixed $id Post ID.
[438] Fix | Delete
*/
[439] Fix | Delete
private static function clear_global_unique_id_if_necessary( $id ) {
[440] Fix | Delete
$product = wc_get_product( $id );
[441] Fix | Delete
if ( $product && ! wc_product_has_global_unique_id( $id, $product->get_global_unique_id() ) ) {
[442] Fix | Delete
$product->set_global_unique_id( '' );
[443] Fix | Delete
$product->save();
[444] Fix | Delete
}
[445] Fix | Delete
}
[446] Fix | Delete
[447] Fix | Delete
/**
[448] Fix | Delete
* Get the post type for a given post.
[449] Fix | Delete
*
[450] Fix | Delete
* @param int $id The post id.
[451] Fix | Delete
* @return string The post type.
[452] Fix | Delete
*/
[453] Fix | Delete
private static function get_post_type( $id ) {
[454] Fix | Delete
return wc_get_container()->get( LegacyProxy::class )->call_function( 'get_post_type', $id );
[455] Fix | Delete
}
[456] Fix | Delete
[457] Fix | Delete
/**
[458] Fix | Delete
* Before deleting an order, do some cleanup.
[459] Fix | Delete
*
[460] Fix | Delete
* @since 3.2.0
[461] Fix | Delete
* @param int $order_id Order ID.
[462] Fix | Delete
*/
[463] Fix | Delete
public static function before_delete_order( $order_id ) {
[464] Fix | Delete
if ( OrderUtil::is_order( $order_id, wc_get_order_types() ) ) {
[465] Fix | Delete
// Clean up user.
[466] Fix | Delete
$order = wc_get_order( $order_id );
[467] Fix | Delete
[468] Fix | Delete
// Check for `get_customer_id`, since this may be e.g. a refund order (which doesn't implement it).
[469] Fix | Delete
$customer_id = is_callable( array( $order, 'get_customer_id' ) ) ? $order->get_customer_id() : 0;
[470] Fix | Delete
[471] Fix | Delete
if ( $customer_id > 0 && 'shop_order' === $order->get_type() ) {
[472] Fix | Delete
$customer = new WC_Customer( $customer_id );
[473] Fix | Delete
$order_count = $customer->get_order_count();
[474] Fix | Delete
$order_count --;
[475] Fix | Delete
[476] Fix | Delete
if ( 0 === $order_count ) {
[477] Fix | Delete
$customer->set_is_paying_customer( false );
[478] Fix | Delete
$customer->save();
[479] Fix | Delete
}
[480] Fix | Delete
[481] Fix | Delete
// Delete order count and last order meta.
[482] Fix | Delete
delete_user_meta( $customer_id, '_order_count' );
[483] Fix | Delete
delete_user_meta( $customer_id, '_last_order' );
[484] Fix | Delete
}
[485] Fix | Delete
[486] Fix | Delete
// Clean up items.
[487] Fix | Delete
self::delete_order_items( $order_id );
[488] Fix | Delete
self::delete_order_downloadable_permissions( $order_id );
[489] Fix | Delete
}
[490] Fix | Delete
}
[491] Fix | Delete
[492] Fix | Delete
/**
[493] Fix | Delete
* Remove item meta on permanent deletion.
[494] Fix | Delete
*
[495] Fix | Delete
* @param int $postid Post ID.
[496] Fix | Delete
*/
[497] Fix | Delete
public static function delete_order_items( $postid ) {
[498] Fix | Delete
global $wpdb;
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function