PluginProbe
Route ‑ Shipping Protection / 2.4.18
Route ‑ Shipping Protection v2.4.18
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.18, at includes/tracking-providers/class-routeapp-shippingeasy.php

181 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 self::assert_successful_shipment_create( $response );
125 } catch (Exception $exception) {
126 $routeapp_public = self::get_route_public_instance();
127 $params['tracking_id'] = $tracking_number;
128 $extraData = array(
129 'params' => $params,
130 'method' => 'POST'
131 );
132 $routeapp_public->routeapp_log($exception, $extraData);
133 return false;
134 }
135 }
136 }
137 if ($existing_tracking_numbers[0]!='' && !empty($tracking_numbers_array)) {
138 foreach ($existing_tracking_numbers as $existing_tracking_number) {
139 if (!in_array($existing_tracking_number, $tracking_numbers_array)) {
140 $route_app->routeapp_cancel_tracking_order( $order_id, $existing_tracking_number, $product_ids );
141 }
142 }
143 }
144 $tracking_numbers = implode(self::SEPARATOR_PIPE, $tracking_numbers_array);
145 if (!empty($tracking_number)) {
146 $this->add_custom_post_meta($order_id, $tracking_numbers);
147 }
148 return true;
149 }else {
150 /* If user removed the order note from Woo */
151 if ($existing_tracking_numbers[0]!='') {
152 foreach ($existing_tracking_numbers as $existing_tracking_number) {
153 $route_app->routeapp_cancel_tracking_order( $order_id, $existing_tracking_number, $product_ids );
154 }
155 }
156 }
157 }
158
159 private function parse_individual_order_note($note) {
160 $tracking_data = [];
161 if (strpos($note, 'Tracking Number')) {
162 $lines = explode(PHP_EOL, $note);
163 foreach ($lines as $line) {
164 $line = explode(':', $line);
165 switch (trim($line[0])) {
166 case 'Shipment Tracking Number':
167 $tracking_data['tracking_number']= trim($line[1]);
168 break;
169 case 'Carrier Key':
170 $tracking_data['courier_id']= trim($line[1]);
171 break;
172 }
173 }
174 if (isset($tracking_data['tracking_number']) && isset($tracking_data['courier_id'])) {
175 return $tracking_data;
176 }
177 }
178 return false;
179 }
180 }
181