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

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

147 lines 4.4 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
75 if ($mailerliteClient->getApiType() === ApiType::CLASSIC) {
76 $settings['double_optin'] = $result->double_optin;
77 update_option('double_optin', $result->double_optin);
78 }
79
80 if ((int)get_option('woo_mailerlite_platform', 1) !== $mailerliteClient->getApiType()) {
81 $resetSettings->resetShop();
82 }
83
84 $shop_id = get_option('woo_ml_shop_id', false);
85
86 if ($shop_id !== false) {
87
88 $verify_shop = $mailerliteClient->getShop($shop_id);
89
90 if ($verify_shop === false) {
91
92 $resetSettings->resetShop();
93
94 delete_option('woo_ml_shop_id');
95 }
96 }
97
98 update_option('woo_mailerlite_platform', $mailerliteClient->getApiType());
99
100 if ($mailerliteClient->getApiType() === ApiType::CURRENT) {
101
102 update_option('account_id', $result->id);
103 update_option('account_subdomain', '');
104
105 $settings['double_optin'] = $result->double_optin ? 'yes' : 'no';
106 update_option('double_optin', $result->double_optin ? 'yes' : 'no');
107 }
108
109 update_option('woocommerce_mailerlite_settings', $settings, false);
110
111 return true;
112 } else {
113
114 switch ($mailerliteClient->responseCode()) {
115 case 401 :
116 set_transient('ml-admin-notice-invalid-key', 'Invalid API Key', 5);
117 break;
118 case 0 :
119 set_transient('ml-admin-notice-invalid-key', $mailerliteClient->getResponseBody(), 5);
120 break;
121 default:
122 set_transient('ml-admin-notice-invalid-key', 'Error: ' . $mailerliteClient->responseCode(), 5);
123 }
124 }
125
126 } catch (\Exception $e) {
127 return false;
128 }
129
130 return false;
131 }
132
133 /**
134 * Check wether API key exists or not
135 * mailerlite_wp_api_key_exists
136 * @return bool
137 */
138 public function wpApiKeyExists()
139 {
140 if (defined('MAILERLITE_WP_API_KEY') && ! empty(MAILERLITE_WP_API_KEY)) {
141 return true;
142 }
143
144 return false;
145 }
146
147 }