| 1 |
<?php |
| 2 |
|
| 3 |
namespace MailerLite\Includes\Classes\Settings; |
| 4 |
|
| 5 |
use MailerLite\Includes\Classes\Singleton; |
| 6 |
use MailerLite\Includes\Shared\Api\PlatformAPI; |
| 7 |
use MailerLite\Includes\Classes\Data\TrackingData; |
| 8 |
use MailerLite\Includes\Classes\Process\ProductProcess; |
| 9 |
use MailerLite\Includes\Shared\Api\ApiType; |
| 10 |
|
| 11 |
class MailerLiteSettings extends Singleton |
| 12 |
{ |
| 13 |
/** |
| 14 |
* Class instance |
| 15 |
* @var $instance |
| 16 |
*/ |
| 17 |
protected static $instance; |
| 18 |
|
| 19 |
/** |
| 20 |
* Check if checkout action is active |
| 21 |
* woo_ml_is_active |
| 22 |
* @return mixed |
| 23 |
*/ |
| 24 |
public function isActive() |
| 25 |
{ |
| 26 |
$api_status = $this->getMlOption('api_status', false); |
| 27 |
|
| 28 |
return $api_status; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get settings option |
| 33 |
* woo_ml_get_option |
| 34 |
* |
| 35 |
* @param $key |
| 36 |
* @param null $default |
| 37 |
* |
| 38 |
* @return null |
| 39 |
*/ |
| 40 |
public function getMlOption($key, $default = null) |
| 41 |
{ |
| 42 |
$settings = get_option('woocommerce_mailerlite_settings'); |
| 43 |
|
| 44 |
return (isset($settings[$key])) ? $settings[$key] : $default; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* |
| 49 |
* woo_ml_sync_failed |
| 50 |
* @return bool |
| 51 |
*/ |
| 52 |
public function syncFailed() |
| 53 |
{ |
| 54 |
return get_option('woo_ml_resource_sync_failed') || (get_option('woo_ml_sync_active') && ! get_transient('woo_ml_resource_sync_in_progress')); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get settings group options |
| 59 |
* woo_ml_settings_get_group_options |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
public function getGroupOptions() |
| 63 |
{ |
| 64 |
|
| 65 |
if ( ! is_admin()) { |
| 66 |
return []; |
| 67 |
} |
| 68 |
|
| 69 |
$options = array(); |
| 70 |
|
| 71 |
$groups = mailerlite_wp_get_groups(); |
| 72 |
|
| 73 |
if (is_array($groups) && sizeof($groups) > 0) { |
| 74 |
$options[''] = __('Select group', 'woo-mailerlite'); |
| 75 |
foreach ($groups as $group) { |
| 76 |
if (isset($group['id']) && isset($group['name'])) { |
| 77 |
$options[$group['id']] = $group['name']; |
| 78 |
} |
| 79 |
} |
| 80 |
} else { |
| 81 |
$options[''] = __('No groups found', 'woo-mailerlite'); |
| 82 |
} |
| 83 |
|
| 84 |
return $options; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Map array in correct structure for WooCommerce Integration |
| 89 |
* woo_ml_remap_list |
| 90 |
* @return array |
| 91 |
*/ |
| 92 |
public function remapList($products) |
| 93 |
{ |
| 94 |
|
| 95 |
return array_map('strval', array_keys($products)); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Creates the ml_data table and sets the ml_data_table option flag |
| 100 |
* woo_create_mailer_data_table |
| 101 |
*/ |
| 102 |
public function createMailerDataTable() |
| 103 |
{ |
| 104 |
|
| 105 |
global $wpdb; |
| 106 |
|
| 107 |
$charset_collate = $wpdb->get_charset_collate(); |
| 108 |
|
| 109 |
$table = $wpdb->prefix . 'ml_data'; |
| 110 |
|
| 111 |
$sql = "CREATE TABLE $table ( |
| 112 |
data_name varchar(45) NOT NULL, |
| 113 |
data_value text NOT NULL, |
| 114 |
PRIMARY KEY (data_name) |
| 115 |
) $charset_collate;"; |
| 116 |
|
| 117 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 118 |
dbDelta($sql); |
| 119 |
|
| 120 |
update_option('ml_data_table', 1); |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Set WooCommerce MailerLite settings options |
| 126 |
* woo_ml_set_option |
| 127 |
* |
| 128 |
* @param $key |
| 129 |
* @param $value |
| 130 |
* |
| 131 |
* @return bool |
| 132 |
*/ |
| 133 |
public function setOption($key, $value) |
| 134 |
{ |
| 135 |
$settings = get_option('woocommerce_mailerlite_settings'); |
| 136 |
|
| 137 |
if (isset($settings[$key])) { |
| 138 |
$settings[$key] = $value; |
| 139 |
} |
| 140 |
|
| 141 |
update_option('woocommerce_mailerlite_settings', $settings); |
| 142 |
|
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Remove product from ignored |
| 148 |
* woo_ml_remove_ignore_product |
| 149 |
* |
| 150 |
* @param $product_id |
| 151 |
*/ |
| 152 |
public function removeIgnoreProduct($product_id) |
| 153 |
{ |
| 154 |
|
| 155 |
delete_post_meta($product_id, '_woo_ml_product_ignored'); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Mark product as ignored for automation |
| 160 |
* woo_ml_ignore_product |
| 161 |
* |
| 162 |
* @param $product_id |
| 163 |
*/ |
| 164 |
public function ignoreProduct($product_id) |
| 165 |
{ |
| 166 |
|
| 167 |
add_post_meta($product_id, '_woo_ml_product_ignored', true, true); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Mark category as being tracked |
| 172 |
* woo_ml_complete_category_tracking |
| 173 |
* |
| 174 |
* @param $category_id |
| 175 |
* @param $ecommerce_id |
| 176 |
*/ |
| 177 |
public function completeCategoryTracking($category_id, $ecommerce_id) |
| 178 |
{ |
| 179 |
|
| 180 |
add_term_meta($category_id, '_woo_ml_category_tracked', $ecommerce_id, true); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Mark product as being tracked |
| 185 |
* woo_ml_complete_product_tracking |
| 186 |
* |
| 187 |
* @param $product_id |
| 188 |
*/ |
| 189 |
public function completeProductTracking($product_id) |
| 190 |
{ |
| 191 |
|
| 192 |
add_post_meta($product_id, '_woo_ml_product_tracked', true, true); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Check whether product was already tracked or not |
| 197 |
* woo_ml_product_tracking_completed |
| 198 |
* |
| 199 |
* @param $product_id |
| 200 |
* |
| 201 |
* @return bool |
| 202 |
*/ |
| 203 |
public function checkProductTracking($product_id) |
| 204 |
{ |
| 205 |
|
| 206 |
$product_tracked = get_post_meta($product_id, '_woo_ml_product_tracked', true); |
| 207 |
|
| 208 |
return ('1' == $product_tracked) ? true : false; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Get subscriber fields from customer data |
| 213 |
* woo_ml_get_subscriber_fields_from_customer_data |
| 214 |
* |
| 215 |
* @param $customer_data |
| 216 |
* |
| 217 |
* @return array |
| 218 |
*/ |
| 219 |
public function getSubscriberFieldsFromCustomerData($customer_data) |
| 220 |
{ |
| 221 |
|
| 222 |
$subscriber_fields = array(); |
| 223 |
|
| 224 |
if ( ! empty($customer_data['first_name'])) { |
| 225 |
$subscriber_fields['name'] = $customer_data['first_name']; |
| 226 |
} |
| 227 |
|
| 228 |
if ( ! empty($customer_data['last_name'])) { |
| 229 |
$subscriber_fields['last_name'] = $customer_data['last_name']; |
| 230 |
} |
| 231 |
|
| 232 |
if ( ! empty($customer_data['company'])) { |
| 233 |
$subscriber_fields['company'] = $customer_data['company']; |
| 234 |
} |
| 235 |
|
| 236 |
if ( ! empty($customer_data['city'])) { |
| 237 |
$subscriber_fields['city'] = $customer_data['city']; |
| 238 |
} |
| 239 |
|
| 240 |
if ( ! empty($customer_data['postcode'])) { |
| 241 |
$subscriber_fields['zip'] = $customer_data['postcode']; |
| 242 |
} |
| 243 |
|
| 244 |
if ( ! empty($customer_data['state'])) { |
| 245 |
$subscriber_fields['state'] = $customer_data['state']; |
| 246 |
} |
| 247 |
|
| 248 |
if ( ! empty($customer_data['country'])) { |
| 249 |
$subscriber_fields['country'] = $customer_data['country']; |
| 250 |
} |
| 251 |
|
| 252 |
if ( ! empty($customer_data['phone'])) { |
| 253 |
$subscriber_fields['phone'] = $customer_data['phone']; |
| 254 |
} |
| 255 |
|
| 256 |
return $subscriber_fields; |
| 257 |
} |
| 258 |
|
| 259 |
public function createGroup($name) |
| 260 |
{ |
| 261 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 262 |
$response = $mailerliteClient->createGroup($name); |
| 263 |
if ($mailerliteClient->responseCode() === 200 || $mailerliteClient->responseCode() === 201) { |
| 264 |
set_transient('ml-admin-group-created', 'Group created successfully', 5); |
| 265 |
} else { |
| 266 |
set_transient('ml-admin-notice-invalid-key', 'Error: ' . $mailerliteClient->responseCode(), 5); |
| 267 |
} |
| 268 |
return $response; |
| 269 |
} |
| 270 |
|
| 271 |
public function updateSettings($data) |
| 272 |
{ |
| 273 |
$data['resubscribe'] = $data['resubscribe'] ?? 'no'; |
| 274 |
$data['additional_sub_fields'] = $data['additional_sub_fields'] ?? 'no'; |
| 275 |
$data['checkout_preselect'] = $data['checkout_preselect'] ?? 'no'; |
| 276 |
$data['disable_checkout_sync'] = $data['disable_checkout_sync'] ?? 'no'; |
| 277 |
$data['popups'] = $data['popups'] ?? 'no'; |
| 278 |
$data['auto_update_plugin'] = $data['auto_update_plugin'] ?? 'no'; |
| 279 |
$data['double_optin'] = $data['double_optin'] ?? 'no'; |
| 280 |
$data['checkout_hide'] = $data['checkout_hide'] ?? 'no'; |
| 281 |
$data['checkout'] = $data['checkout'] ?? 'no'; |
| 282 |
$data['checkout_label'] = (isset($data['checkout_label']) && ($data['checkout_label'] != '')) ? $data['checkout_label'] : 'Yes, I want to receive your newsletter.'; |
| 283 |
$settings = get_option('woocommerce_mailerlite_settings', null); |
| 284 |
$data['api_key'] = $settings['api_key'] ?? null; |
| 285 |
$data['api_status'] = $settings['api_status'] ?? false; |
| 286 |
|
| 287 |
if ( ! isset($data['group'])) { |
| 288 |
$data['group'] = $settings['group'] ?? ''; |
| 289 |
} |
| 290 |
|
| 291 |
if ( ! isset($data['ignore_product_list'])) { |
| 292 |
$data['ignore_product_list'] = $settings['ignore_product_list'] ?? ''; |
| 293 |
} |
| 294 |
|
| 295 |
$setup_integration = false; |
| 296 |
$revoke_integration_setup = false; |
| 297 |
|
| 298 |
if (isset($data['api_key'])) { |
| 299 |
|
| 300 |
$api_status = $settings['api_status']; |
| 301 |
$api_key = $settings['api_key']; |
| 302 |
|
| 303 |
if (empty($data['api_key'])) { |
| 304 |
|
| 305 |
$api_status = false; |
| 306 |
$revoke_integration_setup = true; |
| 307 |
delete_option('woo_ml_key'); |
| 308 |
delete_option('ml_account_authenticated'); |
| 309 |
} elseif ( ! empty($data['api_key']) && $data['api_key'] != $api_key) { |
| 310 |
$validation = ApiSettings::getInstance()->validateApiKey(esc_html($data['api_key'])); |
| 311 |
$api_status = ($validation); |
| 312 |
|
| 313 |
if ($api_status) { |
| 314 |
$setup_integration = true; |
| 315 |
update_option('woo_ml_key', $data['api_key']); |
| 316 |
} |
| 317 |
$data['api_key'] = "...." . substr($data['api_key'], -4); |
| 318 |
} |
| 319 |
|
| 320 |
// Store API validation |
| 321 |
$data['api_status'] = $api_status; |
| 322 |
|
| 323 |
} |
| 324 |
|
| 325 |
// Handle Double Opt-In |
| 326 |
if (isset($data['double_optin'])) { |
| 327 |
|
| 328 |
if ($data['double_optin'] != $settings['double_optin']) { |
| 329 |
|
| 330 |
$double_optin = ('yes' === $data['double_optin']) ? true : false; |
| 331 |
|
| 332 |
$data['double_optin'] = mailerlite_wp_set_double_optin($double_optin) === true ? 'yes' : 'no'; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
// Handle Additional Subscriber |
| 337 |
if (isset($data['additional_sub_fields'])) { |
| 338 |
$sub_fields_enabled = $data['additional_sub_fields'] === 'yes' ? 1 : 0; |
| 339 |
update_option('mailerlite_additional_sub_fields', $sub_fields_enabled); |
| 340 |
|
| 341 |
if ($sub_fields_enabled) { |
| 342 |
woo_ml_setup_additional_sub_fields(); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
// Handle Do not add subscribers on checkout |
| 347 |
if (isset($data['disable_checkout_sync'])) { |
| 348 |
$disable_checkout_sync = $data['disable_checkout_sync'] === 'yes' ? 1 : 0; |
| 349 |
update_option('mailerlite_disable_checkout_sync', $disable_checkout_sync); |
| 350 |
} |
| 351 |
|
| 352 |
if (isset($data['popups']) && ($data['popups'] !== $settings['popups'])) { |
| 353 |
$popups_disabled = $data['popups'] === 'no' ? 1 : 0; |
| 354 |
update_option('mailerlite_popups_disabled', $popups_disabled); |
| 355 |
} |
| 356 |
|
| 357 |
if (isset($data['auto_update_plugin'])) { |
| 358 |
$auto_update_enabled = $data['auto_update_plugin'] === 'yes' ? 1 : 0; |
| 359 |
update_option('woo_ml_auto_update', $auto_update_enabled); |
| 360 |
} |
| 361 |
|
| 362 |
// save shop to our db for ecommerce tracking |
| 363 |
// hiding the ck and cs values once save performed as we don't need to have them saved here anyway |
| 364 |
// we only need them for backwards connection from classic api to plugin to get products and categories. |
| 365 |
if (( ! empty($data['consumer_key']) && ! empty($data['consumer_secret'])) || (int)get_option('woo_mailerlite_platform', |
| 366 |
1) === ApiType::CURRENT) { |
| 367 |
|
| 368 |
if ((int)get_option('woo_mailerlite_platform', 1) === ApiType::CURRENT) { |
| 369 |
$data['consumer_key'] = ''; |
| 370 |
$data['consumer_secret'] = ''; |
| 371 |
} |
| 372 |
|
| 373 |
$resubscribe = isset($data['resubscribe']) && ($data['resubscribe'] === 'yes') ? 1 : 0; |
| 374 |
|
| 375 |
$result = ShopSettings::getInstance()->wpSetConsumerData( |
| 376 |
$data['consumer_key'], |
| 377 |
$data['consumer_secret'], |
| 378 |
$data['group'], |
| 379 |
$resubscribe, |
| 380 |
$data['ignore_product_list']); |
| 381 |
if (isset($result['errors']) || $result === false) { |
| 382 |
$data['consumer_key'] = ''; |
| 383 |
$data['consumer_secret'] = ''; |
| 384 |
do_action('mailerlite_alerts', $result); |
| 385 |
set_transient('invalid_consumer_keys', $result['errors'], 10); |
| 386 |
} else { |
| 387 |
$data['consumer_key'] = '....' . substr($data['consumer_key'], -4); |
| 388 |
$data['consumer_secret'] = '....' . substr($data['consumer_secret'], -4); |
| 389 |
|
| 390 |
//update product ignore list in ml_data |
| 391 |
$ignore_list = ProductProcess::getInstance()->getIgnoredProductList(); |
| 392 |
|
| 393 |
$products = array_filter($ignore_list, function ($k) use ($data) { |
| 394 |
return in_array($k, $data['ignore_product_list']); |
| 395 |
}, ARRAY_FILTER_USE_KEY); |
| 396 |
|
| 397 |
TrackingData::getInstance()->updateData($products); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
// Handle integration setup |
| 402 |
if ($revoke_integration_setup) { |
| 403 |
woo_ml_revoke_integration_setup(); |
| 404 |
} |
| 405 |
|
| 406 |
if ($setup_integration) { |
| 407 |
woo_ml_setup_integration(); |
| 408 |
} |
| 409 |
do_action('mailerlite_alerts', [ |
| 410 |
'success' => 'Your settings have been saved.' |
| 411 |
]); |
| 412 |
|
| 413 |
return $data; |
| 414 |
} |
| 415 |
|
| 416 |
public function softReset() |
| 417 |
{ |
| 418 |
do { |
| 419 |
$finished = \MailerLite\Includes\Classes\Settings\ResetSettings::getInstance()->resetTrackedResources(true); |
| 420 |
} while (!$finished); |
| 421 |
ShopSettings::getInstance()->toggleShopConnection(0); |
| 422 |
header('Location: ' . admin_url('admin.php?page=mailerlite')); |
| 423 |
} |
| 424 |
} |
| 425 |
|