AfterShip – WooCommerce Tracking - Version 1.12.0

Version Description

  • Add v5/orders API endpoint
Download this release

Release Info

Developer aftership
Plugin Icon 128x128 AfterShip – WooCommerce Tracking
Version 1.12.0
Comparing to
See all releases

Code changes from version 1.11.5 to 1.12.0

aftership-woocommerce-tracking.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Ecommerce Order Tracking and Shipment Notifications - AfterShip
4
  Plugin URI: http://aftership.com/
5
  Description: Effortless order tracking synced from all shipping providers for your ecommerce customers. Include a branded tracking page and automated delivery notifications.
6
- Version: 1.11.5
7
  Author: AfterShip
8
  Author URI: http://aftership.com
9
 
@@ -20,7 +20,7 @@ if ( ! defined( 'ABSPATH' ) ) {
20
 
21
  require_once( 'woo-includes/woo-functions.php' );
22
 
23
- define( 'AFTERSHIP_VERSION', '1.11.5' );
24
 
25
  if ( is_woocommerce_active() ) {
26
 
3
  Plugin Name: Ecommerce Order Tracking and Shipment Notifications - AfterShip
4
  Plugin URI: http://aftership.com/
5
  Description: Effortless order tracking synced from all shipping providers for your ecommerce customers. Include a branded tracking page and automated delivery notifications.
6
+ Version: 1.12.0
7
  Author: AfterShip
8
  Author URI: http://aftership.com
9
 
20
 
21
  require_once( 'woo-includes/woo-functions.php' );
22
 
23
+ define( 'AFTERSHIP_VERSION', '1.12.0' );
24
 
25
  if ( is_woocommerce_active() ) {
26
 
includes/api/class-aftership-api.php CHANGED
@@ -14,7 +14,7 @@ if ( ! defined( 'ABSPATH' ) ) {
14
  exit; // Exit if accessed directly
15
  }
16
 
17
- define( 'AFTERSHIP_LATEST_API_VERSION', 'v4' );
18
 
19
  class AfterShip_API {
20
 
@@ -162,6 +162,8 @@ class AfterShip_API {
162
  include_once( 'v3/class-aftership-api-orders.php' );
163
  include_once( 'v4/class-aftership-api-orders.php' );
164
  include_once( 'v4/class-aftership-api-settings.php' );
 
 
165
 
166
  }
167
 
@@ -180,6 +182,7 @@ class AfterShip_API {
180
  'AfterShip_API_V3_Orders',
181
  'AfterShip_API_V4_Orders',
182
  'AfterShip_API_V4_Settings',
 
183
  )
184
  );
185
 
14
  exit; // Exit if accessed directly
15
  }
16
 
17
+ define( 'AFTERSHIP_LATEST_API_VERSION', 'v5' );
18
 
19
  class AfterShip_API {
20
 
162
  include_once( 'v3/class-aftership-api-orders.php' );
163
  include_once( 'v4/class-aftership-api-orders.php' );
164
  include_once( 'v4/class-aftership-api-settings.php' );
165
+ include_once( 'v5/class-aftership-api-orders.php' );
166
+ include_once( 'v5/class-rest-orders-helper.php' );
167
 
168
  }
169
 
182
  'AfterShip_API_V3_Orders',
183
  'AfterShip_API_V4_Orders',
184
  'AfterShip_API_V4_Settings',
185
+ 'AfterShip_API_V5_Orders',
186
  )
187
  );
188
 
