PluginProbe
MailerLite – WooCommerce integration / 2.0.7
MailerLite – WooCommerce integration v2.0.7
3.1.27 3.1.25 3.1.26 3.1.24 3.1.23 3.1.22 3.1.21 3.1.20 3.1.19 3.1.18 3.1.17 3.1.16 3.1.15 1.8.7 1.8.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 All 148 releases
woo-mailerlite / includes / class.woo-mailerlite-integration.php

class.woo-mailerlite-integration.php in MailerLite – WooCommerce integration 2.0.7, at includes/class.woo-mailerlite-integration.php

276 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $untracked_customers_count = TrackingData::getInstance()->getUntrackedCustomersCount();
117
118 $tracked_categories_count = 0;
119 $tracked_products_count = 0;
120
121 if ((int)get_option('woo_mailerlite_platform', 1) === ApiType::CURRENT) {
122 $tracked_categories_count = TrackingData::getInstance()->getTrackedCategoriesCount();
123 $tracked_products_count = TrackingData::getInstance()->getTrackedProductCount();
124 }
125 $total_tracked = TrackingData::getInstance()->getCustomersCount() - $untracked_customers_count;
126
127 $total_untracked_resources = $untracked_categories_count + $untracked_products_count + $untracked_customers_count;
128 $this->settings = get_option('woocommerce_mailerlite_settings');
129
130 $totalResourcesToTrack = $total_untracked_resources ?? 0;
131 $total_untracked_resources = max($total_untracked_resources, 0);
132 }
133 echo "<div id='woo_ml_loader'></div>";
134 if (get_option('woo_ml_wizard_setup', 0) == 2) {
135 if ( ! woo_ml_integration_setup_completed()) {
136 woo_ml_setup_integration();
137 }
138 require_once WOO_MAILERLITE_DIR . 'includes/views/app.php';
139 } else {
140 $currentStep = get_option('woo_ml_wizard_setup', 0);
141 require_once WOO_MAILERLITE_DIR . 'includes/views/wizard.php';
142 }
143 }
144
145 /**
146 * Hide Wordpress Thank you (footer)
147 * @return void
148 */
149 public function hide_wordpress_thankyou()
150 {
151 echo '<style type="text/css">#wpfooter {display:none;}</style>';
152 echo '<style type="text/css">.notice {display:none;}</style>';
153 }
154
155 /**
156 * Fetch groups from API
157 * @return array
158 */
159 public function getGroups()
160 {
161 $mailerLiteSettings = \MailerLite\Includes\Classes\Settings\MailerLiteSettings::getInstance();
162 return [
163 'groups' => $mailerLiteSettings->getGroupOptions(),
164 'current' => $mailerLiteSettings->getMlOption('group')
165 ];
166 }
167
168 public function getAllCustomers()
169 {
170 global $wpdb;
171 $query = "SELECT count(DISTINCT email) FROM {$wpdb->prefix}wc_customer_lookup where email IS NOT NULL AND email != '';";
172 return $wpdb->get_var($query);
173 }
174
175
176 /**
177 * Create new initial segments
178 * @return void
179 */
180 public function create_new_initial_segments()
181 {
182 if ( ! get_option('ml_new_group_segments')) {
183 ShopSettings::getInstance()->wpSetConsumerData("....", "....", $this->get_option('group'),
184 $this->get_option('resubscribe'), [], true);
185 update_option('ml_new_group_segments', true);
186 }
187 }
188
189 /**
190 * Getting groups, selected group, double opt-in and popups
191 * settings from MailerLite, only on load of the integrations page.
192 */
193 public function getShopSettingsFromDb()
194 {
195 $result = ShopSettings::getInstance()->getShopSettingsFromDb();
196
197 $api_key = get_option('woo_ml_key');
198 if ( ! $api_key) {
199 if ( ! empty($this->get_option('api_key'))) {
200 update_option('woo_ml_key', $this->get_option('api_key'));
201
202 $temp_key = '';
203 if ( ! empty($api_key)) {
204 $temp_key = "...." . substr($api_key, -4);
205 }
206 $this->update_option('api_key', $temp_key);
207 }
208 } else {
209 $temp_key = '';
210 if ( ! empty($api_key)) {
211 $temp_key = "...." . substr($api_key, -4);
212 }
213 $this->update_option('api_key', $temp_key);
214 }
215
216 if ( ! empty($result) && isset($result->settings)) {
217
218 $settings = $result->settings;
219
220 $doi = (get_option('double_optin') == true || get_option('double_optin') == 'yes') ? 'yes' : 'no';
221
222 if ((int)get_option('woo_mailerlite_platform',
223 1) === ApiType::CLASSIC && $doi !== $settings->double_optin) {
224
225 $doi = $settings->double_optin;
226 update_option('double_optin', $doi);
227 }
228
229 $this->update_option('double_optin', $doi);
230
231 $additional_sub_fields = (get_option('mailerlite_additional_sub_fields') == true) ? 'yes' : 'no';
232 $this->update_option('additional_sub_fields', $additional_sub_fields);
233
234 $disable_checkout_sync = (get_option('mailerlite_disable_checkout_sync') == true) ? 'yes' : 'no';
235 $this->update_option('disable_checkout_sync', $disable_checkout_sync);
236
237 $resubscribe = $settings->resubscribe ? 'yes' : 'no';
238 $this->update_option('resubscribe', $resubscribe);
239 $this->update_option('group', $settings->group_id);
240 update_option('woo_ml_last_manually_tracked_order_id', $settings->last_tracked_order_id);
241 $popups_disabled = get_option('mailerlite_popups_disabled');
242 $this->update_option('popups', $popups_disabled ? 'no' : 'yes');
243 $auto_update_enabled = get_option('woo_ml_auto_update');
244 $this->update_option('auto_update_plugin', $auto_update_enabled ? 'yes' : 'no');
245 } elseif (isset($result->active_state)) {
246 update_option('ml_shop_not_active', true);
247 }
248
249 }
250
251 /**
252 * Update option
253 * @param $key
254 * @param $value
255 *
256 * @return bool
257 */
258 public function update_option($key, $value)
259 {
260 $this->settings[ $key ] = $value;
261 return update_option( 'woocommerce_mailerlite_settings', apply_filters( 'woocommerce_settings_api_sanitized_fields_mailerlite', $this->settings ), 'yes' );
262 }
263
264 /**
265 * Get option
266 * @param $key
267 *
268 * @return mixed|string|null
269 */
270 public function get_option($key)
271 {
272 return $this->settings[$key] ?? null;
273 }
274 }
275
276 endif;