Improved the WC_Customer class, removed dead variables, removed duplicated code and added new docblocks

This commit is contained in:
claudiosmweb 2014-11-18 12:32:48 -02:00
parent a39353fc73
commit 771f25d7b7
1 changed files with 54 additions and 79 deletions

View File

@ -4,24 +4,46 @@
* *
* The WooCommerce customer class handles storage of the current customer's data, such as location. * The WooCommerce customer class handles storage of the current customer's data, such as location.
* *
* @class WC_Customer * @class WC_Customer
* @version 2.2.0 * @version 2.3.0
* @package WooCommerce/Classes * @package WooCommerce/Classes
* @category Class * @category Class
* @author WooThemes * @author WooThemes
*
* @property string $country
* @property string $state
* @property string $postcode
* @property string $city
* @property string $address
* @property string $address_2
* @property string $shipping_country
* @property string $shipping_state
* @property string $shipping_postcode
* @property string $shipping_city
* @property string $shipping_address
* @property string $shipping_address_2
* @property string $is_vat_exempt
* @property string $calculated_shipping
*/ */
class WC_Customer { class WC_Customer {
/** Stores customer data as an array */ /**
* Stores customer data
*
* @var array
*/
protected $_data; protected $_data;
/** Stores bool when data is changed */ /**
* Stores bool when data is changed
*
* @var bool
*/
private $_changed = false; private $_changed = false;
/** /**
* Constructor for the customer class loads the customer data. * Constructor for the customer class loads the customer data.
* *
* @access public
*/ */
public function __construct() { public function __construct() {
$this->_data = WC()->session->get( 'customer' ); $this->_data = WC()->session->get( 'customer' );
@ -50,9 +72,7 @@ class WC_Customer {
} }
/** /**
* save_data function. * Save data function.
*
* @access public
*/ */
public function save_data() { public function save_data() {
if ( $this->_changed ) { if ( $this->_changed ) {
@ -62,7 +82,7 @@ class WC_Customer {
/** /**
* __set function. * __set function.
* @access public *
* @param mixed $property * @param mixed $property
* @return bool * @return bool
*/ */
@ -73,7 +93,6 @@ class WC_Customer {
/** /**
* __get function. * __get function.
* *
* @access public
* @param string $property * @param string $property
* @return string * @return string
*/ */
@ -84,7 +103,6 @@ class WC_Customer {
/** /**
* __set function. * __set function.
* *
* @access public
* @param mixed $property * @param mixed $property
* @param mixed $value * @param mixed $value
*/ */
@ -136,7 +154,6 @@ class WC_Customer {
/** /**
* has_calculated_shipping function. * has_calculated_shipping function.
* *
* @access public
* @return bool * @return bool
*/ */
public function has_calculated_shipping() { public function has_calculated_shipping() {
@ -145,8 +162,6 @@ class WC_Customer {
/** /**
* Set customer address to match shop base address. * Set customer address to match shop base address.
*
* @access public
*/ */
public function set_to_base() { public function set_to_base() {
$this->country = $this->get_default_country(); $this->country = $this->get_default_country();
@ -157,8 +172,6 @@ class WC_Customer {
/** /**
* Set customer shipping address to base address. * Set customer shipping address to base address.
*
* @access public
*/ */
public function set_shipping_to_base() { public function set_shipping_to_base() {
$this->shipping_country = $this->get_default_country(); $this->shipping_country = $this->get_default_country();
@ -170,34 +183,31 @@ class WC_Customer {
/** /**
* Is customer outside base country (for tax purposes)? * Is customer outside base country (for tax purposes)?
* *
* @access public
* @return bool * @return bool
*/ */
public function is_customer_outside_base() { public function is_customer_outside_base() {
list( $country, $state, $postcode, $city ) = $this->get_taxable_address(); list( $country, $state ) = $this->get_taxable_address();
if ( $country ) { if ( $country ) {
$default = get_option('woocommerce_default_country'); $default = $this->get_default_location();
if ( strstr( $default, ':' ) ) { if ( $default['country'] !== $country ) {
list( $default_country, $default_state ) = explode( ':', $default ); return true;
} else {
$default_country = $default;
$default_state = '';
} }
if ( $default_country !== $country ) return true; if ( $default['state'] && $default['state'] !== $state ) {
if ( $default_state && $default_state !== $state ) return true; return true;
}
} }
return false; return false;
} }
/** /**
* Is the user a paying customer? * Is the user a paying customer?
* *
* @access public
* @return bool * @return bool
*/ */
function is_paying_customer( $user_id ) { function is_paying_customer( $user_id ) {
@ -207,7 +217,6 @@ class WC_Customer {
/** /**
* Is customer VAT exempt? * Is customer VAT exempt?
* *
* @access public
* @return bool * @return bool
*/ */
public function is_vat_exempt() { public function is_vat_exempt() {
@ -217,7 +226,6 @@ class WC_Customer {
/** /**
* Gets the state from the current session. * Gets the state from the current session.
* *
* @access public
* @return string * @return string
*/ */
public function get_state() { public function get_state() {
@ -227,7 +235,6 @@ class WC_Customer {
/** /**
* Gets the country from the current session * Gets the country from the current session
* *
* @access public
* @return string * @return string
*/ */
public function get_country() { public function get_country() {
@ -237,7 +244,6 @@ class WC_Customer {
/** /**
* Gets the postcode from the current session. * Gets the postcode from the current session.
* *
* @access public
* @return string * @return string
*/ */
public function get_postcode() { public function get_postcode() {
@ -247,7 +253,6 @@ class WC_Customer {
/** /**
* Get the city from the current session. * Get the city from the current session.
* *
* @access public
* @return string * @return string
*/ */
public function get_city() { public function get_city() {
@ -257,7 +262,6 @@ class WC_Customer {
/** /**
* Gets the address from the current session. * Gets the address from the current session.
* *
* @access public
* @return string * @return string
*/ */
public function get_address() { public function get_address() {
@ -267,7 +271,6 @@ class WC_Customer {
/** /**
* Gets the address_2 from the current session. * Gets the address_2 from the current session.
* *
* @access public
* @return string * @return string
*/ */
public function get_address_2() { public function get_address_2() {
@ -277,7 +280,6 @@ class WC_Customer {
/** /**
* Gets the state from the current session. * Gets the state from the current session.
* *
* @access public
* @return string * @return string
*/ */
public function get_shipping_state() { public function get_shipping_state() {
@ -288,7 +290,6 @@ class WC_Customer {
/** /**
* Gets the country from the current session. * Gets the country from the current session.
* *
* @access public
* @return string * @return string
*/ */
public function get_shipping_country() { public function get_shipping_country() {
@ -299,7 +300,6 @@ class WC_Customer {
/** /**
* Gets the postcode from the current session. * Gets the postcode from the current session.
* *
* @access public
* @return string * @return string
*/ */
public function get_shipping_postcode() { public function get_shipping_postcode() {
@ -310,7 +310,6 @@ class WC_Customer {
/** /**
* Gets the city from the current session. * Gets the city from the current session.
* *
* @access public
* @return string * @return string
*/ */
public function get_shipping_city() { public function get_shipping_city() {
@ -320,7 +319,6 @@ class WC_Customer {
/** /**
* Gets the address from the current session. * Gets the address from the current session.
* *
* @access public
* @return string * @return string
*/ */
public function get_shipping_address() { public function get_shipping_address() {
@ -330,7 +328,6 @@ class WC_Customer {
/** /**
* Gets the address_2 from the current session. * Gets the address_2 from the current session.
* *
* @access public
* @return string * @return string
*/ */
public function get_shipping_address_2() { public function get_shipping_address_2() {
@ -340,43 +337,38 @@ class WC_Customer {
/** /**
* get_taxable_address function. * get_taxable_address function.
* *
* @access public
* @return array * @return array
*/ */
public function get_taxable_address() { public function get_taxable_address() {
$tax_based_on = get_option( 'woocommerce_tax_based_on' ); $tax_based_on = get_option( 'woocommerce_tax_based_on' );
// Check shipping method at this point to see if we need special handling // Check shipping method at this point to see if we need special handling
if ( apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true ) == true && WC()->cart->needs_shipping() && sizeof( array_intersect( WC()->session->get( 'chosen_shipping_methods', array( get_option( 'woocommerce_default_shipping_method' ) ) ), apply_filters( 'woocommerce_local_pickup_methods', array( 'local_pickup' ) ) ) ) > 0 ) { if ( true == apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true ) && WC()->cart->needs_shipping() && sizeof( array_intersect( WC()->session->get( 'chosen_shipping_methods', array( get_option( 'woocommerce_default_shipping_method' ) ) ), apply_filters( 'woocommerce_local_pickup_methods', array( 'local_pickup' ) ) ) ) > 0 ) {
$tax_based_on = 'base'; $tax_based_on = 'base';
} }
if ( $tax_based_on == 'base' ) { if ( $tax_based_on == 'base' ) {
$default = get_option( 'woocommerce_default_country' ); $default = $this->get_default_location();
if ( strstr( $default, ':' ) ) {
list( $country, $state ) = explode( ':', $default );
} else {
$country = $default;
$state = '';
}
$postcode = ''; $country = $default['country'];
$city = ''; $state = $default['state'];
$postcode = '';
$city = '';
} elseif ( $tax_based_on == 'billing' ) { } elseif ( $tax_based_on == 'billing' ) {
$country = $this->get_country(); $country = $this->get_country();
$state = $this->get_state(); $state = $this->get_state();
$postcode = $this->get_postcode(); $postcode = $this->get_postcode();
$city = $this->get_city(); $city = $this->get_city();
} else { } else {
$country = $this->get_shipping_country(); $country = $this->get_shipping_country();
$state = $this->get_shipping_state(); $state = $this->get_shipping_state();
$postcode = $this->get_shipping_postcode(); $postcode = $this->get_shipping_postcode();
$city = $this->get_shipping_city(); $city = $this->get_shipping_city();
} }
@ -387,7 +379,6 @@ class WC_Customer {
/** /**
* Sets session data for the location. * Sets session data for the location.
* *
* @access public
* @param string $country * @param string $country
* @param string $state * @param string $state
* @param string $postcode (default: '') * @param string $postcode (default: '')
@ -403,7 +394,6 @@ class WC_Customer {
/** /**
* Sets session data for the country. * Sets session data for the country.
* *
* @access public
* @param mixed $country * @param mixed $country
*/ */
public function set_country( $country ) { public function set_country( $country ) {
@ -413,7 +403,6 @@ class WC_Customer {
/** /**
* Sets session data for the state. * Sets session data for the state.
* *
* @access public
* @param mixed $state * @param mixed $state
*/ */
public function set_state( $state ) { public function set_state( $state ) {
@ -423,7 +412,6 @@ class WC_Customer {
/** /**
* Sets session data for the postcode. * Sets session data for the postcode.
* *
* @access public
* @param mixed $postcode * @param mixed $postcode
*/ */
public function set_postcode( $postcode ) { public function set_postcode( $postcode ) {
@ -433,7 +421,6 @@ class WC_Customer {
/** /**
* Sets session data for the city. * Sets session data for the city.
* *
* @access public
* @param mixed $city * @param mixed $city
*/ */
public function set_city( $city ) { public function set_city( $city ) {
@ -443,7 +430,6 @@ class WC_Customer {
/** /**
* Sets session data for the address. * Sets session data for the address.
* *
* @access public
* @param mixed $address * @param mixed $address
*/ */
public function set_address( $address ) { public function set_address( $address ) {
@ -453,7 +439,6 @@ class WC_Customer {
/** /**
* Sets session data for the address_2. * Sets session data for the address_2.
* *
* @access public
* @param mixed $address_2 * @param mixed $address_2
*/ */
public function set_address_2( $address_2 ) { public function set_address_2( $address_2 ) {
@ -463,7 +448,6 @@ class WC_Customer {
/** /**
* Sets session data for the location. * Sets session data for the location.
* *
* @access public
* @param string $country * @param string $country
* @param string $state (default: '') * @param string $state (default: '')
* @param string $postcode (default: '') * @param string $postcode (default: '')
@ -479,7 +463,6 @@ class WC_Customer {
/** /**
* Sets session data for the country. * Sets session data for the country.
* *
* @access public
* @param string $country * @param string $country
*/ */
public function set_shipping_country( $country ) { public function set_shipping_country( $country ) {
@ -489,7 +472,6 @@ class WC_Customer {
/** /**
* Sets session data for the state. * Sets session data for the state.
* *
* @access public
* @param string $state * @param string $state
*/ */
public function set_shipping_state( $state ) { public function set_shipping_state( $state ) {
@ -499,7 +481,6 @@ class WC_Customer {
/** /**
* Sets session data for the postcode. * Sets session data for the postcode.
* *
* @access public
* @param string $postcode * @param string $postcode
*/ */
public function set_shipping_postcode( $postcode ) { public function set_shipping_postcode( $postcode ) {
@ -509,7 +490,6 @@ class WC_Customer {
/** /**
* Sets session data for the city. * Sets session data for the city.
* *
* @access public
* @param string $city * @param string $city
*/ */
public function set_shipping_city( $city ) { public function set_shipping_city( $city ) {
@ -519,7 +499,6 @@ class WC_Customer {
/** /**
* Sets session data for the address. * Sets session data for the address.
* *
* @access public
* @param string $address * @param string $address
*/ */
public function set_shipping_address( $address ) { public function set_shipping_address( $address ) {
@ -529,7 +508,6 @@ class WC_Customer {
/** /**
* Sets session data for the address_2. * Sets session data for the address_2.
* *
* @access public
* @param string $address_2 * @param string $address_2
*/ */
public function set_shipping_address_2( $address_2 ) { public function set_shipping_address_2( $address_2 ) {
@ -539,7 +517,6 @@ class WC_Customer {
/** /**
* Sets session data for the tax exemption. * Sets session data for the tax exemption.
* *
* @access public
* @param bool $is_vat_exempt * @param bool $is_vat_exempt
*/ */
public function set_is_vat_exempt( $is_vat_exempt ) { public function set_is_vat_exempt( $is_vat_exempt ) {
@ -549,7 +526,6 @@ class WC_Customer {
/** /**
* calculated_shipping function. * calculated_shipping function.
* *
* @access public
* @param boolean $calculated * @param boolean $calculated
*/ */
public function calculated_shipping( $calculated = true ) { public function calculated_shipping( $calculated = true ) {
@ -559,7 +535,6 @@ class WC_Customer {
/** /**
* Gets a user's downloadable products if they are logged in. * Gets a user's downloadable products if they are logged in.
* *
* @access public
* @return array Array of downloadable products * @return array Array of downloadable products
*/ */
public function get_downloadable_products() { public function get_downloadable_products() {