Allow access to coupon properties directly to allow for back-compat usage, but throw a deprecated notice. Also adds a test to make sure these properties work as they should.

This commit is contained in:
Justin Shreve 2016-02-25 08:10:49 -08:00
parent d738dec05d
commit 447462b274
3 changed files with 111 additions and 35 deletions

View File

@ -2,7 +2,7 @@
include_once( 'legacy/class-wc-legacy-coupon.php' );
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit;
}
/**
@ -95,7 +95,6 @@ class WC_Coupon extends WC_Legacy_Coupon implements WC_Data {
$this->set_code( $code );
$this->read( absint( self::get_coupon_id_from_code( $code ) ) );
}
parent::__construct(); // Call legacy constructor
}
/**
@ -819,7 +818,6 @@ class WC_Coupon extends WC_Legacy_Coupon implements WC_Data {
* @throws Exception
*/
private function validate_exists() {
error_log( print_r ( $this->get_id(), 1 ) );
if ( ! $this->get_id() ) {
throw new Exception( self::E_WC_COUPON_NOT_EXIST );
}

View File

@ -1,7 +1,6 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit;
}
/**
@ -18,52 +17,93 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
class WC_Legacy_Coupon {
/** @public bool Coupon exists */
public $exists = false;
public function __construct() {
$this->exists = ( $this->get_id() > 0 ) ? true : false;
}
/**
* Magic __isset method for backwards compatibility.
* Magic __isset method for backwards compatibility. Legacy properties which could be accessed directly in the past.
* @param string $key
* @return bool
*/
public function __isset( $key ) {
// Legacy properties which could be accessed directly in the past.
if ( in_array( $key, array( 'coupon_custom_fields', 'type', 'amount', 'code' ) ) ) {
$legacy_keys = array(
'exists', 'coupon_custom_fields', 'type', 'discount_type', 'amount', 'code',
'individual_use', 'product_ids', 'exclude_product_ids', 'usage_limit', 'usage_limit_per_user',
'limit_usage_to_x_items', 'usage_count', 'expiry_date', 'product_categories',
'exclude_product_categories', 'minimum_amount', 'maximum_amount', 'customer_email',
);
if ( in_array( $key, $legacy_keys ) ) {
return true;
}
return false;
}
/**
* Magic __get method for backwards compatibility.
* Magic __get method for backwards compatibility. Maps legacy vars to new getters.
* @param string $key
* @return mixed
*/
public function __get( $key ) {
/**
* Maps legacy vars to new getters.
* @todo finish mapping these..
*/
if ( 'coupon_custom_fields' === $key ) {
_doing_it_wrong( $key, 'Coupon properties should not be accessed directly.', '2.6' );
$value = $this->id ? $this->get_custom_fields() : array();
} elseif ( 'type' === $key ) {
_doing_it_wrong( $key, 'Coupon properties should not be accessed directly.', '2.6' );
$value = $this->get_discount_type();
} elseif ( 'amount' === $key ) {
_doing_it_wrong( $key, 'Coupon properties should not be accessed directly.', '2.6' );
$value = $this->get_amount();
} elseif ( 'code' === $key ) {
_doing_it_wrong( $key, 'Coupon properties should not be accessed directly.', '2.6' );
$value = $this->get_code();
} else {
_doing_it_wrong( $key, 'Coupon properties should not be accessed directly.', '2.6' );
$value = '';
_doing_it_wrong( $key, 'Coupon properties should not be accessed directly.', '2.6' );
switch( $key ) {
case 'exists' :
$value = ( $this->get_id() > 0 ) ? true : false;
break;
case 'coupon_custom_fields' :
$value = $this->get_id() ? $this->get_custom_fields() : array();
break;
case 'type' :
case 'discount_type' :
$value = $this->get_discount_type();
break;
case 'amount' :
$value = $this->get_amount();
break;
case 'code' :
$value = $this->get_code();
break;
case 'individual_use' :
$value = ( true === $this->get_is_individual_use() ) ? 'yes' : 'no';
break;
case 'product_ids' :
$value = $this->get_product_ids();
break;
case 'exclude_product_ids' :
$value = $this->get_excluded_product_ids();
break;
case 'usage_limit' :
$value = $this->get_usage_limit();
break;
case 'usage_limit_per_user' :
$value = $this->get_usage_limit_per_user();
break;
case 'limit_usage_to_x_items' :
$value = $this->get_limit_usage_to_x_items();
break;
case 'usage_count' :
$value = $this->get_usage_count();
break;
case 'expiry_date' :
$value = $this->get_expiry_date();
break;
case 'product_categories' :
$value = $this->get_product_categories();
break;
case 'exclude_product_categories' :
$value = $this->get_excluded_product_categories();
break;
case 'minimum_amount' :
$value = $this->get_minimum_amount();
break;
case 'maximum_amount' :
$value = $this->get_maximum_amount();
break;
case 'customer_email' :
$value = $this->get_email_restrictions();
break;
default :
$value = '';
break;
}
return $value;
}

View File

@ -92,5 +92,43 @@ class CouponCRUD extends \WC_Unit_Test_Case {
$this->assertNotEquals( 0, $new_coupon_id );
}
/**
* Test that properties can still be accessed directly for backwards
* compat sake. They throw a deprecated notice.
* @since 2.6.0
*/
public function test_coupon_backwards_compat_props_use_correct_getters() {
// Accessing properties directly will throw some wanted deprected notices
// So we need to let PHPUnit know we are expecting them and it's fine to continue
$legacy_keys = array(
'exists', 'coupon_custom_fields', 'type', 'discount_type', 'amount', 'code',
'individual_use', 'product_ids', 'exclude_product_ids', 'usage_limit', 'usage_limit_per_user',
'limit_usage_to_x_items', 'usage_count', 'expiry_date', 'product_categories',
'exclude_product_categories', 'minimum_amount', 'maximum_amount', 'customer_email',
);
$this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, $legacy_keys );
$coupon = \WC_Helper_Coupon::create_coupon();
$this->assertEquals( ( ( $coupon->get_id() > 0 ) ? true : false ), $coupon->exists );
$this->assertEquals( $coupon->get_custom_fields(), $coupon->coupon_custom_fields );
$this->assertEquals( $coupon->get_discount_type(), $coupon->type );
$this->assertEquals( $coupon->get_discount_type(), $coupon->discount_type );
$this->assertEquals( $coupon->get_amount(), $coupon->amount );
$this->assertEquals( $coupon->get_code(), $coupon->code );
$this->assertEquals( $coupon->get_is_individual_use(), ( 'yes' === $coupon->individual_use ? true : false ) );
$this->assertEquals( $coupon->get_product_ids(), $coupon->product_ids );
$this->assertEquals( $coupon->get_excluded_product_ids(), $coupon->exclude_product_ids );
$this->assertEquals( $coupon->get_usage_limit(), $coupon->usage_limit );
$this->assertEquals( $coupon->get_usage_limit_per_user(), $coupon->usage_limit_per_user );
$this->assertEquals( $coupon->get_limit_usage_to_x_items(), $coupon->limit_usage_to_x_items );
$this->assertEquals( $coupon->get_usage_count(), $coupon->usage_count );
$this->assertEquals( $coupon->get_expiry_date(), $coupon->expiry_date );
$this->assertEquals( $coupon->get_product_categories(), $coupon->product_categories );
$this->assertEquals( $coupon->get_excluded_product_categories(), $coupon->exclude_product_categories );
$this->assertEquals( $coupon->get_minimum_amount(), $coupon->minimum_amount );
$this->assertEquals( $coupon->get_maximum_amount(), $coupon->maximum_amount );
$this->assertEquals( $coupon->get_email_restrictions(), $coupon->customer_email );
}
}