PluginProbe
MailerLite – WooCommerce integration / 2.0.9
MailerLite – WooCommerce integration v2.0.9
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 / classes / settings / ShopSettings.php

ShopSettings.php in MailerLite – WooCommerce integration 2.0.9, at includes/classes/settings/ShopSettings.php

265 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MailerLite\Includes\Classes\Settings;
4
5
6 use MailerLite\Includes\Classes\Process\CheckoutProcess;
7 use MailerLite\Includes\Classes\Singleton;
8 use MailerLite\Includes\Shared\Api\ApiType;
9 use MailerLite\Includes\Shared\Api\PlatformAPI;
10
11 class ShopSettings extends Singleton
12 {
13 /**
14 * Get triggered on deactivate plugin event. Sends store name to api
15 * to toggle its active status
16 * mailerlite_wp_toggle_shop_connection
17 *
18 * @param bool $active_state
19 *
20 * @return bool|void
21 */
22 public function wpToggleShopConnection($active_state)
23 {
24 if (!ApiSettings::getInstance()->wpApiKeyExists()) {
25 return false;
26 }
27
28 try {
29 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
30
31 $store = home_url();
32
33 $shop_id = get_option('woo_ml_shop_id', false);
34
35 if ($mailerliteClient->getApiType() === ApiType::CURRENT && $shop_id === false) {
36
37 return false;
38 }
39
40 $shop_name = get_bloginfo('name');;
41
42 return $mailerliteClient->toggleShop($store, $active_state, $shop_id, $shop_name);
43
44 } catch (\Exception $e) {
45 return false;
46 }
47 }
48
49 /**
50 * Clears ml specific options from the database,
51 * Drops mailerlite_checkouts table,
52 * Sends api request
53 * woo_ml_toggle_shop_connection
54 *
55 * @param Bool $active_status
56 *
57 * @return void
58 */
59 public function toggleShopConnection($active_status)
60 {
61 if (!$active_status) {
62 delete_option('woocommerce_mailerlite_settings');
63 delete_option('ml_account_authenticated');
64 delete_option('double_optin');
65 delete_option('woo_ml_version');
66 delete_option('woo_ml_wizard_setup');
67 delete_option('woo_ml_guests_sync_count');
68 delete_option('woo_ml_shop_id');
69 delete_option('woo_ml_account_name');
70 delete_option('woo_ml_integration_setup');
71 delete_option('woo_ml_last_synced_customer');
72 CheckoutProcess::getInstance()->dropMailerliteCheckoutsTable();
73 if (!function_exists('WC')) {
74 return false;
75 }
76 $this->wpToggleShopConnection($active_status);
77 delete_option('woo_ml_key');
78 } else {
79 CheckoutProcess::getInstance()->createMailerliteCheckoutsTable();
80 update_option('ml_account_authenticated', false);
81 }
82 }
83
84 /**
85 * API call to get all shop settings from the MailerLite side
86 * mailerlite_wp_get_shop_settings_from_db
87 * @return array|bool
88 */
89 public function getShopSettingsFromDb()
90 {
91 if (!ApiSettings::getInstance()->wpApiKeyExists()) {
92 return false;
93 }
94
95 try {
96 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
97
98 if ($mailerliteClient->getApiType() === ApiType::CLASSIC) {
99 $result = $mailerliteClient->getShopSettings(home_url());
100
101 if ($result) {
102
103 if (isset($result->deactivate) && $result->deactivate) {
104 $warning_msg = __('Your shop appears to be deactivated, please save the configuration to re-activate.',
105 'woo-mailerlite');
106
107 add_action('admin_notices', function () use ($warning_msg) {
108
109 printf('<div class="%1$s"><p>%2$s</p></div>',
110 esc_attr('notice notice-warning is-dismissible'), esc_html($warning_msg));
111 });
112 } else {
113 return $result;
114 }
115 } else {
116 return false;
117 }
118 }
119
120 if ($mailerliteClient->getApiType() === ApiType::CURRENT) {
121
122 return true;
123 }
124 } catch (\Exception $e) {
125 return false;
126 }
127 }
128
129 /**
130 * Update ignore product list
131 * woo_ml_update_ignore_product_list
132 * @return boolean
133 */
134 public function updateIgnoreProductList($products)
135 {
136
137 $settings = $this->getShopSettingsFromDb();
138
139 if ($settings !== false) {
140
141 $resubscribe = (MailerLiteSettings::getInstance()->getMlOption('resubscribe', 'no') == 'yes') ? 1 : 0;
142
143 if (isset($settings->settings->resubscribe)) {
144
145 $resubscribe = $settings->settings->resubscribe;
146 }
147 $mailerLiteSettings = MailerLiteSettings::getInstance();
148 $results = $this->wpSetConsumerData(
149 $mailerLiteSettings->getMlOption('consumer_key'),
150 $mailerLiteSettings->getMlOption('consumer_secret'),
151 $mailerLiteSettings->getMlOption('group'),
152 $resubscribe,
153 $products);
154
155 return $results;
156 }
157
158 return false;
159 }
160
161 /**
162 * Sends to api shop data needed to make back and forth connection with woo commerce
163 * Api returns account id and subdomain used to for universal script
164 * mailerlite_wp_set_consumer_data
165 *
166 * @param string $consumerKey
167 * @param string $consumerSecret
168 * @param string $apiKey
169 *
170 * @return array|bool
171 */
172 public function wpSetConsumerData(
173 $consumerKey,
174 $consumerSecret,
175 $group,
176 $resubscribe,
177 $ignoreList = [],
178 $create_segments = false
179 )
180 {
181 if (!ApiSettings::getInstance()->wpApiKeyExists()) {
182 return false;
183 }
184
185 try {
186 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
187
188 $store = home_url();
189 $currency = get_option('woocommerce_currency');
190
191 if (empty($group)) {
192 return ['errors' => 'MailerLite - WooCommerce integration: Please select a group.'];
193 }
194
195 if (strpos($store, 'https://') !== false ) {
196
197 $shop_name = get_bloginfo('name');
198 $shop_id = get_option('woo_ml_shop_id', false);
199 $popups_enabled = !get_option('mailerlite_popups_disabled');
200
201 if (empty($shop_name)) {
202 $shop_name = $store;
203 }
204
205 if ($mailerliteClient->getApiType() === ApiType::CURRENT && $shop_id === false) {
206
207 $shops = $mailerliteClient->getShops();
208
209 foreach ($shops as $shop) {
210
211 if ($shop->url == $store) {
212
213 $shop_id = $shop->id;
214 update_option('woo_ml_shop_id', $shop->id);
215 break;
216 }
217 }
218 }
219
220 $result = $mailerliteClient->setConsumerData($consumerKey, $consumerSecret, $store, $currency, $group,
221 $resubscribe, $ignoreList, $create_segments, $shop_name, $shop_id, $popups_enabled);
222
223 if ($mailerliteClient->getApiType() === ApiType::CLASSIC) {
224
225 if (isset($result->account_id) && (isset($result->account_subdomain))) {
226
227 update_option('account_id', $result->account_id);
228 update_option('account_subdomain', $result->account_subdomain);
229 update_option('new_plugin_enabled', true);
230 update_option('ml_shop_not_active', false);
231 } elseif (isset($result->errors)) {
232 return ['errors' => $result->errors];
233 }
234 }
235 if ($mailerliteClient->getApiType() === ApiType::CURRENT) {
236
237 if (isset($result->id) && ($mailerliteClient->responseCode() === 200 || $mailerliteClient->responseCode() === 201)) {
238
239 update_option('woo_ml_shop_id', $result->id);
240 update_option('new_plugin_enabled', true);
241 update_option('ml_shop_not_active', false);
242 update_option('mailerlite_popups_disabled', $result->enable_popups);
243 } elseif (isset($result->errors)) {
244
245 return ['errors' => $result->errors];
246 } elseif ($result === false) {
247
248 $response = json_decode($mailerliteClient->getResponseBody());
249
250 $message = $response->message ?? 'Unknown error.';
251
252 return ['errors' => $message];
253 }
254 }
255
256 return true;
257 } else {
258 return ['errors' => 'MailerLite - WooCommerce integration: Your shop url does not have the right security protocol'];
259 }
260 } catch (\Exception $e) {
261 return false;
262 }
263 }
264 }
265