-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelpers.php
More file actions
104 lines (88 loc) · 2.48 KB
/
helpers.php
File metadata and controls
104 lines (88 loc) · 2.48 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
<?php
/**
* Check if WooCommerce is activated
*
* @return boolean
*/
function tapfiliate_is_woocommerce_activated() {
return class_exists('WooCommerce');
}
/**
* Check if WooCommerce v3
*
* @return bool
*/
function tapfiliate_is_woo3() {
if (tapfiliate_is_woocommerce_activated()) {
global $woocommerce;
return version_compare($woocommerce->version, "3.0", ">=");
}
return false;
}
/**
* Rounding function adapted from woocommerce
*
* @param number $amount The amount
*
* @return number Rounded amount
*/
function tapfiliate_woo_round($amount) {
return number_format((float) $amount, wc_get_price_decimals(), '.', '');
}
/**
* Check if has WooCommerce subscriptions
*
* @return bool
*/
function tapfiliate_has_woo_subscriptions() {
return function_exists('wcs_order_contains_subscription');
}
/**
* Check if we have all the required webhooks
*
* @return string none|partial|full
*/
function tapfiliate_get_woocommerce_connection_status() {
$data_store = WC_Data_Store::load('webhook');
$webhooks = $data_store->search_webhooks();
$required_webhooks = [
"order.deleted",
"order.updated",
"order.created",
"customer.deleted",
"customer.updated",
"customer.created"
];
if (tapfiliate_has_woo_subscriptions()) {
$required_webhooks = array_merge($required_webhooks, [
"subscription.switched",
"subscription.updated",
"subscription.created",
"subscription.deleted",
]);
}
$current_webhooks = array_reduce(
$webhooks,
function ($carry, $item) {
$webhook = new WC_Webhook($item);
$name = $webhook->get_name();
if (strpos($name, 'Tapfiliate') !== false) {
$carry[] = $webhook->get_topic();
}
return $carry;
},
[]
);
// If there are no webhooks we're not connected
if (!count($current_webhooks)) {
return "none";
}
$missing_webhooks = array_diff($required_webhooks, array_unique($current_webhooks));
if (count($missing_webhooks) > 0) {
if (count($required_webhooks) !== count($missing_webhooks)) {
echo '<div class="error"><p><strong>Tapfiliate is missing the following webhooks: ' . implode(", ", $missing_webhooks) . '. You can reconnect to Tapfiliate to fix this.</strong></p></div>';
}
return "partial";
}
return "full";
}