PluginProbe
MailerLite – WooCommerce integration / 2.0
MailerLite – WooCommerce integration v2.0
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 2.1.0 All 147 releases
woo-mailerlite / includes / classes / settings / ShopSettings.php

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

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