-
Notifications
You must be signed in to change notification settings - Fork 20
/
woocommerce-services.php
2035 lines (1714 loc) · 74.8 KB
/
woocommerce-services.php
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
/**
* Plugin Name: WooCommerce Shipping & Tax
* Requires Plugins: woocommerce
* Plugin URI: https://woocommerce.com/
* Description: Hosted services for WooCommerce: automated tax calculation, shipping label printing, and smoother payment setup.
* Author: WooCommerce
* Author URI: https://woocommerce.com/
* Text Domain: woocommerce-services
* Domain Path: /i18n/languages/
* Version: 2.8.6
* Requires at least: 6.5
* Tested up to: 6.7
* WC requires at least: 9.2
* WC tested up to: 9.4
*
* Copyright (c) 2017-2023 Automattic
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* WooCommerce Shipping & Tax incorporates code from WooCommerce Sales Tax Plugin by TaxJar, Copyright 2014-2017 TaxJar.
* WooCommerce Sales Tax Plugin by TaxJar is distributed under the terms of the GNU GPL, Version 2 (or later).
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
require_once __DIR__ . '/vendor/autoload_packages.php';
require_once __DIR__ . '/classes/class-wc-connect-extension-compatibility.php';
require_once __DIR__ . '/classes/class-wc-connect-functions.php';
require_once __DIR__ . '/classes/class-wc-connect-jetpack.php';
require_once __DIR__ . '/classes/class-wc-connect-options.php';
if ( ! class_exists( 'WC_Connect_Loader' ) ) {
define( 'WOOCOMMERCE_CONNECT_MINIMUM_WOOCOMMERCE_VERSION', '2.6' );
define( 'WOOCOMMERCE_CONNECT_MAX_JSON_DECODE_DEPTH', 32 );
if ( ! defined( 'WOOCOMMERCE_CONNECT_SERVER_API_VERSION' ) ) {
define( 'WOOCOMMERCE_CONNECT_SERVER_API_VERSION', '5' );
}
class WC_Connect_Loader {
/**
* @var WC_Connect_Logger
*/
protected $logger;
/**
* @var WC_Connect_Logger
*/
protected $shipping_logger;
/**
* @var WC_Connect_API_Client
*/
protected $api_client;
/**
* @var WC_Connect_Service_Schemas_Store
*/
protected $service_schemas_store;
/**
* @var WC_Connect_Service_Settings_Store
*/
protected $service_settings_store;
/**
* @var WC_Connect_Payment_Methods_Store
*/
protected $payment_methods_store;
/**
* @var WC_REST_Connect_Account_Settings_Controller
*/
protected $rest_account_settings_controller;
/**
* @var WC_REST_Connect_Packages_Controller
*/
protected $rest_packages_controller;
/**
* @var WC_REST_Connect_Services_Controller
*/
protected $rest_services_controller;
/**
* @var WC_REST_Connect_Self_Help_Controller
*/
protected $rest_self_help_controller;
/**
* @var WC_REST_Connect_Shipping_Label_Controller
*/
protected $rest_shipping_label_controller;
/**
* @var WC_REST_Connect_Shipping_Label_Status_Controller
*/
protected $rest_shipping_label_status_controller;
/**
* @var WC_REST_Connect_Shipping_Label_Refund_Controller
*/
protected $rest_shipping_label_refund_controller;
/**
* @var WC_REST_Connect_Shipping_Label_Preview_Controller
*/
protected $rest_shipping_label_preview_controller;
/**
* @var WC_REST_Connect_Shipping_Label_Print_Controller
*/
protected $rest_shipping_label_print_controller;
/**
* @var WC_REST_Connect_Shipping_Rates_Controller
*/
protected $rest_shipping_rates_controller;
/**
* @var WC_REST_Connect_Address_Normalization_Controller
*/
protected $rest_address_normalization_controller;
/**
*
* WC_REST_Connect_Shipping_Carrier_Types_Controller
*
* @var WC_REST_Connect_Shipping_Carrier_Types_Controller
*/
protected $rest_carrier_types_controller;
/**
* WC_REST_Connect_Assets_Controller
*
* @var WC_REST_Connect_Assets_Controller
*/
protected $rest_assets_controller;
/**
* WC_REST_Connect_Shipping_Carrier_Controller
*
* @var WC_REST_Connect_Shipping_Carrier_Controller
*/
protected $rest_carrier_controller;
/**
* WC_REST_Connect_Shipping_Carriers_Controller
*
* @var WC_REST_Connect_Shipping_Carriers_Controller
*/
protected $rest_carriers_controller;
/**
* WC_REST_Connect_Subscriptions_Controller
*
* @var WC_REST_Connect_Subscriptions_Controller
*/
protected $rest_subscriptions_controller;
/**
* WC_REST_Connect_Subscription_Activate_Controller
*
* @var WC_REST_Connect_Subscription_Activate_Controller
*/
protected $rest_subscription_activate_controller;
/**
* WC_REST_Connect_Shipping_Carrier_Delete_Controller
*
* @var WC_REST_Connect_Shipping_Carrier_Delete_Controller
*/
protected $rest_carrier_delete_controller;
/**
* WC_REST_Connect_Migration_Flag_Controller
*
* @var WC_REST_Connect_Migration_Flag_Controller
*/
protected $rest_migration_flag_controller;
/**
* @var WC_Connect_Service_Schemas_Validator
*/
protected $service_schemas_validator;
/**
* @var WC_Connect_Settings_Pages
*/
protected $settings_pages;
/**
* @var WC_Connect_Help_View
*/
protected $help_view;
/**
* @var WC_Connect_Shipping_Label
*/
protected $shipping_label;
/**
* @var WC_Connect_Nux
*/
protected $nux;
/**
* @var WC_Connect_TaxJar_Integration
*/
protected $taxjar;
/**
* @var WC_Connect_PayPal_EC
*/
protected $paypal_ec;
/**
* @var WC_Connect_Tracks
*/
protected $tracks;
/**
* @var WC_Connect_Label_Reports
*/
protected $label_reports;
/**
* @var WC_REST_Connect_Tos_Controller
*/
protected $rest_tos_controller;
protected $services = array();
protected $service_object_cache = array();
protected $wc_connect_base_url;
protected static $wcs_version;
public const MIGRATION_DISMISSAL_COOKIE_KEY = 'wcst-wcshipping-migration-dismissed';
public static function plugin_deactivation() {
wp_clear_scheduled_hook( 'wc_connect_fetch_service_schemas' );
/*
* When we deactivate the plugin after wcshipping_migration_state has started,
* that means the migration is done. We can mark it as completed before we deactivate the plugin.
*/
require_once __DIR__ . '/classes/class-wc-connect-logger.php';
require_once __DIR__ . '/classes/class-wc-connect-tracks.php';
require_once __DIR__ . '/classes/class-wc-connect-wcst-to-wcshipping-migration-state-enum.php';
$migration_state = intval( get_option( 'wcshipping_migration_state' ) );
if ( $migration_state === WC_Connect_WCST_To_WCShipping_Migration_State_Enum::COMPLETED ) {
$core_logger = new WC_Logger();
$logger = new WC_Connect_Logger( $core_logger );
$tracks = new WC_Connect_Tracks( $logger, __FILE__ );
$tracks->record_user_event(
'migration_flag_state_update',
array(
'migration_state' => $migration_state,
'updated' => false,
)
);
}
}
public static function plugin_uninstall() {
WC_Connect_Options::delete_all_options();
self::delete_notices();
}
/**
* Deletes WC Admin notices.
*/
public static function delete_notices() {
if ( self::can_add_wc_admin_notice() ) {
require_once __DIR__ . '/classes/class-wc-connect-note-dhl-live-rates-available.php';
WC_Connect_Note_DHL_Live_Rates_Available::possibly_delete_note();
}
}
/**
* Checks if WC Admin is active and includes needed classes.
*
* @return bool true|false.
*/
public static function can_add_wc_admin_notice() {
if ( ! class_exists( 'WC_Data_Store' ) ) {
return false;
}
try {
WC_Data_Store::load( 'admin-note' );
} catch ( Exception $e ) {
return false;
}
return trait_exists( 'Automattic\WooCommerce\Admin\Notes\NoteTraits' ) && class_exists( 'Automattic\WooCommerce\Admin\Notes\Note' );
}
/**
* Get WCS plugin version
*
* @return string
*/
public static function get_wcs_version() {
if ( null === self::$wcs_version ) {
$plugin_data = get_file_data( __FILE__, array( 'Version' => 'Version' ) );
self::$wcs_version = $plugin_data['Version'];
}
return self::$wcs_version;
}
/**
* Get base url.
*
* @return string
*/
private static function get_wc_connect_base_url() {
return trailingslashit( defined( 'WOOCOMMERCE_CONNECT_DEV_SERVER_URL' ) ? WOOCOMMERCE_CONNECT_DEV_SERVER_URL : plugins_url( 'dist/', __FILE__ ) );
}
/**
* Get WCS admin script url.
*
* @return string
*/
public static function get_wcs_admin_script_url() {
return self::get_wc_connect_base_url() . 'woocommerce-services-' . self::get_wcs_version() . '.js';
}
/**
* Get WCS admin css url.
*
* @return string
*/
public static function get_wcs_admin_style_url() {
if ( ! defined( 'WOOCOMMERCE_CONNECT_DEV_SERVER_URL' ) ) {
return self::get_wc_connect_base_url() . 'woocommerce-services-' . self::get_wcs_version() . '.css';
} else {
return '';
}
}
public function __construct() {
$this->wc_connect_base_url = self::get_wc_connect_base_url();
add_action(
'before_woocommerce_init',
function () {
if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', 'woocommerce-services/woocommerce-services.php' );
}
}
);
add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ) );
add_action( 'plugins_loaded', array( $this, 'jetpack_on_plugins_loaded' ), 1 );
/**
* Used to let WC Tax know WCS&T will handle the plugin's coexistence.
*
* WCS&T does it by not registering its functionality and displaying an appropriate notice
* in WP admin.
*
* The filter also includes "woo_shipping" in its name, but we've since implemented conditional
* loading of all shipping functionality inside WCS&T, so there's no special need for any
* "parallel support" being done in WC Shipping anymore.
*
* This new feature is represented via the "wc_services_will_disable_shipping_logic" hook.
*/
if ( $this->are_woo_shipping_and_woo_tax_active() ) {
add_filter( 'wc_services_will_handle_coexistence_with_woo_shipping_and_woo_tax', '__return_true' );
}
// Let WC Shipping know that the current version of WCS&T supports conditional shipping logic loading.
add_filter( 'wc_services_will_disable_shipping_logic', '__return_true' );
}
public function get_logger() {
return $this->logger;
}
public function set_logger( WC_Connect_Logger $logger ) {
$this->logger = $logger;
}
public function get_shipping_logger() {
return $this->shipping_logger;
}
public function set_shipping_logger( WC_Connect_Logger $logger ) {
$this->shipping_logger = $logger;
}
public function get_api_client() {
return $this->api_client;
}
public function set_api_client( WC_Connect_API_Client $api_client ) {
$this->api_client = $api_client;
}
public function get_service_schemas_store() {
return $this->service_schemas_store;
}
public function set_service_schemas_store( WC_Connect_Service_Schemas_Store $schemas_store ) {
$this->service_schemas_store = $schemas_store;
}
public function get_service_settings_store() {
return $this->service_settings_store;
}
public function set_service_settings_store( WC_Connect_Service_Settings_Store $settings_store ) {
$this->service_settings_store = $settings_store;
}
public function get_payment_methods_store() {
return $this->payment_methods_store;
}
public function set_payment_methods_store( WC_Connect_Payment_Methods_Store $payment_methods_store ) {
$this->payment_methods_store = $payment_methods_store;
}
public function get_tracks() {
return $this->tracks;
}
public function set_tracks( WC_Connect_Tracks $tracks ) {
$this->tracks = $tracks;
}
public function get_rest_account_settings_controller() {
return $this->rest_account_settings_controller;
}
public function set_rest_tos_controller( WC_REST_Connect_Tos_Controller $rest_tos_controller ) {
$this->rest_tos_controller = $rest_tos_controller;
}
public function set_rest_assets_controller( WC_REST_Connect_Assets_Controller $rest_assets_controller ) {
$this->rest_assets_controller = $rest_assets_controller;
}
public function set_rest_carriers_controller( WC_REST_Connect_Shipping_Carriers_Controller $rest_carriers_controller ) {
$this->rest_carriers_controller = $rest_carriers_controller;
}
public function set_rest_subscriptions_controller( WC_REST_Connect_Subscriptions_Controller $rest_subscriptions_controller ) {
$this->rest_subscriptions_controller = $rest_subscriptions_controller;
}
public function set_rest_subscription_activate_controller( WC_REST_Connect_Subscription_Activate_Controller $rest_subscription_activate_controller ) {
$this->rest_subscription_activate_controller = $rest_subscription_activate_controller;
}
public function set_rest_carrier_controller( WC_REST_Connect_Shipping_Carrier_Controller $rest_carrier_controller ) {
$this->rest_carrier_controller = $rest_carrier_controller;
}
public function set_rest_carrier_delete_controller( WC_REST_Connect_Shipping_Carrier_Delete_Controller $rest_carrier_delete_controller ) {
$this->rest_carrier_delete_controller = $rest_carrier_delete_controller;
}
public function set_rest_packages_controller( WC_REST_Connect_Packages_Controller $rest_packages_controller ) {
$this->rest_packages_controller = $rest_packages_controller;
}
public function set_rest_account_settings_controller( WC_REST_Connect_Account_Settings_Controller $rest_account_settings_controller ) {
$this->rest_account_settings_controller = $rest_account_settings_controller;
}
public function get_rest_services_controller() {
return $this->rest_services_controller;
}
public function set_rest_services_controller( WC_REST_Connect_Services_Controller $rest_services_controller ) {
$this->rest_services_controller = $rest_services_controller;
}
public function get_rest_self_help_controller() {
return $this->rest_self_help_controller;
}
public function set_rest_self_help_controller( WC_REST_Connect_Self_Help_Controller $rest_self_help_controller ) {
$this->rest_self_help_controller = $rest_self_help_controller;
}
public function get_rest_shipping_label_controller() {
return $this->rest_shipping_label_controller;
}
public function set_rest_shipping_label_controller( WC_REST_Connect_Shipping_Label_Controller $rest_shipping_label_controller ) {
$this->rest_shipping_label_controller = $rest_shipping_label_controller;
}
public function get_rest_shipping_label_status_controller() {
return $this->rest_shipping_label_status_controller;
}
public function set_rest_shipping_label_status_controller( WC_REST_Connect_Shipping_Label_Status_Controller $rest_shipping_label_status_controller ) {
$this->rest_shipping_label_status_controller = $rest_shipping_label_status_controller;
}
public function get_rest_shipping_label_refund_controller() {
return $this->rest_shipping_label_refund_controller;
}
public function set_rest_shipping_label_refund_controller( WC_REST_Connect_Shipping_Label_Refund_Controller $rest_shipping_label_refund_controller ) {
$this->rest_shipping_label_refund_controller = $rest_shipping_label_refund_controller;
}
public function get_rest_shipping_label_preview_controller() {
return $this->rest_shipping_label_preview_controller;
}
public function set_rest_shipping_label_preview_controller( WC_REST_Connect_Shipping_Label_Preview_Controller $rest_shipping_label_preview_controller ) {
$this->rest_shipping_label_preview_controller = $rest_shipping_label_preview_controller;
}
public function get_rest_shipping_label_print_controller() {
return $this->rest_shipping_label_print_controller;
}
public function set_rest_shipping_label_print_controller( WC_REST_Connect_Shipping_Label_Print_Controller $rest_shipping_label_print_controller ) {
$this->rest_shipping_label_print_controller = $rest_shipping_label_print_controller;
}
public function set_rest_shipping_rates_controller( WC_REST_Connect_Shipping_Rates_Controller $rest_shipping_rates_controller ) {
$this->rest_shipping_rates_controller = $rest_shipping_rates_controller;
}
public function set_rest_address_normalization_controller( WC_REST_Connect_Address_Normalization_Controller $rest_address_normalization_controller ) {
$this->rest_address_normalization_controller = $rest_address_normalization_controller;
}
public function set_carrier_types_controller( WC_REST_Connect_Shipping_Carrier_Types_Controller $rest_carrier_types_controller ) {
$this->rest_carrier_types_controller = $rest_carrier_types_controller;
}
public function set_rest_migration_flag_controller( WC_REST_Connect_Migration_Flag_Controller $rest_migration_flag_controller ) {
$this->rest_migration_flag_controller = $rest_migration_flag_controller;
}
public function get_carrier_types_controller() {
return $this->rest_carrier_types_controller;
}
public function get_service_schemas_validator() {
return $this->service_schemas_validator;
}
public function set_service_schemas_validator( WC_Connect_Service_Schemas_Validator $validator ) {
$this->service_schemas_validator = $validator;
}
public function get_settings_pages() {
return $this->settings_pages;
}
public function set_settings_pages( WC_Connect_Settings_Pages $settings_pages ) {
$this->settings_pages = $settings_pages;
}
public function get_help_view() {
return $this->help_view;
}
public function set_help_view( WC_Connect_Help_View $help_view ) {
$this->help_view = $help_view;
}
public function set_shipping_label( WC_Connect_Shipping_Label $shipping_label ) {
$this->shipping_label = $shipping_label;
}
public function set_nux( WC_Connect_Nux $nux ) {
$this->nux = $nux;
}
public function set_taxjar( WC_Connect_TaxJar_Integration $taxjar ) {
$this->taxjar = $taxjar;
}
public function set_paypal_ec( WC_Connect_PayPal_EC $paypal_ec ) {
$this->paypal_ec = $paypal_ec;
}
public function set_label_reports( WC_Connect_Label_Reports $label_reports ) {
$this->label_reports = $label_reports;
}
/**
* Load our textdomain
*
* @codeCoverageIgnore
*/
public function load_textdomain() {
load_plugin_textdomain( 'woocommerce-services', false, dirname( plugin_basename( __FILE__ ) ) . '/i18n/languages' );
}
public function jetpack_on_plugins_loaded() {
$jetpack_config = new Automattic\Jetpack\Config();
$jetpack_config->ensure(
'connection',
array(
'slug' => WC_Connect_Jetpack::JETPACK_PLUGIN_SLUG,
'name' => __( 'WooCommerce Shipping & Tax', 'woocommerce-services' ),
)
);
}
public function on_plugins_loaded() {
add_action( 'after_setup_theme', array( $this, 'load_textdomain' ) );
/**
* Allow third party logic to determine if this plugin should initiate its logic.
*
* The primary purpose here is to allow a smooth transition between the new WC Tax plugin
* and WooCommerce Shipping & Tax (this plugin), by letting them take over all responsibilities if all three
* plugins are activated at the same time.
*
* @since {{next-release}}
*
* @param bool $status The value will determine if we should initiate the plugins logic or not.
*/
if ( apply_filters( 'wc_services_will_handle_coexistence_with_woo_shipping_and_woo_tax', false ) ) {
// Show message that WCS&T is no longer doing anything.
add_action( 'admin_notices', array( $this, 'display_woo_shipping_and_woo_tax_are_active_notice' ) );
// Bail, so none of our tax and shipping logic will be initiated.
return;
}
if ( ! class_exists( 'WooCommerce' ) ) {
add_action(
'admin_notices',
function () {
/* translators: %s WC download URL link. */
echo '<div class="error"><p><strong>' . sprintf( esc_html__( 'WooCommerce Shipping & Tax requires the WooCommerce plugin to be installed and active. You can download %s here.', 'woocommerce-services' ), '<a href="https://wordpress.org/plugins/woocommerce/" target="_blank">WooCommerce</a>' ) . '</strong></p></div>';
}
);
return;
}
add_action( 'before_woocommerce_init', array( $this, 'pre_wc_init' ) );
}
/**
* Perform plugin bootstrapping that needs to happen before WC init.
*
* This allows the modification of extensions, integrations, etc.
*/
public function pre_wc_init() {
$this->load_dependencies();
$tos_accepted = WC_Connect_Options::get_option( 'tos_accepted' );
// Prevent presenting users with TOS they've already
// accepted in the core WC Setup Wizard or on WP.com.
if ( ! $tos_accepted &&
( get_option( 'woocommerce_setup_jetpack_opted_in' ) || WC_Connect_Jetpack::is_atomic_site() )
) {
WC_Connect_Options::update_option( 'tos_accepted', true );
delete_option( 'woocommerce_setup_jetpack_opted_in' );
$tos_accepted = true;
}
add_action( 'admin_init', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'admin_init', array( $this->nux, 'set_up_nux_notices' ) );
if ( WC_Connect_Nux::JETPACK_NOT_CONNECTED === $this->nux->get_jetpack_install_status() ) {
return;
}
add_action( 'rest_api_init', array( $this, 'tos_rest_init' ) );
// The entire plugin should be enabled if dev mode or connected + TOS.
if ( ! $tos_accepted ) {
return;
}
add_action( 'woocommerce_init', array( $this, 'after_wc_init' ) );
}
public function get_service_schema_defaults( $schema ) {
$defaults = array();
if ( ! property_exists( $schema, 'properties' ) ) {
return $defaults;
}
foreach ( get_object_vars( $schema->properties ) as $prop_id => $prop_schema ) {
if ( property_exists( $prop_schema, 'default' ) ) {
$defaults[ $prop_id ] = $prop_schema->default;
}
if (
property_exists( $prop_schema, 'type' ) &&
'object' === $prop_schema->type
) {
$defaults[ $prop_id ] = $this->get_service_schema_defaults( $prop_schema );
}
}
return $defaults;
}
public function save_defaults_to_shipping_method( $instance_id, $service_id, $zone_id ) {
$shipping_method = WC_Shipping_Zones::get_shipping_method( $instance_id );
$schema = $shipping_method->get_service_schema();
$defaults = (object) $this->get_service_schema_defaults( $schema->service_settings );
WC_Connect_Options::update_shipping_method_option( 'form_settings', $defaults, $service_id, $instance_id );
}
protected function add_method_to_shipping_zone( $zone_id, $method_id ) {
$method = $this->get_service_schemas_store()->get_service_schema_by_id( $method_id );
if ( empty( $method ) ) {
return;
}
$zone = WC_Shipping_Zones::get_zone( $zone_id );
$instance_id = $zone->add_shipping_method( $method->method_id );
$zone->save();
// Dismiss the "add a method to zone" pointer.
$this->nux->dismiss_pointer( 'wc_services_add_service_to_zone' );
}
/**
* Bootstrap our plugin and hook into WP/WC core.
*
* @codeCoverageIgnore
*/
public function after_wc_init() {
$this->schedule_service_schemas_fetch();
$this->service_settings_store->migrate_legacy_services();
$this->attach_hooks();
}
/**
* Load all plugin dependencies.
*/
public function load_dependencies() {
require_once __DIR__ . '/classes/class-wc-connect-wcst-to-wcshipping-migration-state-enum.php';
require_once __DIR__ . '/classes/class-wc-connect-utils.php';
require_once __DIR__ . '/classes/class-wc-connect-logger.php';
require_once __DIR__ . '/classes/class-wc-connect-service-schemas-validator.php';
require_once __DIR__ . '/classes/class-wc-connect-taxjar-integration.php';
require_once __DIR__ . '/classes/class-wc-connect-error-notice.php';
require_once __DIR__ . '/classes/class-wc-connect-compatibility.php';
require_once __DIR__ . '/classes/class-wc-connect-compatibility-wcshipping-packages.php';
require_once __DIR__ . '/classes/class-wc-connect-shipping-method.php';
require_once __DIR__ . '/classes/class-wc-connect-service-schemas-store.php';
require_once __DIR__ . '/classes/class-wc-connect-service-settings-store.php';
require_once __DIR__ . '/classes/class-wc-connect-payment-methods-store.php';
require_once __DIR__ . '/classes/class-wc-connect-tracks.php';
require_once __DIR__ . '/classes/class-wc-connect-help-view.php';
require_once __DIR__ . '/classes/class-wc-connect-shipping-label.php';
require_once __DIR__ . '/classes/class-wc-connect-nux.php';
require_once __DIR__ . '/classes/class-wc-connect-paypal-ec.php';
require_once __DIR__ . '/classes/class-wc-connect-label-reports.php';
require_once __DIR__ . '/classes/class-wc-connect-privacy.php';
require_once __DIR__ . '/classes/class-wc-connect-account-settings.php';
require_once __DIR__ . '/classes/class-wc-connect-package-settings.php';
require_once __DIR__ . '/classes/class-wc-connect-continents.php';
require_once __DIR__ . '/classes/class-wc-connect-order-presenter.php';
require_once __DIR__ . '/classes/class-wc-connect-cart-validation.php';
$core_logger = new WC_Logger();
$logger = new WC_Connect_Logger( $core_logger );
$taxes_logger = new WC_Connect_Logger( $core_logger, 'taxes' );
$shipping_logger = new WC_Connect_Logger( $core_logger, 'shipping' );
WC_Connect_Compatibility_WCShipping_Packages::maybe_enable();
$validator = new WC_Connect_Service_Schemas_Validator();
if ( defined( 'WOOCOMMERCE_SERVICES_LOCAL_TEST_MODE' ) ) {
require_once __DIR__ . '/tests/php/class-wc-connect-api-client-local-test-mock.php';
$api_client = new WC_Connect_API_Client_Local_Test_Mock( $validator, $this );
} else {
require_once __DIR__ . '/classes/class-wc-connect-api-client-live.php';
$api_client = new WC_Connect_API_Client_Live( $validator, $this );
}
$schemas_store = new WC_Connect_Service_Schemas_Store( $api_client, $logger );
$settings_store = new WC_Connect_Service_Settings_Store( $schemas_store, $api_client, $logger );
$payment_methods_store = new WC_Connect_Payment_Methods_Store( $settings_store, $api_client, $logger );
$tracks = new WC_Connect_Tracks( $logger, __FILE__ );
$shipping_label = new WC_Connect_Shipping_Label( $api_client, $settings_store, $schemas_store, $payment_methods_store );
$nux = new WC_Connect_Nux( $tracks, $shipping_label );
$taxjar = new WC_Connect_TaxJar_Integration( $api_client, $taxes_logger, $this->wc_connect_base_url );
$paypal_ec = new WC_Connect_PayPal_EC( $api_client, $nux );
$label_reports = new WC_Connect_Label_Reports( $settings_store );
new WC_Connect_Privacy( $settings_store, $api_client );
$this->set_logger( $logger );
$this->set_shipping_logger( $shipping_logger );
$this->set_api_client( $api_client );
$this->set_service_schemas_validator( $validator );
$this->set_service_schemas_store( $schemas_store );
$this->set_service_settings_store( $settings_store );
$this->set_payment_methods_store( $payment_methods_store );
$this->set_tracks( $tracks );
$this->set_shipping_label( $shipping_label );
$this->set_nux( $nux );
$this->set_taxjar( $taxjar );
$this->set_paypal_ec( $paypal_ec );
$this->set_label_reports( $label_reports );
$cart_validation = new WC_Connect_Cart_Validation();
$cart_validation->register_filters();
$cart_validation->register_actions();
}
/**
* Load admin-only plugin dependencies.
*/
public function load_admin_dependencies() {
require_once __DIR__ . '/classes/class-wc-connect-debug-tools.php';
new WC_Connect_Debug_Tools( $this->api_client );
$schema = $this->get_service_schemas_store();
$settings = $this->get_service_settings_store();
$logger = $this->get_logger();
$this->set_help_view( new WC_Connect_Help_View( $schema, $this->taxjar, $settings, $logger ) );
add_action( 'admin_notices', array( WC_Connect_Error_Notice::instance(), 'render_notice' ) );
add_action( 'admin_notices', array( $this, 'render_schema_notices' ) );
// We only use the settings page for shipping since tax settings are part of
// the core "WooCommerce > Settings > Tax" tab.
require_once __DIR__ . '/classes/class-wc-connect-settings-pages.php';
$settings_pages = new WC_Connect_Settings_Pages( $this->api_client, $this->get_service_schemas_store() );
$this->set_settings_pages( $settings_pages );
// Add WC Admin Notices.
if ( ! self::is_wc_shipping_activated() && self::can_add_wc_admin_notice() ) {
require_once __DIR__ . '/classes/class-wc-connect-note-dhl-live-rates-available.php';
WC_Connect_Note_DHL_Live_Rates_Available::init( $schema );
}
}
/**
* Hook plugin classes into WP/WC core.
*/
public function attach_hooks() {
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
add_action( 'admin_enqueue_scripts', array( $this->nux, 'show_pointers' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_plugin_action_links' ) );
add_action( 'enqueue_wc_connect_script', array( $this, 'enqueue_wc_connect_script' ), 10, 2 );
$tracks = $this->get_tracks();
$tracks->init();
$this->taxjar->init();
$this->paypal_ec->init();
// Only register shipping label-related logic if WC Shipping is not active.
if ( ! self::is_wc_shipping_activated() ) {
add_action( 'rest_api_init', array( $this, 'wc_api_dev_init' ), 9999 );
$this->init_shipping_labels();
}
/*
* Regardless of disabling shipping labels if WC Shipping is active,
* keep live rates enabled if the store supports them.
*/
$this->init_live_rates();
if ( is_admin() ) {
$this->load_admin_dependencies();
}
}
/**
* Register shipping labels-related hooks.
*
* @return void
*/
public function init_shipping_labels() {
add_filter( 'woocommerce_admin_reports', array( $this, 'reports_tabs' ) );
// Changing the postcode, currency, weight or dimension units affect the returned schema from the server.
// Make sure to update the service schemas when these options change.
// TODO: Add other options that change the schema here, or figure out a way to do it automatically.
add_action( 'update_option_woocommerce_store_postcode', array( $this, 'queue_service_schema_refresh' ) );
add_action( 'update_option_woocommerce_currency', array( $this, 'queue_service_schema_refresh' ) );
add_action( 'update_option_woocommerce_weight_unit', array( $this, 'queue_service_schema_refresh' ) );
add_action( 'update_option_woocommerce_dimension_unit', array( $this, 'queue_service_schema_refresh' ) );
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 5, 2 );
add_action( 'woocommerce_admin_shipping_fields', array( $this, 'add_shipping_phone_to_order_fields' ) );
add_filter( 'woocommerce_shipping_fields', array( $this, 'add_shipping_phone_to_checkout' ) );
add_filter( 'woocommerce_get_order_address', array( $this, 'get_shipping_or_billing_phone_from_order' ), 10, 3 );
add_action( 'woocommerce_email_after_order_table', array( $this, 'add_tracking_info_to_emails' ), 10, 3 );
add_action( 'admin_print_footer_scripts', array( $this, 'add_sift_js_tracker' ) );
add_filter( 'woocommerce_hidden_order_itemmeta', array( $this, 'hide_wc_connect_package_meta_data' ) );
add_filter( 'is_protected_meta', array( $this, 'hide_wc_connect_order_meta_data' ), 10, 3 );
add_action( 'current_screen', array( $this, 'maybe_render_upgrade_banner' ) );
}
/**
* Register live rates-related hooks.
*
* @return void
*/
public function init_live_rates() {
$schemas_store = $this->get_service_schemas_store();
$schemas = $schemas_store->get_service_schemas();
if ( $schemas ) {
add_filter( 'woocommerce_shipping_methods', array( $this, 'woocommerce_shipping_methods' ) );
add_action( 'woocommerce_load_shipping_methods', array( $this, 'woocommerce_load_shipping_methods' ) );
add_filter( 'woocommerce_payment_gateways', array( $this, 'woocommerce_payment_gateways' ) );
add_action( 'wc_connect_service_init', array( $this, 'init_service' ), 10, 2 );
add_action( 'wc_connect_service_admin_options', array( $this, 'localize_and_enqueue_service_script' ), 10, 2 );
add_action( 'woocommerce_shipping_zone_method_added', array( $this, 'shipping_zone_method_added' ), 10, 3 );
add_action( 'wc_connect_shipping_zone_method_added', array( $this, 'save_defaults_to_shipping_method' ), 10, 3 );
add_action( 'woocommerce_shipping_zone_method_deleted', array( $this, 'shipping_zone_method_deleted' ), 10, 3 );
add_action( 'woocommerce_shipping_zone_method_status_toggled', array( $this, 'shipping_zone_method_status_toggled' ), 10, 4 );
}
add_action( 'wc_connect_fetch_service_schemas', array( $schemas_store, 'fetch_service_schemas_from_connect_server' ) );
add_filter( 'wc_connect_shipping_service_settings', array( $this, 'shipping_service_settings' ), 10, 3 );
add_action( 'woocommerce_checkout_order_processed', array( $this, 'track_completed_order' ), 10, 3 );
}
/**
* Queue up a service schema refresh (on shutdown) if there isn't one already.
*/
public function queue_service_schema_refresh() {
$schemas_store = $this->get_service_schemas_store();
if ( has_action( 'shutdown', array( $schemas_store, 'fetch_service_schemas_from_connect_server' ) ) ) {
return;
}
add_action( 'shutdown', array( $schemas_store, 'fetch_service_schemas_from_connect_server' ) );
}
public function tos_rest_init() {
$settings_store = $this->get_service_settings_store();
$logger = $this->get_logger();
require_once __DIR__ . '/classes/class-wc-rest-connect-base-controller.php';
require_once __DIR__ . '/classes/class-wc-rest-connect-tos-controller.php';
$rest_tos_controller = new WC_REST_Connect_Tos_Controller( $this->api_client, $settings_store, $logger );
$this->set_rest_tos_controller( $rest_tos_controller );
$rest_tos_controller->register_routes();
}
/**
* Hook the REST API
* Note that we cannot load our controller until this time, because prior to
* rest_api_init firing, WP_REST_Controller is not yet defined
*/
public function rest_api_init() {
$schemas_store = $this->get_service_schemas_store();
$settings_store = $this->get_service_settings_store();
$payment_methods_store = $this->get_payment_methods_store();
$logger = $this->get_logger();
if ( ! class_exists( 'WP_REST_Controller' ) ) {
$this->logger->debug( 'Error. WP_REST_Controller could not be found', __FUNCTION__ );
return;
}
require_once __DIR__ . '/classes/class-wc-rest-connect-base-controller.php';
require_once __DIR__ . '/classes/class-wc-rest-connect-account-settings-controller.php';
$rest_account_settings_controller = new WC_REST_Connect_Account_Settings_Controller( $this->api_client, $settings_store, $logger, $this->payment_methods_store );
$this->set_rest_account_settings_controller( $rest_account_settings_controller );
$rest_account_settings_controller->register_routes();
require_once __DIR__ . '/classes/class-wc-rest-connect-services-controller.php';
$rest_services_controller = new WC_REST_Connect_Services_Controller( $this->api_client, $settings_store, $logger, $schemas_store );