説明なし

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. 'shipping_phone',
  49. );
  50. /**
  51. * Simply update the session.
  52. *
  53. * @param WC_Customer $customer Customer object.
  54. */
  55. public function create( &$customer ) {
  56. $this->save_to_session( $customer );
  57. }
  58. /**
  59. * Simply update the session.
  60. *
  61. * @param WC_Customer $customer Customer object.
  62. */
  63. public function update( &$customer ) {
  64. $this->save_to_session( $customer );
  65. }
  66. /**
  67. * Saves all customer data to the session.
  68. *
  69. * @param WC_Customer $customer Customer object.
  70. */
  71. public function save_to_session( $customer ) {
  72. $data = array();
  73. foreach ( $this->session_keys as $session_key ) {
  74. $function_key = $session_key;
  75. if ( 'billing_' === substr( $session_key, 0, 8 ) ) {
  76. $session_key = str_replace( 'billing_', '', $session_key );
  77. }
  78. $data[ $session_key ] = (string) $customer->{"get_$function_key"}( 'edit' );
  79. }
  80. WC()->session->set( 'customer', $data );
  81. }
  82. /**
  83. * Read customer data from the session unless the user has logged in, in
  84. * which case the stored ID will differ from the actual ID.
  85. *
  86. * @since 3.0.0
  87. * @param WC_Customer $customer Customer object.
  88. */
  89. public function read( &$customer ) {
  90. $data = (array) WC()->session->get( 'customer' );
  91. /**
  92. * There is a valid session if $data is not empty, and the ID matches the logged in user ID.
  93. *
  94. * 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.
  95. */
  96. if ( isset( $data['id'], $data['date_modified'] ) && $data['id'] === (string) $customer->get_id() && $data['date_modified'] === (string) $customer->get_date_modified( 'edit' ) ) {
  97. foreach ( $this->session_keys as $session_key ) {
  98. if ( in_array( $session_key, array( 'id', 'date_modified' ), true ) ) {
  99. continue;
  100. }
  101. $function_key = $session_key;
  102. if ( 'billing_' === substr( $session_key, 0, 8 ) ) {
  103. $session_key = str_replace( 'billing_', '', $session_key );
  104. }
  105. if ( isset( $data[ $session_key ] ) && is_callable( array( $customer, "set_{$function_key}" ) ) ) {
  106. $customer->{"set_{$function_key}"}( wp_unslash( $data[ $session_key ] ) );
  107. }
  108. }
  109. }
  110. $this->set_defaults( $customer );
  111. $customer->set_object_read( true );
  112. }
  113. /**
  114. * Load default values if props are unset.
  115. *
  116. * @param WC_Customer $customer Customer object.
  117. */
  118. protected function set_defaults( &$customer ) {
  119. try {
  120. $default = wc_get_customer_default_location();
  121. $has_shipping_address = $customer->has_shipping_address();
  122. if ( ! $customer->get_billing_country() ) {
  123. $customer->set_billing_country( $default['country'] );
  124. }
  125. if ( ! $customer->get_shipping_country() && ! $has_shipping_address ) {
  126. $customer->set_shipping_country( $customer->get_billing_country() );
  127. }
  128. if ( ! $customer->get_billing_state() ) {
  129. $customer->set_billing_state( $default['state'] );
  130. }
  131. if ( ! $customer->get_shipping_state() && ! $has_shipping_address ) {
  132. $customer->set_shipping_state( $customer->get_billing_state() );
  133. }
  134. if ( ! $customer->get_billing_email() && is_user_logged_in() ) {
  135. $current_user = wp_get_current_user();
  136. $customer->set_billing_email( $current_user->user_email );
  137. }
  138. } catch ( WC_Data_Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
  139. }
  140. }
  141. /**
  142. * Deletes a customer from the database.
  143. *
  144. * @since 3.0.0
  145. * @param WC_Customer $customer Customer object.
  146. * @param array $args Array of args to pass to the delete method.
  147. */
  148. public function delete( &$customer, $args = array() ) {
  149. WC()->session->set( 'customer', null );
  150. }
  151. /**
  152. * Gets the customers last order.
  153. *
  154. * @since 3.0.0
  155. * @param WC_Customer $customer Customer object.
  156. * @return WC_Order|false
  157. */
  158. public function get_last_order( &$customer ) {
  159. return false;
  160. }
  161. /**
  162. * Return the number of orders this customer has.
  163. *
  164. * @since 3.0.0
  165. * @param WC_Customer $customer Customer object.
  166. * @return integer
  167. */
  168. public function get_order_count( &$customer ) {
  169. return 0;
  170. }
  171. /**
  172. * Return how much money this customer has spent.
  173. *
  174. * @since 3.0.0
  175. * @param WC_Customer $customer Customer object.
  176. * @return float
  177. */
  178. public function get_total_spent( &$customer ) {
  179. return 0;
  180. }
  181. }