forked from mailchimp/mc-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.php
1406 lines (1228 loc) · 48.1 KB
/
bootstrap.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
// If this file is called directly, abort.
if (!defined( 'WPINC')) {
die;
}
$mailchimp_woocommerce_spl_autoloader = true;
spl_autoload_register(function($class) {
$classes = array(
// includes root
'MailChimp_Service' => 'includes/class-mailchimp-woocommerce-service.php',
'MailChimp_WooCommerce_Options' => 'includes/class-mailchimp-woocommerce-options.php',
'MailChimp_Newsletter' => 'includes/class-mailchimp-woocommerce-newsletter.php',
'MailChimp_WooCommerce_Loader' => 'includes/class-mailchimp-woocommerce-loader.php',
'MailChimp_WooCommerce_i18n' => 'includes/class-mailchimp-woocommerce-i18n.php',
'MailChimp_WooCommerce_Deactivator' => 'includes/class-mailchimp-woocommerce-deactivator.php',
'MailChimp_WooCommerce_Activator' => 'includes/class-mailchimp-woocommerce-activator.php',
'MailChimp_WooCommerce' => 'includes/class-mailchimp-woocommerce.php',
'MailChimp_WooCommerce_Privacy' => 'includes/class-mailchimp-woocommerce-privacy.php',
'Mailchimp_Woocommerce_Deactivation_Survey' => 'includes/class-mailchimp-woocommerce-deactivation-survey.php',
'MailChimp_WooCommerce_Rest_Api' => 'includes/class-mailchimp-woocommerce-rest-api.php',
'Mailchimp_Wocoomerce_CLI' => 'includes/class-mailchimp-woocommerce-cli.php',
// includes/api/assets
'MailChimp_WooCommerce_Address' => 'includes/api/assets/class-mailchimp-address.php',
'MailChimp_WooCommerce_Cart' => 'includes/api/assets/class-mailchimp-cart.php',
'MailChimp_WooCommerce_Customer' => 'includes/api/assets/class-mailchimp-customer.php',
'MailChimp_WooCommerce_LineItem' => 'includes/api/assets/class-mailchimp-line-item.php',
'MailChimp_WooCommerce_Order' => 'includes/api/assets/class-mailchimp-order.php',
'MailChimp_WooCommerce_Product' => 'includes/api/assets/class-mailchimp-product.php',
'MailChimp_WooCommerce_ProductVariation' => 'includes/api/assets/class-mailchimp-product-variation.php',
'MailChimp_WooCommerce_PromoCode' => 'includes/api/assets/class-mailchimp-promo-code.php',
'MailChimp_WooCommerce_PromoRule' => 'includes/api/assets/class-mailchimp-promo-rule.php',
'MailChimp_WooCommerce_Store' => 'includes/api/assets/class-mailchimp-store.php',
// includes/api/errors
'MailChimp_WooCommerce_Error' => 'includes/api/errors/class-mailchimp-error.php',
'MailChimp_WooCommerce_RateLimitError' => 'includes/api/errors/class-mailchimp-rate-limit-error.php',
'MailChimp_WooCommerce_ServerError' => 'includes/api/errors/class-mailchimp-server-error.php',
// includes/api/helpers
'MailChimp_WooCommerce_CurrencyCodes' => 'includes/api/helpers/class-mailchimp-woocommerce-api-currency-codes.php',
'MailChimp_Api_Locales' => 'includes/api/helpers/class-mailchimp-woocommerce-api-locales.php',
// includes/api
'MailChimp_WooCommerce_MailChimpApi' => 'includes/api/class-mailchimp-api.php',
'MailChimp_WooCommerce_Api' => 'includes/api/class-mailchimp-woocommerce-api.php',
'MailChimp_WooCommerce_CreateListSubmission' => 'includes/api/class-mailchimp-woocommerce-create-list-submission.php',
'MailChimp_WooCommerce_Transform_Coupons' => 'includes/api/class-mailchimp-woocommerce-transform-coupons.php',
'MailChimp_WooCommerce_Transform_Orders' => 'includes/api/class-mailchimp-woocommerce-transform-orders-wc3.php',
'MailChimp_WooCommerce_Transform_Products' => 'includes/api/class-mailchimp-woocommerce-transform-products.php',
// includes/processes
'Mailchimp_Woocommerce_Job' => 'includes/processes/class-mailchimp-woocommerce-job.php',
'MailChimp_WooCommerce_Abstract_Sync' => 'includes/processes/class-mailchimp-woocommerce-abstract-sync.php',
'MailChimp_WooCommerce_Cart_Update' => 'includes/processes/class-mailchimp-woocommerce-cart-update.php',
'MailChimp_WooCommerce_Process_Coupons' => 'includes/processes/class-mailchimp-woocommerce-process-coupons.php',
'MailChimp_WooCommerce_Process_Orders' => 'includes/processes/class-mailchimp-woocommerce-process-orders.php',
'MailChimp_WooCommerce_Process_Products' => 'includes/processes/class-mailchimp-woocommerce-process-products.php',
'MailChimp_WooCommerce_SingleCoupon' => 'includes/processes/class-mailchimp-woocommerce-single-coupon.php',
'MailChimp_WooCommerce_Single_Order' => 'includes/processes/class-mailchimp-woocommerce-single-order.php',
'MailChimp_WooCommerce_Single_Product' => 'includes/processes/class-mailchimp-woocommerce-single-product.php',
'MailChimp_WooCommerce_User_Submit' => 'includes/processes/class-mailchimp-woocommerce-user-submit.php',
'MailChimp_WooCommerce_Process_Full_Sync_Manager' => 'includes/processes/class-mailchimp-woocommerce-full-sync-manager.php',
'MailChimp_WooCommerce_Subscriber_Sync' => 'includes/processes/class-mailchimp-woocommerce-subscriber-sync.php',
'MailChimp_WooCommerce_Public' => 'public/class-mailchimp-woocommerce-public.php',
'MailChimp_WooCommerce_Admin' => 'admin/class-mailchimp-woocommerce-admin.php',
);
// if the file exists, require it
$path = plugin_dir_path( __FILE__ );
if (array_key_exists($class, $classes) && file_exists($path.$classes[$class])) {
require $path.$classes[$class];
}
});
/**
* @return object
*/
function mailchimp_environment_variables() {
global $wp_version;
$o = get_option('mailchimp-woocommerce', false);
return (object) array(
'repo' => 'master',
'environment' => 'production', // staging or production
'version' => '2.6.2',
'php_version' => phpversion(),
'wp_version' => (empty($wp_version) ? 'Unknown' : $wp_version),
'wc_version' => function_exists('WC') ? WC()->version : null,
'logging' => ($o && is_array($o) && isset($o['mailchimp_logging'])) ? $o['mailchimp_logging'] : 'standard',
);
}
/**
* Push a job onto the Action Scheduler queue.
*
* @param Mailchimp_Woocommerce_Job $job
* @param int $delay
*
* @return true
*/
function mailchimp_as_push( Mailchimp_Woocommerce_Job $job, $delay = 0 ) {
global $wpdb;
$current_page = isset($job->current_page) && $job->current_page >= 0 ? $job->current_page : false;
$job_id = isset($job->id) ? $job->id : ($current_page ? $job->current_page : get_class($job));
$message = ($job_id != get_class($job)) ? ' :: '. (isset($job->current_page) ? 'page ' : 'obj_id ') . $job_id : '';
$attempts = $job->get_attempts() > 0 ? ' attempt:' . $job->get_attempts() : '';
if ($job->get_attempts() <= 5) {
$args = array(
'job' => maybe_serialize($job),
'obj_id' => $job_id,
'created_at' => gmdate( 'Y-m-d H:i:s', time() )
);
$existing_actions = function_exists('as_get_scheduled_actions') ? as_get_scheduled_actions(array(
'hook' => get_class($job),
'status' => ActionScheduler_Store::STATUS_PENDING,
'args' => array(
'obj_id' => isset($job->id) ? $job->id : null),
'group' => 'mc-woocommerce'
)
) : null;
if (!empty($existing_actions)) {
try {
as_unschedule_action(get_class($job), array('obj_id' => $job->id), 'mc-woocommerce');
} catch (\Exception $e) {}
}
else {
$inserted = $wpdb->insert($wpdb->prefix."mailchimp_jobs", $args);
if (!$inserted) {
try {
if (mailchimp_string_contains($wpdb->last_error, 'Table')) {
mailchimp_debug('DB Issue: `mailchimp_job` table was not found!', 'Creating Tables');
install_mailchimp_queue();
$inserted = $wpdb->insert($wpdb->prefix."mailchimp_jobs", $args);
if (!$inserted) {
mailchimp_debug('Queue Job '.get_class($job), $wpdb->last_error);
}
}
} catch (\Exception $e) {
mailchimp_error_trace($e, 'trying to create queue tables');
}
}
}
$action_args = array(
'obj_id' => $job_id,
);
if ($current_page !== false) {
$action_args['page'] = $current_page;
}
$action = as_schedule_single_action( strtotime( '+'.$delay.' seconds' ), get_class($job), $action_args, "mc-woocommerce");
if (!empty($existing_actions)) {
mailchimp_debug('action_scheduler.reschedule_job', get_class($job) . ($delay > 0 ? ' restarts in '.$delay. ' seconds' : ' re-queued' ) . $message . $attempts);
}
else {
mailchimp_log('action_scheduler.queue_job', get_class($job) . ($delay > 0 ? ' starts in '.$delay. ' seconds' : ' queued' ) . $message . $attempts);
}
return $action;
}
else {
$job->set_attempts(0);
mailchimp_log('action_scheduler.fail_job', get_class($job) . ' cancelled. Too many attempts' . $message . $attempts);
return false;
}
}
/**
* @param Mailchimp_Woocommerce_Job $job
* @param int $delay
* @param bool $force_now
*/
function mailchimp_handle_or_queue(Mailchimp_Woocommerce_Job $job, $delay = 0)
{
if ($job instanceof \MailChimp_WooCommerce_Single_Order && isset($job->id)) {
// if this is a order process already queued - just skip this
if (get_site_transient("mailchimp_order_being_processed_{$job->id}") == true) {
return;
}
// tell the system the order is already queued for processing in this saving process - and we don't need to process it again.
set_site_transient( "mailchimp_order_being_processed_{$job->id}", true, 30);
}
$as_job_id = mailchimp_as_push($job, $delay);
if (!is_int($as_job_id)) {
mailchimp_log('action_scheduler.queue_fail', get_class($job) .' FAILED :: as_job_id: '.$as_job_id);
}
}
function mailchimp_get_remaining_jobs_count($job_hook) {
$existing_actions = function_exists('as_get_scheduled_actions') ? as_get_scheduled_actions(
array(
'hook' => $job_hook,
'status' => ActionScheduler_Store::STATUS_PENDING,
'group' => 'mc-woocommerce',
'per_page' => -1,
), 'ids'
) : null;
// mailchimp_log('sync.full_sync_manager.queue', "counting {$job_hook} actions:", array($existing_actions));
return count($existing_actions);
}
function mailchimp_submit_subscribed_only() {
return ! (bool) mailchimp_get_option('mailchimp_ongoing_sync_status', '1');
}
/**
* @return bool
*/
function mailchimp_carts_disabled() {
return mailchimp_get_option('mailchimp_cart_tracking', 'all') === 'disabled';
}
/**
* @return bool
*/
function mailchimp_carts_subscribers_only() {
return mailchimp_get_option('mailchimp_cart_tracking', 'all') === 'subscribed';
}
/**
* @param $email
* @return string|null
*/
function mailchimp_get_subscriber_status($email) {
try {
return mailchimp_get_api()->member(mailchimp_get_list_id(), $email)['status'];
} catch (\Exception $e) {
return null;
}
}
/**
* @param bool $force
* @return bool
*/
function mailchimp_list_has_double_optin($force = false) {
if (!mailchimp_is_configured()) {
return false;
}
$key = 'double_optin';
$double_optin = mailchimp_get_transient($key);
if (!$force && ($double_optin === 'yes' || $double_optin === 'no')) {
return $double_optin === 'yes';
}
try {
$data = mailchimp_get_api()->getList(mailchimp_get_list_id());
$double_optin = array_key_exists('double_optin', $data) ? ($data['double_optin'] ? 'yes' : 'no') : 'no';
mailchimp_set_transient($key, $double_optin, 600);
return $double_optin === 'yes';
} catch (\Exception $e) {
mailchimp_error('api.list', __('Error retrieving list for double_optin check', 'mailchimp-for-woocommerce'));
throw $e;
}
return $double_optin === 'yes';
}
/**
* @return bool
*/
function mailchimp_is_configured() {
return (bool) (mailchimp_get_api_key() && mailchimp_get_list_id());
}
/**
* @return bool
*/
function mailchimp_action_scheduler_exists() {
return ( did_action( 'plugins_loaded' ) && ! doing_action( 'plugins_loaded' ) && class_exists( 'ActionScheduler', false ) );
}
/**
* @return bool|int
*/
function mailchimp_get_api_key() {
return mailchimp_get_option('mailchimp_api_key', false);
}
/**
* @return bool|int
*/
function mailchimp_get_list_id() {
return mailchimp_get_option('mailchimp_list', false);
}
/**
* @return string
*/
function mailchimp_get_store_id() {
$store_id = mailchimp_get_data('store_id', false);
// if the store ID is not empty, let's check the last time the store id's have been verified correctly
if (!empty($store_id)) {
// see if we have a record of the last verification set for this job.
$last_verification = mailchimp_get_data('store-id-last-verified');
// if it's less than 300 seconds, we don't need to beat up on Mailchimp's API to do this so often.
// just return the store ID that was in memory.
if ((!empty($last_verification) && is_numeric($last_verification)) && ((time() - $last_verification) < 600)) {
//mailchimp_log('debug.performance', 'prevented store endpoint api call');
return $store_id;
}
}
$api = mailchimp_get_api();
if (mailchimp_is_configured()) {
//mailchimp_log('debug.performance', 'get_store_id - calling STORE endpoint.');
// let's retrieve the store for this domain, through the API
$store = $api->getStore($store_id, false);
// if there's no store, try to fetch from mc a store related to the current domain
if (!$store) {
//mailchimp_log('debug.performance', 'get_store_id - no store found - calling STORES endpoint to update site id.');
$stores = $api->stores();
if (!empty($stores)) {
//iterate thru stores, find correct store ID and save it to db
foreach ($stores as $mc_store) {
if ($mc_store->getDomain() === get_option('siteurl')) {
update_option('mailchimp-woocommerce-store_id', $mc_store->getId(), 'yes');
$store_id = $mc_store->getId();
}
}
}
}
}
if (empty($store_id)) {
mailchimp_set_data('store_id', $store_id = uniqid(), 'yes');
}
// tell the system the last time we verified this store ID is valid with a timestamp.
mailchimp_set_data('store-id-last-verified', time(), 'yes');
//mailchimp_log('debug.performance', 'setting store id in memory for 300 seconds.');
return $store_id;
}
/**
* @return array
*/
function mailchimp_get_user_tags_to_update($email = null, $order = null) {
$tags = mailchimp_get_option('mailchimp_user_tags');
$formatted_tags = array();
if (!empty($tags)) {
$tags = explode(',', $tags);
foreach ($tags as $tag) {
$formatted_tags[] = array("name" => $tag, "status" => 'active');
}
}
// apply filter to user custom tags addition/removal
$formatted_tags = apply_filters('mailchimp_user_tags', $formatted_tags, $email, $order);
if (empty($formatted_tags)){
return false;
}
return $formatted_tags;
}
/**
* @return bool|MailChimp_WooCommerce_MailChimpApi
*/
function mailchimp_get_api() {
if (($api = MailChimp_WooCommerce_MailChimpApi::getInstance())) {
return $api;
}
if (($key = mailchimp_get_api_key())) {
return MailChimp_WooCommerce_MailChimpApi::constructInstance($key);
}
return false;
}
/**
* @param $key
* @param null $default
* @return null
*/
function mailchimp_get_option($key, $default = null) {
$options = get_option('mailchimp-woocommerce');
if (!is_array($options)) {
return $default;
}
if (!array_key_exists($key, $options)) {
return $default;
}
return $options[$key];
}
/**
* @param $key
* @param null $default
* @return mixed
*/
function mailchimp_get_data($key, $default = null) {
return get_option('mailchimp-woocommerce-'.$key, $default);
}
/**
* @param $key
* @param $value
* @param string $autoload
* @return bool
*/
function mailchimp_set_data($key, $value, $autoload = 'yes') {
return update_option('mailchimp-woocommerce-'.$key, $value, $autoload);
}
/**
* @param $date
* @return DateTime
*/
function mailchimp_date_utc($date) {
$timezone = wc_timezone_string();
if (is_numeric($date)) {
$stamp = $date;
$date = new \DateTime('now', new DateTimeZone($timezone));
$date->setTimestamp($stamp);
} else {
$date = new \DateTime($date, new DateTimeZone($timezone));
}
$date->setTimezone(new DateTimeZone('UTC'));
return $date;
}
/**
* @param $date
* @return DateTime
*/
function mailchimp_date_local($date) {
$timezone = str_replace(':', '', mailchimp_get_timezone());
if (is_numeric($date)) {
$stamp = $date;
$date = new \DateTime('now', new DateTimeZone('UTC'));
$date->setTimestamp($stamp);
} else {
$date = new \DateTime($date, new DateTimeZone('UTC'));
}
$date->setTimezone(new DateTimeZone($timezone));
return $date;
}
/**
* @param array $data
* @return mixed
*/
function mailchimp_array_remove_empty($data) {
if (empty($data) || !is_array($data)) {
return array();
}
foreach ($data as $key => $value) {
if ($value === null || $value === '' || (is_array($value) && empty($value))) {
unset($data[$key]);
}
}
return $data;
}
/**
* @return array
*/
function mailchimp_get_timezone_list() {
$zones_array = array();
$timestamp = time();
$current = date_default_timezone_get();
foreach(timezone_identifiers_list() as $key => $zone) {
date_default_timezone_set($zone);
$zones_array[$key]['zone'] = $zone;
$zones_array[$key]['diff_from_GMT'] = 'UTC/GMT ' . date('P', $timestamp);
}
date_default_timezone_set($current);
return $zones_array;
}
/**
* Gets the current tomezone from wordpress settings
*
* @return String timezone
*/
function mailchimp_get_timezone($humanReadable = false) {
// get timezone data from options
$timezone_string = get_option( 'timezone_string' );
$offset = get_option( 'gmt_offset' );
$signal = ($offset <=> 0 ) < 0 ? "-" : "+";
$offset = sprintf('%1s%02d:%02d', $signal, abs((int) $offset), abs(fmod($offset, 1) * 60));
// shows timezone name + offset in hours and minutes, or only the timezone name. If no timezone string is set, show only offset
if (!$humanReadable && $timezone_string) {
$timezone = $timezone_string;
}
else if ($humanReadable && $timezone_string) {
$timezone = "UTC" . $offset .' '. $timezone_string;
}
else if ($humanReadable && !$timezone_string) {
$timezone = "UTC" . $offset;
}
else if (!$timezone_string) {
$timezone = $offset;
}
return $timezone;
}
/**
* @return bool
*/
function mailchimp_check_woocommerce_plugin_status()
{
// if you are using a custom folder name other than woocommerce just define the constant to TRUE
if (defined("RUNNING_CUSTOM_WOOCOMMERCE") && RUNNING_CUSTOM_WOOCOMMERCE === true) {
return true;
}
// it the plugin is active, we're good.
if (in_array('woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option('active_plugins')))) {
return true;
}
if (!is_multisite()) return false;
$plugins = get_site_option( 'active_sitewide_plugins');
return isset($plugins['woocommerce/woocommerce.php']);
}
/**
* Get all the registered image sizes along with their dimensions
*
* @global array $_wp_additional_image_sizes
*
* @link http://core.trac.wordpress.org/ticket/18947 Reference ticket
*
* @return array $image_sizes The image sizes
*/
function mailchimp_woocommerce_get_all_image_sizes() {
global $_wp_additional_image_sizes;
$image_sizes = array();
$default_image_sizes = get_intermediate_image_sizes();
foreach ($default_image_sizes as $size) {
$image_sizes[$size]['width'] = intval( get_option("{$size}_size_w"));
$image_sizes[$size]['height'] = intval( get_option("{$size}_size_h"));
$image_sizes[$size]['crop'] = get_option("{$size}_crop") ? get_option("{$size}_crop") : false;
}
if (isset($_wp_additional_image_sizes) && count($_wp_additional_image_sizes)) {
$image_sizes = array_merge( $image_sizes, $_wp_additional_image_sizes );
}
return $image_sizes;
}
/**
* @return array
*/
function mailchimp_woocommerce_get_all_image_sizes_list() {
$response = array();
foreach (mailchimp_woocommerce_get_all_image_sizes() as $key => $data) {
$label = ucwords(str_replace('_', ' ', $key));
$label = __($label);
$response[$key] = "{$label} ({$data['width']} x {$data['height']})";
}
return $response;
}
/**
* The code that runs during plugin activation.
* This action is documented in includes/class-mailchimp-woocommerce-activator.php
*/
function activate_mailchimp_woocommerce() {
// if we don't have any of these dependencies,
// we need to display a horrible error message before the plugin is installed.
mailchimp_check_curl_is_installed();
mailchimp_check_woocommerce_is_installed();
// good to go - activate the plugin.
MailChimp_WooCommerce_Activator::activate();
}
function mailchimp_check_curl_is_installed() {
if (!function_exists('curl_exec')) {
// Deactivate the plugin
deactivate_plugins(__FILE__);
$error_message = __('The MailChimp For WooCommerce plugin requires <a href="https://www.php.net/manual/en/book.curl.php/">curl</a> to be enabled!', 'woocommerce');
wp_die($error_message);
}
return true;
}
function mailchimp_check_woocommerce_is_installed() {
if (!mailchimp_check_woocommerce_plugin_status()) {
// Deactivate the plugin
deactivate_plugins(__FILE__);
$error_message = __('The MailChimp For WooCommerce plugin requires the <a href="http://wordpress.org/extend/plugins/woocommerce/">WooCommerce</a> plugin to be active!', 'woocommerce');
wp_die($error_message);
}
return true;
}
/**
* Create the queue tables
*/
function install_mailchimp_queue() {
MailChimp_WooCommerce_Activator::create_queue_tables();
}
/**
* The code that runs during plugin deactivation.
* This action is documented in includes/class-mailchimp-woocommerce-deactivator.php
*/
function deactivate_mailchimp_woocommerce() {
MailChimp_WooCommerce_Deactivator::deactivate();
}
/**
* @param $action
* @param $message
* @param null $data
*/
function mailchimp_debug($action, $message, $data = null) {
if (mailchimp_environment_variables()->logging === 'debug' && function_exists('wc_get_logger')) {
if (is_array($data) && !empty($data)) $message .= " :: ".wc_print_r($data, true);
wc_get_logger()->debug("{$action} :: {$message}", array('source' => 'mailchimp_woocommerce'));
}
}
/**
* @param $action
* @param $message
* @param array $data
* @return array|WP_Error
*/
function mailchimp_log($action, $message, $data = array()) {
if (mailchimp_environment_variables()->logging !== 'none' && function_exists('wc_get_logger')) {
if (is_array($data) && !empty($data)) $message .= " :: ".wc_print_r($data, true);
wc_get_logger()->notice("{$action} :: {$message}", array('source' => 'mailchimp_woocommerce'));
}
}
/**
* @param $action
* @param $message
* @param array $data
* @return array|WP_Error
*/
function mailchimp_error($action, $message, $data = array()) {
if (mailchimp_environment_variables()->logging !== 'none' && function_exists('wc_get_logger')) {
if ($message instanceof \Exception) $message = mailchimp_error_trace($message);
if (is_array($data) && !empty($data)) $message .= " :: ".wc_print_r($data, true);
wc_get_logger()->error("{$action} :: {$message}", array('source' => 'mailchimp_woocommerce'));
}
}
/**
* @param Exception $e
* @param string $wrap
* @return string
*/
function mailchimp_error_trace($e, $wrap = "") {
$error = "Error Code {$e->getCode()} :: {$e->getMessage()} on {$e->getLine()} in {$e->getFile()}";
if (empty($wrap)) return $error;
return "{$wrap} :: {$error}";
}
/**
* Determine if a given string contains a given substring.
*
* @param string $haystack
* @param string|array $needles
* @return bool
*/
function mailchimp_string_contains($haystack, $needles) {
$has_mb = function_exists('mb_strpos');
foreach ((array) $needles as $needle) {
$has_needle = $needle != '';
// make sure the server has "mb_strpos" otherwise this fails. Fallback to "strpos"
$position = $has_mb ? mb_strpos($haystack, $needle) : strpos($haystack, $needle);
if ($has_needle && $position !== false) {
return true;
}
}
return false;
}
/**
* @return int
*/
function mailchimp_get_coupons_count() {
$posts = mailchimp_count_posts('shop_coupon');
unset($posts['auto-draft'], $posts['trash']);
$total = 0;
foreach ($posts as $status => $count) {
$total += $count;
}
return $total;
}
/**
* @return int
*/
function mailchimp_get_product_count() {
$posts = mailchimp_count_posts('product');
unset($posts['auto-draft'], $posts['trash']);
$total = 0;
foreach ($posts as $status => $count) {
$total += $count;
}
return $total;
}
/**
* @return int
*/
function mailchimp_get_order_count() {
$posts = mailchimp_count_posts('shop_order');
unset($posts['auto-draft'], $posts['trash']);
$total = 0;
foreach ($posts as $status => $count) {
$total += $count;
}
return $total;
}
/**
* @param $type
* @return array|null|object
*/
function mailchimp_count_posts($type) {
global $wpdb;
if ($type === 'shop_order') {
$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s AND post_status = %s";
$posts = $wpdb->get_results( $wpdb->prepare($query, $type, 'wc-completed'));
} else if ($type === 'product') {
$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s AND post_status IN (%s, %s, %s) group BY post_status";
$posts = $wpdb->get_results( $wpdb->prepare($query, $type, 'private', 'publish', 'draft'));
} else {
$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s AND post_status = %s";
$posts = $wpdb->get_results( $wpdb->prepare($query, $type, 'publish'));
}
$response = array();
foreach ($posts as $post) {
$response[$post->post_status] = $post->num_posts;
}
return $response;
}
/**
* @return bool
*/
function mailchimp_update_connected_site_script() {
// pull the store ID
$store_id = mailchimp_get_store_id();
// if the api is configured
if ($store_id && ($api = mailchimp_get_api())) {
// if we have a store
if (($store = $api->getStore($store_id))) {
return mailchimpi_refresh_connected_site_script($store);
}
}
return false;
}
/**
* @return bool|DateTime
*/
function mailchimp_get_updated_connected_site_since_as_date_string() {
$updated_at = get_option('mailchimp-woocommerce-script_updated_at', false);
if (empty($updated_at)) return '';
try {
$date = new \DateTime();
$date->setTimestamp($updated_at);
return $date->format('D, M j, Y g:i A');
} catch (\Exception $e) {
return '';
}
}
/**
* @return int
*/
function mailchimp_get_updated_connected_site_since() {
$updated_at = get_option('mailchimp-woocommerce-script_updated_at', false);
return empty($updated_at) ? 1000000 : (time() - $updated_at);
}
/**
* @param int $seconds
* @return bool
*/
function mailchimp_should_update_connected_site_script($seconds = 600) {
return mailchimp_get_updated_connected_site_since() >= $seconds;
}
/**
*
*/
function mailchimp_update_connected_site_script_from_cdn() {
if (mailchimp_is_configured() && mailchimp_should_update_connected_site_script() && ($store_id = mailchimp_get_store_id())) {
try {
// pull the store, refresh the connected site url
mailchimpi_refresh_connected_site_script(mailchimp_get_api()->getStore($store_id));
} catch (\Exception $e) {
mailchimp_error("admin.update_connected_site_script", $e->getMessage());
}
}
}
/**
* @param MailChimp_WooCommerce_Store $store
* @return bool
*/
function mailchimpi_refresh_connected_site_script(MailChimp_WooCommerce_Store $store) {
$api = mailchimp_get_api();
$url = $store->getConnectedSiteScriptUrl();
$fragment = $store->getConnectedSiteScriptFragment();
// if it's not empty we need to set the values
if ($url && $fragment) {
// update the options for script_url and script_fragment
update_option('mailchimp-woocommerce-script_url', $url);
update_option('mailchimp-woocommerce-script_fragment', $fragment);
update_option('mailchimp-woocommerce-script_updated_at', time());
// check to see if the site is connected
if (!$api->checkConnectedSite($store->getId())) {
// if it's not, connect it now.
$api->connectSite($store->getId());
}
return true;
}
return false;
}
/**
* @return string|false
*/
function mailchimp_get_connected_site_script_url() {
return get_option('mailchimp-woocommerce-script_url', false);
}
/**
* @return string|false
*/
function mailchimp_get_connected_site_script_fragment() {
return get_option('mailchimp-woocommerce-script_fragment', false);
}
/**
* @param $email
* @return bool
*/
function mailchimp_email_is_allowed($email) {
if (!is_email($email) || mailchimp_email_is_amazon($email) || mailchimp_email_is_privacy_protected($email)) {
return false;
}
return true;
}
/**
* @param $email
* @return bool
*/
function mailchimp_email_is_privacy_protected($email) {
return $email === '[email protected]';
}
/**
* @param $email
* @return bool
*/
function mailchimp_email_is_amazon($email) {
return mailchimp_string_contains($email, '@marketplace.amazon.');
}
/**
* @param $str
* @return string
*/
function mailchimp_hash_trim_lower($str) {
return md5(trim(strtolower($str)));
}
/**
* @param $key
* @param null $default
* @return mixed|null
*/
function mailchimp_get_transient($key, $default = null) {
$transient = get_site_transient("mailchimp-woocommerce.{$key}");
return empty($transient) ? $default : $transient;
}
/**
* @param $key
* @param $value
* @param int $seconds
* @return bool
*/
function mailchimp_set_transient($key, $value, $seconds = 60) {
mailchimp_delete_transient($key);
return set_site_transient("mailchimp-woocommerce.{$key}", array(
'value' => $value,
'expires' => time()+$seconds,
), $seconds);
}
/**
* @param $key
* @return bool
*/
function mailchimp_delete_transient($key) {
return delete_site_transient("mailchimp-woocommerce.{$key}");
}
/**
* @param $key
* @param null $default
* @return mixed|null
*/
function mailchimp_get_transient_value($key, $default = null) {
$transient = mailchimp_get_transient($key, false);
return (is_array($transient) && array_key_exists('value', $transient)) ? $transient['value'] : $default;
}
/**
* @param $key
* @param $value
* @return bool|null
*/
function mailchimp_check_serialized_transient_changed($key, $value) {
if (($saved = mailchimp_get_transient_value($key)) && !empty($saved)) {
return serialize($saved) === serialize($value);
}
return null;
}
/**
* @param $email
* @return bool|string
*/
function mailchimp_get_transient_email_key($email) {
$email = md5(trim(strtolower($email)));
return empty($email) ? false : 'MailChimp_WooCommerce_User_Submit@'.$email;
}
/**
* @param $email
* @param $status_meta
* @param int $seconds
* @return bool
*/
function mailchimp_tell_system_about_user_submit($email, $status_meta, $seconds = 60) {
return mailchimp_set_transient(mailchimp_get_transient_email_key($email), $status_meta, $seconds);
}
/**
* @param $subscribed
* @return array
*/
function mailchimp_get_subscriber_status_options($subscribed) {
try {
$requires = mailchimp_list_has_double_optin();
} catch (\Exception $e) {
return false;
}
// if it's true - we set this value to NULL so that we do a 'pending' association on the member.