<?php

defined('ABSPATH') || exit;
require_once dirname(__FILE__) . '/legacy/class-wc-legacy-customer.php';
class WC_Customer extends WC_Legacy_Customer
{
    protected $data = array('date_created' => null, 'date_modified' => null, 'email' => '', 'first_name' => '', 'last_name' => '', 'display_name' => '', 'role' => 'customer', 'username' => '', 'billing' => array('first_name' => '', 'last_name' => '', 'company' => '', 'address_1' => '', 'address_2' => '', 'city' => '', 'postcode' => '', 'country' => '', 'state' => '', 'email' => '', 'phone' => ''), 'shipping' => array('first_name' => '', 'last_name' => '', 'company' => '', 'address_1' => '', 'address_2' => '', 'city' => '', 'postcode' => '', 'country' => '', 'state' => '', 'phone' => ''), 'is_paying_customer' => false);
    protected $password = '';
    protected $is_vat_exempt = false;
    protected $calculated_shipping = false;
    protected $object_type = 'customer';
    public function __construct($data = 0, $is_session = false)
    {
        parent::__construct($data);
        if ($data instanceof WC_Customer) {
            $this->set_id(absint($data->get_id()));
        } elseif (is_numeric($data)) {
            $this->set_id($data);
        }
        $this->data_store = WC_Data_Store::load('customer');
        if ($this->get_id()) {
            try {
                $this->data_store->read($this);
            } catch (Exception $e) {
                $this->set_id(0);
                $this->set_object_read(true);
            }
        } else {
            $this->set_object_read(true);
        }
        if ($is_session && isset(WC()->session)) {
            $this->data_store = WC_Data_Store::load('customer-session');
            $this->data_store->read($this);
        }
    }
    public function delete_and_reassign($reassign = null)
    {
        if ($this->data_store) {
            $this->data_store->delete($this, array('force_delete' => true, 'reassign' => $reassign));
            $this->set_id(0);
            return true;
        }
        return false;
    }
    public function is_customer_outside_base()
    {
        list($country, $state) = $this->get_taxable_address();
        if ($country) {
            $default = wc_get_base_location();
            if ($default['country'] !== $country) {
                return true;
            }
            if ($default['state'] && $default['state'] !== $state) {
                return true;
            }
        }
        return false;
    }
    public function get_avatar_url()
    {
        return get_avatar_url($this->get_email());
    }
    public function get_taxable_address()
    {
        $tax_based_on = get_option('woocommerce_tax_based_on');
        if (true === apply_filters('woocommerce_apply_base_tax_for_local_pickup', true) && count(array_intersect(wc_get_chosen_shipping_method_ids(), apply_filters('woocommerce_local_pickup_methods', array('legacy_local_pickup', 'local_pickup')))) > 0) {
            $tax_based_on = 'base';
        }
        if ('base' === $tax_based_on) {
            $country = WC()->countries->get_base_country();
            $state = WC()->countries->get_base_state();
            $postcode = WC()->countries->get_base_postcode();
            $city = WC()->countries->get_base_city();
        } elseif ('billing' === $tax_based_on) {
            $country = $this->get_billing_country();
            $state = $this->get_billing_state();
            $postcode = $this->get_billing_postcode();
            $city = $this->get_billing_city();
        } else {
            $country = $this->get_shipping_country();
            $state = $this->get_shipping_state();
            $postcode = $this->get_shipping_postcode();
            $city = $this->get_shipping_city();
        }
        return apply_filters('woocommerce_customer_taxable_address', array($country, $state, $postcode, $city));
    }
    public function get_downloadable_products()
    {
        $downloads = array();
        if ($this->get_id()) {
            $downloads = wc_get_customer_available_downloads($this->get_id());
        }
        return apply_filters('woocommerce_customer_get_downloadable_products', $downloads);
    }
    public function is_vat_exempt()
    {
        return $this->get_is_vat_exempt();
    }
    public function has_calculated_shipping()
    {
        return $this->get_calculated_shipping();
    }
    public function has_shipping_address()
    {
        foreach ($this->get_shipping() as $address_field) {
            if (strlen(trim($address_field)) > 0) {
                return true;
            }
        }
        return false;
    }
    public function get_is_vat_exempt()
    {
        return $this->is_vat_exempt;
    }
    public function get_password()
    {
        return $this->password;
    }
    public function get_calculated_shipping()
    {
        return $this->calculated_shipping;
    }
    public function set_is_vat_exempt($is_vat_exempt)
    {
        $this->is_vat_exempt = wc_string_to_bool($is_vat_exempt);
    }
    public function set_calculated_shipping($calculated = true)
    {
        $this->calculated_shipping = wc_string_to_bool($calculated);
    }
    public function set_password($password)
    {
        $this->password = $password;
    }
    public function get_last_order()
    {
        return $this->data_store->get_last_order($this);
    }
    public function get_order_count()
    {
        return $this->data_store->get_order_count($this);
    }
    public function get_total_spent()
    {
        return $this->data_store->get_total_spent($this);
    }
    public function get_username($context = 'view')
    {
        return $this->get_prop('username', $context);
    }
    public function get_email($context = 'view')
    {
        return $this->get_prop('email', $context);
    }
    public function get_first_name($context = 'view')
    {
        return $this->get_prop('first_name', $context);
    }
    public function get_last_name($context = 'view')
    {
        return $this->get_prop('last_name', $context);
    }
    public function get_display_name($context = 'view')
    {
        return $this->get_prop('display_name', $context);
    }
    public function get_role($context = 'view')
    {
        return $this->get_prop('role', $context);
    }
    public function get_date_created($context = 'view')
    {
        return $this->get_prop('date_created', $context);
    }
    public function get_date_modified($context = 'view')
    {
        return $this->get_prop('date_modified', $context);
    }
    protected function get_address_prop($prop, $address = 'billing', $context = 'view')
    {
        $value = null;
        if (array_key_exists($prop, $this->data[$address])) {
            $value = isset($this->changes[$address][$prop]) ? $this->changes[$address][$prop] : $this->data[$address][$prop];
            if ('view' === $context) {
                $value = apply_filters($this->get_hook_prefix() . $address . '_' . $prop, $value, $this);
            }
        }
        return $value;
    }
    public function get_billing($context = 'view')
    {
        $value = null;
        $prop = 'billing';
        if (array_key_exists($prop, $this->data)) {
            $changes = array_key_exists($prop, $this->changes) ? $this->changes[$prop] : array();
            $value = array_merge($this->data[$prop], $changes);
            if ('view' === $context) {
                $value = apply_filters($this->get_hook_prefix() . $prop, $value, $this);
            }
        }
        return $value;
    }
    public function get_billing_first_name($context = 'view')
    {
        return $this->get_address_prop('first_name', 'billing', $context);
    }
    public function get_billing_last_name($context = 'view')
    {
        return $this->get_address_prop('last_name', 'billing', $context);
    }
    public function get_billing_company($context = 'view')
    {
        return $this->get_address_prop('company', 'billing', $context);
    }
    public function get_billing_address($context = 'view')
    {
        return $this->get_billing_address_1($context);
    }
    public function get_billing_address_1($context = 'view')
    {
        return $this->get_address_prop('address_1', 'billing', $context);
    }
    public function get_billing_address_2($context = 'view')
    {
        return $this->get_address_prop('address_2', 'billing', $context);
    }
    public function get_billing_city($context = 'view')
    {
        return $this->get_address_prop('city', 'billing', $context);
    }
    public function get_billing_state($context = 'view')
    {
        return $this->get_address_prop('state', 'billing', $context);
    }
    public function get_billing_postcode($context = 'view')
    {
        return $this->get_address_prop('postcode', 'billing', $context);
    }
    public function get_billing_country($context = 'view')
    {
        return $this->get_address_prop('country', 'billing', $context);
    }
    public function get_billing_email($context = 'view')
    {
        return $this->get_address_prop('email', 'billing', $context);
    }
    public function get_billing_phone($context = 'view')
    {
        return $this->get_address_prop('phone', 'billing', $context);
    }
    public function get_shipping($context = 'view')
    {
        $value = null;
        $prop = 'shipping';
        if (array_key_exists($prop, $this->data)) {
            $changes = array_key_exists($prop, $this->changes) ? $this->changes[$prop] : array();
            $value = array_merge($this->data[$prop], $changes);
            if ('view' === $context) {
                $value = apply_filters($this->get_hook_prefix() . $prop, $value, $this);
            }
        }
        return $value;
    }
    public function get_shipping_first_name($context = 'view')
    {
        return $this->get_address_prop('first_name', 'shipping', $context);
    }
    public function get_shipping_last_name($context = 'view')
    {
        return $this->get_address_prop('last_name', 'shipping', $context);
    }
    public function get_shipping_company($context = 'view')
    {
        return $this->get_address_prop('company', 'shipping', $context);
    }
    public function get_shipping_address($context = 'view')
    {
        return $this->get_shipping_address_1($context);
    }
    public function get_shipping_address_1($context = 'view')
    {
        return $this->get_address_prop('address_1', 'shipping', $context);
    }
    public function get_shipping_address_2($context = 'view')
    {
        return $this->get_address_prop('address_2', 'shipping', $context);
    }
    public function get_shipping_city($context = 'view')
    {
        return $this->get_address_prop('city', 'shipping', $context);
    }
    public function get_shipping_state($context = 'view')
    {
        return $this->get_address_prop('state', 'shipping', $context);
    }
    public function get_shipping_postcode($context = 'view')
    {
        return $this->get_address_prop('postcode', 'shipping', $context);
    }
    public function get_shipping_country($context = 'view')
    {
        return $this->get_address_prop('country', 'shipping', $context);
    }
    public function get_shipping_phone($context = 'view')
    {
        return $this->get_address_prop('phone', 'shipping', $context);
    }
    public function get_is_paying_customer($context = 'view')
    {
        return $this->get_prop('is_paying_customer', $context);
    }
    public function set_username($username)
    {
        $this->set_prop('username', $username);
    }
    public function set_email($value)
    {
        if ($value && !is_email($value)) {
            $this->error('customer_invalid_email', __('Invalid email address', 'woocommerce'));
        }
        $this->set_prop('email', sanitize_email($value));
    }
    public function set_first_name($first_name)
    {
        $this->set_prop('first_name', $first_name);
    }
    public function set_last_name($last_name)
    {
        $this->set_prop('last_name', $last_name);
    }
    public function set_display_name($display_name)
    {
        $this->set_prop('display_name', is_email($display_name) ? sprintf(_x('%1$s %2$s', 'display name', 'woocommerce'), $this->get_first_name(), $this->get_last_name()) : $display_name);
    }
    public function set_role($role)
    {
        global $wp_roles;
        if ($role && !empty($wp_roles->roles) && !in_array($role, array_keys($wp_roles->roles), true)) {
            $this->error('customer_invalid_role', __('Invalid role', 'woocommerce'));
        }
        $this->set_prop('role', $role);
    }
    public function set_date_created($date = null)
    {
        $this->set_date_prop('date_created', $date);
    }
    public function set_date_modified($date = null)
    {
        $this->set_date_prop('date_modified', $date);
    }
    public function set_billing_address_to_base()
    {
        $base = wc_get_customer_default_location();
        $this->set_billing_location($base['country'], $base['state'], '', '');
    }
    public function set_shipping_address_to_base()
    {
        $base = wc_get_customer_default_location();
        $this->set_shipping_location($base['country'], $base['state'], '', '');
    }
    public function set_billing_location($country, $state = '', $postcode = '', $city = '')
    {
        $address_data = $this->get_prop('billing', 'edit');
        $address_data['address_1'] = '';
        $address_data['address_2'] = '';
        $address_data['city'] = $city;
        $address_data['state'] = $state;
        $address_data['postcode'] = $postcode;
        $address_data['country'] = $country;
        $this->set_prop('billing', $address_data);
    }
    public function set_shipping_location($country, $state = '', $postcode = '', $city = '')
    {
        $address_data = $this->get_prop('shipping', 'edit');
        $address_data['address_1'] = '';
        $address_data['address_2'] = '';
        $address_data['city'] = $city;
        $address_data['state'] = $state;
        $address_data['postcode'] = $postcode;
        $address_data['country'] = $country;
        $this->set_prop('shipping', $address_data);
    }
    protected function set_address_prop($prop, $address, $value)
    {
        if (array_key_exists($prop, $this->data[$address])) {
            if (true === $this->object_read) {
                if ($value !== $this->data[$address][$prop] || isset($this->changes[$address]) && array_key_exists($prop, $this->changes[$address])) {
                    $this->changes[$address][$prop] = $value;
                }
            } else {
                $this->data[$address][$prop] = $value;
            }
        }
    }
    public function set_billing_first_name($value)
    {
        $this->set_address_prop('first_name', 'billing', $value);
    }
    public function set_billing_last_name($value)
    {
        $this->set_address_prop('last_name', 'billing', $value);
    }
    public function set_billing_company($value)
    {
        $this->set_address_prop('company', 'billing', $value);
    }
    public function set_billing_address($value)
    {
        $this->set_billing_address_1($value);
    }
    public function set_billing_address_1($value)
    {
        $this->set_address_prop('address_1', 'billing', $value);
    }
    public function set_billing_address_2($value)
    {
        $this->set_address_prop('address_2', 'billing', $value);
    }
    public function set_billing_city($value)
    {
        $this->set_address_prop('city', 'billing', $value);
    }
    public function set_billing_state($value)
    {
        $this->set_address_prop('state', 'billing', $value);
    }
    public function set_billing_postcode($value)
    {
        $this->set_address_prop('postcode', 'billing', $value);
    }
    public function set_billing_country($value)
    {
        $this->set_address_prop('country', 'billing', $value);
    }
    public function set_billing_email($value)
    {
        if ($value && !is_email($value)) {
            $this->error('customer_invalid_billing_email', __('Invalid billing email address', 'woocommerce'));
        }
        $this->set_address_prop('email', 'billing', sanitize_email($value));
    }
    public function set_billing_phone($value)
    {
        $this->set_address_prop('phone', 'billing', $value);
    }
    public function set_shipping_first_name($value)
    {
        $this->set_address_prop('first_name', 'shipping', $value);
    }
    public function set_shipping_last_name($value)
    {
        $this->set_address_prop('last_name', 'shipping', $value);
    }
    public function set_shipping_company($value)
    {
        $this->set_address_prop('company', 'shipping', $value);
    }
    public function set_shipping_address($value)
    {
        $this->set_shipping_address_1($value);
    }
    public function set_shipping_address_1($value)
    {
        $this->set_address_prop('address_1', 'shipping', $value);
    }
    public function set_shipping_address_2($value)
    {
        $this->set_address_prop('address_2', 'shipping', $value);
    }
    public function set_shipping_city($value)
    {
        $this->set_address_prop('city', 'shipping', $value);
    }
    public function set_shipping_state($value)
    {
        $this->set_address_prop('state', 'shipping', $value);
    }
    public function set_shipping_postcode($value)
    {
        $this->set_address_prop('postcode', 'shipping', $value);
    }
    public function set_shipping_country($value)
    {
        $this->set_address_prop('country', 'shipping', $value);
    }
    public function set_shipping_phone($value)
    {
        $this->set_address_prop('phone', 'shipping', $value);
    }
    public function set_is_paying_customer($is_paying_customer)
    {
        $this->set_prop('is_paying_customer', (bool) $is_paying_customer);
    }
}