PluginProbe
MakeCommerce for WooCommerce / 3.0.2
MakeCommerce for WooCommerce v3.0.2
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.2, at shipping/shipping.php

700 lines 22.6 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 update_post_meta( $post_id, '_parcel_machine', $_POST['_mk_machine_id'] );
272 $this->register_shipment( $post_id );
273 }
274 }
275
276 /**
277 * Registers shipment(s) via MakeCommerce API
278 *
279 * @since 3.0.0
280 */
281 public function register_shipment( $post_ids, $select_carrier = null ) {
282
283 //we always expect an array of post ids
284 if ( !is_array( $post_ids ) ) {
285 $post_ids = array( $post_ids );
286 }
287
288 //initialize to delete transient and set WC session
289 $this->initalize();
290
291 $shipping_request = array( 'credentials' => array(), 'orders' => array() );
292
293 //setup shipping classes map
294 $shipping_classes_map = array();
295 foreach ( $this->shipping_methods as $shipping_method ) {
296
297 $shipping_classes_map[$shipping_method["method"]] = $shipping_method["class"];
298 }
299
300 //loop all post ids
301 foreach ( $post_ids as $post_id ) {
302
303 //if parcel machine shipment id already exists then skip
304 $oldId = get_post_meta( $post_id, '_parcel_machine_shipment_id', true );
305
306 if ( strlen( $oldId ) > 6 ) {
307 continue;
308 }
309
310 $order = new \WC_Order( $post_id );
311
312 //check if the post state is even paid, if not, ignore this post...
313 //this could perhaps be in a way better location. Why is the function even called when post state is set to failed.
314 if ( ( string )$order->get_status() !== "completed" && ( string )$order->get_status() !== "processing" ) {
315 continue;
316 }
317
318 //skip if the order doesnt have a shipping method
319 $shipping_methods = $order->get_shipping_methods();
320 if ( empty( $shipping_methods ) ) {
321 continue;
322 }
323
324 //loop all MakeCommerce shipping methods
325 foreach ( $shipping_methods as $shipping_method ) {
326
327 $shipping_id = explode( ':', $shipping_method['method_id'] );
328 $shipping_class = $shipping_id[0];
329 $shipping_instance = !empty( $shipping_id[1] ) ? $shipping_id[1] : null;
330
331 //if chosen method is not the current method then move on to the next one
332 if ( empty( $shipping_classes_map[$shipping_class] ) ) {
333 continue;
334 }
335
336 $transport_class = new $shipping_classes_map[$shipping_class]( $shipping_instance );
337 $carrier_uc = mb_strtoupper( $transport_class->carrier );
338
339 if ( $transport_class->type === 'apt' ) {
340
341 $parcel_machine = get_post_meta( $post_id, '_parcel_machine', true );
342
343 if ( !$parcel_machine ) {
344 continue;
345 }
346
347 list( $carrier, $machine_id ) = explode( '||', $parcel_machine );
348
349 if ( !$carrier || !$machine_id ) {
350 continue;
351 }
352 }
353
354 if ( empty( $shipping_request['credentials'][$carrier_uc] ) ) {
355
356 $api_user = $transport_class->settings['service_user'];
357 $api_password = $transport_class->settings['service_password'];
358
359 //change carrier_uc if it has been set in the interface.
360 if ( isset( $transport_class->settings['service_carrier'] ) ) {
361 $carrier_uc = $transport_class->settings['service_carrier'];
362 }
363
364 $use_mk_contract = false;
365 if ( isset( $transport_class->settings['use_mk_contract'] ) ) {
366 $use_mk_contract = $transport_class->settings['use_mk_contract'];
367 }
368
369 if ( ( !$api_user || !$api_password ) and ( !$use_mk_contract ) ) {
370 add_action( 'admin_notices', 'mk_admin_error' );
371
372 continue;
373 }
374
375 // if $use_mk_contract is enabled, do not add credentials array to the request
376 if ( !$use_mk_contract ) {
377 $shipping_request['credentials'][$carrier_uc] = array( 'carrier' => $carrier_uc, 'username' => $api_user, 'password' => $api_password );
378 }
379 }
380
381 $sender = array(
382 'name' => !empty( $transport_class->settings['shop_name'] ) ? $transport_class->settings['shop_name'] : '',
383 'phone' => !empty( $transport_class->settings['shop_phone'] ) ? $transport_class->settings['shop_phone'] : '',
384 'email' => !empty( $transport_class->settings['shop_email'] ) ? $transport_class->settings['shop_email'] : '',
385 'country' => !empty( $transport_class->settings['shop_address_country'] ) ? $transport_class->settings['shop_address_country'] : '',
386 'city' => !empty( $transport_class->settings['shop_address_city'] ) ? $transport_class->settings['shop_address_city'] : '',
387 'street' => !empty( $transport_class->settings['shop_address_street'] ) ? $transport_class->settings['shop_address_street'] : '',
388 'postalCode' => !empty( $transport_class->settings['shop_postal_code'] ) ? $transport_class->settings['shop_postal_code'] : ''
389 );
390
391 $s_first_name = method_exists( $order, 'get_shipping_first_name' ) ? $order->get_shipping_first_name() : $order->shipping_first_name;
392 $s_last_name = method_exists( $order, 'get_shipping_last_name' ) ? $order->get_shipping_last_name() : $order->shipping_last_name;
393 $b_first_name = method_exists( $order, 'get_billing_first_name' ) ? $order->get_billing_first_name() : $order->billing_first_name;
394 $b_last_name = method_exists( $order, 'get_billing_last_name' ) ? $order->get_billing_last_name() : $order->billing_last_name;
395 $b_phone = method_exists( $order, 'get_billing_phone' ) ? $order->get_billing_phone() : $order->billing_phone;
396 $b_email = method_exists( $order, 'get_billing_email' ) ? $order->get_billing_email() : $order->billing_email;
397 $recipient_name = $s_first_name && $s_last_name ? $s_first_name . ' ' . $s_last_name : $b_first_name . ' ' . $b_last_name;
398
399 $sr_order = array(
400 'carrier' => $carrier_uc,
401 'orderId' => $order->get_id(),
402 'recipient' => array( 'name' => $recipient_name, 'phone' => $b_phone, 'email' => $b_email ),
403 'sender' => $sender,
404 'destination' => array(),
405 );
406
407 $m_service_type = null;
408 if ( $transport_class->carrier === 'omniva' && $transport_class->type === 'atp' ) {
409 $m_service_type = 'PA';
410 } else if ( !empty( $transport_class->settings['registerOnPaymentCode'] ) ) {
411 $m_service_type = $transport_class->settings['registerOnPaymentCode'];
412 }
413
414 if ( !empty( $machine_id ) ) {
415 $sr_order['destination']['destinationId'] = $machine_id;
416 }
417
418 if ( $transport_class->type === 'cou' ) {
419
420 $s_postcode = method_exists( $order, 'get_shipping_postcode' ) ? $order->get_shipping_postcode() : $order->shipping_postcode;
421 $s_country = method_exists( $order, 'get_shipping_country' ) ? $order->get_shipping_country() : $order->shipping_country;
422 $s_state = method_exists( $order, 'get_shipping_state' ) ? $order->get_shipping_state() : $order->shipping_state;
423 $s_city = method_exists( $order, 'get_shipping_city' ) ? $order->get_shipping_city() : $order->shipping_city;
424 $s_address_1 = method_exists( $order, 'get_shipping_address_1' ) ? $order->get_shipping_address_1() : $order->shipping_address_1;
425 $s_address_2 = method_exists( $order, 'get_shipping_address_2' ) ? $order->get_shipping_address_2() : $order->shipping_address_2;
426
427 $sr_order['destination'] = array(
428 'postalCode' => $s_postcode,
429 'country' => $s_country,
430 'county' => $s_state,
431 'city' => $s_city,
432 'street' => $s_address_1 . ' ' . $s_address_2
433 );
434
435 if ( $shipping_class == "courier_smartpost" ) {
436
437 $delivery_time = get_post_meta( $post_id, '_delivery_time', true );
438
439 if ( $delivery_time ) {
440 $sr_order['destination']['timeWindow'] = $delivery_time;
441 }
442 }
443 }
444
445 if ( $m_service_type ) {
446 $sr_order['services'] = array( 'serviceType' => $m_service_type );
447 }
448
449 $shipping_request['orders'][] = $sr_order;
450 }
451 }
452
453 $shipping_request['credentials'] = array_values( $shipping_request['credentials'] );
454
455 if ( empty( $shipping_request['orders'] ) ) {
456 return;
457 }
458
459 $MK = \MakeCommerce::get_api();
460 if ( !$MK ) {
461 return;
462 }
463
464 try {
465 $response = $MK->createShipments( $shipping_request );
466 } catch ( \Exception $e ) {
467 error_log( 'Error while creating shipment ['.$e->getMessage().']' );
468 return;
469 }
470
471 $shipments = !empty( $response->shipments ) ? $response->shipments : $response;
472 $manifest = !empty( $response->manifests ) ? $response->manifests[0] : false;
473
474 foreach ( $shipments as $order ) {
475 if ( $manifest ) {
476 update_post_meta( ( int )$order->orderId, '_parcel_machine_manifest', sanitize_text_field( $manifest ) );
477 }
478
479 if ( !empty( $order->orderId ) && !empty( $order->shipmentId ) ) {
480
481 update_post_meta( ( int )$order->orderId, '_parcel_machine_shipment_id', sanitize_text_field( $order->shipmentId ) );
482 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
483 delete_post_meta( ( int )$order->orderId, '_parcel_machine_error' );
484 } else if ( !empty( $order->orderId ) && !empty( $order->barCode ) ) {
485
486 update_post_meta( ( int )$order->orderId, '_parcel_machine_shipment_id', sanitize_text_field( $order->barCode ) );
487 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
488 delete_post_meta( ( int )$order->orderId, '_parcel_machine_error' );
489 } else if ( !empty( $order->orderId ) && !empty( $order->errorMessage ) ) {
490
491 update_post_meta( ( int )$order->orderId, '_parcel_machine_error', sanitize_text_field( $order->errorMessage ) );
492 }
493 }
494 }
495
496 /**
497 * Returns the correct link to be used for tracking link
498 *
499 * @since 3.0.0
500 */
501 public function get_tracking_link( $carrier, $order_id, $shopLocation = false ) {
502
503 //do we use shop location or delivery location
504 if ( $shopLocation ) {
505 //get_base_country returns either EE, LT or LV. Everything else is irrelevant
506 $tld = substr( strtolower( WC()->countries->get_base_country() ), 0, 2);
507
508 $userLang = $tld;
509 } else {
510
511 //get order delivery location. Returns EE, LT or LV. Evertyhing else is irrelevant
512 $tld = substr( strtolower( get_post_meta( $order_id, '_shipping_country', true ) ), 0, 2 );
513 $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
514 }
515
516 //
517 // create array for different options [carrier][tld][lang]
518 // first one in the list must be default, this goes for the tld as well as for the userLang.
519 //
520
521 /* Omniva */
522 //EST
523 $trackingURLS["omniva"]["ee"]["ee"] = 'https://www.omniva.ee/abi/jalgimine?barcode=';
524 $trackingURLS["omniva"]["ee"]["en"] = 'https://www.omniva.ee/private/track_and_trace?barcode=';
525 $trackingURLS["omniva"]["ee"]["ru"] = 'https://www.omniva.ee/chastnyj/otslezhivanie_posylki?barcode=';
526
527 //LT
528 $trackingURLS["omniva"]["lt"]["lt"] = 'https://www.omniva.lt/verslo/siuntos_sekimas?barcode=';
529 $trackingURLS["omniva"]["lt"]["en"] = 'https://www.omniva.lt/business/track_and_trace?barcode=';
530
531 //LV
532 $trackingURLS["omniva"]["lv"]["lv"] = 'https://www.omniva.lv/privats/sutijuma_atrasanas_vieta?barcode=';
533 $trackingURLS["omniva"]["lv"]["en"] = 'https://www.omniva.lv/private/track_and_trace?barcode=';
534 $trackingURLS["omniva"]["lv"]["ru"] = 'https://www.omniva.lv/chastnyj/mestonahozhdenie_posylki?barcode=';
535
536 /* DPD */
537 //EE
538 $trackingURLS["dpd"]["ee"]["ee"] = 'https://tracking.dpd.de/status/et_EE/parcel/';
539 $trackingURLS["dpd"]["ee"]["en"] = 'https://tracking.dpd.de/status/en_EE/parcel/';
540
541 //LT
542 $trackingURLS["dpd"]["lt"]["lt"] = 'https://tracking.dpd.de/status/lt_LT/parcel/';
543 $trackingURLS["dpd"]["lt"]["en"] = 'https://tracking.dpd.de/status/en_LT/parcel/';
544
545 //LV
546 $trackingURLS["dpd"]["lv"]["lv"] = 'https://tracking.dpd.de/status/lv_LV/parcel/';
547 $trackingURLS["dpd"]["lv"]["en"] = 'https://tracking.dpd.de/status/en_LV/parcel/';
548
549 /* Smartpost */
550 //EE
551 $trackingURLS["smartpost"]["ee"]["ee"] = 'https://itella.ee/eraklient/saadetise-jalgimine/?trackingCode=';
552 $trackingURLS["smartpost"]["ee"]["en"] = 'https://itella.ee/en/private-customer/parcel-tracking/?trackingCode=';
553 $trackingURLS["smartpost"]["ee"]["ru"] = 'https://itella.ee/ru/chastnyj-klijent/otslezhivanije-otpravlenija/?trackingCode=';
554
555 //FI
556 $trackingURLS["smartpost"]["fi"]["en"] = 'https://www.posti.fi/en/tracking#/lahetys/';
557 $trackingURLS["smartpost"]["fi"]["fi"] = 'https://www.posti.fi/fi/seuranta#/lahetys/';
558 $trackingURLS["smartpost"]["fi"]["sv"] = 'https://www.posti.fi/sv/uppfoljning#/lahetys/';
559
560 //LT
561 $trackingURLS["smartpost"]["lt"]["lt"] = 'https://itella.lt/private-customer/krovinio-sekimas/?trackingCode=';
562 $trackingURLS["smartpost"]["lt"]["en"] = 'https://itella.lt/en/private-customer/track-shipment/?trackingCode=';
563
564 //LV
565 $trackingURLS["smartpost"]["lv"]["lv"] = 'https://itella.lv/private-customer/sutijuma-meklesana/?trackingCode=';
566 $trackingURLS["smartpost"]["lv"]["en"] = 'https://itella.lv/en/private-customer/parcel-tracking/?trackingCode=';
567
568
569 //carrier exists
570 if ( isset( $trackingURLS[$carrier] ) ) {
571 //country exists
572 if ( isset( $trackingURLS[$carrier][$tld] ) ) {
573 if ( isset( $trackingURLS[$carrier][$tld][$userLang] ) ) {
574 return $trackingURLS[$carrier][$tld][$userLang];
575 } else {
576 //selected language not found, return carrier tld default language.
577 return current( $trackingURLS[$carrier][$tld] );
578 }
579 } else {
580 //selected tld not found, return carrier default tld + language
581 $defaultCarrierTLD = current( $trackingURLS[$carrier] );
582
583 return current( $defaultCarrierTLD );
584 }
585 }
586
587 //not even carrier was found. Return nothing
588 return "";
589 }
590
591 /**
592 * Returns all available parcelmachines
593 *
594 * @since 3.0.0
595 */
596 public static function mk_get_machines( $carrier, $country = null ) {
597
598 //set machine cache
599 $data = get_option( 'mk_machines_cache', false );
600 $data_expires = get_option( 'mk_machines_expires', false );
601
602 if ( !$data || empty( $data ) || $data_expires < time() ) {
603
604 $MK = \MakeCommerce::get_api();
605
606 if( !$MK ) {
607 return array();
608 }
609
610 $data = $MK->getDestinations( array( 'type' => 'APT,PUP' ) );
611
612 update_option( 'mk_machines_cache', $data, 'no' );
613 update_option( 'mk_machines_expires', time() + 3 * 60 * 60, 'no' );
614 }
615
616 //return empty if no machines found for destination
617 if ( !$data || empty( $data ) ) {
618
619 return array();
620 }
621
622 //create machines array
623 $machines = array();
624 foreach ( $data as $machine ) {
625
626 if ( ( !$country || $machine->country === $country ) &&
627 ( $machine->type === 'APT' || $machine->type === 'PUP' ) &&
628 ( $carrier === '*' || strtolower( $carrier ) === strtolower( $machine->carrier ) ) ) {
629
630 $machines[] = array(
631 'carrier' => strtolower( $machine->carrier ),
632 'id' => $machine->id,
633 'name' => $machine->name,
634 'city' => $machine->city,
635 'address' => !empty( $machine->address ) ? $machine->address : '',
636 );
637 }
638 }
639
640 //sort machines
641 usort( $machines, function( $a, $b ) {
642 if ( $a['city'] === $b['city'] ) {
643
644 return $a['name'] > $b['name'];
645 }
646
647 return $a['city'] > $b['city'];
648 });
649
650 return $machines;
651 }
652
653 /**
654 * Returns a specific parecelmachine
655 *
656 * @since 3.0.0
657 */
658 public static function mk_get_machine( $carrier, $id ) {
659
660 //get all available machines
661 $machines = self::mk_get_machines( $carrier );
662
663 //loop and return the machine we are searching for if found
664 foreach ( $machines as $machine ) {
665
666 if ( $machine['carrier'] === $carrier && $machine['id'] == $id ) {
667
668 return $machine;
669 }
670 }
671
672 return false;
673 }
674
675 /**
676 * Register the stylesheets.
677 *
678 * @since 3.0.0
679 */
680 public function enqueue_styles() {
681
682 if ( is_admin() ) {
683 wp_enqueue_style( $this->plugin_name . "-parcelmachine-admin", plugin_dir_url(__FILE__) . 'css/parcelmachine-admin.css', array(), $this->version );
684 }
685 }
686
687 /**
688 * Register the JavaScript.
689 *
690 * @since 3.0.0
691 */
692 public function enqueue_scripts() {
693
694 if ( is_admin() ) {
695 wp_enqueue_script( $this->plugin_name . "parcelmachine-admin", plugin_dir_url(__FILE__) . 'js/parcelmachine-admin.js', array( 'jquery' ), $this->version, false );
696 } else {
697 wp_enqueue_script( $this->plugin_name . "parcelmachine", plugin_dir_url(__FILE__) . 'js/parcelmachine.js', array( 'jquery' ), $this->version, false );
698 }
699 }
700 }