WC_GZD_REST_Customers_Controller

Class WC_GZD_REST_Customers_Controller.

Defined (1)

The class is defined in the following location(s).

/includes/api/class-wc-gzd-rest-customers-controller.php  
  1. class WC_GZD_REST_Customers_Controller { 
  2.  
  3. /** 
  4. * @var WC_GZD_Gateway_Direct_Debit 
  5. */ 
  6. private $direct_debit_gateway = null; 
  7.  
  8. /** 
  9. * ExtendOrdersController constructor. 
  10. */ 
  11. public function __construct() { 
  12. $this->direct_debit_gateway = new WC_GZD_Gateway_Direct_Debit(); 
  13.  
  14. add_filter( 'woocommerce_rest_prepare_customer', array( $this, 'prepare' ), 10, 3 ); 
  15. add_action( 'woocommerce_rest_insert_customer', array( $this, 'insert' ), 10, 3 ); 
  16.  
  17. add_filter( 'woocommerce_rest_customer_schema', array( $this, 'schema' ) ); 
  18.  
  19. /** 
  20. * Filter customer data returned from the REST API. 
  21. * @since 1.0.0 
  22. * @wp-hook woocommerce_rest_prepare_customer 
  23. * @param \WP_REST_Response $response The response object. 
  24. * @param \WP_User $customer User object used to create response. 
  25. * @param \WP_REST_Request $request Request object. 
  26. * @return \WP_REST_Response 
  27. */ 
  28. public function prepare( $response, $customer, $request ) { 
  29.  
  30. $response_customer_data = $response->get_data(); 
  31.  
  32. $response_customer_data['billing']['title'] = $customer->billing_title; 
  33. $response_customer_data['shipping']['title'] = $customer->shipping_title; 
  34.  
  35. $response_customer_data['shipping']['parcelshop'] = $customer->shipping_parcelshop == '1'; 
  36. $response_customer_data['shipping']['parcelshop_post_number'] = $customer->shipping_parcelshop_post_number; 
  37.  
  38. $holder = $customer->direct_debit_holder; 
  39. $iban = $customer->direct_debit_iban; 
  40. $bic = $customer->direct_debit_bic; 
  41.  
  42. if ( $this->direct_debit_gateway ) { 
  43. $iban = $this->direct_debit_gateway->maybe_decrypt( $iban ); 
  44. $bic = $this->direct_debit_gateway->maybe_decrypt( $bic ); 
  45.  
  46. $response_customer_data['direct_debit'] = array( 
  47. 'holder' => $holder,  
  48. 'iban' => $iban,  
  49. 'bic' => $bic 
  50. ); 
  51.  
  52. $response->set_data( $response_customer_data ); 
  53.  
  54. return $response; 
  55.  
  56. /** 
  57. * Prepare a single customer for create or update. 
  58. * @since 1.0.0 
  59. * @wp-hook woocommerce_rest_insert_customer 
  60. * @param \WP_User $customer Data used to create the customer. 
  61. * @param \WP_REST_Request $request Request object. 
  62. * @param bool $creating True when creating item, false when updating. 
  63. */ 
  64. public function insert( $customer, $request, $creating ) { 
  65.  
  66. if ( isset( $request['billing']['title'] ) ) { 
  67. update_user_meta( $customer->ID, 'billing_title', absint( $request['billing']['title'] ) ); 
  68.  
  69. if ( isset( $request['shipping']['title'] ) ) { 
  70. update_user_meta( $customer->ID, 'shipping_title', absint( $request['shipping']['title'] ) ); 
  71.  
  72. if ( isset( $request['shipping']['parcelshop'] ) ) { 
  73. if ( ! $request['shipping']['parcelshop'] || empty( $request['shipping']['parcelshop'] ) ) { 
  74. delete_user_meta( $customer->ID, 'shipping_parcelshop' ); 
  75. } else { 
  76. update_user_meta( $customer->ID, 'shipping_parcelshop', true ); 
  77.  
  78. if ( isset( $request['shipping']['parcelshop_post_number'] ) ) { 
  79. update_user_meta( $customer->ID, 'shipping_parcelshop_post_number', sanitize_text_field( $request['shipping']['parcelshop_post_number'] ) ); 
  80.  
  81. if ( isset( $request['direct_debit'] ) ) { 
  82. if ( isset( $request['direct_debit']['holder'] ) ) { 
  83. update_user_meta( $customer->ID, 'direct_debit_holder', sanitize_text_field( $request['direct_debit']['holder'] ) ); 
  84.  
  85. if ( isset( $request['direct_debit']['iban'] ) ) { 
  86. $iban = sanitize_text_field( $request['direct_debit']['iban'] ); 
  87. if ( $this->direct_debit_gateway ) { 
  88. $iban = $this->direct_debit_gateway->maybe_encrypt( $iban ); 
  89. update_user_meta( $customer->ID, 'direct_debit_iban', $iban ); 
  90.  
  91. if ( isset( $request['direct_debit']['bic'] ) ) { 
  92. $bic = sanitize_text_field( $request['direct_debit']['bic'] ); 
  93. if ( $this->direct_debit_gateway ) { 
  94. $bic = $this->direct_debit_gateway->maybe_encrypt( $bic ); 
  95. update_user_meta( $customer->ID, 'direct_debit_bic', $bic ); 
  96.  
  97. /** 
  98. * Extend schema. 
  99. * @since 1.0.0 
  100. * @wp-hook woocommerce_rest_customer_schema 
  101. * @param array $schema_properties Data used to create the customer. 
  102. * @return array 
  103. */ 
  104. public function schema( $schema_properties ) { 
  105.  
  106. $schema_properties['billing']['properties']['title'] = array( 
  107. 'description' => __( 'Title', 'woocommerce-germanized' ),  
  108. 'type' => 'integer',  
  109. 'context' => array( 'view', 'edit' ),  
  110. 'enum' => array( 1, 2 ) 
  111. ); 
  112.  
  113. $schema_properties['shipping']['properties']['title'] = array( 
  114. 'description' => __( 'Title', 'woocommerce-germanized' ),  
  115. 'type' => 'integer',  
  116. 'context' => array( 'view', 'edit' ),  
  117. 'enum' => array( 1, 2 ) 
  118. ); 
  119.  
  120. $schema_properties['shipping']['properties']['parcelshop'] = array( 
  121. 'description' => __( 'Send to DHL Parcel Shop?', 'woocommerce-germanized' ),  
  122. 'type' => 'boolean',  
  123. 'context' => array( 'view', 'edit' ),  
  124. ); 
  125.  
  126. $schema_properties['shipping']['properties']['parcelshop_post_number'] = array( 
  127. 'description' => __( 'Postnumber', 'woocommerce-germanized' ),  
  128. 'type' => 'string',  
  129. 'context' => array( 'view', 'edit' ),  
  130. ); 
  131.  
  132. $schema_properties['direct_debit'] = array( 
  133. 'description' => __( 'Direct Debit', 'woocommerce-germanized' ),  
  134. 'type' => 'array',  
  135. 'context' => array( 'view', 'edit' ),  
  136. 'properties' => array( 
  137. 'holder' => array( 
  138. 'description' => __( 'Account Holder', 'woocommerce-germanized' ),  
  139. 'type' => 'string',  
  140. 'context' => array( 'view', 'edit' ) 
  141. ),  
  142. 'iban' => array( 
  143. 'description' => __( 'IBAN', 'woocommerce-germanized' ),  
  144. 'type' => 'string',  
  145. 'context' => array( 'view', 'edit' ) 
  146. ),  
  147. 'bic' => array( 
  148. 'description' => __( 'BIC/SWIFT', 'woocommerce-germanized' ),  
  149. 'type' => 'string',  
  150. 'context' => array( 'view', 'edit' ) 
  151. ); 
  152.  
  153. return $schema_properties; 
  154.