| 1 |
<?php |
| 2 |
|
| 3 |
use MailerLite\Includes\Classes\Settings\ApiSettings; |
| 4 |
use MailerLite\Includes\Shared\Api\PlatformAPI; |
| 5 |
use MailerLite\Includes\Shared\Api\ApiType; |
| 6 |
|
| 7 |
/** |
| 8 |
* Check if order tracking setup was finished |
| 9 |
*/ |
| 10 |
function woo_ml_integration_setup_completed() |
| 11 |
{ |
| 12 |
|
| 13 |
$integration_setup = get_option('woo_ml_integration_setup', false); |
| 14 |
|
| 15 |
return ('1' == $integration_setup) ? true : false; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Mark order tracking setup as completed |
| 20 |
*/ |
| 21 |
function woo_ml_complete_integration_setup() |
| 22 |
{ |
| 23 |
add_option('woo_ml_integration_setup', true); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Revoke order tracking setup completion |
| 28 |
*/ |
| 29 |
function woo_ml_revoke_integration_setup() |
| 30 |
{ |
| 31 |
delete_option('woo_ml_integration_setup'); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Setup MailerLite integration |
| 36 |
*/ |
| 37 |
function woo_ml_setup_integration() |
| 38 |
{ |
| 39 |
$setup_custom_fields = woo_ml_setup_integration_custom_fields(); |
| 40 |
|
| 41 |
if ($setup_custom_fields) { |
| 42 |
woo_ml_complete_integration_setup(); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Setup Integration Custom Fields |
| 48 |
* |
| 49 |
* - Get existing custom fields via API |
| 50 |
* - Check if our custom fields were already created |
| 51 |
* - Create missing custom fields |
| 52 |
* |
| 53 |
* @return bool |
| 54 |
*/ |
| 55 |
function woo_ml_setup_integration_custom_fields($fields = null) |
| 56 |
{ |
| 57 |
|
| 58 |
$api_type = (int)get_option('woo_mailerlite_platform', 1); |
| 59 |
|
| 60 |
$ml_fields = mailerlite_wp_get_custom_fields(); |
| 61 |
|
| 62 |
if ( ! $fields) { |
| 63 |
|
| 64 |
$fields = woo_ml_get_integration_custom_fields($api_type); |
| 65 |
} |
| 66 |
|
| 67 |
if (is_array($ml_fields)) { |
| 68 |
|
| 69 |
foreach ($ml_fields as $ml_field) { |
| 70 |
|
| 71 |
$ml_field = (array)$ml_field; |
| 72 |
|
| 73 |
if (isset($ml_field['key']) && isset($fields[$ml_field['key']])) { |
| 74 |
|
| 75 |
unset($fields[$ml_field['key']]); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if (sizeof($fields) > 0) { |
| 80 |
foreach ($fields as $field_data) { |
| 81 |
|
| 82 |
mailerlite_wp_create_custom_field($field_data); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get integration custom fields |
| 92 |
* |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
function woo_ml_get_integration_custom_fields($api_type) |
| 96 |
{ |
| 97 |
|
| 98 |
if ($api_type !== ApiType::CURRENT) { |
| 99 |
|
| 100 |
return [ |
| 101 |
'woo_orders_count' => [ |
| 102 |
'title' => 'Woo Orders Count', |
| 103 |
'type' => 'NUMBER' |
| 104 |
], |
| 105 |
'woo_total_spent' => [ |
| 106 |
'title' => 'Woo Total Spent', |
| 107 |
'type' => 'NUMBER' |
| 108 |
], |
| 109 |
'woo_last_order' => [ |
| 110 |
'title' => 'Woo Last Order', |
| 111 |
'type' => 'DATE' |
| 112 |
], |
| 113 |
'woo_last_order_id' => [ |
| 114 |
'title' => 'Woo Last Order ID', |
| 115 |
'type' => 'NUMBER' |
| 116 |
], |
| 117 |
]; |
| 118 |
} else { |
| 119 |
|
| 120 |
$shopUrl = home_url(); |
| 121 |
$shopKey = preg_replace('/[^A-Za-z0-9 ]/', '', $shopUrl); |
| 122 |
|
| 123 |
$shop_name = get_bloginfo('name'); |
| 124 |
|
| 125 |
if (empty($shop_name)) { |
| 126 |
$shop_name = $shopUrl; |
| 127 |
} |
| 128 |
|
| 129 |
return [ |
| 130 |
$shopKey . '_total_spent' => [ |
| 131 |
'key' => $shopKey, |
| 132 |
'name' => $shop_name, |
| 133 |
'title' => 'Total spent', |
| 134 |
'type' => 'number', |
| 135 |
], |
| 136 |
$shopKey . '_orders_count' => [ |
| 137 |
'key' => $shopKey, |
| 138 |
'name' => $shop_name, |
| 139 |
'title' => 'Orders count', |
| 140 |
'type' => 'number', |
| 141 |
], |
| 142 |
$shopKey . '_accepts_marketing' => [ |
| 143 |
'key' => $shopKey, |
| 144 |
'name' => $shop_name, |
| 145 |
'title' => 'Accepts marketing', |
| 146 |
'type' => 'number', |
| 147 |
], |
| 148 |
]; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
function woo_ml_old_integration() |
| 153 |
{ |
| 154 |
return ! get_option('new_plugin_enabled'); |
| 155 |
} |
| 156 |
|
| 157 |
function woo_ml_shop_not_active() |
| 158 |
{ |
| 159 |
return get_option('ml_shop_not_active'); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get subscriber additional fields |
| 164 |
* |
| 165 |
* @return array |
| 166 |
*/ |
| 167 |
function woo_ml_get_additional_fields() |
| 168 |
{ |
| 169 |
|
| 170 |
return [ |
| 171 |
'subscriber_language' => [ |
| 172 |
'title' => 'Subscriber Language', |
| 173 |
'type' => 'text', |
| 174 |
], |
| 175 |
]; |
| 176 |
} |
| 177 |
|
| 178 |
function woo_ml_setup_additional_sub_fields() |
| 179 |
{ |
| 180 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 181 |
|
| 182 |
$ml_fields = $mailerliteClient->getFields(); |
| 183 |
|
| 184 |
$sub_fields = woo_ml_get_additional_fields(); |
| 185 |
|
| 186 |
if (is_array($ml_fields)) { |
| 187 |
|
| 188 |
foreach ($ml_fields as $ml_field) { |
| 189 |
|
| 190 |
$ml_field = (array)$ml_field; |
| 191 |
|
| 192 |
if (isset($ml_field['key']) && isset($sub_fields[$ml_field['key']])) { |
| 193 |
|
| 194 |
unset($sub_fields[$ml_field['key']]); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if (sizeof($sub_fields) > 0) { |
| 199 |
foreach ($sub_fields as $field_data) { |
| 200 |
|
| 201 |
woo_ml_create_additional_sub_fields($field_data); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
function woo_ml_create_additional_sub_fields($field_data) |
| 208 |
{ |
| 209 |
if ( ! ApiSettings::getInstance()->wpApiKeyExists()) { |
| 210 |
|
| 211 |
return false; |
| 212 |
} |
| 213 |
|
| 214 |
if ( ! isset($field_data['title']) || ! isset($field_data['type'])) { |
| 215 |
|
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
try { |
| 220 |
|
| 221 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 222 |
|
| 223 |
return $mailerliteClient->createField($field_data['title'], $field_data['type']); |
| 224 |
} catch (Exception $e) { |
| 225 |
return false; |
| 226 |
} |
| 227 |
} |
| 228 |
|