| 1 |
<?php |
| 2 |
|
| 3 |
use MailerLite\Includes\Classes\Settings\ApiSettings; |
| 4 |
use MailerLite\Includes\Shared\Api\ApiType; |
| 5 |
use MailerLite\Includes\Shared\Api\PlatformAPI; |
| 6 |
|
| 7 |
/** |
| 8 |
* Functions inside this file are being used in different MailerLite related WordPress plugins |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! function_exists('mailerlite_wp_get_groups')) : |
| 12 |
/** |
| 13 |
* Get groups from API |
| 14 |
* |
| 15 |
* @param $api_key |
| 16 |
* |
| 17 |
* @return array|bool |
| 18 |
*/ |
| 19 |
function mailerlite_wp_get_groups() |
| 20 |
{ |
| 21 |
|
| 22 |
if ( ! ApiSettings::getInstance()->wpApiKeyExists()) { |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
$groups = array(); |
| 27 |
|
| 28 |
try { |
| 29 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 30 |
|
| 31 |
if ($mailerliteClient->getApiType() === ApiType::INVALID) { |
| 32 |
|
| 33 |
return false; |
| 34 |
} |
| 35 |
|
| 36 |
$results = $mailerliteClient->getGroups(); |
| 37 |
|
| 38 |
if ($results) { |
| 39 |
|
| 40 |
if (is_array($results) && ! isset($results->error->message)) { |
| 41 |
if (sizeof($results) > 0) { |
| 42 |
foreach ($results as $result) { |
| 43 |
$groups[] = (array)$result; |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return $groups; |
| 49 |
} |
| 50 |
|
| 51 |
return false; |
| 52 |
} catch (Exception $e) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
} |
| 56 |
endif; |
| 57 |
|
| 58 |
if ( ! function_exists('mailerlite_wp_get_subscriber_by_email')) : |
| 59 |
/** |
| 60 |
* Get subscriber from API by email |
| 61 |
* |
| 62 |
* @param $email |
| 63 |
* |
| 64 |
* @return mixed |
| 65 |
*/ |
| 66 |
function mailerlite_wp_get_subscriber_by_email($email) |
| 67 |
{ |
| 68 |
|
| 69 |
if ( ! ApiSettings::getInstance()->wpApiKeyExists()) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
if (empty($email)) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
try { |
| 78 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 79 |
|
| 80 |
$subscriber = $mailerliteClient->searchSubscriber($email); |
| 81 |
|
| 82 |
if (isset($subscriber->id)) { |
| 83 |
return $subscriber; |
| 84 |
} else { |
| 85 |
return false; |
| 86 |
} |
| 87 |
} catch (Exception $e) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
} |
| 91 |
endif; |
| 92 |
|
| 93 |
if ( ! function_exists('mailerlite_wp_update_subscriber')) : |
| 94 |
/** |
| 95 |
* Update subscriber via API |
| 96 |
* |
| 97 |
* @param $subscriber_email |
| 98 |
* @param array $subscriber_data |
| 99 |
* |
| 100 |
* @return mixed |
| 101 |
*/ |
| 102 |
function mailerlite_wp_update_subscriber($subscriber_email, $subscriber_data = array()) |
| 103 |
{ |
| 104 |
|
| 105 |
if ( ! ApiSettings::getInstance()->wpApiKeyExists()) { |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
if (empty($subscriber_email)) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
try { |
| 113 |
|
| 114 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 115 |
|
| 116 |
$subscriber_updated = $mailerliteClient->updateSubscriber($subscriber_email, |
| 117 |
$subscriber_data); // returns updated subscriber |
| 118 |
if (isset($subscriber_updated->id)) { |
| 119 |
return $subscriber_updated; |
| 120 |
} else { |
| 121 |
return false; |
| 122 |
} |
| 123 |
} catch (Exception $e) { |
| 124 |
return false; |
| 125 |
} |
| 126 |
} |
| 127 |
endif; |
| 128 |
|
| 129 |
if ( ! function_exists('mailerlite_wp_sync_ecommerce_customer')) : |
| 130 |
function mailerlite_wp_sync_ecommerce_customer($customer_id) |
| 131 |
{ |
| 132 |
|
| 133 |
$shop = get_option('woo_ml_shop_id', false); |
| 134 |
|
| 135 |
if ($shop === false) { |
| 136 |
|
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 141 |
|
| 142 |
if ($mailerliteClient->getApiType() == ApiType::CURRENT) { |
| 143 |
|
| 144 |
$customer = new WC_Customer($customer_id); |
| 145 |
|
| 146 |
$email = $customer->get_email(); |
| 147 |
|
| 148 |
$fields = [ |
| 149 |
'name' => $customer->get_first_name(), |
| 150 |
'last_name' => $customer->get_last_name(), |
| 151 |
'company' => $customer->get_billing_company(), |
| 152 |
'city' => $customer->get_billing_city(), |
| 153 |
'z_i_p' => $customer->get_billing_postcode(), |
| 154 |
'state' => $customer->get_billing_state(), |
| 155 |
'country' => $customer->get_billing_country(), |
| 156 |
'phone' => $customer->get_billing_phone(), |
| 157 |
'orders_count' => $customer->get_order_count(), |
| 158 |
'total_spent' => $customer->get_total_spent(), |
| 159 |
]; |
| 160 |
|
| 161 |
$mailerliteClient->syncCustomer($shop, $customer_id, $email, $fields); |
| 162 |
} |
| 163 |
} |
| 164 |
endif; |
| 165 |
|
| 166 |
if ( ! function_exists('mailerlite_wp_set_double_optin')) : |
| 167 |
/** |
| 168 |
* Set MailerLite double opt in status |
| 169 |
* |
| 170 |
* @param bool $status |
| 171 |
* |
| 172 |
* @return bool |
| 173 |
*/ |
| 174 |
function mailerlite_wp_set_double_optin($status) |
| 175 |
{ |
| 176 |
|
| 177 |
if ( ! ApiSettings::getInstance()->wpApiKeyExists()) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
try { |
| 182 |
|
| 183 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 184 |
|
| 185 |
if ($mailerliteClient->getApiType() === ApiType::CLASSIC) { |
| 186 |
|
| 187 |
$result = $mailerliteClient->setDoubleOptin($status); |
| 188 |
|
| 189 |
if (isset($result->enabled)) { |
| 190 |
$double_optin = $result->enabled == true ? 'yes' : 'no'; |
| 191 |
update_option('double_optin', $double_optin); |
| 192 |
|
| 193 |
return $result->enabled; |
| 194 |
} else { |
| 195 |
return false; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
if ($mailerliteClient->getApiType() === ApiType::CURRENT) { |
| 200 |
|
| 201 |
$doi = $mailerliteClient->getDoubleOptin(); |
| 202 |
|
| 203 |
if ($doi !== $status) { |
| 204 |
$result = $mailerliteClient->setDoubleOptin($status); |
| 205 |
|
| 206 |
$double_optin = $result == true ? 'yes' : 'no'; |
| 207 |
|
| 208 |
update_option('double_optin', $double_optin); |
| 209 |
|
| 210 |
return $result; |
| 211 |
} |
| 212 |
|
| 213 |
return $doi; |
| 214 |
} |
| 215 |
} catch (Exception $e) { |
| 216 |
return false; |
| 217 |
} |
| 218 |
} |
| 219 |
endif; |
| 220 |
|
| 221 |
if ( ! function_exists('mailerlite_wp_create_custom_field')) : |
| 222 |
/** |
| 223 |
* Create custom field in MailerLite |
| 224 |
* |
| 225 |
* @param array $field_data |
| 226 |
* |
| 227 |
* @return bool |
| 228 |
*/ |
| 229 |
function mailerlite_wp_create_custom_field($field_data) |
| 230 |
{ |
| 231 |
|
| 232 |
if ( ! ApiSettings::getInstance()->wpApiKeyExists()) { |
| 233 |
|
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
if ( ! isset($field_data['title']) || ! isset($field_data['type'])) { |
| 238 |
|
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
try { |
| 243 |
|
| 244 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 245 |
|
| 246 |
$temp_name = false; |
| 247 |
|
| 248 |
if (isset($field_data['key'])) { |
| 249 |
|
| 250 |
$temp_name = $field_data['key'] . ' ' . $field_data['title']; |
| 251 |
} |
| 252 |
|
| 253 |
$field_added = $mailerliteClient->createField($temp_name ?: $field_data['title'], $field_data['type']); |
| 254 |
|
| 255 |
if (isset($field_added->id)) { |
| 256 |
|
| 257 |
if ($mailerliteClient->getApiType() === ApiType::CURRENT) { |
| 258 |
|
| 259 |
$mailerliteClient->updateField($field_added->id, $field_data['name'] . ' ' . $field_data['title']); |
| 260 |
} |
| 261 |
|
| 262 |
return $field_added; |
| 263 |
} else { |
| 264 |
return false; |
| 265 |
} |
| 266 |
|
| 267 |
} catch (Exception $e) { |
| 268 |
return false; |
| 269 |
} |
| 270 |
} |
| 271 |
endif; |
| 272 |
|
| 273 |
if ( ! function_exists('mailerlite_wp_get_custom_fields')) : |
| 274 |
/** |
| 275 |
* Get custom fields from MailerLite |
| 276 |
* |
| 277 |
* @param array $args |
| 278 |
* |
| 279 |
* @return mixed |
| 280 |
*/ |
| 281 |
function mailerlite_wp_get_custom_fields($args = array()) |
| 282 |
{ |
| 283 |
|
| 284 |
if ( ! ApiSettings::getInstance()->wpApiKeyExists()) { |
| 285 |
return false; |
| 286 |
} |
| 287 |
|
| 288 |
try { |
| 289 |
|
| 290 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 291 |
|
| 292 |
return $mailerliteClient->getFields(); |
| 293 |
|
| 294 |
} catch (Exception $e) { |
| 295 |
|
| 296 |
return false; |
| 297 |
} |
| 298 |
} |
| 299 |
endif; |