PluginProbe
MailerLite – WooCommerce integration / 2.0.7
MailerLite – WooCommerce integration v2.0.7
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 / integration-setup-functions.php

integration-setup-functions.php in MailerLite – WooCommerce integration 2.0.7, at includes/integration-setup-functions.php

233 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use MailerLite\Includes\Classes\Settings\ApiSettings;
4 use MailerLite\Includes\Shared\Api\PlatformAPI;
5 use MailerLite\Includes\Shared\Api\ApiType;
6
7 /**
8 * Check if order tracking setup was finished
9 */
10 function woo_ml_integration_setup_completed()
11 {
12
13 $integration_setup = get_option('woo_ml_integration_setup', false);
14
15 return ('1' == $integration_setup) ? true : false;
16 }
17
18 /**
19 * Mark order tracking setup as completed
20 */
21 function woo_ml_complete_integration_setup()
22 {
23 add_option('woo_ml_integration_setup', true);
24 }
25
26 /**
27 * Revoke order tracking setup completion
28 */
29 function woo_ml_revoke_integration_setup()
30 {
31 delete_option('woo_ml_integration_setup');
32 }
33
34 /**
35 * Setup MailerLite integration
36 */
37 function woo_ml_setup_integration()
38 {
39 $setup_custom_fields = woo_ml_setup_integration_custom_fields();
40 $pluginSettings = get_option('woocommerce_mailerlite_settings');
41 $apiStatus = $pluginSettings['api_status'] ?? false;
42 if ($setup_custom_fields) {
43 woo_ml_complete_integration_setup();
44 } else if (get_option('ml_account_authenticated') || $apiStatus) {
45 if ((get_option('woo_ml_shop_id', false) !== false) && (get_option('woo_ml_wizard_setup', 0) == 2)) {
46 woo_ml_complete_integration_setup();
47 }
48 }
49 }
50
51 /**
52 * Setup Integration Custom Fields
53 *
54 * - Get existing custom fields via API
55 * - Check if our custom fields were already created
56 * - Create missing custom fields
57 *
58 * @return bool
59 */
60 function woo_ml_setup_integration_custom_fields($fields = null)
61 {
62
63 $api_type = (int)get_option('woo_mailerlite_platform', 1);
64
65 $ml_fields = mailerlite_wp_get_custom_fields();
66
67 if ( ! $fields) {
68
69 $fields = woo_ml_get_integration_custom_fields($api_type);
70 }
71
72 if (is_array($ml_fields)) {
73
74 foreach ($ml_fields as $ml_field) {
75
76 $ml_field = (array)$ml_field;
77
78 if (isset($ml_field['key']) && isset($fields[$ml_field['key']])) {
79
80 unset($fields[$ml_field['key']]);
81 }
82 }
83
84 if (sizeof($fields) > 0) {
85 foreach ($fields as $field_data) {
86
87 mailerlite_wp_create_custom_field($field_data);
88 }
89 }
90 }
91
92 return true;
93 }
94
95 /**
96 * Get integration custom fields
97 *
98 * @return array
99 */
100 function woo_ml_get_integration_custom_fields($api_type)
101 {
102
103 if ($api_type !== ApiType::CURRENT) {
104
105 return [
106 'woo_orders_count' => [
107 'title' => 'Woo Orders Count',
108 'type' => 'NUMBER'
109 ],
110 'woo_total_spent' => [
111 'title' => 'Woo Total Spent',
112 'type' => 'NUMBER'
113 ],
114 'woo_last_order' => [
115 'title' => 'Woo Last Order',
116 'type' => 'DATE'
117 ],
118 'woo_last_order_id' => [
119 'title' => 'Woo Last Order ID',
120 'type' => 'NUMBER'
121 ],
122 ];
123 } else {
124
125 $shopUrl = home_url();
126 $shopKey = preg_replace('/[^A-Za-z0-9 ]/', '', $shopUrl);
127
128 $shop_name = get_bloginfo('name');
129
130 if (empty($shop_name)) {
131 $shop_name = $shopUrl;
132 }
133
134 return [
135 $shopKey . '_total_spent' => [
136 'key' => $shopKey,
137 'name' => $shop_name,
138 'title' => 'Total spent',
139 'type' => 'number',
140 ],
141 $shopKey . '_orders_count' => [
142 'key' => $shopKey,
143 'name' => $shop_name,
144 'title' => 'Orders count',
145 'type' => 'number',
146 ],
147 $shopKey . '_accepts_marketing' => [
148 'key' => $shopKey,
149 'name' => $shop_name,
150 'title' => 'Accepts marketing',
151 'type' => 'number',
152 ],
153 ];
154 }
155 }
156
157 function woo_ml_old_integration()
158 {
159 return ! get_option('new_plugin_enabled');
160 }
161
162 function woo_ml_shop_not_active()
163 {
164 return get_option('ml_shop_not_active');
165 }
166
167 /**
168 * Get subscriber additional fields
169 *
170 * @return array
171 */
172 function woo_ml_get_additional_fields()
173 {
174
175 return [
176 'subscriber_language' => [
177 'title' => 'Subscriber Language',
178 'type' => 'text',
179 ],
180 ];
181 }
182
183 function woo_ml_setup_additional_sub_fields()
184 {
185 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
186
187 $ml_fields = $mailerliteClient->getFields();
188
189 $sub_fields = woo_ml_get_additional_fields();
190
191 if (is_array($ml_fields)) {
192
193 foreach ($ml_fields as $ml_field) {
194
195 $ml_field = (array)$ml_field;
196
197 if (isset($ml_field['key']) && isset($sub_fields[$ml_field['key']])) {
198
199 unset($sub_fields[$ml_field['key']]);
200 }
201 }
202
203 if (sizeof($sub_fields) > 0) {
204 foreach ($sub_fields as $field_data) {
205
206 woo_ml_create_additional_sub_fields($field_data);
207 }
208 }
209 }
210 }
211
212 function woo_ml_create_additional_sub_fields($field_data)
213 {
214 if ( ! ApiSettings::getInstance()->wpApiKeyExists()) {
215
216 return false;
217 }
218
219 if ( ! isset($field_data['title']) || ! isset($field_data['type'])) {
220
221 return false;
222 }
223
224 try {
225
226 $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY);
227
228 return $mailerliteClient->createField($field_data['title'], $field_data['type']);
229 } catch (Exception $e) {
230 return false;
231 }
232 }
233