PluginProbe
Route ‑ Shipping Protection / 2.4.16
Route ‑ Shipping Protection v2.4.16
2.4.19 2.4.18 2.4.17 2.4.16 2.4.15 2.4.14 2.4.13 2.4.12 2.4.11 2.4.10 2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.2.4 2.2.5 2.2.6 2.2.8 2.2.9 2.3.0 2.3.1 All 153 releases
routeapp / includes / tracking-providers / class-routeapp-shippingeasy.php

class-routeapp-shippingeasy.php in Route ‑ Shipping Protection 2.4.16, at includes/tracking-providers/class-routeapp-shippingeasy.php

183 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 class Routeapp_ShippingEasy extends Routeapp_WooCommerce_Common_Tracking_Provider implements Routeapp_WooCommerce_Tracking_Provider {
5 public function __construct() {
6 }
7
8 public function is_active() {
9 return in_array( 'shippingeasy-for-wp-ecommerce/shippingeasy.php', (array) get_option( 'active_plugins', array() )) || $this->check_for_shipping_easy_integration();
10 }
11
12 public static function get_route_public_instance(){
13 global $routeapp_public;
14 return $routeapp_public;
15 }
16
17 /**
18 * Shipping Easy integrates directly with WooCommerce REST API
19 * making the plugin detection approach useless. So this method intents
20 * to detect ShippingEasy integration based on its default name.
21 *
22 * @return bool
23 */
24 private function check_for_shipping_easy_integration()
25 {
26 global $wpdb;
27
28 $user = $wpdb->get_row(
29 $wpdb->prepare(
30 "
31 SELECT key_id, description, user_id, permissions, consumer_key, consumer_secret, nonces
32 FROM {$wpdb->prefix}woocommerce_api_keys
33 WHERE description LIKE '%ShippingEasy%'
34 ")
35 );
36
37 return !empty($user);
38 }
39
40 public function get_shipping_provider_name($order_id)
41 {
42 $couriers_array = [];
43 $order_notes = wc_get_order_notes([
44 'order_id' => $order_id,
45 'type' => 'system',
46 ]);
47 if (count($order_notes)>0) {
48 foreach ($order_notes as $order_note) {
49 $track_data = $this->parse_individual_order_note($order_note->content);
50 if (isset($track_data['tracking_number']) && isset($track_data['courier_id'])) {
51 $couriers_array[$track_data['tracking_number']] = $track_data['courier_id'];
52 }
53 }
54 }
55 return $couriers_array;
56 }
57
58 public function get_shipping_info($order_id)
59 {
60 $shipping_info = [];
61 $order_notes = wc_get_order_notes([
62 'order_id' => $order_id,
63 'type' => 'system',
64 ]);
65 if (count($order_notes)>0) {
66 $product_ids = $this->get_order_products($order_id);
67 foreach ($order_notes as $order_note) {
68 $track_data = $this->parse_individual_order_note($order_note->content);
69 if (isset($track_data['tracking_number']) && isset($track_data['courier_id'])) {
70 $shipping_info[] = array(
71 'source_order_id' => $order_id,
72 'source_product_ids' => $product_ids,
73 'courier_id' => $track_data['courier_id'],
74 'tracking_number' => $track_data['tracking_number']
75 );
76 }
77 }
78 }
79 return $shipping_info;
80 }
81
82 public function update($order_id, $api_client)
83 {
84 return;
85 }
86
87 public function parse_order_notes($order_id, $route_app)
88 {
89 $order_notes = wc_get_order_notes([
90 'order_id' => $order_id,
91 'type' => 'system',
92 ]);
93 $existing_tracking_numbers = (array) explode( self::SEPARATOR_PIPE,
94 $this->get_meta_data($order_id, 'routeapp_shipment_tracking_number'));
95 if (!$order_notes && !$existing_tracking_numbers) return;
96
97 $product_ids = $this->get_order_products($order_id);
98
99 if (count($order_notes)>0) {
100 $tracking_numbers_array = array();
101 foreach ($order_notes as $order_note) {
102 $track_data = $this->parse_individual_order_note($order_note->content);
103 if (isset($track_data['tracking_number']) && isset($track_data['courier_id'])) {
104 $tracking_number = $track_data['tracking_number'];
105 $tracking_numbers_array[] = $tracking_number;
106 if (in_array($tracking_number, $existing_tracking_numbers)) {
107 continue;
108 }
109
110 $shipmentResponse = $route_app->routeapp_api_client->get_shipment($tracking_number, $order_id);
111 if (is_wp_error($shipmentResponse) || (isset($shipmentResponse['response']['code']) && $shipmentResponse['response']['code'] == 200)) {
112 continue;
113 }
114 $params = array(
115 'source_order_id' => $order_id,
116 'source_product_ids' => $product_ids,
117 'courier_id' => $track_data['courier_id']
118 );
119 $extraData = array();
120 $response = $route_app->routeapp_api_client->create_shipment($tracking_number, $params);
121 $extraData['endpoint'] = 'shipments';
122
123 try{
124 if ( is_wp_error( $response ) ) {
125 throw new Exception($response->get_error_message());
126 }
127 } catch (Exception $exception) {
128 $routeapp_public = self::get_route_public_instance();
129 $params['tracking_id'] = $tracking_number;
130 $extraData = array(
131 'params' => $params,
132 'method' => 'POST'
133 );
134 $routeapp_public->routeapp_log($exception, $extraData);
135 return false;
136 }
137 }
138 }
139 if ($existing_tracking_numbers[0]!='' && !empty($tracking_numbers_array)) {
140 foreach ($existing_tracking_numbers as $existing_tracking_number) {
141 if (!in_array($existing_tracking_number, $tracking_numbers_array)) {
142 $route_app->routeapp_cancel_tracking_order( $order_id, $existing_tracking_number, $product_ids );
143 }
144 }
145 }
146 $tracking_numbers = implode(self::SEPARATOR_PIPE, $tracking_numbers_array);
147 if (!empty($tracking_number)) {
148 $this->add_custom_post_meta($order_id, $tracking_numbers);
149 }
150 return true;
151 }else {
152 /* If user removed the order note from Woo */
153 if ($existing_tracking_numbers[0]!='') {
154 foreach ($existing_tracking_numbers as $existing_tracking_number) {
155 $route_app->routeapp_cancel_tracking_order( $order_id, $existing_tracking_number, $product_ids );
156 }
157 }
158 }
159 }
160
161 private function parse_individual_order_note($note) {
162 $tracking_data = [];
163 if (strpos($note, 'Tracking Number')) {
164 $lines = explode(PHP_EOL, $note);
165 foreach ($lines as $line) {
166 $line = explode(':', $line);
167 switch (trim($line[0])) {
168 case 'Shipment Tracking Number':
169 $tracking_data['tracking_number']= trim($line[1]);
170 break;
171 case 'Carrier Key':
172 $tracking_data['courier_id']= trim($line[1]);
173 break;
174 }
175 }
176 if (isset($tracking_data['tracking_number']) && isset($tracking_data['courier_id'])) {
177 return $tracking_data;
178 }
179 }
180 return false;
181 }
182 }
183