WC_GZD_REST_Orders_Controller

Class WC_GZD_REST_Orders_Controller.

Defined (1)

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

/includes/api/class-wc-gzd-rest-orders-controller.php  
  1. class WC_GZD_REST_Orders_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_shop_order', array( $this, 'prepare' ), 10, 3 ); 
  16. add_action( 'woocommerce_rest_insert_shop_order', array( $this, 'insert' ), 10, 3 ); 
  17. add_filter( 'woocommerce_rest_shop_order_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, $order, $request ) { 
  29.  
  30. $order = wc_get_order( $order->id ); 
  31. $response_order_data = $response->get_data(); 
  32.  
  33. $response_order_data['billing']['title'] = $order->billing_title; 
  34. $response_order_data['shipping']['title'] = $order->shipping_title; 
  35.  
  36. $response->set_data( $response_order_data ); 
  37.  
  38. return $response; 
  39.  
  40. /** 
  41. * Prepare a single customer for create or update. 
  42. * @since 1.0.0 
  43. * @wp-hook woocommerce_rest_insert_customer 
  44. * @param \WP_User $customer Data used to create the customer. 
  45. * @param \WP_REST_Request $request Request object. 
  46. * @param bool $creating True when creating item, false when updating. 
  47. */ 
  48. public function insert( $post, $request, $creating ) { 
  49.  
  50. if ( isset( $request['billing']['title'] ) ) { 
  51. update_post_meta( $post->ID, '_billing_title', absint( $request['billing']['title'] ) ); 
  52.  
  53. if ( isset( $request['shipping']['title'] ) ) { 
  54. update_post_meta( $post->ID, '_shipping_title', absint( $request['shipping']['title'] ) ); 
  55.  
  56.  
  57. /** 
  58. * Extend schema. 
  59. * @since 1.0.0 
  60. * @wp-hook woocommerce_rest_customer_schema 
  61. * @param array $schema_properties Data used to create the customer. 
  62. * @return array 
  63. */ 
  64. public function schema( $schema_properties ) { 
  65.  
  66. $schema_properties['billing']['properties']['title'] = array( 
  67. 'description' => __( 'Title', 'woocommerce-germanized' ),  
  68. 'type' => 'integer',  
  69. 'context' => array( 'view', 'edit' ) 
  70. ); 
  71.  
  72. $schema_properties['shipping']['properties']['title'] = array( 
  73. 'description' => __( 'Title', 'woocommerce-germanized' ),  
  74. 'type' => 'integer',  
  75. 'context' => array( 'view', 'edit' ) 
  76. ); 
  77.  
  78. return $schema_properties; 
  79.  
  80. /** 
  81. * Register 
  82. */ 
  83. public function register_fields() { 
  84.  
  85. register_rest_field( 
  86. 'shop_order',  
  87. 'direct_debit',  
  88. array( 
  89. 'get_callback' => array( $this, 'get_direct_debit' ),  
  90. 'update_callback' => array( $this, 'update_direct_debit' ),  
  91. 'schema' => array( 
  92. 'description' => __( 'Direct Debit', 'woocommerce-germanized' ),  
  93. 'type' => 'array',  
  94. 'context' => array( 'view', 'edit' ),  
  95. 'properties' => array( 
  96. 'holder' => array( 
  97. 'description' => __( 'Account Holder', 'woocommerce-germanized' ),  
  98. 'type' => 'string',  
  99. 'context' => array( 'view', 'edit' ) 
  100. ),  
  101. 'iban' => array( 
  102. 'description' => __( 'IBAN', 'woocommerce-germanized' ),  
  103. 'type' => 'string',  
  104. 'context' => array( 'view', 'edit' ) 
  105. ),  
  106. 'bic' => array( 
  107. 'description' => __( 'BIC/SWIFT', 'woocommerce-germanized' ),  
  108. 'type' => 'string',  
  109. 'context' => array( 'view', 'edit' ) 
  110. ),  
  111. 'mandate_id' => array( 
  112. 'description' => __( 'Mandate Reference ID', 'woocommerce-germanized' ),  
  113. 'type' => 'string',  
  114. 'context' => array( 'view', 'edit' ) 
  115. ); 
  116.  
  117.  
  118. /** 
  119. * Handler for getting custom field data. 
  120. * @param array $object The object from the response 
  121. * @param string $field_name Name of field 
  122. * @param \WP_REST_Request $request Current request 
  123. * @return array 
  124. */ 
  125. public function get_direct_debit( $object, $field_name, $request ) { 
  126.  
  127. $holder = get_post_meta( $object['id'], '_direct_debit_holder', true ); 
  128. $iban = get_post_meta( $object['id'], '_direct_debit_iban', true ); 
  129. $bic = get_post_meta( $object['id'], '_direct_debit_bic', true ); 
  130. $mandate_id = get_post_meta( $object['id'], '_direct_debit_mandate_id', true ); 
  131.  
  132. if ( $this->direct_debit_gateway ) { 
  133. $iban = $this->direct_debit_gateway->maybe_decrypt( $iban ); 
  134. $bic = $this->direct_debit_gateway->maybe_decrypt( $bic ); 
  135.  
  136. return array( 
  137. 'holder' => $holder,  
  138. 'iban' => $iban,  
  139. 'bic' => $bic,  
  140. 'mandate_id' => $mandate_id 
  141. ); 
  142.  
  143. /** 
  144. * Handler for updating custom field data 
  145. * @param mixed $value The value of the field 
  146. * @param WP_Post $object The object from the response 
  147. * @param string $field_name Name of field 
  148. * @return bool|int 
  149. */ 
  150. public function update_direct_debit( $value, $object, $field_name ) { 
  151.  
  152. if ( ! $value || ! is_array( $value ) ) { 
  153. return false; 
  154.  
  155. if ( isset( $value['holder'] ) ) { 
  156. update_post_meta( $object->ID, '_direct_debit_holder', sanitize_text_field( $value['holder'] ) ); 
  157.  
  158. if ( isset( $value['iban'] ) ) { 
  159. $iban = sanitize_text_field( $value['iban'] ); 
  160. if ( $this->direct_debit_gateway ) { 
  161. $iban = $this->direct_debit_gateway->maybe_encrypt( $iban ); 
  162. update_post_meta( $object->ID, '_direct_debit_iban', $iban ); 
  163.  
  164. if ( isset( $value['bic'] ) ) { 
  165. $bic = sanitize_text_field( $value['bic'] ); 
  166. if ( $this->direct_debit_gateway ) { 
  167. $bic = $this->direct_debit_gateway->maybe_encrypt( $bic ); 
  168. update_post_meta( $object->ID, '_direct_debit_bic', $bic ); 
  169.  
  170. if ( isset( $value['mandate_id'] ) ) { 
  171. update_post_meta( $object->ID, '_direct_debit_mandate_id', sanitize_text_field( $value['mandate_id'] ) ); 
  172.  
  173. return true; 
  174.