Merge pull request #13587 from woocommerce/fix-13525

Update coupon date handling
This commit is contained in:
Mike Jolley 2017-03-15 15:26:41 +00:00 committed by GitHub
commit a34dd0480d
8 changed files with 69 additions and 27 deletions

View File

@ -505,7 +505,7 @@ class WC_Admin_Post_Types {
$expiry_date = $the_coupon->get_date_expires();
if ( $expiry_date ) {
echo esc_html( date_i18n( 'F j, Y', $expiry_date ) );
echo esc_html( $expiry_date->date_i18n( 'F j, Y' ) );
} else {
echo '–';
}

View File

@ -92,7 +92,7 @@ class WC_Meta_Box_Coupon_Data {
}
// Expiry date
$expiry_date = $coupon->get_date_expires() ? date( 'Y-m-d', $coupon->get_date_expires() ) : '';
$expiry_date = $coupon->get_date_expires() ? $coupon->get_date_expires()->date( 'Y-m-d' ) : '';
woocommerce_wp_text_input( array(
'id' => 'expiry_date',
'value' => esc_attr( $expiry_date ),

View File

@ -26,11 +26,11 @@ class WC_Coupon extends WC_Legacy_Coupon {
protected $data = array(
'code' => '',
'amount' => 0,
'date_created' => '',
'date_modified' => '',
'date_created' => null,
'date_modified' => null,
'date_expires' => null,
'discount_type' => 'fixed_cart',
'description' => '',
'date_expires' => '',
'usage_count' => 0,
'individual_use' => false,
'product_ids' => array(),
@ -172,7 +172,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
* Get coupon expiration date.
* @since 2.7.0
* @param string $context
* @return int
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
*/
public function get_date_expires( $context = 'view' ) {
return $this->get_prop( 'date_expires', $context );
@ -182,7 +182,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
* Get date_created
* @since 2.7.0
* @param string $context
* @return int
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
*/
public function get_date_created( $context = 'view' ) {
return $this->get_prop( 'date_created', $context );
@ -192,7 +192,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
* Get date_modified
* @since 2.7.0
* @param string $context
* @return int
* @return WC_DateTime|NULL object if the date is set or null if there is no date.
*/
public function get_date_modified( $context = 'view' ) {
return $this->get_prop( 'date_modified', $context );
@ -467,31 +467,31 @@ class WC_Coupon extends WC_Legacy_Coupon {
/**
* Set expiration date.
* @since 2.7.0
* @param string $timestamp Timestamp
* @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date.
* @throws WC_Data_Exception
*/
public function set_date_expires( $timestamp ) {
$this->set_prop( 'date_expires', is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp ) );
public function set_date_expires( $date ) {
$this->set_date_prop( 'date_expires', $date );
}
/**
* Set date_created
* @since 2.7.0
* @param string $timestamp Timestamp
* @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date.
* @throws WC_Data_Exception
*/
public function set_date_created( $timestamp ) {
$this->set_prop( 'date_created', is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp ) );
public function set_date_created( $date ) {
$this->set_date_prop( 'date_created', $date );
}
/**
* Set date_modified
* @since 2.7.0
* @param string $timestamp
* @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date.
* @throws WC_Data_Exception
*/
public function set_date_modified( $timestamp ) {
$this->set_prop( 'date_modified', is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp ) );
public function set_date_modified( $date ) {
$this->set_date_prop( 'date_modified', $date );
}
/**
@ -792,7 +792,7 @@ class WC_Coupon extends WC_Legacy_Coupon {
* @throws Exception
*/
private function validate_expiry_date() {
if ( $this->get_date_expires() && current_time( 'timestamp' ) > $this->get_date_expires() ) {
if ( $this->get_date_expires() && current_time( 'timestamp', true ) > $this->get_date_expires()->getTimestamp() ) {
throw new Exception( $error_code = self::E_WC_COUPON_EXPIRED );
}
}

View File

@ -28,6 +28,7 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
'discount_type',
'coupon_amount',
'expiry_date',
'date_expires',
'usage_count',
'individual_use',
'product_ids',
@ -54,7 +55,7 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
* @param WC_Coupon
*/
public function create( &$coupon ) {
$coupon->set_date_created( current_time( 'timestamp' ) );
$coupon->set_date_created( current_time( 'timestamp', true ) );
$coupon_id = wp_insert_post( apply_filters( 'woocommerce_new_coupon_data', array(
'post_type' => 'shop_coupon',
@ -63,8 +64,8 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
'post_title' => $coupon->get_code(),
'post_content' => '',
'post_excerpt' => $coupon->get_description(),
'post_date' => date( 'Y-m-d H:i:s', $coupon->get_date_created() ),
'post_date_gmt' => get_gmt_from_date( date( 'Y-m-d H:i:s', $coupon->get_date_created() ) ),
'post_date' => date( 'Y-m-d H:i:s', $coupon->get_date_created()->getOffsetTimestamp() ),
'post_date_gmt' => date( 'Y-m-d H:i:s', $coupon->get_date_created()->getTimestamp() ),
) ), true );
if ( $coupon_id ) {
@ -93,9 +94,9 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
$coupon->set_props( array(
'code' => $post_object->post_title,
'description' => $post_object->post_excerpt,
'date_created' => $post_object->post_date,
'date_modified' => $post_object->post_modified,
'date_expires' => get_post_meta( $coupon_id, 'expiry_date', true ),
'date_created' => strtotime( $post_object->post_date_gmt ),
'date_modified' => strtotime( $post_object->post_modified_gmt ),
'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 ),
'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 ),
@ -180,7 +181,7 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
'usage_limit_per_user' => 'usage_limit_per_user',
'limit_usage_to_x_items' => 'limit_usage_to_x_items',
'usage_count' => 'usage_count',
'expiry_date' => 'date_expires',
'date_expires' => 'date_expires',
'free_shipping' => 'free_shipping',
'product_categories' => 'product_categories',
'exclude_product_categories' => 'excluded_product_categories',
@ -210,6 +211,10 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_WP implements WC_Coupon_Dat
case 'email_restrictions' :
$updated = update_post_meta( $coupon->get_id(), $meta_key, 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 );
break;

View File

@ -109,7 +109,7 @@ abstract class WC_Legacy_Coupon extends WC_Data {
$value = $this->get_usage_count();
break;
case 'expiry_date' :
$value = $this->get_date_expires();
$value = ( $this->get_date_expires() ? $this->get_date_expires()->date( 'Y-m-d' ) : '' );
break;
case 'product_categories' :
$value = $this->get_product_categories();

View File

@ -249,4 +249,26 @@ class WC_Tests_Coupon extends WC_Unit_Test_Case {
// Delete product
WC_Helper_Product::delete_product( $product->get_id() );
}
/**
* Test date setters/getters.
*
* @since 3.0.0
*/
public function test_dates() {
$valid_coupon = WC_Helper_Coupon::create_coupon();
$valid_coupon->set_date_expires( time() + 1000 );
$valid_coupon->set_date_created( time() );
$valid_coupon->set_date_modified( time() );
$expired_coupon = WC_Helper_Coupon::create_coupon();
$expired_coupon->set_date_expires( time() - 10 );
$expired_coupon->set_date_created( time() - 20 );
$expired_coupon->set_date_modified( time() - 20 );
$this->assertInstanceOf( 'WC_DateTime', $valid_coupon->get_date_created() );
$this->assertTrue( $valid_coupon->is_valid() );
$this->assertFalse( $expired_coupon->is_valid() );
$this->assertEquals( $expired_coupon->get_error_message(), $expired_coupon->get_coupon_error( WC_Coupon::E_WC_COUPON_EXPIRED ) );
}
}

View File

@ -99,6 +99,22 @@ class WC_Tests_Coupon_Data_Store extends WC_Unit_Test_Case {
$this->assertNotEquals( 0, $new_coupon_id );
}
/**
* Test coupon date saving/loading.
* @since 3.0.0
*/
function test_coupon_date_saving() {
$expiry_date = time() - 10;
$coupon = WC_Helper_Coupon::create_coupon( 'coupon-' . time());
$coupon->set_date_expires( $expiry_date );
$coupon->save();
$coupon_read = new WC_Coupon( $coupon->get_id() );
$this->assertEquals( date( 'Y-m-d', $expiry_date ), date( 'Y-m-d', $coupon_read->get_date_expires()->getTimestamp() ) );
}
/**
* Test coupon increase, decrease, user usage count methods.
* @since 2.7.0

View File

@ -161,7 +161,6 @@ class WC_Tests_Coupon_Data extends WC_Unit_Test_Case {
'description' => 'hello world',
'discount_type' => 'percent',
'amount' => 10.50,
'date_expires' => time(),
'usage_count' => 5,
'individual_use' => true,
'product_ids' => array( 5, 10 ),