-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathShoppingFeedHelper.php
More file actions
1009 lines (867 loc) · 25.5 KB
/
ShoppingFeedHelper.php
File metadata and controls
1009 lines (867 loc) · 25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace ShoppingFeed\ShoppingFeedWC;
// Exit on direct access
defined( 'ABSPATH' ) || exit;
use ShoppingFeed\ShoppingFeedWC\Admin\Options;
use ShoppingFeed\ShoppingFeedWC\Feed\FeedBuilderManager;
use ShoppingFeed\ShoppingFeedWC\ShipmentTracking\ShipmentTrackingManager;
use ShoppingFeed\ShoppingFeedWC\Url\Rewrite;
use WC_Logger;
/**
* Define All needed methods
* Helper class.
*
* @package ShoppingFeed
*/
class ShoppingFeedHelper {
/**
* @var ShoppingFeedHelper
*/
private static $instance;
/**
* Get the singleton instance.
*
* @return ShoppingFeedHelper
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Check if current WooCommerce version is below 3.8.0
*
* @return bool
*/
public static function is_pre_38() {
return version_compare( self::get_wc_version(), '3.8.0', '<' );
}
/**
* Return the WC version
* @return string
*/
public static function get_wc_version() {
return WC()->version;
}
/**
* Return the feed's directory.
*
* @return string
*/
public static function get_feed_directory() {
/**
* Filter the path to the directory where product feeds are stored.
*
* @param string $path Path to the directory.
*/
return (string) apply_filters( 'shopping_feed_feed_directory_path', SF_FEED_DIR );
}
/**
* Return the feed's parts directory.
*
* @return string
*/
public static function get_feed_parts_directory() {
/**
* Filter the path to the directory where product feeds parts are stored.
*
* @param string $path Path to the directory.
*/
return (string) apply_filters( 'shopping_feed_feed_parts_directory_path', SF_FEED_PARTS_DIR );
}
/**
* Return the feed's file name.
*
* The filename doesn't contain the file extension.
*
* @return string
*/
public static function get_feed_filename() {
/**
* Filter the product feed's filename.
*
* The filename must not contain the file extension.
*
* @param string $path Feed's filename.
*/
return (string) apply_filters( 'shopping_feed_feed_filename', 'products' );
}
/**
* Return the feed's public endpoint
* @return string
*/
public static function get_public_feed_endpoint() {
global $wp_rewrite;
return $wp_rewrite->root . Rewrite::FEED_PARAM;
}
/**
* Return SF Configuration for Account
* @return array
*/
public static function get_sf_account_options() {
return get_option( Options::SF_ACCOUNT_OPTIONS, array() );
}
/**
* Return SF Configuration for Yoast
* @return array
*/
public static function get_sf_yoast_options() {
$yoast_options = get_option( Options::SF_YOAST_OPTIONS, [] );
return wp_parse_args(
$yoast_options,
[
'use_principal_categories' => '1',
]
);
}
/**
* Set SF Configuration for Account
* @return bool
*/
public static function set_sf_account_options( $option ) {
return update_option( Options::SF_ACCOUNT_OPTIONS, $option );
}
/**
* Get SF account by store id.
*
* @param int|string $account_id
*
* @return array
*/
public static function get_sf_account_credentials( $account_id ) {
$account_options = self::get_sf_account_options();
$index = array_search( (int) $account_id, array_column( $account_options, 'sf_store_id' ), true );
if ( false === $index || empty( $account_options[ $index ] ) ) {
return array();
}
return $account_options[ $index ];
}
/**
* Get SF account by username.
*
* @param string $username
*
* @return array
*/
public static function get_sf_account_credentials_by_username( $username ) {
$account_options = self::get_sf_account_options();
$index = array_search( $username, array_column( $account_options, 'username' ), true );
if ( false === $index || empty( $account_options[ $index ] ) ) {
return array();
}
return $account_options[ $index ];
}
/**
* Return SF categories to export
* @return array
*/
public static function get_sf_feed_export_categories( string $language = '' ) {
$param = 'categories';
if ( ! empty( $language ) ) {
$param .= '-' . $language;
}
$categories = self::get_sf_feed_options( $param );
if ( empty( $categories ) ) {
return array();
}
return $categories;
}
/**
* Return SF Configuration for Feed Generation
*
* @param string $param
*
* @return mixed|void
*/
public static function get_sf_feed_options( $param = '' ) {
$feed_options = get_option( Options::SF_FEED_OPTIONS );
if ( empty( $param ) ) {
return $feed_options;
}
if ( ! isset( $feed_options[ $param ] ) ) {
return false;
}
return $feed_options[ $param ];
}
/**
* Return identifier for product : id|sku
* @return string
*/
public static function get_sf_feed_product_identifier() {
$product_identifier = self::get_sf_feed_options( 'product_identifier' );
if ( empty( $product_identifier ) ) {
return 'id';
}
return $product_identifier;
}
/**
* Should out of stock products be included in the feed ?
*
* @return bool true if the products should be in the feed, false otherwise.
*/
public static function show_out_of_stock_products_in_feed() {
return 'on' === self::get_sf_feed_options( 'out_of_stock_products_in_feed' );
}
/**
* Return display mode for category
* @return string
*/
public static function get_sf_feed_category_display_mode() {
$display_mode = self::get_sf_feed_options( 'category_display_mode' );
if ( empty( $display_mode ) ) {
return 'normal';
}
return $display_mode;
}
/**
* Return SF default shipping zone id if exist
* @return bool|int
*/
public static function get_sf_default_shipping_zone() {
// Ensure retro compatibility
$shipping_configuration = self::get_sf_feed_options();
if ( is_array( $shipping_configuration ) && isset( $shipping_configuration['zone'] ) && ! empty( $shipping_configuration['zone'] ) ) {
return (int) $shipping_configuration['zone'];
}
$shipping_configuration = self::get_sf_shipping_options();
if ( ! is_array( $shipping_configuration ) || ! isset( $shipping_configuration['zone'] ) ) {
return false;
}
//if none option is selected
if ( empty( $shipping_configuration['zone'] ) ) {
return false;
}
return (int) $shipping_configuration['zone'];
}
/**
* Return SF Configuration for Shipping
*
* @param string $param
*
* @return mixed|void
*/
public static function get_sf_shipping_options( $param = '' ) {
$shipping_options = get_option( Options::SF_SHIPPING_OPTIONS );
if ( empty( $param ) ) {
return $shipping_options;
}
if ( ! isset( $shipping_options[ $param ] ) ) {
return false;
}
return $shipping_options[ $param ];
}
/**
* Return SF default shipping fees if exist
* @return float
*/
public static function get_sf_default_shipping_fees() {
$shipping_configuration = self::get_sf_feed_options();
if ( is_array( $shipping_configuration ) && ! empty( $shipping_configuration['fees'] ) && is_numeric( $shipping_configuration['fees'] ) ) {
return (float) $shipping_configuration['fees'];
}
$shipping_configuration = self::get_sf_feed_options( 'shipping' );
if ( ! is_array( $shipping_configuration ) || empty( $shipping_configuration['fees'] ) || ! is_numeric( $shipping_configuration['fees'] ) ) {
return 0;
}
return (float) $shipping_configuration['fees'];
}
/**
* Return SF feed generation frequency
* default: 6 hours
* @return int
*/
public static function get_sf_feed_generation_frequency() {
$frequency = self::get_sf_feed_options( 'frequency' );
if ( empty( $frequency ) ) {
return 6 * HOUR_IN_SECONDS;
}
return $frequency;
}
/**
* Return SF feed part size
* default: 200 product per file
* @return int
*/
public static function get_sf_part_size() {
$part_size = self::get_sf_feed_options( 'part_size' );
if ( empty( $part_size ) ) {
return 200;
}
return (int) $part_size;
}
/**
* Return action of each wc status
* @return mixed|void
*/
public static function get_sf_statuses_actions() {
$orders_options = self::get_sf_orders_options();
if ( ! is_array( $orders_options ) ) {
return array();
}
if ( empty( $orders_options['statuses_actions'] ) ) {
return array();
}
return $orders_options['statuses_actions'];
}
/**
* Return SF orders configuration
* @return mixed|void
*/
public static function get_sf_orders_options() {
return get_option( Options::SF_ORDERS_OPTIONS );
}
/**
* Return SF default order status
* @return string
*/
public static function get_sf_default_order_status() {
$orders_options = self::get_sf_orders_options();
if ( ! is_array( $orders_options ) ) {
return 'wc-on-hold';
}
if ( empty( $orders_options['default_status'] ) ) {
return 'wc-on-hold';
}
return $orders_options['default_status'];
}
/**
* Return SF fulfilled by marketplace order status.
* @return string
*/
public static function get_sf_fulfilled_by_channel_order_status() {
$orders_options = self::get_sf_orders_options();
if ( ! is_array( $orders_options ) ) {
return 'wc-completed';
}
return ! empty( $orders_options['fulfilled_by_marketplace_order_status'] )
? $orders_options['fulfilled_by_marketplace_order_status']
: 'wc-completed';
}
/**
* Return SF orders import
* default: 15 MINUTES
* @return int
*/
public static function get_sf_orders_import_frequency() {
$orders_options = self::get_sf_orders_options();
if ( ! is_array( $orders_options ) ) {
return 15 * MINUTE_IN_SECONDS;
}
if ( empty( $orders_options['import_frequency'] ) ) {
return 15 * MINUTE_IN_SECONDS;
}
return $orders_options['import_frequency'];
}
/**
* Return zones with related shipping methods
* @return array
*/
public static function get_zones_with_shipping_methods() {
$shipping_methods = array();
$shipping_zones = \WC_Shipping_Zones::get_zones();
if ( empty( $shipping_zones ) ) {
return $shipping_methods;
}
$all_shipping_methods = array();
foreach ( $shipping_zones as $shipping_zone ) {
$shipping_zone = new \WC_Shipping_Zone( $shipping_zone['id'] );
$shipping_methods = $shipping_zone->get_shipping_methods();
if ( empty( $shipping_methods ) ) {
continue;
}
$_shipping_methods = array();
foreach ( $shipping_methods as $shipping_method ) {
$_shipping_methods[] = array(
'method_rate_id' => $shipping_method->id,
'method_id' => $shipping_method->instance_id,
'method_title' => $shipping_method->title,
);
}
$all_shipping_methods[] = array(
'zone_id' => $shipping_zone->get_id(),
'zone_name' => $shipping_zone->get_zone_name(),
'methods' => $_shipping_methods,
);
}
$all_shipping_methods = apply_filters( 'sf_all_shipping_methods', $all_shipping_methods );
return $all_shipping_methods;
}
/**
* Get WC Shipping from SF carrier
*
* @param string $sf_carrier
*
* @return array
*/
public static function get_wc_shipping_from_sf_carrier( $sf_carrier ) {
$sf_carrier = trim( $sf_carrier );
$sf_carrier_id = self::get_sf_carrier_id( $sf_carrier );
if ( empty( $sf_carrier_id ) && ! empty( $sf_carrier ) ) {
$sf_carrier_id = self::add_sf_carrier( $sf_carrier );
}
$matching_shipping_method_list = self::get_matching_shipping_method_list();
if ( empty( $matching_shipping_method_list[ $sf_carrier_id ] ) ) {
if ( is_array( self::get_default_shipping_method() ) ) {
self::add_matching_shipping_method( $sf_carrier_id, self::get_default_shipping_method() );
return self::get_default_shipping_method();
}
return array();
}
return json_decode( $matching_shipping_method_list[ $sf_carrier_id ], true );
}
/**
* Get SF carrier id
*
* @param string $name
*
* @return int
*/
public static function get_sf_carrier_id( $name ) {
$sf_carriers = self::get_sf_carriers();
if ( empty( $sf_carriers ) ) {
return 0;
}
$sf_carrier_id = array_search( $name, $sf_carriers, true );
if ( ! is_int( $sf_carrier_id ) ) {
return 0;
}
return $sf_carrier_id;
}
/**
* Return sf carriers
* @return array
*/
public static function get_sf_carriers() {
$sf_carriers = get_option( Options::SF_CARRIERS );
if ( empty( $sf_carriers ) || ! is_array( $sf_carriers ) ) {
return array();
}
return $sf_carriers;
}
/**
* Add SF carrier
*
* @param string $sf_carrier
*
* @return int
*/
public static function add_sf_carrier( $sf_carrier ) {
$sf_carriers = self::get_sf_carriers();
$index = count( $sf_carriers );
++ $index;
$sf_carriers[ (int) $index ] = $sf_carrier;
update_option( Options::SF_CARRIERS, $sf_carriers );
return $index;
}
/**
* Return matching shipping list
* @return array
*/
public static function get_matching_shipping_method_list() {
$orders_options = self::get_sf_shipping_options();
if ( empty( $orders_options ) || empty( $orders_options['matching_shipping_method'] ) ) {
return array();
}
return (array) $orders_options['matching_shipping_method'];
}
/**
* Return Default shipping method
* @return array
*/
public static function get_default_shipping_method() {
$shipping_options = self::get_sf_shipping_options();
if ( empty( $shipping_options ) || empty( $shipping_options['default_shipping_method'] ) ) {
return array();
}
return (array) json_decode( $shipping_options['default_shipping_method'], true );
}
/**
* Add new matching for sf carrier
*
* @param $carrier_id
* @param $method
*
* @return void
*/
public static function add_matching_shipping_method( $carrier_id, $method ) {
if ( empty( $method ) || ! is_int( $carrier_id ) ) {
return;
}
$sf_shipping_option = self::get_sf_shipping_options();
if ( empty( $sf_shipping_option ) ) {
return;
}
$method['sf_shipping'] = $carrier_id;
$sf_shipping_option['matching_shipping_method'][ $carrier_id ] = wp_json_encode( $method );
update_option( Options::SF_SHIPPING_OPTIONS, $sf_shipping_option );
}
/**
* Get SF Carrier from WC Shipping
*
* @param $wc_order \WC_Order
*
* @return string
*/
public static function get_sf_carrier_from_wc_shipping( $wc_order ) {
/**
* Filter the value of the carrier for the order before it is retrieve.
*
* @param bool|string $pre The value to return instead of the value computed from
* the `sf_shipping` metadata.
* @param \WC_Order $wc_order The order object for the carrier data.
*/
$pre = apply_filters( 'pre_sf_carrier_from_wc_shipping', false, $wc_order );
if ( false !== $pre ) {
return $pre;
}
$sf_shipping = json_decode( $wc_order->get_meta( 'sf_shipping' ), true );
if ( empty( $sf_shipping['sf_shipping'] ) ) {
return $wc_order->get_shipping_method();
}
$sf_carrier_id = $sf_shipping['sf_shipping'];
$default_shipping_method = self::get_default_shipping_method();
$default_shipping_method = ! is_array( $default_shipping_method ) || empty( $default_shipping_method ) ? '' : $default_shipping_method['method_title'];
$matching_shipping_method_list = self::get_matching_shipping_method_list();
if ( empty( $matching_shipping_method_list[ $sf_carrier_id ] ) ) {
return $default_shipping_method;
}
$matching_shipping_method = json_decode( $matching_shipping_method_list[ $sf_carrier_id ], true );
if ( ! is_array( $matching_shipping_method ) && empty( $matching_shipping_method ) ) {
return $default_shipping_method;
}
return (string) $matching_shipping_method['method_title'];
}
/**
* Add filter for category taxonomy
* default: product_cat
* @return string
*/
public static function wc_category_taxonomy() {
return apply_filters( 'shopping_feed_custom_category_taxonomy', 'product_cat' );
}
/**
* Add filter for brand taxonomy
* @return string
*/
public static function wc_brand_taxonomy() {
return apply_filters( 'shopping_feed_custom_brand_taxonomy', '' );
}
/**
* Add filter for product ean field meta key
*
* @param \WC_Product|false $wc_product
*
* @return string
*/
public static function wc_product_ean( $wc_product = false ) {
return apply_filters( 'shopping_feed_custom_ean', '', $wc_product );
}
/**
* Add filter for products list query
*
* @param string $lang The current language associated with the feed. Language slug or empty string if
* site isn't multilingual.
*
* @return array
*/
public static function wc_products_custom_query_args( $lang = '' ) {
/**
* Filter args used to retrieve products for the feed.
*
* @param array $args Custom query args for the `WC_Product_Query`.
* @param string $lang The current language associated with the feed. Language slug or empty string if
* site isn't multilingual.
*/
return apply_filters( 'shopping_feed_products_custom_args', array(), $lang );
}
/**
* Add filter for orders statuses to import
* @return array
*/
public static function sf_order_statuses_to_import() {
$default_statuses = [ 'waiting_shipment' ];
$orders_options = self::get_sf_orders_options();
/**
* Add shipped status if importing fulfilled by marketplace orders
* @see https://support.beapi.fr/issues/60658
*/
if ( isset( $orders_options['import_order_fulfilled_by_marketplace'] ) && true === (bool) $orders_options['import_order_fulfilled_by_marketplace'] ) {
$fullfilled_by_marketplace_statuses = [ 'shipped', 'refunded', 'cancelled' ];
$default_statuses = array_merge( $default_statuses, $fullfilled_by_marketplace_statuses );
}
return apply_filters( 'shopping_feed_orders_to_import', $default_statuses );
}
/**
* Get tracking number for the order.
*
* If the order has multiple tracking numbers, they'll be separated by coma.
*
* @param \WC_Order $wc_order
*
* @return string
*/
public static function wc_tracking_number( $wc_order ) {
$manager = self::wc_tracking_provider_manager();
$tracking_number = '';
$tracking_data = $manager->get_selected_provider()->get_tracking_data( $wc_order );
if ( $tracking_data->has_tracking_data() ) {
$tracking_number = implode( ',', $tracking_data->get_tracking_numbers() );
}
/**
* Filter order's tracking number.
*
* @param string $tracking_number
* @param \WC_Order $wc_order
*/
$filtered_tracking_number = apply_filters( 'shopping_feed_tracking_number', $tracking_number, $wc_order );
/*
* Back-compat: ignore filtered value if it comes from ShoppingFeed Advanced.
*
* Previously, the ShoppingFeed Advanced addon used the filter `shopping_feed_tracking_number` to specify
* the meta key for retrieving tracking numbers. This functionality is now handled by the shipment tracking manager.
*/
if ( defined( 'TRACKING_NUMBER_FIELD_SLUG' ) && TRACKING_NUMBER_FIELD_SLUG === $filtered_tracking_number ) {
$filtered_tracking_number = $tracking_number;
}
// Back-compat: handle case where the filter return a meta key.
if ( $wc_order->meta_exists( $filtered_tracking_number ) ) {
$filtered_tracking_number = (string) $wc_order->get_meta( $filtered_tracking_number );
}
return $filtered_tracking_number;
}
/**
* Get tracking link for the order.
*
* If the order has multiple tracking links, they'll be separated by coma.
*
* @param \WC_Order $wc_order
*
* @return string
*/
public static function wc_tracking_link( $wc_order ) {
$manager = self::wc_tracking_provider_manager();
$tracking_link = '';
$tracking_data = $manager->get_selected_provider()->get_tracking_data( $wc_order );
if ( $tracking_data->has_tracking_data() ) {
$tracking_link = implode( ',', $tracking_data->get_tracking_links() );
}
/**
* Filter order's tracking link.
*
* @param string $tracking_link
* @param \WC_Order $wc_order
*/
$filtered_tracking_link = apply_filters( 'shopping_feed_tracking_link', $tracking_link, $wc_order );
/*
* Back-compat: ignore filtered value if it comes from ShoppingFeed Advanced.
*
* Previously, the ShoppingFeed Advanced addon used the filter `shopping_feed_tracking_number` to specify
* the meta key for retrieving tracking links. This functionality is now handled by the shipment tracking manager.
*/
if ( defined( 'TRACKING_LINK_FIELD_SLUG' ) && TRACKING_LINK_FIELD_SLUG === $filtered_tracking_link ) {
$filtered_tracking_link = $tracking_link;
}
// Back-compat: handle case where the filter return a meta key.
if ( $wc_order->meta_exists( $filtered_tracking_link ) ) {
$filtered_tracking_link = (string) $wc_order->get_meta( $filtered_tracking_link );
}
return $filtered_tracking_link;
}
public static function wc_tracking_provider_manager(): ShipmentTrackingManager {
static $manager;
if ( ! $manager ) {
$shipping_options = self::get_sf_shipping_options();
if ( ! is_array( $shipping_options ) ) {
$shipping_options = [];
}
$manager = ShipmentTrackingManager::create( $shipping_options );
}
return $manager;
}
/**
* Default quantity if product quantity is unset
* @return int
*/
public static function get_default_product_quantity() {
return 100;
}
/**
* Check if a running generation process
*
* @param $group
*
* @return bool
*/
public static function is_process_running( $group ) {
$process = self::get_running_process( $group );
return ! empty( $process );
}
/**
* Get running process list
*
* @param string $group
*
* @return array|int
*/
public static function get_running_process( $group ) {
$action_scheduler = \ActionScheduler::store();
return $action_scheduler->query_actions(
array(
'group' => $group,
'status' => [ \ActionScheduler_Store::STATUS_PENDING, \ActionScheduler_Store::STATUS_RUNNING ],
)
);
}
/**
* @param string $group
*/
public static function clean_process_running( $group ) {
try {
\ActionScheduler::store()->cancel_actions_by_group( $group );
} catch ( \Exception $exception ) {
self::get_logger()->error(
sprintf(
__( 'Cant remove running process', 'shopping-feed' ),
$exception->getMessage()
),
array(
'source' => 'shopping-feed',
)
);
}
wp_safe_redirect( self::get_setting_link(), 302 );
}
/**
* Return WC Logger
* @return WC_Logger
*/
public static function get_logger() {
return wc_get_logger();
}
/**
* Log a message.
*
* @param string $level a valid {@see \WC_Log_Levels `WC_Log_Levels`} level to use for the entry.
* @param string $message the message to use for the entry.
* @param string $channel the channel where to add the entry.
* @param array $context additional data to include in the entry.
*
* @return void
*/
public static function log( string $level, string $message, string $channel = 'shopping-feed', array $context = [] ) {
$logger = self::get_logger();
if ( ! $logger ) {
return;
}
// Set channel in context if missing
if ( ! isset( $context['source'] ) ) {
$context['source'] = $channel;
}
$logger->log(
$level,
$message,
$context
);
}
/**
* Return the settings link for plugin
* @return string
*/
public static function get_setting_link() {
return admin_url( 'admin.php?page=shopping-feed' );
}
/**
* Check if the Site is compatible with addons
* @return bool
*/
public static function tracking_is_compatible_with_addons() {
$shipping_options = self::get_sf_shipping_options();
if ( empty( $shipping_options ) ) {
return false;
}
return ! empty( $shipping_options['retrieval_mode'] ) && 'ADDONS' === $shipping_options['retrieval_mode'];
}
/**
* Check if new customer
* @return bool
*/
public static function sf_new_customer() {
return empty( get_option( Options::SF_ACCOUNT_OPTIONS ) ) &&
empty( get_option( Options::SF_FEED_OPTIONS ) ) &&
empty( get_option( Options::SF_SHIPPING_OPTIONS ) ) &&
empty( get_option( Options::SF_ORDERS_OPTIONS ) );
}
/**
* Singleton instance can't be cloned.
*/
private function __clone() {
}
/**
* Singleton instance can't be serialized.
* @throws \Exception
*/
public function __wakeup() {
throw new \Exception( 'Cannot serialize singleton' );
}
/**
* Check if we have an upgrade
* @return bool
*/
public static function sf_has_upgrade() {
$db_version = get_option( SF_DB_VERSION_SLUG );
return empty( $db_version ) || version_compare( $db_version, SF_DB_VERSION, '<' );
}
/**
* Check if we have a running upgrade
* @return bool
*/
public static function is_upgrade_running() {
return ! empty( get_option( SF_UPGRADE_RUNNING ) );
}
/*
* End upgrade
*/
public static function end_upgrade() {
delete_option( SF_UPGRADE_RUNNING );
update_option( SF_DB_VERSION_SLUG, SF_DB_VERSION );
}
/**
* Get FeedBuilderManager instance.
*
* @return FeedBuilderManager
*/
public static function get_feedbuilder_manager(): FeedBuilderManager {
static $manager;
if ( ! $manager ) {
$manager = new FeedBuilderManager();
$manager->register();
}
return $manager;
}
public static function support_multilingual_feed(): bool {
$feed_manager = self::get_feedbuilder_manager();
return $feed_manager->get_builder()->support_multilingual();
}
public static function get_available_languages_for_feed(): array {
$feed_manager = self::get_feedbuilder_manager();
return $feed_manager->get_builder()->get_languages();
}
public static function current_language() {
$feed_manager = self::get_feedbuilder_manager();
return $feed_manager->get_builder()->current_languages();
}
/**
* Check if order import is disable.
*
* @return bool
*/
public static function is_order_import_disable() {
$orders_options = self::get_sf_orders_options();