PluginProbe
MailerLite – WooCommerce integration / 1.8.8
MailerLite – WooCommerce integration v1.8.8
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 1.8.8, at includes/classes/settings/ShopSettings.php

258 lines 8.3 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('double_optin');
64 delete_option('woo_ml_version');
65 CheckoutProcess::getInstance()->dropMailerliteCheckoutsTable();
66 if (!function_exists('WC')) {
67 return false;
68 }
69 $this->wpToggleShopConnection($active_status);
70 delete_option('woo_ml_key');
71 } else {
72 CheckoutProcess::getInstance()->createMailerliteCheckoutsTable();
73 update_option('ml_account_authenticated', false);
74 }
75 }
76
77 /**
78 * API call to get all shop settings from the MailerLite side
79 * mailerlite_wp_get_shop_settings_from_db
80 * @return array|bool
81 */
82 public function getShopSettingsFromDb()
83 {
84 if (!ApiSettings::getInstance()->wpApiKeyExists()) {
85 return false;
86 }
87
88 try {
89 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
90
91 if ($mailerliteClient->getApiType() === ApiType::CLASSIC) {
92 $result = $mailerliteClient->getShopSettings(home_url());
93
94 if ($result) {
95
96 if (isset($result->deactivate) && $result->deactivate) {
97 $warning_msg = __('Your shop appears to be deactivated, please save the configuration to re-activate.',
98 'woo-mailerlite');
99
100 add_action('admin_notices', function () use ($warning_msg) {
101
102 printf('<div class="%1$s"><p>%2$s</p></div>',
103 esc_attr('notice notice-warning is-dismissible'), esc_html($warning_msg));
104 });
105 } else {
106 return $result;
107 }
108 } else {
109 return false;
110 }
111 }
112
113 if ($mailerliteClient->getApiType() === ApiType::CURRENT) {
114
115 return true;
116 }
117 } catch (\Exception $e) {
118 return false;
119 }
120 }
121
122 /**
123 * Update ignore product list
124 * woo_ml_update_ignore_product_list
125 * @return boolean
126 */
127 public function updateIgnoreProductList($products)
128 {
129
130 $settings = $this->getShopSettingsFromDb();
131
132 if ($settings !== false) {
133
134 $resubscribe = (MailerLiteSettings::getInstance()->getMlOption('resubscribe', 'no') == 'yes') ? 1 : 0;
135
136 if (isset($settings->settings->resubscribe)) {
137
138 $resubscribe = $settings->settings->resubscribe;
139 }
140 $mailerLiteSettings = MailerLiteSettings::getInstance();
141 $results = $this->wpSetConsumerData(
142 $mailerLiteSettings->getMlOption('consumer_key'),
143 $mailerLiteSettings->getMlOption('consumer_secret'),
144 $mailerLiteSettings->getMlOption('group'),
145 $resubscribe,
146 $products);
147
148 return $results;
149 }
150
151 return false;
152 }
153
154 /**
155 * Sends to api shop data needed to make back and forth connection with woo commerce
156 * Api returns account id and subdomain used to for universal script
157 * mailerlite_wp_set_consumer_data
158 *
159 * @param string $consumerKey
160 * @param string $consumerSecret
161 * @param string $apiKey
162 *
163 * @return array|bool
164 */
165 public function wpSetConsumerData(
166 $consumerKey,
167 $consumerSecret,
168 $group,
169 $resubscribe,
170 $ignoreList = [],
171 $create_segments = false
172 )
173 {
174 if (!ApiSettings::getInstance()->wpApiKeyExists()) {
175 return false;
176 }
177
178 try {
179 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
180
181 $store = home_url();
182 $currency = get_option('woocommerce_currency');
183
184 if (empty($group)) {
185 return ['errors' => 'MailerLite - WooCommerce integration: Please select a group.'];
186 }
187
188 if (strpos($store, 'https://') !== false) {
189
190 $shop_name = get_bloginfo('name');
191 $shop_id = get_option('woo_ml_shop_id', false);
192 $popups_enabled = !get_option('mailerlite_popups_disabled');
193
194 if (empty($shop_name)) {
195 $shop_name = $store;
196 }
197
198 if ($mailerliteClient->getApiType() === ApiType::CURRENT && $shop_id === false) {
199
200 $shops = $mailerliteClient->getShops();
201
202 foreach ($shops as $shop) {
203
204 if ($shop->url == $store) {
205
206 $shop_id = $shop->id;
207 update_option('woo_ml_shop_id', $shop->id);
208 break;
209 }
210 }
211 }
212
213 $result = $mailerliteClient->setConsumerData($consumerKey, $consumerSecret, $store, $currency, $group,
214 $resubscribe, $ignoreList, $create_segments, $shop_name, $shop_id, $popups_enabled);
215
216 if ($mailerliteClient->getApiType() === ApiType::CLASSIC) {
217
218 if (isset($result->account_id) && (isset($result->account_subdomain))) {
219
220 update_option('account_id', $result->account_id);
221 update_option('account_subdomain', $result->account_subdomain);
222 update_option('new_plugin_enabled', true);
223 update_option('ml_shop_not_active', false);
224 } elseif (isset($result->errors)) {
225 return ['errors' => $result->errors];
226 }
227 }
228
229 if ($mailerliteClient->getApiType() === ApiType::CURRENT) {
230
231 if (isset($result->id) && ($mailerliteClient->responseCode() === 200 || $mailerliteClient->responseCode() === 201)) {
232
233 update_option('woo_ml_shop_id', $result->id);
234 update_option('new_plugin_enabled', true);
235 update_option('ml_shop_not_active', false);
236 update_option('mailerlite_popups_disabled', $result->enable_popups);
237 } elseif (isset($result->errors)) {
238
239 return ['errors' => $result->errors];
240 } elseif ($result === false) {
241
242 $response = json_decode($mailerliteClient->getResponseBody());
243
244 $message = $response->message ?? 'Unknown error.';
245
246 return ['errors' => $message];
247 }
248 }
249
250 return true;
251 } else {
252 return ['errors' => 'MailerLite - WooCommerce integration: Your shop url does not have the right security protocol'];
253 }
254 } catch (\Exception $e) {
255 return false;
256 }
257 }
258 }