Update coupons and orders to use update_or_delete_post_meta
This commit is contained in:
parent
ea22b1cf47
commit
5749369ba5
|
@ -294,12 +294,15 @@ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP impleme
|
|||
|
||||
foreach ( $props_to_update as $meta_key => $prop ) {
|
||||
$value = $order->{"get_$prop"}( 'edit' );
|
||||
$value = is_string( $value ) ? wp_slash( $value ) : $value;
|
||||
|
||||
if ( 'prices_include_tax' === $prop ) {
|
||||
$value = $value ? 'yes' : 'no';
|
||||
}
|
||||
|
||||
if ( update_post_meta( $order->get_id(), $meta_key, $value ) ) {
|
||||
$updated = $this->update_or_delete_post_meta( $order, $meta_key, $value );
|
||||
|
||||
if ( $updated ) {
|
||||
$updated_props[] = $prop;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -115,7 +115,7 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
|
|||
'description' => $post_object->post_excerpt,
|
||||
'date_created' => 0 < $post_object->post_date_gmt ? wc_string_to_timestamp( $post_object->post_date_gmt ) : null,
|
||||
'date_modified' => 0 < $post_object->post_modified_gmt ? wc_string_to_timestamp( $post_object->post_modified_gmt ) : null,
|
||||
'date_expires' => metadata_exists( 'post', $coupon_id, 'date_expires' ) ? get_post_meta( $coupon_id, 'date_expires', true ) : get_post_meta( $coupon_id, 'expiry_date', true ),
|
||||
'date_expires' => get_post_meta( $coupon_id, 'date_expires', true ),
|
||||
'discount_type' => get_post_meta( $coupon_id, 'discount_type', true ),
|
||||
'amount' => get_post_meta( $coupon_id, 'coupon_amount', true ),
|
||||
'usage_count' => get_post_meta( $coupon_id, 'usage_count', true ),
|
||||
|
@ -248,33 +248,33 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
|
|||
$props_to_update = $this->get_props_to_update( $coupon, $meta_key_to_props );
|
||||
foreach ( $props_to_update as $meta_key => $prop ) {
|
||||
$value = $coupon->{"get_$prop"}( 'edit' );
|
||||
$value = is_string( $value ) ? wp_slash( $value ) : $value;
|
||||
switch ( $prop ) {
|
||||
case 'individual_use':
|
||||
case 'free_shipping':
|
||||
case 'exclude_sale_items':
|
||||
$updated = update_post_meta( $coupon->get_id(), $meta_key, wc_bool_to_string( $value ) );
|
||||
$value = wc_bool_to_string( $value );
|
||||
break;
|
||||
case 'product_ids':
|
||||
case 'excluded_product_ids':
|
||||
$updated = update_post_meta( $coupon->get_id(), $meta_key, implode( ',', array_filter( array_map( 'intval', $value ) ) ) );
|
||||
$value = implode( ',', array_filter( array_map( 'intval', $value ) ) );
|
||||
break;
|
||||
case 'product_categories':
|
||||
case 'excluded_product_categories':
|
||||
$updated = update_post_meta( $coupon->get_id(), $meta_key, array_filter( array_map( 'intval', $value ) ) );
|
||||
$value = array_filter( array_map( 'intval', $value ) );
|
||||
break;
|
||||
case 'email_restrictions':
|
||||
$updated = update_post_meta( $coupon->get_id(), $meta_key, array_filter( array_map( 'sanitize_email', $value ) ) );
|
||||
$value = array_filter( array_map( 'sanitize_email', $value ) );
|
||||
break;
|
||||
case 'date_expires':
|
||||
$updated = update_post_meta( $coupon->get_id(), $meta_key, ( $value ? $value->getTimestamp() : null ) );
|
||||
update_post_meta( $coupon->get_id(), 'expiry_date', ( $value ? $value->date( 'Y-m-d' ) : '' ) ); // Update the old meta key for backwards compatibility.
|
||||
break;
|
||||
default:
|
||||
$updated = update_post_meta( $coupon->get_id(), $meta_key, $value );
|
||||
$value = $value ? $value->getTimestamp() : null;
|
||||
break;
|
||||
}
|
||||
|
||||
$updated = $this->update_or_delete_post_meta( $coupon, $meta_key, $value );
|
||||
|
||||
if ( $updated ) {
|
||||
$updated_props[] = $prop;
|
||||
$this->updated_props[] = $prop;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -218,9 +218,13 @@ class WC_Data_Store_WP {
|
|||
}
|
||||
|
||||
/**
|
||||
* Update non-empty meta, or delete it.
|
||||
* Update meta data in, or delete it from, the database.
|
||||
*
|
||||
* Numeric meta is stored regardless. Also, data-stores can force meta to exist using ``.
|
||||
* Avoids storing meta when it's either an empty string or empty array.
|
||||
* Other empty values such as numeric 0 and null should still be stored.
|
||||
* Data-stores can force meta to exist using `must_exist_meta_keys`.
|
||||
*
|
||||
* Note: WordPress `get_metadata` function returns an empty string when meta data does not exist.
|
||||
*
|
||||
* @param WC_Data $object The WP_Data object (WC_Coupon for coupons, etc).
|
||||
* @param string $meta_key Meta key to update.
|
||||
|
@ -231,10 +235,10 @@ class WC_Data_Store_WP {
|
|||
* @return bool True if updated/deleted.
|
||||
*/
|
||||
protected function update_or_delete_post_meta( $object, $meta_key, $meta_value ) {
|
||||
if ( ! empty( $meta_value ) || is_numeric( $meta_value ) || in_array( $meta_key, $this->must_exist_meta_keys, true ) ) {
|
||||
$updated = update_post_meta( $object->get_id(), $meta_key, $meta_value );
|
||||
} else {
|
||||
if ( in_array( $meta_value, array( array(), '' ), true ) && ! in_array( $meta_key, $this->must_exist_meta_keys, true ) ) {
|
||||
$updated = delete_post_meta( $object->get_id(), $meta_key );
|
||||
} else {
|
||||
$updated = update_post_meta( $object->get_id(), $meta_key, $meta_value );
|
||||
}
|
||||
|
||||
return (bool) $updated;
|
||||
|
|
|
@ -199,26 +199,19 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
|
|||
|
||||
foreach ( $props_to_update as $meta_key => $prop ) {
|
||||
$value = $order->{"get_$prop"}( 'edit' );
|
||||
|
||||
if ( 'date_paid' === $prop ) {
|
||||
// In 3.0.x we store this as a UTC timestamp.
|
||||
update_post_meta( $id, $meta_key, ! is_null( $value ) ? $value->getTimestamp() : '' );
|
||||
|
||||
// In 2.6.x date_paid was stored as _paid_date in local mysql format.
|
||||
update_post_meta( $id, '_paid_date', ! is_null( $value ) ? $value->date( 'Y-m-d H:i:s' ) : '' );
|
||||
|
||||
} elseif ( 'date_completed' === $prop ) {
|
||||
// In 3.0.x we store this as a UTC timestamp.
|
||||
update_post_meta( $id, $meta_key, ! is_null( $value ) ? $value->getTimestamp() : '' );
|
||||
|
||||
// In 2.6.x date_paid was stored as _paid_date in local mysql format.
|
||||
update_post_meta( $id, '_completed_date', ! is_null( $value ) ? $value->date( 'Y-m-d H:i:s' ) : '' );
|
||||
|
||||
} else {
|
||||
update_post_meta( $id, $meta_key, $value );
|
||||
$value = is_string( $value ) ? wp_slash( $value ) : $value;
|
||||
switch ( $prop ) {
|
||||
case 'date_paid':
|
||||
case 'date_completed':
|
||||
$value = ! is_null( $value ) ? $value->getTimestamp() : '';
|
||||
break;
|
||||
}
|
||||
|
||||
$updated_props[] = $prop;
|
||||
$updated = $this->update_or_delete_post_meta( $order, $meta_key, $value );
|
||||
|
||||
if ( $updated ) {
|
||||
$updated_props[] = $prop;
|
||||
}
|
||||
}
|
||||
|
||||
$address_props = array(
|
||||
|
@ -251,10 +244,14 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
|
|||
foreach ( $address_props as $props_key => $props ) {
|
||||
$props_to_update = $this->get_props_to_update( $order, $props );
|
||||
foreach ( $props_to_update as $meta_key => $prop ) {
|
||||
$value = $order->{"get_$prop"}( 'edit' );
|
||||
update_post_meta( $id, $meta_key, $value );
|
||||
$updated_props[] = $prop;
|
||||
$updated_props[] = $props_key;
|
||||
$value = $order->{"get_$prop"}( 'edit' );
|
||||
$value = is_string( $value ) ? wp_slash( $value ) : $value;
|
||||
$updated = $this->update_or_delete_post_meta( $order, $meta_key, $value );
|
||||
|
||||
if ( $updated ) {
|
||||
$updated_props[] = $prop;
|
||||
$updated_props[] = $props_key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -268,6 +265,18 @@ class WC_Order_Data_Store_CPT extends Abstract_WC_Order_Data_Store_CPT implement
|
|||
update_post_meta( $id, '_shipping_address_index', implode( ' ', $order->get_address( 'shipping' ) ) );
|
||||
}
|
||||
|
||||
// Legacy date handling. @todo remove in 4.0.
|
||||
if ( 'date_paid' === $prop ) {
|
||||
$value = $order->get_date_paid( 'edit' );
|
||||
// In 2.6.x date_paid was stored as _paid_date in local mysql format.
|
||||
update_post_meta( $id, '_paid_date', ! is_null( $value ) ? $value->date( 'Y-m-d H:i:s' ) : '' );
|
||||
}
|
||||
if ( 'date_completed' === $prop ) {
|
||||
$value = $order->get_date_completed( 'edit' );
|
||||
// In 2.6.x date_paid was stored as _paid_date in local mysql format.
|
||||
update_post_meta( $id, '_completed_date', ! is_null( $value ) ? $value->date( 'Y-m-d H:i:s' ) : '' );
|
||||
}
|
||||
|
||||
// If customer changed, update any downloadable permissions.
|
||||
if ( in_array( 'customer_id', $updated_props ) || in_array( 'billing_email', $updated_props ) ) {
|
||||
$data_store = WC_Data_Store::load( 'customer-download' );
|
||||
|
|
Loading…
Reference in New Issue