class-wc-legacy-customer.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * Legacy Customer.
  7. *
  8. * @version 3.0.0
  9. * @package WooCommerce/Classes
  10. * @category Class
  11. * @author WooThemes
  12. */
  13. abstract class WC_Legacy_Customer extends WC_Data {
  14. /**
  15. * __isset legacy.
  16. * @param mixed $key
  17. * @return bool
  18. */
  19. public function __isset( $key ) {
  20. $legacy_keys = array(
  21. 'id',
  22. 'country',
  23. 'state',
  24. 'postcode',
  25. 'city',
  26. 'address_1',
  27. 'address',
  28. 'address_2',
  29. 'shipping_country',
  30. 'shipping_state',
  31. 'shipping_postcode',
  32. 'shipping_city',
  33. 'shipping_address_1',
  34. 'shipping_address',
  35. 'shipping_address_2',
  36. 'is_vat_exempt',
  37. 'calculated_shipping',
  38. );
  39. $key = $this->filter_legacy_key( $key );
  40. return in_array( $key, $legacy_keys );
  41. }
  42. /**
  43. * __get function.
  44. * @param string $key
  45. * @return string
  46. */
  47. public function __get( $key ) {
  48. wc_doing_it_wrong( $key, 'Customer properties should not be accessed directly.', '3.0' );
  49. $key = $this->filter_legacy_key( $key );
  50. if ( in_array( $key, array( 'country', 'state', 'postcode', 'city', 'address_1', 'address', 'address_2' ) ) ) {
  51. $key = 'billing_' . $key;
  52. }
  53. return is_callable( array( $this, "get_{$key}" ) ) ? $this->{"get_{$key}"}() : '';
  54. }
  55. /**
  56. * __set function.
  57. *
  58. * @param string $key
  59. * @param mixed $value
  60. */
  61. public function __set( $key, $value ) {
  62. wc_doing_it_wrong( $key, 'Customer properties should not be set directly.', '3.0' );
  63. $key = $this->filter_legacy_key( $key );
  64. if ( is_callable( array( $this, "set_{$key}" ) ) ) {
  65. $this->{"set_{$key}"}( $value );
  66. }
  67. }
  68. /**
  69. * Address and shipping_address are aliased, so we want to get the 'real' key name.
  70. * For all other keys, we can just return it.
  71. * @since 3.0.0
  72. * @param string $key
  73. * @return string
  74. */
  75. private function filter_legacy_key( $key ) {
  76. if ( 'address' === $key ) {
  77. $key = 'address_1';
  78. }
  79. if ( 'shipping_address' === $key ) {
  80. $key = 'shipping_address_1';
  81. }
  82. return $key;
  83. }
  84. /**
  85. * Sets session data for the location.
  86. *
  87. * @param string $country
  88. * @param string $state
  89. * @param string $postcode (default: '')
  90. * @param string $city (default: '')
  91. */
  92. public function set_location( $country, $state, $postcode = '', $city = '' ) {
  93. $this->set_billing_location( $country, $state, $postcode, $city );
  94. $this->set_shipping_location( $country, $state, $postcode, $city );
  95. }
  96. /**
  97. * Get default country for a customer.
  98. * @return string
  99. */
  100. public function get_default_country() {
  101. wc_deprecated_function( 'WC_Customer::get_default_country', '3.0', 'wc_get_customer_default_location' );
  102. $default = wc_get_customer_default_location();
  103. return $default['country'];
  104. }
  105. /**
  106. * Get default state for a customer.
  107. * @return string
  108. */
  109. public function get_default_state() {
  110. wc_deprecated_function( 'WC_Customer::get_default_state', '3.0', 'wc_get_customer_default_location' );
  111. $default = wc_get_customer_default_location();
  112. return $default['state'];
  113. }
  114. /**
  115. * Set customer address to match shop base address.
  116. */
  117. public function set_to_base() {
  118. wc_deprecated_function( 'WC_Customer::set_to_base', '3.0', 'WC_Customer::set_billing_address_to_base' );
  119. $this->set_billing_address_to_base();
  120. }
  121. /**
  122. * Set customer shipping address to base address.
  123. */
  124. public function set_shipping_to_base() {
  125. wc_deprecated_function( 'WC_Customer::set_shipping_to_base', '3.0', 'WC_Customer::set_shipping_address_to_base' );
  126. $this->set_shipping_address_to_base();
  127. }
  128. /**
  129. * Calculated shipping.
  130. * @param boolean $calculated
  131. */
  132. public function calculated_shipping( $calculated = true ) {
  133. wc_deprecated_function( 'WC_Customer::calculated_shipping', '3.0', 'WC_Customer::set_calculated_shipping' );
  134. $this->set_calculated_shipping( $calculated );
  135. }
  136. /**
  137. * Set default data for a customer.
  138. */
  139. public function set_default_data() {
  140. wc_deprecated_function( 'WC_Customer::set_default_data', '3.0' );
  141. }
  142. /**
  143. * Save data function.
  144. */
  145. public function save_data() {
  146. $this->save();
  147. }
  148. /**
  149. * Is the user a paying customer?
  150. *
  151. * @param int $user_id
  152. *
  153. * @return bool
  154. */
  155. function is_paying_customer( $user_id = '' ) {
  156. wc_deprecated_function( 'WC_Customer::is_paying_customer', '3.0', 'WC_Customer::get_is_paying_customer' );
  157. if ( ! empty( $user_id ) ) {
  158. $user_id = get_current_user_id();
  159. }
  160. return '1' === get_user_meta( $user_id, 'paying_customer', true );
  161. }
  162. /**
  163. * Legacy get address.
  164. */
  165. function get_address() {
  166. wc_deprecated_function( 'WC_Customer::get_address', '3.0', 'WC_Customer::get_billing_address_1' );
  167. return $this->get_billing_address_1();
  168. }
  169. /**
  170. * Legacy get address 2.
  171. */
  172. function get_address_2() {
  173. wc_deprecated_function( 'WC_Customer::get_address_2', '3.0', 'WC_Customer::get_billing_address_2' );
  174. return $this->get_billing_address_2();
  175. }
  176. /**
  177. * Legacy get country.
  178. */
  179. function get_country() {
  180. wc_deprecated_function( 'WC_Customer::get_country', '3.0', 'WC_Customer::get_billing_country' );
  181. return $this->get_billing_country();
  182. }
  183. /**
  184. * Legacy get state.
  185. */
  186. function get_state() {
  187. wc_deprecated_function( 'WC_Customer::get_state', '3.0', 'WC_Customer::get_billing_state' );
  188. return $this->get_billing_state();
  189. }
  190. /**
  191. * Legacy get postcode.
  192. */
  193. function get_postcode() {
  194. wc_deprecated_function( 'WC_Customer::get_postcode', '3.0', 'WC_Customer::get_billing_postcode' );
  195. return $this->get_billing_postcode();
  196. }
  197. /**
  198. * Legacy get city.
  199. */
  200. function get_city() {
  201. wc_deprecated_function( 'WC_Customer::get_city', '3.0', 'WC_Customer::get_billing_city' );
  202. return $this->get_billing_city();
  203. }
  204. /**
  205. * Legacy set country.
  206. *
  207. * @param string $country
  208. */
  209. function set_country( $country ) {
  210. wc_deprecated_function( 'WC_Customer::set_country', '3.0', 'WC_Customer::set_billing_country' );
  211. $this->set_billing_country( $country );
  212. }
  213. /**
  214. * Legacy set state.
  215. *
  216. * @param string $state
  217. */
  218. function set_state( $state ) {
  219. wc_deprecated_function( 'WC_Customer::set_state', '3.0', 'WC_Customer::set_billing_state' );
  220. $this->set_billing_state( $state );
  221. }
  222. /**
  223. * Legacy set postcode.
  224. *
  225. * @param string $postcode
  226. */
  227. function set_postcode( $postcode ) {
  228. wc_deprecated_function( 'WC_Customer::set_postcode', '3.0', 'WC_Customer::set_billing_postcode' );
  229. $this->set_billing_postcode( $postcode );
  230. }
  231. /**
  232. * Legacy set city.
  233. *
  234. * @param string $city
  235. */
  236. function set_city( $city ) {
  237. wc_deprecated_function( 'WC_Customer::set_city', '3.0', 'WC_Customer::set_billing_city' );
  238. $this->set_billing_city( $city );
  239. }
  240. /**
  241. * Legacy set address.
  242. *
  243. * @param string $address
  244. */
  245. function set_address( $address ) {
  246. wc_deprecated_function( 'WC_Customer::set_address', '3.0', 'WC_Customer::set_billing_address' );
  247. $this->set_billing_address( $address );
  248. }
  249. /**
  250. * Legacy set address.
  251. *
  252. * @param string $address
  253. */
  254. function set_address_2( $address ) {
  255. wc_deprecated_function( 'WC_Customer::set_address_2', '3.0', 'WC_Customer::set_billing_address_2' );
  256. $this->set_billing_address_2( $address );
  257. }
  258. }