PluginProbe
MakeCommerce for WooCommerce / 3.0.12
MakeCommerce for WooCommerce v3.0.12
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
makecommerce / shipping / shipping.php

shipping.php in MakeCommerce for WooCommerce 3.0.12, at shipping/shipping.php

785 lines 25.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MakeCommerce;
4
5 /**
6 * The admin-specific functionality of the plugin.
7 *
8 * @link https://makecommerce.net/
9 * @since 3.0.0
10 *
11 * @package Makecommerce
12 * @subpackage Makecommerce/shipping
13 */
14
15 /**
16 * The shipping-specific functionality of the plugin.
17 *
18 * Defines the plugin name, version, and two examples hooks for how to
19 * enqueue the admin-specific stylesheet and JavaScript.
20 *
21 * @package Makecommerce
22 * @subpackage Makecommerce/shipping
23 * @author Maksekeskus AS <support@maksekeskus.ee>
24 */
25 class Shipping {
26
27 /**
28 * The ID of this plugin.
29 *
30 * @since 3.0.0
31 * @access private
32 * @var string $plugin_name The ID of this plugin.
33 */
34 private $plugin_name;
35
36 /**
37 * The version of this plugin.
38 *
39 * @since 3.0.0
40 * @access private
41 * @var string $version The current version of this plugin.
42 */
43 private $version;
44
45 private $loader;
46
47 private Shipping\Label $label;
48 private Shipping\Product $product;
49 private Shipping\Order $order;
50
51 /**
52 * Define all shipping methods
53 */
54 public $shipping_methods = array(
55 array(
56 "option" => "mk_transport_apt_omniva",
57 "method" => "parcelmachine_omniva",
58 "class" => "MakeCommerce\Shipping\Method\ParcelMachine\Omniva"
59 ),
60 array(
61 "option" => "mk_transport_apt_smartpost",
62 "method" => "parcelmachine_smartpost",
63 "class" => "MakeCommerce\Shipping\Method\ParcelMachine\Smartpost"
64 ),
65 array(
66 "option" => "mk_transport_apt_dpd",
67 "method" => "parcelmachine_dpd",
68 "class" => "MakeCommerce\Shipping\Method\ParcelMachine\DPD"
69 ),
70 array(
71 "option" => "mk_transport_courier_omniva",
72 "method" => "courier_omniva",
73 "class" => "MakeCommerce\Shipping\Method\Courier\Omniva"
74 ),
75 array(
76 "option" => "mk_transport_courier_smartpost",
77 "method" => "courier_smartpost",
78 "class" => "MakeCommerce\Shipping\Method\Courier\Smartpost"
79 )
80 );
81
82 /**
83 * Initialize the class and set its properties.
84 *
85 * @since 3.0.0
86 * @param string $plugin_name The name of this plugin.
87 * @param string $version The version of this plugin.
88 */
89 public function __construct( $plugin_name, $version, Loader $loader ) {
90
91 $this->plugin_name = $plugin_name;
92 $this->version = $version;
93 $this->loader = $loader;
94
95 //loads all child classes such as label, admin specific functions, email functions
96 $this->load_children();
97
98 if ( is_admin() ) {
99 add_action( 'wp_ajax_verify_feature_swc', array( $this, 'verify_makecommerce_shipment_mediaton_availablity' ) );
100 }
101 }
102
103 /**
104 * Check if the customer has shipment mediation for Omniva enabled in MakeCommerce
105 *
106 * @since 3.0.0
107 */
108 public function verify_makecommerce_shipment_mediaton_availablity() {
109
110 $status = $this->check_shipment_mediaton_availablity( 'shipments_without_credentials' );
111
112 wp_send_json( array( 'feature_name' => 'shipments_without_credentials', 'feature_status' => $status ) );
113
114 exit;
115 }
116
117 /**
118 * Check if a shipment mediation is enabled in MakeCommerce
119 *
120 * @since 3.0.0
121 */
122 public function check_shipment_mediaton_availablity( $feature_name = '' ) {
123
124 $feature_enabled = false;
125
126 $MK = \MakeCommerce::get_api();
127
128 if ( !$MK ) {
129 return false;
130 }
131
132 try {
133 $shopConfig = $MK->getShopConfig( \MakeCommerce::config_request_parameters( "makecommerce feature_staus_check" ) );
134 $features = $shopConfig->features;
135
136 } catch ( \Exception $e ) {
137
138 error_log( print_r( $e, 1 ) );
139
140 return false;
141 }
142
143 if ( isset( $features ) ) {
144
145 foreach ( $features as $feature ) {
146
147 if ( $feature->name == $feature_name ) {
148 $feature_enabled = $feature->enabled;
149 }
150 }
151 }
152
153 return $feature_enabled;
154 }
155
156 /**
157 * Removes transient cache to be able to override shipping cost
158 *
159 * @since 3.0.0
160 */
161 public function initalize() {
162
163 global $wpdb;
164
165 $transients = $wpdb->get_col( "SELECT `option_name` FROM `".$wpdb->options."` WHERE `option_name` LIKE '_transient_wc_ship%'" );
166 if ( count($transients) ) {
167 foreach ( $transients as $tr ) {
168 delete_transient( substr( $tr, 11 ) );
169 }
170 }
171
172 $transient_value = get_transient( 'shipping-transient-version' );
173
174 \WC_Cache_Helper::delete_version_transients( $transient_value );
175
176 if ( WC()->session ) {
177 WC()->session->set( 'shipping_for_package', '' );
178 }
179 }
180
181 /**
182 * Load all supporting child classes
183 *
184 * @since 3.0.0
185 */
186 public function load_children() {
187
188 //load label class (everything to do with shipping labels and printing)
189 $this->label = new Shipping\Label( $this->loader );
190
191 //load product class (everything to do with shipping and products)
192 $this->product = new Shipping\Product( $this->loader );
193
194 //load order class (everything to do with shipping and orders)
195 $this->order = new Shipping\Order( $this->loader );
196 }
197
198 /**
199 * Manually add all different shipping methods to WooCommerce
200 *
201 * @since 3.0.0
202 */
203 public function add_shipping_methods( $methods ) {
204
205 foreach ( $this->shipping_methods as $shipping_method ) {
206
207 if ( get_option( $shipping_method["option"] ) === "yes" ) {
208 $methods[$shipping_method["method"]] = $shipping_method["class"];
209 }
210 }
211
212 return $methods;
213 }
214
215 /**
216 * Clears shipping rates cache, needed for calculation shipping cost
217 *
218 * @since 3.0.0
219 */
220 public function clear_shipping_rates_cache() {
221
222 $packages = WC()->cart->get_shipping_packages();
223
224 foreach ( $packages as $key => $value ) {
225
226 $shipping_session = "shipping_for_package_$key";
227 unset( WC()->session->$shipping_session );
228 }
229 }
230
231 /**
232 * Changes order view WHERE clause to include orders using MakeCommerce shipping
233 *
234 * @since 3.0.0
235 */
236 public function shipping_filter( $where, $wp_query ) {
237
238 global $pagenow, $wpdb;
239
240 $method = !empty( $_REQUEST['_shipping_method'] ) ? $_REQUEST['_shipping_method'] : false;
241
242 if ( is_admin() && $pagenow=='edit.php' && $wp_query->query_vars['post_type'] == 'shop_order' && !empty( $method ) ) {
243 $where .= $GLOBALS['wpdb']->prepare( ' AND ID
244 IN (
245 SELECT items.order_id
246 FROM '.$wpdb->prefix.'woocommerce_order_itemmeta meta, '.$wpdb->prefix.'woocommerce_order_items items
247 WHERE meta.order_item_id = items.order_item_id
248 AND meta.meta_key = "method_id"
249 AND meta.meta_value = %s
250 ) ', $method );
251 }
252
253 return $where;
254 }
255
256 /**
257 * Set order parcel machine meta
258 *
259 * @since 3.0.0
260 */
261 public function set_parcel_machine_meta( $post_id ) {
262
263 $post_type = get_post_type( $post_id );
264
265 if ( 'shop_order' !== $post_type ) {
266 return;
267 }
268
269 if ( isset( $_POST['_mk_machine_id'] ) ) {
270
271 //if the machine id is not the same as it was before then delete the _parcel_machine_shipment_id so a new one can be created.
272 $current_parcel_machine = get_post_meta( $post_id, '_parcel_machine', true );
273
274 if ( $_POST['_mk_machine_id'] != $current_parcel_machine ) {
275
276 //set new parcel machine
277 update_post_meta( $post_id, '_parcel_machine', $_POST['_mk_machine_id'] );
278
279 //remove old shipment id
280 update_post_meta( $post_id, '_parcel_machine_shipment_id', "" );
281
282 //register shipment again
283 $this->register_shipment( $post_id );
284
285 //update address
286 self::update_order_parcelmachine_meta( $_POST['_mk_machine_id'], $post_id );
287 }
288 }
289 }
290
291 /**
292 * Update parcel machine meta data for an order
293 * Adds all the shipping information possible.
294 *
295 * @since 3.0.3
296 */
297 public static function update_order_parcelmachine_meta( $machine_id, $order_id ) {
298
299 list( $carrier, $machine ) = explode( '||', $machine_id );
300
301 $machine = self::mk_get_machine( $carrier, $machine );
302
303 if ( !empty( $machine['id'] ) ) {
304
305 if ( empty( get_post_meta( $order_id, "_shipping_first_name", true ) ) ) {
306 update_post_meta( $order_id, '_shipping_first_name', get_post_meta( $order_id, '_billing_first_name', true ) );
307 }
308
309 if ( empty( get_post_meta( $order_id, "_shipping_last_name", true ) ) ) {
310 update_post_meta( $order_id, '_shipping_last_name', get_post_meta( $order_id, '_billing_last_name', true ) );
311 }
312
313
314 update_post_meta( $order_id, '_shipping_address_1', sanitize_text_field( $machine['name'] ) );
315 update_post_meta( $order_id, '_shipping_address_2', sanitize_text_field( $machine['address'] ) );
316 update_post_meta( $order_id, '_shipping_city', sanitize_text_field( $machine['city'] ) );
317
318 update_post_meta( $order_id, '_shipping_postcode', sanitize_text_field( $machine['zip'] ) );
319 update_post_meta( $order_id, '_parcel_machine', sanitize_text_field( $machine_id ) );
320 }
321 }
322
323 /**
324 * Registers shipment(s) via MakeCommerce API
325 *
326 * @since 3.0.0
327 */
328 public function register_shipment( $post_ids, $select_carrier = null ) {
329
330 //we always expect an array of post ids
331 if ( !is_array( $post_ids ) ) {
332 $post_ids = array( $post_ids );
333 }
334
335 //initialize to delete transient and set WC session
336 $this->initalize();
337
338 $shipping_request = array( 'credentials' => array(), 'orders' => array() );
339
340 //setup shipping classes map
341 $shipping_classes_map = array();
342 foreach ( $this->shipping_methods as $shipping_method ) {
343
344 $shipping_classes_map[$shipping_method["method"]] = $shipping_method["class"];
345 }
346
347 //loop all post ids
348 foreach ( $post_ids as $post_id ) {
349
350 //if parcel machine shipment id already exists then skip
351 $oldId = get_post_meta( $post_id, '_parcel_machine_shipment_id', true );
352
353 if ( strlen( $oldId ) > 6 ) {
354 continue;
355 }
356
357 $order = new \WC_Order( $post_id );
358
359 //check if the post state is even paid, if not, ignore this post...
360 //this could perhaps be in a way better location. Why is the function even called when post state is set to failed.
361 if ( ( string )$order->get_status() !== "completed" && ( string )$order->get_status() !== "processing" ) {
362 continue;
363 }
364
365 //skip if the order doesnt have a shipping method
366 $shipping_methods = $order->get_shipping_methods();
367 if ( empty( $shipping_methods ) ) {
368 continue;
369 }
370
371 //loop all MakeCommerce shipping methods
372 foreach ( $shipping_methods as $shipping_method ) {
373
374 $shipping_id = explode( ':', $shipping_method['method_id'] );
375 $shipping_class = $shipping_id[0];
376 $shipping_instance = !empty( $shipping_id[1] ) ? $shipping_id[1] : null;
377
378 //if chosen method is not the current method then move on to the next one
379 if ( empty( $shipping_classes_map[$shipping_class] ) ) {
380 continue;
381 }
382 $transport_class = new $shipping_classes_map[$shipping_class]( $shipping_instance );
383 $carrier_uc = mb_strtoupper( $transport_class->carrier );
384
385 if ( $transport_class->type === 'apt' ) {
386
387 $parcel_machine = get_post_meta( $post_id, '_parcel_machine', true );
388
389 if ( !$parcel_machine ) {
390 continue;
391 }
392
393 list( $carrier, $machine_id ) = explode( '||', $parcel_machine );
394
395 if ( !$carrier || !$machine_id ) {
396 continue;
397 }
398 }
399
400 if ( empty( $shipping_request['credentials'][$carrier_uc] ) ) {
401
402 $api_user = $transport_class->settings['service_user'];
403 $api_password = $transport_class->settings['service_password'];
404
405 //change carrier_uc if it has been set in the interface.
406 if ( isset( $transport_class->settings['service_carrier'] ) ) {
407 $carrier_uc = $transport_class->settings['service_carrier'];
408 }
409
410 $use_mk_contract = false;
411 if ( isset( $transport_class->settings['use_mk_contract'] ) ) {
412 $use_mk_contract = $transport_class->settings['use_mk_contract'];
413 }
414
415 if ( ( !$api_user || !$api_password ) and ( !$use_mk_contract ) ) {
416 add_action( 'admin_notices', 'mk_admin_error' );
417
418 continue;
419 }
420
421 // if $use_mk_contract is enabled, do not add credentials array to the request
422 if ( !$use_mk_contract ) {
423 $shipping_request['credentials'][$carrier_uc] = array( 'carrier' => $carrier_uc, 'username' => $api_user, 'password' => $api_password );
424 }
425 }
426
427 $sender = array(
428 'name' => !empty( $transport_class->settings['shop_name'] ) ? $transport_class->settings['shop_name'] : '',
429 'phone' => !empty( $transport_class->settings['shop_phone'] ) ? $transport_class->settings['shop_phone'] : '',
430 'email' => !empty( $transport_class->settings['shop_email'] ) ? $transport_class->settings['shop_email'] : '',
431 'country' => !empty( $transport_class->settings['shop_address_country'] ) ? $transport_class->settings['shop_address_country'] : '',
432 'city' => !empty( $transport_class->settings['shop_address_city'] ) ? $transport_class->settings['shop_address_city'] : '',
433 'street' => !empty( $transport_class->settings['shop_address_street'] ) ? $transport_class->settings['shop_address_street'] : '',
434 'postalCode' => !empty( $transport_class->settings['shop_postal_code'] ) ? $transport_class->settings['shop_postal_code'] : ''
435 );
436
437 $shipping_information = $this->get_order_shipping_information( $order );
438
439 $sr_order = array(
440 'carrier' => $carrier_uc,
441 'orderId' => $order->get_id(),
442 'recipient' => array( 'name' => $shipping_information['recipient_name'], 'phone' => $shipping_information['phone'], 'email' => $shipping_information['email'] ),
443 'sender' => $sender,
444 'destination' => array(),
445 );
446
447 $m_service_type = null;
448 if ( $transport_class->carrier === 'omniva' && $transport_class->type === 'atp' ) {
449 $m_service_type = 'PA';
450 } else if ( !empty( $transport_class->settings['registerOnPaymentCode'] ) ) {
451 $m_service_type = $transport_class->settings['registerOnPaymentCode'];
452 }
453
454 if ( !empty( $machine_id ) ) {
455 $sr_order['destination']['destinationId'] = $machine_id;
456 }
457
458 if ( $transport_class->type === 'cou' ) {
459
460 $s_postcode = method_exists( $order, 'get_shipping_postcode' ) ? $order->get_shipping_postcode() : $order->shipping_postcode;
461 $s_country = method_exists( $order, 'get_shipping_country' ) ? $order->get_shipping_country() : $order->shipping_country;
462 $s_state = method_exists( $order, 'get_shipping_state' ) ? $order->get_shipping_state() : $order->shipping_state;
463 $s_city = method_exists( $order, 'get_shipping_city' ) ? $order->get_shipping_city() : $order->shipping_city;
464 $s_address_1 = method_exists( $order, 'get_shipping_address_1' ) ? $order->get_shipping_address_1() : $order->shipping_address_1;
465 $s_address_2 = method_exists( $order, 'get_shipping_address_2' ) ? $order->get_shipping_address_2() : $order->shipping_address_2;
466
467 $sr_order['destination'] = array(
468 'postalCode' => $s_postcode,
469 'country' => $s_country,
470 'county' => $s_state,
471 'city' => $s_city,
472 'street' => $s_address_1 . ' ' . $s_address_2
473 );
474
475 if ( $shipping_class == "courier_smartpost" ) {
476
477 $delivery_time = get_post_meta( $post_id, '_delivery_time', true );
478
479 if ( $delivery_time ) {
480 $sr_order['destination']['timeWindow'] = $delivery_time;
481 }
482 }
483 }
484
485 if ( $m_service_type ) {
486 $sr_order['services'] = array( 'serviceType' => $m_service_type );
487 }
488
489 $shipping_request['orders'][] = $sr_order;
490 }
491 }
492
493 $shipping_request['credentials'] = array_values( $shipping_request['credentials'] );
494
495 if ( empty( $shipping_request['orders'] ) ) {
496 return;
497 }
498
499 $MK = \MakeCommerce::get_api();
500 if ( !$MK ) {
501 return;
502 }
503
504 try {
505 $response = $MK->createShipments( $shipping_request );
506 } catch ( \Exception $e ) {
507 error_log( 'Error while creating shipment ['.$e->getMessage().']' );
508 return;
509 }
510
511 $shipments = !empty( $response->shipments ) ? $response->shipments : $response;
512 $manifest = !empty( $response->manifests ) ? $response->manifests[0] : false;
513
514 foreach ( $shipments as $order ) {
515 if ( $manifest ) {
516 update_post_meta( ( int )$order->orderId, '_parcel_machine_manifest', sanitize_text_field( $manifest ) );
517 }
518
519 if ( !empty( $order->orderId ) && !empty( $order->shipmentId ) ) {
520
521 update_post_meta( ( int )$order->orderId, '_parcel_machine_shipment_id', sanitize_text_field( $order->shipmentId ) );
522 update_post_meta( ( int )$order->orderId, '_tracking_number', sanitize_text_field( $order->shipmentId ) ); //default value used by other plugins, such as WooCommerce PDF Invoices and so on
523 delete_post_meta( ( int )$order->orderId, '_parcel_machine_error' );
524 } else if ( !empty( $order->orderId ) && !empty( $order->barCode ) ) {
525
526 update_post_meta( ( int )$order->orderId, '_parcel_machine_shipment_id', sanitize_text_field( $order->barCode ) );
527 update_post_meta( ( int )$order->orderId, '_tracking_number', sanitize_text_field( $order->barCode ) ); //default value used by other plugins, such as WooCommerce PDF Invoices and so on
528 delete_post_meta( ( int )$order->orderId, '_parcel_machine_error' );
529 } else if ( !empty( $order->orderId ) && !empty( $order->errorMessage ) ) {
530
531 update_post_meta( ( int )$order->orderId, '_parcel_machine_error', sanitize_text_field( $order->errorMessage ) );
532 }
533 }
534 }
535
536 /**
537 * Returns shipping information for order
538 *
539 * @since 3.0.9
540 */
541 public function get_order_shipping_information( $order) {
542
543 $shipping_information = [];
544
545 $shipping_address = $order->get_address( 'shipping' );
546 $billing_address = $order->get_address( 'billing' );
547
548 $phone = '';
549 if ( isset( $shipping_address['phone'] ) ) {
550 $phone = $shipping_address['phone'];
551 }
552
553 if ( $phone == '' && isset( $billing_address['phone'] ) ) {
554 $phone = $billing_address['phone'];
555 }
556
557 $shipping_information['first_name'] = $shipping_address['first_name'] ? $shipping_address['first_name'] : $billing_address['first_name'];
558 $shipping_information['last_name'] = $shipping_address['last_name'] ? $shipping_address['last_name'] : $billing_address['last_name'];
559 $shipping_information['phone'] = $phone;
560 $shipping_information['email'] = get_post_meta( $order->get_id(), '_shipping_email', true ) ? get_post_meta( $order->get_id(), '_shipping_email', true ) : $billing_address['email'];
561
562 $shipping_information['recipient_name'] = $shipping_information['first_name'] . ' ' . $shipping_information['last_name'];
563
564 return $shipping_information;
565 }
566
567 /**
568 * Returns the correct link to be used for tracking link
569 *
570 * @since 3.0.0
571 */
572 public function get_tracking_link( $carrier, $order_id, $shopLocation = false ) {
573
574 //do we use shop location or delivery location
575 if ( $shopLocation ) {
576 //get_base_country returns either EE, LT or LV. Everything else is irrelevant
577 $tld = substr( strtolower( WC()->countries->get_base_country() ), 0, 2);
578
579 $userLang = $tld;
580 } else {
581
582 //get order delivery location. Returns EE, LT or LV. Evertyhing else is irrelevant
583 $tld = substr( strtolower( get_post_meta( $order_id, '_shipping_country', true ) ), 0, 2 );
584 $userLang = substr( strtolower( get_post_meta( $order_id, 'wpml_language', true ) ), 0, 2 ); //returns nothing if it doesnt exist, otherwise returns en, et, lt, lv, ru
585 }
586
587 //
588 // create array for different options [carrier][tld][lang]
589 // first one in the list must be default, this goes for the tld as well as for the userLang.
590 //
591
592 /* Omniva */
593 //EST
594 $trackingURLS["omniva"]["ee"]["ee"] = 'https://www.omniva.ee/abi/jalgimine?barcode=';
595 $trackingURLS["omniva"]["ee"]["en"] = 'https://www.omniva.ee/private/track_and_trace?barcode=';
596 $trackingURLS["omniva"]["ee"]["ru"] = 'https://www.omniva.ee/chastnyj/otslezhivanie_posylki?barcode=';
597
598 //LT
599 $trackingURLS["omniva"]["lt"]["lt"] = 'https://www.omniva.lt/verslo/siuntos_sekimas?barcode=';
600 $trackingURLS["omniva"]["lt"]["en"] = 'https://www.omniva.lt/business/track_and_trace?barcode=';
601
602 //LV
603 $trackingURLS["omniva"]["lv"]["lv"] = 'https://www.omniva.lv/privats/sutijuma_atrasanas_vieta?barcode=';
604 $trackingURLS["omniva"]["lv"]["en"] = 'https://www.omniva.lv/private/track_and_trace?barcode=';
605 $trackingURLS["omniva"]["lv"]["ru"] = 'https://www.omniva.lv/chastnyj/mestonahozhdenie_posylki?barcode=';
606
607 /* DPD */
608 //EE
609 $trackingURLS["dpd"]["ee"]["ee"] = 'https://tracking.dpd.de/status/et_EE/parcel/';
610 $trackingURLS["dpd"]["ee"]["en"] = 'https://tracking.dpd.de/status/en_EE/parcel/';
611
612 //LT
613 $trackingURLS["dpd"]["lt"]["lt"] = 'https://tracking.dpd.de/status/lt_LT/parcel/';
614 $trackingURLS["dpd"]["lt"]["en"] = 'https://tracking.dpd.de/status/en_LT/parcel/';
615
616 //LV
617 $trackingURLS["dpd"]["lv"]["lv"] = 'https://tracking.dpd.de/status/lv_LV/parcel/';
618 $trackingURLS["dpd"]["lv"]["en"] = 'https://tracking.dpd.de/status/en_LV/parcel/';
619
620 /* Smartpost */
621 //EE
622 $trackingURLS["smartpost"]["ee"]["ee"] = 'https://itella.ee/eraklient/saadetise-jalgimine/?trackingCode=';
623 $trackingURLS["smartpost"]["ee"]["en"] = 'https://itella.ee/en/private-customer/parcel-tracking/?trackingCode=';
624 $trackingURLS["smartpost"]["ee"]["ru"] = 'https://itella.ee/ru/chastnyj-klijent/otslezhivanije-otpravlenija/?trackingCode=';
625
626 //FI
627 $trackingURLS["smartpost"]["fi"]["en"] = 'https://www.posti.fi/en/tracking#/lahetys/';
628 $trackingURLS["smartpost"]["fi"]["fi"] = 'https://www.posti.fi/fi/seuranta#/lahetys/';
629 $trackingURLS["smartpost"]["fi"]["sv"] = 'https://www.posti.fi/sv/uppfoljning#/lahetys/';
630
631 //LT
632 $trackingURLS["smartpost"]["lt"]["lt"] = 'https://itella.lt/private-customer/krovinio-sekimas/?trackingCode=';
633 $trackingURLS["smartpost"]["lt"]["en"] = 'https://itella.lt/en/private-customer/track-shipment/?trackingCode=';
634
635 //LV
636 $trackingURLS["smartpost"]["lv"]["lv"] = 'https://itella.lv/private-customer/sutijuma-meklesana/?trackingCode=';
637 $trackingURLS["smartpost"]["lv"]["en"] = 'https://itella.lv/en/private-customer/parcel-tracking/?trackingCode=';
638
639 //carrier exists
640 if ( isset( $trackingURLS[$carrier] ) ) {
641 //country exists
642 if ( isset( $trackingURLS[$carrier][$tld] ) ) {
643 if ( isset( $trackingURLS[$carrier][$tld][$userLang] ) ) {
644 return $trackingURLS[$carrier][$tld][$userLang];
645 } else {
646 //selected language not found, return carrier tld default language.
647 return current( $trackingURLS[$carrier][$tld] );
648 }
649 } else {
650 // selected tld not found, return tld based on store country
651 $tld = substr( strtolower( WC()->countries->get_base_country() ), 0, 2);
652 if ( isset( $trackingURLS[$carrier][$tld] ) ) {
653 $defaultCarrierTLD = current( $trackingURLS[$carrier][$tld] );
654 } else {
655 // no store country was found either
656 $defaultCarrierTLD = current( $trackingURLS[$carrier] );
657 }
658
659 return current( $defaultCarrierTLD );
660 }
661 }
662
663 //not even carrier was found. Return nothing
664 return "";
665 }
666
667 /**
668 * Returns all available parcelmachines
669 *
670 * @since 3.0.0
671 */
672 public static function mk_get_machines( $carrier, $country = null, $aptopts = [] ) {
673
674 //set machine cache
675 $data = get_option( 'mk_machines_cache', false );
676 $data_expires = get_option( 'mk_machines_expires', false );
677
678 if ( !$data || empty( $data ) || $data_expires < time() ) {
679
680 $MK = \MakeCommerce::get_api();
681
682 if( !$MK ) {
683 return array();
684 }
685
686 $data = $MK->getDestinations( array( 'type' => 'APT,PUP' ) );
687
688 update_option( 'mk_machines_cache', $data, 'no' );
689 update_option( 'mk_machines_expires', time() + 3 * 60 * 60, 'no' );
690 }
691
692 //return empty if no machines found for destination
693 if ( !$data || empty( $data ) ) {
694
695 return array();
696 }
697
698 //create machines array
699 $machines = array();
700
701 foreach ( $data as $machine ) {
702
703 if ( ( !$country || $machine->country === $country ) &&
704 ( $machine->type === 'APT' || $machine->type === 'PUP' ) &&
705 ( $carrier === '*' || strtolower( $carrier ) === strtolower( $machine->carrier ) ) ) {
706
707 if ( !(isset($aptopts['use_white_apts']) && $aptopts['use_white_apts'] == 1 &&
708 $machine->carrier == 'SMARTPOST' && strpos($machine->name, 'valge') != false)) {
709 $machines[] = array(
710 'carrier' => strtolower( $machine->carrier ),
711 'id' => $machine->id,
712 'name' => $machine->name,
713 'city' => $machine->city,
714 'address' => !empty( $machine->address ) ? $machine->address : '',
715 'zip' => $machine->zip
716 );
717 }
718 }
719 }
720
721 //sort machines
722 usort( $machines, function( $a, $b ) {
723 if ( $a['city'] === $b['city'] ) {
724
725 return $a['name'] > $b['name'] ? 1 : -1;
726 }
727
728 return $a['city'] > $b['city'] ? 1 : -1;
729 });
730
731 return $machines;
732 }
733
734 /**
735 * Returns a specific parecelmachine
736 *
737 * @since 3.0.0
738 */
739 public static function mk_get_machine( $carrier, $id ) {
740
741 //get all available machines
742 $machines = self::mk_get_machines( $carrier );
743
744 //loop and return the machine we are searching for if found
745 foreach ( $machines as $machine ) {
746
747 if ( $machine['carrier'] === $carrier && $machine['id'] == $id ) {
748
749 return $machine;
750 }
751 }
752
753 return false;
754 }
755
756 /**
757 * Register the stylesheets.
758 *
759 * @since 3.0.0
760 */
761 public function enqueue_styles() {
762
763 if ( is_admin() ) {
764 wp_enqueue_style( $this->plugin_name . "-parcelmachine-admin", plugin_dir_url(__FILE__) . 'css/parcelmachine-admin.css', array(), $this->version );
765 }
766 }
767
768 /**
769 * Register the JavaScript.
770 *
771 * @since 3.0.0
772 */
773 public function enqueue_scripts() {
774
775 if ( is_admin() ) {
776 \MakeCommerce::mc_enqueue_script('MC_PARCELMACHINE_JS_ADMIN', dirname(__FILE__) . '/js/parcelmachine-admin.js', [], [ 'jquery' ]);
777 } else {
778 \MakeCommerce::mc_enqueue_script('MC_PARCELMACHINE_SEARCHABLE_JS', dirname(__FILE__) . '/js/parcelmachine_searchable.js',
779 [ array( 'placeholder' => __( '-- select parcel machine --', 'wc_makecommerce_domain' ) ) ], [ 'jquery' ]
780 );
781 \MakeCommerce::mc_enqueue_script('MC_PARCELMACHINE_JS', dirname(__FILE__) . '/js/parcelmachine.js', [], [ 'jquery' ]);
782 }
783 }
784 }
785