Cleanup methods

This commit is contained in:
Mike Jolley 2019-03-07 15:08:22 +00:00
parent 3173167e7e
commit 663f724bdd
4 changed files with 70 additions and 2 deletions

View File

@ -839,7 +839,7 @@ CREATE TABLE {$wpdb->prefix}wc_product_meta_lookup (
KEY `virtual` (`virtual`),
KEY `downloadable` (`downloadable`),
KEY `stock_status` (`stock_status`),
KEY `stock_status` (`stock_quantity`),
KEY `stock_quantity` (`stock_quantity`),
KEY min_max_price (`min_price`, `max_price`)
) $collate;
";

View File

@ -290,6 +290,7 @@ class WC_Post_Data {
case 'product':
$data_store = WC_Data_Store::load( 'product-variable' );
$data_store->delete_variations( $id, true );
$data_store->delete_from_lookup_table( $id, 'wc_product_meta_lookup' );
$parent_id = wp_get_post_parent_id( $id );
if ( $parent_id ) {
@ -297,6 +298,8 @@ class WC_Post_Data {
}
break;
case 'product_variation':
$data_store = WC_Data_Store::load( 'product' );
$data_store->delete_from_lookup_table( $id, 'wc_product_meta_lookup' );
wc_delete_product_transients( wp_get_post_parent_id( $id ) );
break;
case 'shop_order':

View File

@ -512,6 +512,29 @@ class WC_Data_Store_WP {
return apply_filters( 'wp_search_stopwords', $stopwords );
}
/**
* Get data to save to a lookup table.
*
* @since 3.6.0
* @param int $id ID of object to update.
* @param string $table Lookup table name.
* @return array
*/
protected function get_data_for_lookup_table( $id, $table ) {
return array();
}
/**
* Get primary key name for lookup table.
*
* @since 3.6.0
* @param string $table Lookup table name.
* @return string
*/
protected function get_primary_key_for_lookup_table( $table ) {
return '';
}
/**
* Update a lookup table for an object.
*
@ -540,4 +563,32 @@ class WC_Data_Store_WP {
wp_cache_set( 'lookup_table', $update_data, 'object_' . $id );
}
}
/**
* Delete lookup table data for an ID.
*
* @since 3.6.0
* @param int $id ID of object to update.
* @param string $table Lookup table name.
*/
public function delete_from_lookup_table( $id, $table ) {
global $wpdb;
$id = absint( $id );
$table = sanitize_key( $table );
if ( empty( $id ) || empty( $table ) ) {
return false;
}
$pk = $this->get_primary_key_for_lookup_table( $table );
$wpdb->delete(
$wpdb->$table,
array(
$pk => $id,
)
);
wp_cache_delete( 'lookup_table', 'object_' . $id );
}
}

View File

@ -624,7 +624,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
}
}
if ( array_intersect( $this->updated_props, array( 'sku', 'regular_price', 'sale_price', 'date_on_sale_from', 'date_on_sale_to', 'total_sales', 'average_rating', 'stock_quantity', 'manage_stock' ) ) ) {
if ( array_intersect( $this->updated_props, array( 'sku', 'regular_price', 'sale_price', 'date_on_sale_from', 'date_on_sale_to', 'total_sales', 'average_rating', 'stock_quantity', 'stock_status', 'manage_stock', 'downloadable', 'virtual' ) ) ) {
$this->update_lookup_table( $product->get_id(), 'wc_product_meta_lookup' );
}
@ -1894,4 +1894,18 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
}
return array();
}
/**
* Get primary key name for lookup table.
*
* @since 3.6.0
* @param string $table Lookup table name.
* @return string
*/
protected function get_primary_key_for_lookup_table( $table ) {
if ( 'wc_product_meta_lookup' === $table ) {
return 'product_id';
}
return '';
}
}