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

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