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 / ApiSettings.php

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

174 lines 5.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 use MailerLite\Includes\Classes\Singleton;
6 use MailerLite\Includes\Shared\Api\ApiType;
7 use MailerLite\Includes\Shared\Api\PlatformAPI;
8
9 class ApiSettings extends Singleton
10 {
11
12 /**
13 * Get settings api key status
14 * woo_ml_settings_get_api_key_status
15 * @return string
16 */
17 public function getApiKeyStatus()
18 {
19
20 $api_status = MailerLiteSettings::getInstance()->getMlOption('api_status', false);
21
22 return ($api_status) ? '<span style="color: green;">' . __('Valid',
23 'woo-mailerlite') . '</span>' : '<span style="color: red;">' . __('Invalid',
24 'woo-mailerlite') . '</span>';
25 }
26
27 /**
28 * Validate given API key
29 * woo_ml_validate_api_key
30 *
31 * @param $api_key
32 *
33 * @return bool
34 */
35 public function validateApiKey($api_key)
36 {
37 if (empty($api_key)) {
38 return false;
39 }
40
41 return $this->apiKeyValidation($api_key);
42 }
43
44 /**
45 * Check MailerLite API connection
46 * mailerlite_wp_api_key_validation
47 *
48 * @param $api_key
49 *
50 * @return bool
51 */
52 public function apiKeyValidation($api_key)
53 {
54
55 if (empty($api_key)) {
56 return false;
57 }
58
59 try {
60 $resetSettings = ResetSettings::getInstance();
61 // delete cached options preventing some sites to update settings
62 wp_cache_delete('alloptions', 'options');
63
64 $mailerliteClient = new PlatformAPI($api_key);
65 $result = $mailerliteClient->validateAccount();
66
67 if (isset($result) && ! isset($result->errors) && $mailerliteClient->responseCode() !== 401 && $result !== false) {
68 $settings = get_option('woocommerce_mailerlite_settings');
69 $settings['api_key'] = $api_key;
70 $settings['api_status'] = true;
71
72 update_option('ml_account_authenticated', true, false);
73 update_option('woo_ml_key', $api_key, false);
74 update_option('woo_ml_wizard_setup', 1, false);
75 if ($mailerliteClient->getApiType() === ApiType::CLASSIC) {
76 $settings['double_optin'] = $result->double_optin;
77 $settings['consumer_key'] = '';
78 $settings['consumer_secret'] = '';
79 update_option('double_optin', $result->double_optin);
80 }
81
82 if ((int)get_option('woo_mailerlite_platform', 1) !== $mailerliteClient->getApiType()) {
83 $resetSettings->resetShop();
84 }
85
86 $shop_id = get_option('woo_ml_shop_id', false);
87
88 if ($shop_id !== false) {
89
90 $verify_shop = $mailerliteClient->getShop($shop_id);
91
92 if ($verify_shop === false) {
93
94 $resetSettings->resetShop();
95
96 delete_option('woo_ml_shop_id');
97 }
98 }
99
100 update_option('woo_mailerlite_platform', $mailerliteClient->getApiType());
101
102 if ($mailerliteClient->getApiType() === ApiType::CURRENT) {
103
104 update_option('account_id', $result->id);
105 update_option('account_subdomain', '');
106
107 $settings['double_optin'] = $result->double_optin ? 'yes' : 'no';
108 update_option('double_optin', $result->double_optin ? 'yes' : 'no');
109
110 if (isset($result->name)) {
111 update_option('woo_ml_account_name', $result->name);
112 }
113 } else if ($mailerliteClient->getApiType() === ApiType::CLASSIC) {
114 $accountDetails = $mailerliteClient->getAccountDetails();
115 if(isset($accountDetails->account)) {
116 update_option('woo_ml_account_name', $accountDetails->account->name ?? '');
117 }
118 }
119
120 $initAdditionalSettings = [
121 'resubscribe' => '',
122 'checkout' => '',
123 'checkout_position' => '',
124 'checkout_preselect' => '',
125 'checkout_hide' => '',
126 'checkout_label' => '',
127 'additional_sub_fields' => '',
128 'disable_checkout_sync' => '',
129 'popups' => '',
130 'ignore_product_list' => '',
131 'auto_update_plugin' => ''
132 ];
133 $settings = array_merge($settings, $initAdditionalSettings);
134
135 update_option('woocommerce_mailerlite_settings', $settings, false);
136
137 return true;
138 } else {
139
140 switch ($mailerliteClient->responseCode()) {
141 case 401 :
142 set_transient('ml-admin-notice-invalid-key', 'Invalid API Key. Please enter a correct one and click Connect account again.', 5);
143 break;
144 case 0 :
145 set_transient('ml-admin-notice-invalid-key', $mailerliteClient->getResponseBody(), 5);
146 break;
147 default:
148 set_transient('ml-admin-notice-invalid-key', 'Error: ' . $mailerliteClient->responseCode(), 5);
149 }
150 }
151
152 } catch (\Exception $e) {
153 return false;
154 }
155
156 return false;
157 }
158
159 /**
160 * Check wether API key exists or not
161 * mailerlite_wp_api_key_exists
162 * @return bool
163 */
164 public function wpApiKeyExists()
165 {
166 if (defined('MAILERLITE_WP_API_KEY') && ! empty(MAILERLITE_WP_API_KEY)) {
167 return true;
168 }
169
170 return false;
171 }
172
173 }
174