includes/api/v5/class-aftership-api-orders.php ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * AfterShip API Orders Class
4
+ *
5
+ * Handles requests to the /orders endpoint
6
+ *
7
+ * @author AfterShip
8
+ * @category API
9
+ * @package AfterShip/API
10
+ * @since 1.0
11
+ */
12
+
13
+ if ( ! defined( 'ABSPATH' ) ) {
14
+ exit;
15
+ }
16
+
17
+ class AfterShip_API_V5_Orders extends AfterShip_API_V4_Orders {
18
+
19
+ /**
20
+ * Base router path.
21
+ *
22
+ * @var string $base base router path
23
+ */
24
+ protected $base = '/v5/orders';
25
+
26
+ /**
27
+ * Register the routes for this class
28
+ *
29
+ * @param array $routes reg routers.
30
+ *
31
+ * @return array
32
+ */
33
+ public function register_routes( $routes ) {
34
+ $routes[ $this->base . '/ping' ] = array(
35
+ array( array( $this, 'ping' ), AfterShip_API_Server::READABLE ),
36
+ );
37
+
38
+ $routes[ $this->base ] = array(
39
+ array( array( $this, 'get_orders' ), AfterShip_API_Server::READABLE ),
40
+ );
41
+
42
+ $routes[ $this->base . '/(?P<id>[\d]+)' ] = array(
43
+ array( array( $this, 'get_order' ), AfterShip_API_Server::READABLE ),
44
+ );
45
+
46
+ return $routes;
47
+ }
48
+
49
+ /**
50
+ * Get single order by id.
51
+ *
52
+ * @param int $id order id.
53
+ * @param string $fields order fields.
54
+ * @return array|int|WP_Error
55
+ */
56
+ public function get_order( $id, $fields = null ) {
57
+ $tracking_order = parent::get_order( $id, $fields );
58
+
59
+ $object = new WC_Order( $id );
60
+ // get order detial like rest v3 api
61
+ $restOrders = new Rest_Orders_Helper();
62
+ $rest_raw_order = $restOrders->get_formatted_item_data( $object );
63
+ $tracking_order['raw_data'] = $rest_raw_order;
64
+
65
+ return $tracking_order;
66
+ }
67
+
68
+ }
includes/api/v5/class-rest-orders-helper.php ADDED
@@ -0,0 +1,250 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Wc Rest v3 Orders API Helper
4
+ *
5
+ * Handles requests to the /orders endpoint
6
+ *
7
+ * @author AfterShip
8
+ * @category API
9
+ * @package AfterShip/API
10
+ * @since 1.0
11
+ */
12
+
13
+ if ( ! defined( 'ABSPATH' ) ) {
14
+ exit;
15
+ }
16
+
17
+
18
+ class Rest_Orders_Helper {
19
+
20
+ /**
21
+ * Return the number of decimals after the decimal point.
22
+ *
23
+ * @since 2.3
24
+ * @return int
25
+ */
26
+ protected $dp;
27
+
28
+ public function __construct() {
29
+ $this->dp = wc_get_price_decimals();
30
+ }
31
+
32
+ /**
33
+ * Merge the `$formatted_meta_data` `display_key` and `display_value` attribute values into the corresponding
34
+ * {@link WC_Meta_Data}. Returns the merged array.
35
+ *
36
+ * @param WC_Meta_Data $meta_item An object from {@link WC_Order_Item::get_meta_data()}.
37
+ * @param array $formatted_meta_data An object result from {@link WC_Order_Item::get_formatted_meta_data}.
38
+ * The keys are the IDs of {@link WC_Meta_Data}.
39
+ *
40
+ * @return array
41
+ */
42
+ private function merge_meta_item_with_formatted_meta_display_attributes( $meta_item, $formatted_meta_data ) {
43
+ $result = array(
44
+ 'id' => $meta_item->id,
45
+ 'key' => $meta_item->key,
46
+ 'value' => $meta_item->value,
47
+ 'display_key' => $meta_item->key, // Default to original key, in case a formatted key is not available.
48
+ 'display_value' => $meta_item->value, // Default to original value, in case a formatted value is not available.
49
+ );
50
+
51
+ if ( array_key_exists( $meta_item->id, $formatted_meta_data ) ) {
52
+ $formatted_meta_item = $formatted_meta_data[ $meta_item->id ];
53
+
54
+ $result['display_key'] = wc_clean( $formatted_meta_item->display_key );
55
+ $result['display_value'] = wc_clean( $formatted_meta_item->display_value );
56
+ }
57
+
58
+ return $result;
59
+ }
60
+
61
+ /**
62
+ * Expands an order item to get its data.
63
+ *
64
+ * @param WC_Order_item $item Order item data.
65
+ * @return array
66
+ */
67
+ protected function get_order_item_data( $item ) {
68
+ $data = $item->get_data();
69
+ $format_decimal = array( 'subtotal', 'subtotal_tax', 'total', 'total_tax', 'tax_total', 'shipping_tax_total' );
70
+
71
+ // Format decimal values.
72
+ foreach ( $format_decimal as $key ) {
73
+ if ( isset( $data[ $key ] ) ) {
74
+ $data[ $key ] = wc_format_decimal( $data[ $key ], $this->dp );
75
+ }
76
+ }
77
+
78
+ // Add SKU and PRICE to products.
79
+ if ( is_callable( array( $item, 'get_product' ) ) ) {
80
+ $data['sku'] = $item->get_product() ? $item->get_product()->get_sku() : null;
81
+ $data['price'] = $item->get_quantity() ? $item->get_total() / $item->get_quantity() : 0;
82
+ }
83
+
84
+ // Add parent_name if the product is a variation.
85
+ if ( is_callable( array( $item, 'get_product' ) ) ) {
86
+ $product = $item->get_product();
87
+
88
+ if ( is_callable( array( $product, 'get_parent_data' ) ) ) {
89
+ $data['parent_name'] = $product->get_title();
90
+ } else {
91
+ $data['parent_name'] = null;
92
+ }
93
+ }
94
+
95
+ // Format taxes.
96
+ if ( ! empty( $data['taxes']['total'] ) ) {
97
+ $taxes = array();
98
+
99
+ foreach ( $data['taxes']['total'] as $tax_rate_id => $tax ) {
100
+ $taxes[] = array(
101
+ 'id' => $tax_rate_id,
102
+ 'total' => $tax,
103
+ 'subtotal' => isset( $data['taxes']['subtotal'][ $tax_rate_id ] ) ? $data['taxes']['subtotal'][ $tax_rate_id ] : '',
104
+ );
105
+ }
106
+ $data['taxes'] = $taxes;
107
+ } elseif ( isset( $data['taxes'] ) ) {
108
+ $data['taxes'] = array();
109
+ }
110
+
111
+ // Remove names for coupons, taxes and shipping.
112
+ if ( isset( $data['code'] ) || isset( $data['rate_code'] ) || isset( $data['method_title'] ) ) {
113
+ unset( $data['name'] );
114
+ }
115
+
116
+ // Remove props we don't want to expose.
117
+ unset( $data['order_id'] );
118
+ unset( $data['type'] );
119
+
120
+ // Expand meta_data to include user-friendly values.
121
+ $formatted_meta_data = $item->get_formatted_meta_data( null, true );
122
+ $data['meta_data'] = array_map(
123
+ array( $this, 'merge_meta_item_with_formatted_meta_display_attributes' ),
124
+ $data['meta_data'],
125
+ array_fill( 0, count( $data['meta_data'] ), $formatted_meta_data )
126
+ );
127
+
128
+ return $data;
129
+ }
130
+
131
+ /**
132
+ * Get formatted item data.
133
+ *
134
+ * @since 3.0.0
135
+ * @param WC_Order $order WC_Data instance.
136
+ *
137
+ * @return array
138
+ */
139
+ public function get_formatted_item_data( $order ) {
140
+ $extra_fields = array( 'meta_data', 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines', 'refunds' );
141
+ $format_decimal = array( 'discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'shipping_total', 'shipping_tax', 'cart_tax', 'total', 'total_tax' );
142
+ $format_date = array( 'date_created', 'date_modified', 'date_completed', 'date_paid' );
143
+ $format_line_items = array( 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines' );
144
+
145
+ $data = $order->get_base_data();
146
+
147
+ // Add extra data as necessary.
148
+ foreach ( $extra_fields as $field ) {
149
+ switch ( $field ) {
150
+ case 'meta_data':
151
+ $data['meta_data'] = $order->get_meta_data();
152
+ break;
153
+ case 'line_items':
154
+ $data['line_items'] = $order->get_items( 'line_item' );
155
+ break;
156
+ case 'tax_lines':
157
+ $data['tax_lines'] = $order->get_items( 'tax' );
158
+ break;
159
+ case 'shipping_lines':
160
+ $data['shipping_lines'] = $order->get_items( 'shipping' );
161
+ break;
162
+ case 'fee_lines':
163
+ $data['fee_lines'] = $order->get_items( 'fee' );
164
+ break;
165
+ case 'coupon_lines':
166
+ $data['coupon_lines'] = $order->get_items( 'coupon' );
167
+ break;
168
+ case 'refunds':
169
+ $data['refunds'] = array();
170
+ foreach ( $order->get_refunds() as $refund ) {
171
+ $data['refunds'][] = array(
172
+ 'id' => $refund->get_id(),
173
+ 'reason' => $refund->get_reason() ? $refund->get_reason() : '',
174
+ 'total' => '-' . wc_format_decimal( $refund->get_amount(), $this->dp ),
175
+ );
176
+ }
177
+ break;
178
+ }
179
+ }
180
+
181
+ // Format decimal values.
182
+ foreach ( $format_decimal as $key ) {
183
+ $data[ $key ] = wc_format_decimal( $data[ $key ], $this->dp );
184
+ }
185
+
186
+ // Format date values.
187
+ foreach ( $format_date as $key ) {
188
+ $datetime = $data[ $key ];
189
+ $data[ $key ] = wc_rest_prepare_date_response( $datetime, false );
190
+ $data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime );
191
+ }
192
+
193
+ // Format the order status.
194
+ $data['status'] = 'wc-' === substr( $data['status'], 0, 3 ) ? substr( $data['status'], 3 ) : $data['status'];
195
+
196
+ // Format line items.
197
+ foreach ( $format_line_items as $key ) {
198
+ $data[ $key ] = array_values( array_map( array( $this, 'get_order_item_data' ), $data[ $key ] ) );
199
+ }
200
+
201
+ $allowed_fields = array(
202
+ 'id',
203
+ 'parent_id',
204
+ 'number',
205
+ 'order_key',
206
+ 'created_via',
207
+ 'version',
208
+ 'status',
209
+ 'currency',
210
+ 'date_created',
211
+ 'date_created_gmt',
212
+ 'date_modified',
213
+ 'date_modified_gmt',
214
+ 'discount_total',
215
+ 'discount_tax',
216
+ 'shipping_total',
217
+ 'shipping_tax',
218
+ 'cart_tax',
219
+ 'total',
220
+ 'total_tax',
221
+ 'prices_include_tax',
222
+ 'customer_id',
223
+ 'customer_ip_address',
224
+ 'customer_user_agent',
225
+ 'customer_note',
226
+ 'billing',
227
+ 'shipping',
228
+ 'payment_method',
229
+ 'payment_method_title',
230
+ 'transaction_id',
231
+ 'date_paid',
232
+ 'date_paid_gmt',
233
+ 'date_completed',
234
+ 'date_completed_gmt',
235
+ 'cart_hash',
236
+ 'meta_data',
237
+ 'line_items',
238
+ 'tax_lines',
239
+ 'shipping_lines',
240
+ 'fee_lines',
241
+ 'coupon_lines',
242
+ 'refunds',
243
+ );
244
+
245
+ $data = array_intersect_key( $data, array_flip( $allowed_fields ) );
246
+
247
+ return $data;
248
+ }
249
+
250
+ }
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://www.aftership.com/
4
  Tags: ecommerce, shipping, shipment, order, ups, usps, fedex, dhl, tnt, dpd, post, carrier, courier, woocommerce, tracking number, aftership, package tracking, woo commerce, woocommerce shipment tracking, shipping details plugin, widget, shipstation, track, package
5
  Requires at least: 2.9
6
  Tested up to: 5.6
7
- Stable tag: 1.11.5
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -95,8 +95,8 @@ You'll find the FAQ on [AfterShip.com](https://aftership.uservoice.com/knowledge
95
 
96
  == Changelog ==
97
 
98
- = 1.11.5 =
99
- * Fix tracking button url issue.
100
 
101
  = 1.11.4 =
102
  * Fix tracking missing when update plugin.
4
  Tags: ecommerce, shipping, shipment, order, ups, usps, fedex, dhl, tnt, dpd, post, carrier, courier, woocommerce, tracking number, aftership, package tracking, woo commerce, woocommerce shipment tracking, shipping details plugin, widget, shipstation, track, package
5
  Requires at least: 2.9
6
  Tested up to: 5.6
7
+ Stable tag: 1.12.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
95
 
96
  == Changelog ==
97
 
98
+ = 1.12.0 =
99
+ * Add v5/orders API endpoint
100
 
101
  = 1.11.4 =
102
  * Fix tracking missing when update plugin.