PluginProbe
MailerLite – WooCommerce integration / 2.0.9
MailerLite – WooCommerce integration v2.0.9
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 / data / TrackingData.php

TrackingData.php in MailerLite – WooCommerce integration 2.0.9, at includes/classes/data/TrackingData.php

373 lines 11.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\Data;
4
5 use MailerLite\Includes\Classes\Settings\MailerLiteSettings;
6 use MailerLite\Includes\Classes\Settings\ShopSettings;
7 use MailerLite\Includes\Classes\Singleton;
8 use Automattic\WooCommerce\Utilities\OrderUtil;
9
10 class TrackingData extends Singleton
11 {
12 private $wpdb;
13
14 private $hposEnabled = false;
15 public function __construct()
16 {
17 global $wpdb;
18
19 $this->wpdb = $wpdb;
20 if (class_exists('OrderUtil') && OrderUtil::custom_orders_table_usage_is_enabled() ) {
21 $this->hposEnabled = true;
22 }
23
24 }
25
26
27 /**
28 * Get untracked product categories
29 * woo_ml_get_untracked_categories
30 * @return array
31 */
32 public function getUntrackedCategories()
33 {
34
35 $term_args = array(
36 'taxonomy' => 'product_cat',
37 'hide_empty' => false,
38 'orderby' => 'none',
39 'meta_key' => '_woo_ml_category_tracked',
40 'meta_compare' => 'NOT EXISTS'
41 );
42
43 return get_terms($term_args);
44 }
45
46 /**
47 * Get untracked products
48 * woo_ml_get_untracked_products
49 *
50 * @param array $args
51 *
52 * @return array
53 */
54 public function getUntrackedProducts($args = array())
55 {
56
57 $defaults = array(
58 'post_type' => 'product',
59 'posts_per_page' => 100,
60 'meta_key' => '_woo_ml_product_tracked',
61 'meta_compare' => 'NOT EXISTS'
62 );
63
64 $args = wp_parse_args($args, $defaults);
65 $product_posts_query = new \WP_Query($args);
66
67 $products = [];
68
69 if ($product_posts_query->have_posts()) {
70 $products = $product_posts_query->posts;
71 }
72
73 return $products;
74 }
75
76 /**
77 * Get tracked product categories count
78 * woo_ml_get_tracked_categories_count
79 * @return int
80 */
81 public function getTrackedCategoriesCount()
82 {
83
84 $term_args = array(
85 'taxonomy' => 'product_cat',
86 'hide_empty' => false,
87 'orderby' => 'none',
88 'meta_key' => '_woo_ml_category_tracked',
89 'meta_compare' => 'EXISTS'
90 );
91
92 $categories = get_terms($term_args);
93
94 return count($categories);
95 }
96
97 /**
98 *
99 * woo_ml_count_untracked_products_count
100 * @return mixed
101 */
102 public function getUntrackedProductsCount()
103 {
104 $defaults = array(
105 'post_type' => 'product',
106 'posts_per_page' => 1,
107 'meta_key' => '_woo_ml_product_tracked',
108 'meta_compare' => 'NOT EXISTS'
109 );
110
111 $args = wp_parse_args($defaults);
112 $products_query = new \WP_Query($args);
113
114 return $products_query->found_posts;
115 }
116
117 /**
118 *
119 * woo_ml_get_untracked_customers_count
120 * @return mixed
121 */
122 public function getUntrackedCustomersCount()
123 {
124 $lastSyncedCustomer = get_option('woo_ml_last_synced_customer', 0);
125 $query = "SELECT
126 count(DISTINCT wcl.customer_id)
127 FROM
128 {$this->wpdb->prefix}wc_customer_lookup wcl
129 INNER JOIN {$this->wpdb->prefix}wc_order_stats wcos ON wcos.customer_id = wcl.customer_id
130 WHERE
131 wcos.status IN('wc-processing', 'wc-completed')
132 AND wcl.email != ''
133 AND wcl.customer_id > {$lastSyncedCustomer};";
134
135 return $this->wpdb->get_var($query);
136 }
137
138 /**
139 *
140 * woo_ml_get_tracked_products_count
141 * @return int
142 */
143 public function getTrackedProductCount()
144 {
145 $defaults = array(
146 'post_type' => 'product',
147 'posts_per_page' => 100,
148 'meta_key' => '_woo_ml_product_tracked',
149 'meta_compare' => 'EXISTS'
150 );
151
152 $args = wp_parse_args($defaults);
153 $product_posts_query = new \WP_Query($args);
154
155 $product_posts = [];
156
157 if ($product_posts_query->have_posts()) {
158 $product_posts = $product_posts_query->get_posts();
159 }
160
161 return count($product_posts);
162 }
163
164 /**
165 *
166 * woo_ml_count_untracked_categories_count
167 * @return int
168 */
169 public function getUntrackedCategoriesCount()
170 {
171 return count($this->getUntrackedCategories());
172 }
173
174 /**
175 * Get settings page url
176 * woo_ml_get_settings_page_url
177 * @return string
178 */
179 public function getSettingsPageUrl()
180 {
181 return admin_url('admin.php?page=wc-settings&tab=integration&section=mailerlite');
182 }
183
184 /**
185 * Get complete integration setup url
186 * woo_ml_get_complete_integration_setup_url
187 * @return string
188 */
189 public function getCompleteIntegrationSetupUrl()
190 {
191 return add_query_arg('woo_ml_action', 'setup_integration', $this->getSettingsPageUrl());
192 }
193
194 /**
195 * Update ignore product list in ml_data table
196 * woo_ml_update_data
197 * @return mixed
198 */
199 public function updateData($products)
200 {
201
202 $table = $this->wpdb->prefix . 'ml_data';
203
204 $tableCreated = get_option('ml_data_table');
205
206 if ($tableCreated != 1) {
207
208 MailerLiteSettings::getInstance()->createMailerDataTable();
209 }
210
211 $updateQuery = $this->wpdb->prepare("
212 INSERT INTO $table (data_name, data_value) VALUES ('products', %s) ON DUPLICATE KEY UPDATE data_value = %s
213 ", json_encode($products), json_encode($products));
214
215 return $this->wpdb->query($updateQuery);
216 }
217
218 /**
219 * Save ignored products to WooCommerce Integration and ml_data table
220 * woo_ml_save_local_ignore_products
221 */
222 public function saveLocalIgnoreProducts($products)
223 {
224
225 $ignore_map = MailerLiteSettings::getInstance()->remapList($products);
226
227 if (ShopSettings::getInstance()->updateIgnoreProductList($ignore_map) === true) {
228
229 // save updated ignore product list to WooCommerce Integration
230 $settings = get_option('woocommerce_mailerlite_settings');
231
232 if (!isset($settings['ignore_product_list'])) {
233 $settings['ignore_product_list'] = array();
234 }
235
236 $settings['ignore_product_list'] = $ignore_map;
237
238 update_option('woocommerce_mailerlite_settings', $settings);
239
240 //save product ignore list to ml_data
241 $this->updateData($products);
242 }
243 }
244
245 /**
246 * Remove product from product ignore list for ml_data
247 * woo_ml_remove_product_from_list
248 * @return array
249 */
250 public function removeProductFromList($products, $remove_list)
251 {
252
253 return array_filter($products, function ($k) use ($remove_list) {
254 return !in_array($k, $remove_list);
255 }, ARRAY_FILTER_USE_KEY);
256 }
257
258 /**
259 *
260 * woo_ml_get_customers_count
261 * @return mixed
262 */
263 public function getCustomersCount()
264 {
265
266 $query = "SELECT
267 count(DISTINCT wcl.customer_id)
268 FROM
269 {$this->wpdb->prefix}wc_customer_lookup wcl
270 INNER JOIN {$this->wpdb->prefix}wc_order_stats wcos on wcos.customer_id = wcl.customer_id
271 WHERE wcl.email != ''
272 AND wcos.status IN ('wc-processing', 'wc-completed');";
273 return $this->wpdb->get_var($query);
274 }
275
276 public function getCustomersToSync($lastTrackedCustomer = 0, $limit = 100)
277 {
278
279 if($this->hposEnabled) {
280 return $this->getCustomersToSyncToSyncForHpos($lastTrackedCustomer, $limit);
281 }
282 $query = "SELECT
283 wcos.customer_id AS resource_id,
284 wcl.email,
285 CASE WHEN (
286 SELECT
287 wpm.meta_value
288 FROM
289 {$this->wpdb->prefix}postmeta wpm
290 WHERE
291 wpm.meta_key = '_woo_ml_subscribe'
292 AND wpm.post_id = max(wcos.order_id)
293 LIMIT 1) THEN
294 TRUE
295 ELSE
296 FALSE
297 END AS create_subscriber,
298 count(DISTINCT (wcos.order_id)) AS orders_count,
299 sum(DISTINCT (wcos.total_sales)) AS total_spent,
300 wcl.first_name AS name,
301 wcl.last_name AS last_name,
302 wcl.city AS city,
303 wcl.state AS state,
304 wcl.country AS country,
305 wcl.postcode AS postcode,
306 max(wcos.order_id) AS last_order_id,
307 max(wcos.date_created) AS last_order
308 FROM
309 {$this->wpdb->prefix}wc_customer_lookup wcl
310 INNER JOIN {$this->wpdb->prefix}wc_order_stats wcos ON wcos.customer_id = wcl.customer_id
311 WHERE
312 wcos.status IN('wc-processing', 'wc-completed')
313 AND wcl.email != ''
314 AND wcos.customer_id > {$lastTrackedCustomer}
315 GROUP BY
316 wcos.customer_id,
317 email
318 ORDER BY
319 resource_id
320 LIMIT {$limit};";
321
322 return $this->wpdb->get_results($query, 'ARRAY_A');
323 }
324
325
326 public function getCustomersToSyncToSyncForHpos($lastTrackedCustomer, $limit)
327 {
328 $query = "SELECT
329 wcos.customer_id AS resource_id,
330 wcl.email,
331 CASE WHEN (
332 SELECT
333 wca.meta_value
334 FROM
335 {$this->wpdb->prefix}wc_orders_meta wca
336 WHERE
337 wca.meta_key = '_woo_ml_subscribe'
338 AND wca.order_id = max(wcos.order_id)
339 LIMIT 1) THEN
340 TRUE
341 ELSE
342 FALSE
343 END AS create_subscriber,
344 count(DISTINCT (wcos.order_id)) AS orders_count,
345 sum(DISTINCT (wcos.total_sales)) AS total_spent,
346 wcl.first_name AS name,
347 wcl.last_name AS last_name,
348 min(wca.company) AS company,
349 wcl.city AS city,
350 wcl.state AS state,
351 wcl.country AS country,
352 min(wca.phone) AS phone,
353 wcl.postcode AS postcode,
354 max(wcos.order_id) AS last_order_id,
355 max(wcos.date_created) AS last_order
356 FROM
357 {$this->wpdb->prefix}wc_orders_meta AS wpm
358 INNER JOIN {$this->wpdb->prefix}wc_order_stats wcos ON wcos.order_id = wpm.order_id
359 INNER JOIN {$this->wpdb->prefix}wc_customer_lookup wcl ON wcl.customer_id = wcos.customer_id
360 INNER JOIN {$this->wpdb->prefix}wc_order_addresses wca ON wca.order_id = wpm.order_id
361 WHERE
362 wcos.status IN('wc-processing', 'wc-completed')
363 AND wca.address_type = 'billing'
364 AND wcos.customer_id > {$lastTrackedCustomer}
365 GROUP BY
366 wcos.customer_id,
367 email
368 ORDER BY
369 resource_id
370 LIMIT {$limit};";
371 return $this->wpdb->get_results($query, 'ARRAY_A');
372 }
373 }