| 1 |
<?php |
| 2 |
|
| 3 |
use MailerLite\Includes\Classes\Data\TrackingData; |
| 4 |
use MailerLite\Includes\Classes\Process\ProductProcess; |
| 5 |
use MailerLite\Includes\Classes\Settings\ApiSettings; |
| 6 |
use MailerLite\Includes\Classes\Settings\ShopSettings; |
| 7 |
use MailerLite\Includes\Shared\Api\ApiType; |
| 8 |
|
| 9 |
/** |
| 10 |
* Integration Demo Integration. |
| 11 |
* |
| 12 |
* @package Woo_Mailerlite_Integration |
| 13 |
* @category Integration |
| 14 |
*/ |
| 15 |
|
| 16 |
if ( ! class_exists('Woo_Mailerlite_Integration')) : |
| 17 |
|
| 18 |
class Woo_Mailerlite_Integration |
| 19 |
{ |
| 20 |
/** |
| 21 |
* MailerLite Settings. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
private $settings; |
| 26 |
|
| 27 |
/** |
| 28 |
* MailerLite alerts. |
| 29 |
* |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
private $alerts; |
| 33 |
|
| 34 |
/** |
| 35 |
* Init and hook in the integration. |
| 36 |
*/ |
| 37 |
public function __construct() |
| 38 |
{ |
| 39 |
$this->settings = get_option('woocommerce_mailerlite_settings'); |
| 40 |
$request = $_REQUEST; |
| 41 |
//making a request only on load of the integrations page |
| 42 |
if (isset($request['page']) && $request['page'] == 'mailerlite') { |
| 43 |
$this->getShopSettingsFromDb(); |
| 44 |
add_action('admin_head', [$this, 'hide_wordpress_thankyou']); |
| 45 |
add_action('mailerlite_alerts', [$this, 'showAlerts'], 10, 1); |
| 46 |
} |
| 47 |
if ((isset($request['page']) && $request['page'] == 'wc-settings') && (isset($request['tab']) && $request['tab'] == 'integration') && (isset($request['section']) && $request['section'] == 'mailerlite')) { |
| 48 |
wp_redirect(admin_url('admin.php?page=mailerlite')); |
| 49 |
exit; |
| 50 |
} |
| 51 |
// Load the settings. |
| 52 |
$this->create_new_initial_segments(); |
| 53 |
|
| 54 |
|
| 55 |
if (get_option('ml_account_authenticated') || $this->get_option('api_status')) { |
| 56 |
if ((get_option('woo_ml_shop_id', false) !== false) && ( ! in_array(get_option('woo_ml_wizard_setup', |
| 57 |
0), [1, 2]))) { |
| 58 |
update_option('woo_ml_wizard_setup', 2); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
add_action('admin_menu', [$this, 'register_mailerlite_submenu_page'], 99); |
| 63 |
// Filters. |
| 64 |
add_filter('woocommerce_settings_api_sanitized_fields_mailerlite', |
| 65 |
[\MailerLite\Includes\Classes\Settings\MailerLiteSettings::getInstance(), 'updateSettings']); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Show alerts. |
| 70 |
* @param $alerts |
| 71 |
* |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function showAlerts($alerts) |
| 75 |
{ |
| 76 |
if(isset($alerts['errors'])) { |
| 77 |
$this->alerts['error'] = $alerts['errors']; |
| 78 |
} |
| 79 |
if(isset($alerts['success'])) { |
| 80 |
$this->alerts['success'] = $alerts['success']; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Register MailerLite settings page |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function register_mailerlite_submenu_page() |
| 89 |
{ |
| 90 |
add_submenu_page('woocommerce', 'MailerLite', 'MailerLite', 'manage_options', 'mailerlite', |
| 91 |
[$this, 'mailerlite_settings_page_callback']); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* MailerLite settings page render |
| 96 |
* @return void |
| 97 |
*/ |
| 98 |
public function mailerlite_settings_page_callback() |
| 99 |
{ |
| 100 |
if (get_transient('ml-admin-notice-invalid-key')) { |
| 101 |
$this->showAlerts([ |
| 102 |
'errors' => get_transient('ml-admin-notice-invalid-key') |
| 103 |
]); |
| 104 |
} |
| 105 |
|
| 106 |
if (get_option('woo_ml_wizard_setup', 0) >= 1) { |
| 107 |
|
| 108 |
$untracked_categories_count = 0; |
| 109 |
$untracked_products_count = 0; |
| 110 |
|
| 111 |
if ((int)get_option('woo_mailerlite_platform', 1) === ApiType::CURRENT) { |
| 112 |
$untracked_categories_count = TrackingData::getInstance()->getUntrackedCategoriesCount(); |
| 113 |
$untracked_products_count = TrackingData::getInstance()->getUntrackedProductsCount(); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
$total_customers_count = TrackingData::getInstance()->getCustomersCount(); |
| 118 |
|
| 119 |
$tracked_customers_count = TrackingData::getInstance()->getTrackedCustomersCount(); |
| 120 |
|
| 121 |
$tracked_guest_count = TrackingData::getInstance()->getTrackedGuestCustomersCount(); |
| 122 |
|
| 123 |
$untracked_customers_count = $total_customers_count - $tracked_customers_count - $tracked_guest_count; |
| 124 |
|
| 125 |
if ($total_customers_count == $untracked_customers_count) { |
| 126 |
// let's use total count including guests |
| 127 |
$untracked_customers_count = TrackingData::getInstance()->getCustomersCount(); |
| 128 |
} |
| 129 |
$tracked_categories_count = 0; |
| 130 |
$tracked_products_count = 0; |
| 131 |
|
| 132 |
if ((int)get_option('woo_mailerlite_platform', 1) === ApiType::CURRENT) { |
| 133 |
$tracked_categories_count = TrackingData::getInstance()->getTrackedCategoriesCount(); |
| 134 |
$tracked_products_count = TrackingData::getInstance()->getTrackedProductCount(); |
| 135 |
} |
| 136 |
$total_tracked = $tracked_categories_count + $tracked_products_count + ($tracked_customers_count + $tracked_guest_count); |
| 137 |
|
| 138 |
$total_untracked_resources = $untracked_categories_count + $untracked_products_count + $untracked_customers_count; |
| 139 |
$this->settings = get_option('woocommerce_mailerlite_settings'); |
| 140 |
|
| 141 |
$totalResourcesToTrack = $total_untracked_resources ?? 0; |
| 142 |
$total_untracked_resources = max($total_untracked_resources, 0); |
| 143 |
} |
| 144 |
echo "<div id='woo_ml_loader'></div>"; |
| 145 |
if (get_option('woo_ml_wizard_setup', 0) == 2) { |
| 146 |
require_once WOO_MAILERLITE_DIR . 'includes/views/app.php'; |
| 147 |
} else { |
| 148 |
$currentStep = get_option('woo_ml_wizard_setup', 0); |
| 149 |
require_once WOO_MAILERLITE_DIR . 'includes/views/wizard.php'; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Hide Wordpress Thank you (footer) |
| 155 |
* @return void |
| 156 |
*/ |
| 157 |
public function hide_wordpress_thankyou() |
| 158 |
{ |
| 159 |
echo '<style type="text/css">#wpfooter {display:none;}</style>'; |
| 160 |
echo '<style type="text/css">.notice {display:none;}</style>'; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Fetch groups from API |
| 165 |
* @return array |
| 166 |
*/ |
| 167 |
public function getGroups() |
| 168 |
{ |
| 169 |
$mailerLiteSettings = \MailerLite\Includes\Classes\Settings\MailerLiteSettings::getInstance(); |
| 170 |
return [ |
| 171 |
'groups' => $mailerLiteSettings->getGroupOptions(), |
| 172 |
'current' => $mailerLiteSettings->getMlOption('group') |
| 173 |
]; |
| 174 |
} |
| 175 |
|
| 176 |
public function getAllCustomers() |
| 177 |
{ |
| 178 |
global $wpdb; |
| 179 |
$query = "SELECT count(DISTINCT email) FROM {$wpdb->prefix}wc_customer_lookup where email IS NOT NULL AND email != '';"; |
| 180 |
return $wpdb->get_var($query); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Verify custom fields |
| 185 |
* @return void |
| 186 |
*/ |
| 187 |
public function verify_custom_fields() |
| 188 |
{ |
| 189 |
if (is_admin() && get_option('ml_account_authenticated')) { |
| 190 |
woo_ml_setup_integration_custom_fields(); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Create new initial segments |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
public function create_new_initial_segments() |
| 199 |
{ |
| 200 |
if ( ! get_option('ml_new_group_segments')) { |
| 201 |
ShopSettings::getInstance()->wpSetConsumerData("....", "....", $this->get_option('group'), |
| 202 |
$this->get_option('resubscribe'), [], true); |
| 203 |
update_option('ml_new_group_segments', true); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Getting groups, selected group, double opt-in and popups |
| 209 |
* settings from MailerLite, only on load of the integrations page. |
| 210 |
*/ |
| 211 |
public function getShopSettingsFromDb() |
| 212 |
{ |
| 213 |
$result = ShopSettings::getInstance()->getShopSettingsFromDb(); |
| 214 |
|
| 215 |
$api_key = get_option('woo_ml_key'); |
| 216 |
if ( ! $api_key) { |
| 217 |
if ( ! empty($this->get_option('api_key'))) { |
| 218 |
update_option('woo_ml_key', $this->get_option('api_key')); |
| 219 |
|
| 220 |
$temp_key = ''; |
| 221 |
if ( ! empty($api_key)) { |
| 222 |
$temp_key = "...." . substr($api_key, -4); |
| 223 |
} |
| 224 |
$this->update_option('api_key', $temp_key); |
| 225 |
} |
| 226 |
} else { |
| 227 |
$temp_key = ''; |
| 228 |
if ( ! empty($api_key)) { |
| 229 |
$temp_key = "...." . substr($api_key, -4); |
| 230 |
} |
| 231 |
$this->update_option('api_key', $temp_key); |
| 232 |
} |
| 233 |
|
| 234 |
if ( ! empty($result) && isset($result->settings)) { |
| 235 |
|
| 236 |
$settings = $result->settings; |
| 237 |
|
| 238 |
$doi = (get_option('double_optin') == true || get_option('double_optin') == 'yes') ? 'yes' : 'no'; |
| 239 |
|
| 240 |
if ((int)get_option('woo_mailerlite_platform', |
| 241 |
1) === ApiType::CLASSIC && $doi !== $settings->double_optin) { |
| 242 |
|
| 243 |
$doi = $settings->double_optin; |
| 244 |
update_option('double_optin', $doi); |
| 245 |
} |
| 246 |
|
| 247 |
$this->update_option('double_optin', $doi); |
| 248 |
|
| 249 |
$additional_sub_fields = (get_option('mailerlite_additional_sub_fields') == true) ? 'yes' : 'no'; |
| 250 |
$this->update_option('additional_sub_fields', $additional_sub_fields); |
| 251 |
|
| 252 |
$disable_checkout_sync = (get_option('mailerlite_disable_checkout_sync') == true) ? 'yes' : 'no'; |
| 253 |
$this->update_option('disable_checkout_sync', $disable_checkout_sync); |
| 254 |
|
| 255 |
$resubscribe = $settings->resubscribe ? 'yes' : 'no'; |
| 256 |
$this->update_option('resubscribe', $resubscribe); |
| 257 |
$this->update_option('group', $settings->group_id); |
| 258 |
update_option('woo_ml_last_manually_tracked_order_id', $settings->last_tracked_order_id); |
| 259 |
$popups_disabled = get_option('mailerlite_popups_disabled'); |
| 260 |
$this->update_option('popups', $popups_disabled ? 'no' : 'yes'); |
| 261 |
$auto_update_enabled = get_option('woo_ml_auto_update'); |
| 262 |
$this->update_option('auto_update_plugin', $auto_update_enabled ? 'yes' : 'no'); |
| 263 |
} elseif (isset($result->active_state)) { |
| 264 |
update_option('ml_shop_not_active', true); |
| 265 |
} |
| 266 |
|
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Update option |
| 271 |
* @param $key |
| 272 |
* @param $value |
| 273 |
* |
| 274 |
* @return bool |
| 275 |
*/ |
| 276 |
public function update_option($key, $value) |
| 277 |
{ |
| 278 |
$this->settings[ $key ] = $value; |
| 279 |
return update_option( 'woocommerce_mailerlite_settings', apply_filters( 'woocommerce_settings_api_sanitized_fields_mailerlite', $this->settings ), 'yes' ); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Get option |
| 284 |
* @param $key |
| 285 |
* |
| 286 |
* @return mixed|string|null |
| 287 |
*/ |
| 288 |
public function get_option($key) |
| 289 |
{ |
| 290 |
return $this->settings[$key] ?? null; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
endif; |