WC_Correios_REST_API

WC_Correios_REST_API class.

Defined (1)

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

/includes/class-wc-correios-rest-api.php  
  1. class WC_Correios_REST_API { 
  2.  
  3. /** 
  4. * Init REST API actions. 
  5. */ 
  6. public function __construct() { 
  7. add_filter( 'woocommerce_api_order_response', array( $this, 'legacy_orders_response' ), 100, 3 ); 
  8. add_filter( 'woocommerce_api_create_order', array( $this, 'legacy_orders_update' ), 100, 2 ); 
  9. add_filter( 'woocommerce_api_edit_order', array( $this, 'legacy_orders_update' ), 100, 2 ); 
  10. add_action( 'rest_api_init', array( $this, 'register_tracking_code' ), 100 ); 
  11.  
  12. /** 
  13. * Add the tracking code to the legacy WooCOmmerce REST API. 
  14. * @param array $data Endpoint response. 
  15. * @param WC_Order $order Order object. 
  16. * @param array $fields Fields filter. 
  17. * @return array 
  18. */ 
  19. public function legacy_orders_response( $data, $order, $fields ) { 
  20. $data['correios_tracking_code'] = implode( ', ', wc_correios_get_tracking_codes( $order ) ); 
  21.  
  22. if ( $fields ) { 
  23. $data = WC()->api->WC_API_Customers->filter_response_fields( $data, $order, $fields ); 
  24.  
  25. return $data; 
  26.  
  27. /** 
  28. * Update tracking code using the legacy WooCOmmerce REST API. 
  29. * @param int $order_id Order ID. 
  30. * @param array $data Posted data. 
  31. */ 
  32. public function legacy_orders_update( $order_id, $data ) { 
  33. if ( isset( $data['correios_tracking_code'] ) ) { 
  34. wc_correios_update_tracking_code( $order_id, $data['correios_tracking_code'] ); 
  35.  
  36. /** 
  37. * Register tracking code field in WP REST API. 
  38. */ 
  39. public function register_tracking_code() { 
  40. if ( ! function_exists( 'register_rest_field' ) ) { 
  41. return; 
  42.  
  43. register_rest_field( 'shop_order',  
  44. 'correios_tracking_code',  
  45. array( 
  46. 'get_callback' => array( $this, 'get_tracking_code_callback' ),  
  47. 'update_callback' => array( $this, 'update_tracking_code_callback' ),  
  48. 'schema' => array( 
  49. 'description' => __( 'Correios tracking code.', 'woocommerce-correios' ),  
  50. 'type' => 'string',  
  51. 'context' => array( 'view', 'edit' ),  
  52. ),  
  53. ); 
  54.  
  55. /** 
  56. * Get tracking code callback. 
  57. * @param array $data Details of current response. 
  58. * @param string $field Name of field. 
  59. * @param WP_REST_Request $request Current request. 
  60. * @return string 
  61. */ 
  62. function get_tracking_code_callback( $data, $field, $request ) { 
  63. return implode( ', ', wc_correios_get_tracking_codes( $data['id'] ) ); 
  64.  
  65. /** 
  66. * Update tracking code callback. 
  67. * @param string $value The value of the field. 
  68. * @param WP_Post $object The object from the response. 
  69. * @return bool 
  70. */ 
  71. function update_tracking_code_callback( $value, $object ) { 
  72. if ( ! $value || ! is_string( $value ) ) { 
  73. return; 
  74.  
  75. return wc_correios_update_tracking_code( $object->ID, $value );