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. * @param WC_Payment_Gateways $payment_gateways 
  11. */ 
  12. public function __construct() { 
  13. $this->direct_debit_gateway = new WC_GZD_Gateway_Direct_Debit(); 
  14.  
  15. add_filter( 'woocommerce_rest_prepare_customer', array( $this, 'prepare' ), 10, 3 ); 
  16. add_action( 'woocommerce_rest_insert_customer', array( $this, 'insert' ), 10, 3 ); 
  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_order 
  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->set_data( $response_customer_data ); 
  36.  
  37. return $response; 
  38.  
  39. /** 
  40. * Prepare a single customer for create or update. 
  41. * @since 1.0.0 
  42. * @wp-hook woocommerce_rest_insert_customer 
  43. * @param \WP_User $customer Data used to create the customer. 
  44. * @param \WP_REST_Request $request Request object. 
  45. * @param bool $creating True when creating item, false when updating. 
  46. */ 
  47. public function insert( $customer, $request, $creating ) { 
  48.  
  49. if ( isset( $request['billing']['title'] ) ) { 
  50. update_user_meta( $customer->ID, '_billing_title', absint( $request['billing']['title'] ) ); 
  51.  
  52. if ( isset( $request['shipping']['title'] ) ) { 
  53. update_user_meta( $customer->ID, '_shipping_title', absint( $request['shipping']['title'] ) ); 
  54.  
  55.  
  56. /** 
  57. * Extend schema. 
  58. * @since 1.0.0 
  59. * @wp-hook woocommerce_rest_customer_schema 
  60. * @param array $schema_properties Data used to create the customer. 
  61. * @return array 
  62. */ 
  63. public function schema( $schema_properties ) { 
  64.  
  65. $schema_properties['billing']['properties']['title'] = array( 
  66. 'description' => __( 'Title', 'woocommerce-germanized-pro' ),  
  67. 'type' => 'integer',  
  68. 'context' => array( 'view', 'edit' ) 
  69. ); 
  70.  
  71. $schema_properties['shipping']['properties']['title'] = array( 
  72. 'description' => __( 'Title', 'woocommerce-germanized-pro' ),  
  73. 'type' => 'integer',  
  74. 'context' => array( 'view', 'edit' ) 
  75. ); 
  76.  
  77. return $schema_properties; 
  78.  
  79. /** 
  80. * Register 
  81. */ 
  82. public function register_fields() { 
  83.  
  84. register_rest_field( 
  85. 'customer',  
  86. 'direct_debit',  
  87. array( 
  88. 'get_callback' => array( $this, 'get_direct_debit' ),  
  89. 'update_callback' => array( $this, 'update_direct_debit' ),  
  90. 'schema' => array( 
  91. 'description' => __( 'Direct Debit', 'woocommerce-germanized' ),  
  92. 'type' => 'array',  
  93. 'context' => array( 'view', 'edit' ),  
  94. 'properties' => array( 
  95. 'holder' => array( 
  96. 'description' => __( 'Account Holder', 'woocommerce-germanized' ),  
  97. 'type' => 'string',  
  98. 'context' => array( 'view', 'edit' ) 
  99. ),  
  100. 'iban' => array( 
  101. 'description' => __( 'IBAN', 'woocommerce-germanized' ),  
  102. 'type' => 'string',  
  103. 'context' => array( 'view', 'edit' ) 
  104. ),  
  105. 'bic' => array( 
  106. 'description' => __( 'BIC/SWIFT', 'woocommerce-germanized' ),  
  107. 'type' => 'string',  
  108. 'context' => array( 'view', 'edit' ) 
  109. ); 
  110.  
  111.  
  112.  
  113. /** 
  114. * Handler for getting custom field data. 
  115. * @param array $object The object from the response 
  116. * @param string $field_name Name of field 
  117. * @param \WP_REST_Request $request Current request 
  118. * @return array 
  119. */ 
  120. public function get_direct_debit( $object, $field_name, $request ) { 
  121.  
  122. $holder = get_user_meta( $object['id'], 'direct_debit_holder', true ); 
  123. $iban = get_user_meta( $object['id'], 'direct_debit_iban', true ); 
  124. $bic = get_user_meta( $object['id'], 'direct_debit_bic', true ); 
  125.  
  126. if ( $this->direct_debit_gateway ) { 
  127. $iban = $this->direct_debit_gateway->maybe_decrypt( $iban ); 
  128. $bic = $this->direct_debit_gateway->maybe_decrypt( $bic ); 
  129.  
  130. return array( 
  131. 'holder' => $holder,  
  132. 'iban' => $iban,  
  133. 'bic' => $bic 
  134. ); 
  135.  
  136. /** 
  137. * Handler for updating custom field data. 
  138. * @param mixed $value The value of the field 
  139. * @param WP_User $object The object from the response 
  140. * @param string $field_name Name of field 
  141. * @return bool|int 
  142. */ 
  143. public function update_direct_debit( $value, $object, $field_name ) { 
  144.  
  145. if ( ! $value || ! is_array( $value ) ) { 
  146. return false; 
  147.  
  148. if ( isset( $value['holder'] ) ) { 
  149. update_user_meta( $object->ID, 'direct_debit_holder', sanitize_text_field( $value['holder'] ) ); 
  150.  
  151. if ( isset( $value['iban'] ) ) { 
  152. $iban = sanitize_text_field( $value['iban'] ); 
  153. if ( $this->direct_debit_gateway ) { 
  154. $iban = $this->direct_debit_gateway->maybe_encrypt( $iban ); 
  155. update_user_meta( $object->ID, 'direct_debit_iban', $iban ); 
  156.  
  157. if ( isset( $value['bic'] ) ) { 
  158. $bic = sanitize_text_field( $value['bic'] ); 
  159. if ( $this->direct_debit_gateway ) { 
  160. $bic = $this->direct_debit_gateway->maybe_encrypt( $bic ); 
  161. update_user_meta( $object->ID, 'direct_debit_bic', $bic ); 
  162.  
  163. return true; 
  164.