class-wc-customer-data-store-session.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Class WC_Customer_Data_Store_Session file.
  4. *
  5. * @package WooCommerce\DataStores
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. /**
  11. * WC Customer Data Store which stores the data in session.
  12. *
  13. * @version 3.0.0
  14. */
  15. class WC_Customer_Data_Store_Session extends WC_Data_Store_WP implements WC_Customer_Data_Store_Interface, WC_Object_Data_Store_Interface {
  16. /**
  17. * Keys which are also stored in a session (so we can make sure they get updated...)
  18. *
  19. * @var array
  20. */
  21. protected $session_keys = array(
  22. 'id',
  23. 'date_modified',
  24. 'billing_postcode',
  25. 'billing_city',
  26. 'billing_address_1',
  27. 'billing_address',
  28. 'billing_address_2',
  29. 'billing_state',
  30. 'billing_country',
  31. 'shipping_postcode',
  32. 'shipping_city',
  33. 'shipping_address_1',
  34. 'shipping_address',
  35. 'shipping_address_2',
  36. 'shipping_state',
  37. 'shipping_country',
  38. 'is_vat_exempt',
  39. 'calculated_shipping',
  40. 'billing_first_name',
  41. 'billing_last_name',
  42. 'billing_company',
  43. 'billing_phone',
  44. 'billing_email',
  45. 'shipping_first_name',
  46. 'shipping_last_name',
  47. 'shipping_company',
  48. );
  49. /**
  50. * Simply update the session.
  51. *
  52. * @param WC_Customer $customer Customer object.
  53. */
  54. public function create( &$customer ) {
  55. $this->save_to_session( $customer );
  56. }
  57. /**
  58. * Simply update the session.
  59. *
  60. * @param WC_Customer $customer Customer object.
  61. */
  62. public function update( &$customer ) {
  63. $this->save_to_session( $customer );
  64. }
  65. /**
  66. * Saves all customer data to the session.
  67. *
  68. * @param WC_Customer $customer Customer object.
  69. */
  70. public function save_to_session( $customer ) {
  71. $data = array();
  72. foreach ( $this->session_keys as $session_key ) {
  73. $function_key = $session_key;
  74. if ( 'billing_' === substr( $session_key, 0, 8 ) ) {
  75. $session_key = str_replace( 'billing_', '', $session_key );
  76. }
  77. $data[ $session_key ] = (string) $customer->{"get_$function_key"}( 'edit' );
  78. }
  79. WC()->session->set( 'customer', $data );
  80. }
  81. /**
  82. * Read customer data from the session unless the user has logged in, in
  83. * which case the stored ID will differ from the actual ID.
  84. *
  85. * @since 3.0.0
  86. * @param WC_Customer $customer Customer object.
  87. */
  88. public function read( &$customer ) {
  89. $data = (array) WC()->session->get( 'customer' );
  90. /**
  91. * There is a valid session if $data is not empty, and the ID matches the logged in user ID.
  92. *
  93. * If the user object has been updated since the session was created (based on date_modified) we should not load the session - data should be reloaded.
  94. */
  95. if ( isset( $data['id'], $data['date_modified'] ) && $data['id'] === (string) $customer->get_id() && $data['date_modified'] === (string) $customer->get_date_modified( 'edit' ) ) {
  96. foreach ( $this->session_keys as $session_key ) {
  97. if ( in_array( $session_key, array( 'id', 'date_modified' ), true ) ) {
  98. continue;
  99. }
  100. $function_key = $session_key;
  101. if ( 'billing_' === substr( $session_key, 0, 8 ) ) {
  102. $session_key = str_replace( 'billing_', '', $session_key );
  103. }
  104. if ( ! empty( $data[ $session_key ] ) && is_callable( array( $customer, "set_{$function_key}" ) ) ) {
  105. $customer->{"set_{$function_key}"}( wp_unslash( $data[ $session_key ] ) );
  106. }
  107. }
  108. }
  109. $this->set_defaults( $customer );
  110. $customer->set_object_read( true );
  111. }
  112. /**
  113. * Load default values if props are unset.
  114. *
  115. * @param WC_Customer $customer Customer object.
  116. */
  117. protected function set_defaults( &$customer ) {
  118. try {
  119. $default = wc_get_customer_default_location();
  120. if ( ! $customer->get_billing_country() ) {
  121. $customer->set_billing_country( $default['country'] );
  122. }
  123. if ( ! $customer->get_shipping_country() ) {
  124. $customer->set_shipping_country( $customer->get_billing_country() );
  125. }
  126. if ( ! $customer->get_billing_state() ) {
  127. $customer->set_billing_state( $default['state'] );
  128. }
  129. if ( ! $customer->get_shipping_state() ) {
  130. $customer->set_shipping_state( $customer->get_billing_state() );
  131. }
  132. if ( ! $customer->get_billing_email() && is_user_logged_in() ) {
  133. $current_user = wp_get_current_user();
  134. $customer->set_billing_email( $current_user->user_email );
  135. }
  136. } catch ( WC_Data_Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
  137. }
  138. }
  139. /**
  140. * Deletes a customer from the database.
  141. *
  142. * @since 3.0.0
  143. * @param WC_Customer $customer Customer object.
  144. * @param array $args Array of args to pass to the delete method.
  145. */
  146. public function delete( &$customer, $args = array() ) {
  147. WC()->session->set( 'customer', null );
  148. }
  149. /**
  150. * Gets the customers last order.
  151. *
  152. * @since 3.0.0
  153. * @param WC_Customer $customer Customer object.
  154. * @return WC_Order|false
  155. */
  156. public function get_last_order( &$customer ) {
  157. return false;
  158. }
  159. /**
  160. * Return the number of orders this customer has.
  161. *
  162. * @since 3.0.0
  163. * @param WC_Customer $customer Customer object.
  164. * @return integer
  165. */
  166. public function get_order_count( &$customer ) {
  167. return 0;
  168. }
  169. /**
  170. * Return how much money this customer has spent.
  171. *
  172. * @since 3.0.0
  173. * @param WC_Customer $customer Customer object.
  174. * @return float
  175. */
  176. public function get_total_spent( &$customer ) {
  177. return 0;
  178. }
  179. }