| 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 |
|
| 9 |
class TrackingData extends Singleton |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Get untracked product categories |
| 13 |
* woo_ml_get_untracked_categories |
| 14 |
* @return array |
| 15 |
*/ |
| 16 |
public function getUntrackedCategories() |
| 17 |
{ |
| 18 |
|
| 19 |
$term_args = array( |
| 20 |
'taxonomy' => 'product_cat', |
| 21 |
'hide_empty' => false, |
| 22 |
'orderby' => 'none', |
| 23 |
'meta_key' => '_woo_ml_category_tracked', |
| 24 |
'meta_compare' => 'NOT EXISTS' |
| 25 |
); |
| 26 |
|
| 27 |
return get_terms($term_args); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Get untracked products |
| 32 |
* woo_ml_get_untracked_products |
| 33 |
* |
| 34 |
* @param array $args |
| 35 |
* |
| 36 |
* @return array |
| 37 |
*/ |
| 38 |
public function getUntrackedProducts($args = array()) |
| 39 |
{ |
| 40 |
|
| 41 |
$defaults = array( |
| 42 |
'post_type' => 'product', |
| 43 |
'posts_per_page' => 100, |
| 44 |
'meta_key' => '_woo_ml_product_tracked', |
| 45 |
'meta_compare' => 'NOT EXISTS' |
| 46 |
); |
| 47 |
|
| 48 |
$args = wp_parse_args($args, $defaults); |
| 49 |
$product_posts_query = new \WP_Query($args); |
| 50 |
|
| 51 |
$products = []; |
| 52 |
|
| 53 |
if ($product_posts_query->have_posts()) { |
| 54 |
$products = $product_posts_query->posts; |
| 55 |
} |
| 56 |
|
| 57 |
return $products; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get tracked product categories count |
| 62 |
* woo_ml_get_tracked_categories_count |
| 63 |
* @return int |
| 64 |
*/ |
| 65 |
public function getTrackedCategoriesCount() |
| 66 |
{ |
| 67 |
|
| 68 |
$term_args = array( |
| 69 |
'taxonomy' => 'product_cat', |
| 70 |
'hide_empty' => false, |
| 71 |
'orderby' => 'none', |
| 72 |
'meta_key' => '_woo_ml_category_tracked', |
| 73 |
'meta_compare' => 'EXISTS' |
| 74 |
); |
| 75 |
|
| 76 |
$categories = get_terms($term_args); |
| 77 |
|
| 78 |
return count($categories); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* |
| 83 |
* woo_ml_count_untracked_products_count |
| 84 |
* @return mixed |
| 85 |
*/ |
| 86 |
public function getUntrackedProductsCount() |
| 87 |
{ |
| 88 |
$defaults = array( |
| 89 |
'post_type' => 'product', |
| 90 |
'posts_per_page' => 1, |
| 91 |
'meta_key' => '_woo_ml_product_tracked', |
| 92 |
'meta_compare' => 'NOT EXISTS' |
| 93 |
); |
| 94 |
|
| 95 |
$args = wp_parse_args($defaults); |
| 96 |
$products_query = new \WP_Query($args); |
| 97 |
|
| 98 |
return $products_query->found_posts; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* |
| 103 |
* woo_ml_get_untracked_customers_count |
| 104 |
* @return mixed |
| 105 |
*/ |
| 106 |
public function getUntrackedCustomersCount() |
| 107 |
{ |
| 108 |
$customer_query = new \WP_User_Query( |
| 109 |
[ |
| 110 |
'fields' => 'ID', |
| 111 |
'role__in' => ['customer', 'administrator'], |
| 112 |
'meta_query' => [ |
| 113 |
'relation' => 'AND', |
| 114 |
[ |
| 115 |
'relation' => 'OR', |
| 116 |
[ |
| 117 |
'key' => 'last_order_date', |
| 118 |
'compare' => 'EXISTS', |
| 119 |
], |
| 120 |
[ |
| 121 |
'key' => 'billing_last_name', |
| 122 |
'compare' => 'EXISTS', |
| 123 |
], |
| 124 |
], |
| 125 |
[ |
| 126 |
'key' => '_woo_ml_customer_tracked', |
| 127 |
'compare' => 'NOT EXISTS' |
| 128 |
] |
| 129 |
], |
| 130 |
'number' => 1, |
| 131 |
] |
| 132 |
); |
| 133 |
|
| 134 |
return $customer_query->get_total(); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* |
| 139 |
* woo_ml_get_tracked_products_count |
| 140 |
* @return int |
| 141 |
*/ |
| 142 |
public function getTrackedProductCount() |
| 143 |
{ |
| 144 |
$defaults = array( |
| 145 |
'post_type' => 'product', |
| 146 |
'posts_per_page' => 100, |
| 147 |
'meta_key' => '_woo_ml_product_tracked', |
| 148 |
'meta_compare' => 'EXISTS' |
| 149 |
); |
| 150 |
|
| 151 |
$args = wp_parse_args($defaults); |
| 152 |
$product_posts_query = new \WP_Query($args); |
| 153 |
|
| 154 |
$product_posts = []; |
| 155 |
|
| 156 |
if ($product_posts_query->have_posts()) { |
| 157 |
$product_posts = $product_posts_query->get_posts(); |
| 158 |
} |
| 159 |
|
| 160 |
return count($product_posts); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* |
| 165 |
* woo_ml_count_untracked_categories_count |
| 166 |
* @return int |
| 167 |
*/ |
| 168 |
public function getUntrackedCategoriesCount() |
| 169 |
{ |
| 170 |
return count($this->getUntrackedCategories()); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* |
| 175 |
* woo_ml_get_tracked_customers_count |
| 176 |
* @return mixed |
| 177 |
*/ |
| 178 |
public function getTrackedCustomersCount() |
| 179 |
{ |
| 180 |
$customer_query = new \WP_User_Query( |
| 181 |
[ |
| 182 |
'fields' => 'ID', |
| 183 |
'role__in' => ['customer', 'administrator'], |
| 184 |
'meta_query' => [ |
| 185 |
'relation' => 'AND', |
| 186 |
[ |
| 187 |
'relation' => 'OR', |
| 188 |
[ |
| 189 |
'key' => 'last_order_date', |
| 190 |
'compare' => 'EXISTS', |
| 191 |
], |
| 192 |
[ |
| 193 |
'key' => 'billing_last_name', |
| 194 |
'compare' => 'EXISTS', |
| 195 |
], |
| 196 |
], |
| 197 |
[ |
| 198 |
'key' => '_woo_ml_customer_tracked', |
| 199 |
'compare' => 'EXISTS' |
| 200 |
] |
| 201 |
], |
| 202 |
'number' => 1, |
| 203 |
] |
| 204 |
); |
| 205 |
|
| 206 |
return $customer_query->get_total(); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* |
| 211 |
* woo_ml_get_untracked_customers |
| 212 |
* @return array|array[] |
| 213 |
*/ |
| 214 |
public function getUntrackedCustomers() |
| 215 |
{ |
| 216 |
|
| 217 |
$customer_query = new \WP_User_Query( |
| 218 |
[ |
| 219 |
'fields' => 'ID', |
| 220 |
'role__in' => ['customer', 'administrator'], |
| 221 |
'meta_query' => [ |
| 222 |
'relation' => 'AND', |
| 223 |
[ |
| 224 |
'relation' => 'OR', |
| 225 |
[ |
| 226 |
'key' => 'last_order_date', |
| 227 |
'compare' => 'EXISTS', |
| 228 |
], |
| 229 |
[ |
| 230 |
'key' => 'billing_last_name', |
| 231 |
'compare' => 'EXISTS', |
| 232 |
], |
| 233 |
], |
| 234 |
[ |
| 235 |
'key' => '_woo_ml_customer_tracked', |
| 236 |
'compare' => 'NOT EXISTS' |
| 237 |
] |
| 238 |
], |
| 239 |
'number' => 100, |
| 240 |
] |
| 241 |
); |
| 242 |
|
| 243 |
return array_map(function ($customer) { |
| 244 |
return [ |
| 245 |
'id' => $customer |
| 246 |
]; |
| 247 |
}, $customer_query->get_results()); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Get settings page url |
| 252 |
* woo_ml_get_settings_page_url |
| 253 |
* @return string |
| 254 |
*/ |
| 255 |
public function getSettingsPageUrl() |
| 256 |
{ |
| 257 |
return admin_url('admin.php?page=wc-settings&tab=integration§ion=mailerlite'); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Get complete integration setup url |
| 262 |
* woo_ml_get_complete_integration_setup_url |
| 263 |
* @return string |
| 264 |
*/ |
| 265 |
public function getCompleteIntegrationSetupUrl() |
| 266 |
{ |
| 267 |
return add_query_arg('woo_ml_action', 'setup_integration', $this->getSettingsPageUrl()); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Update ignore product list in ml_data table |
| 272 |
* woo_ml_update_data |
| 273 |
* @return mixed |
| 274 |
*/ |
| 275 |
public function updateData($products) |
| 276 |
{ |
| 277 |
|
| 278 |
global $wpdb; |
| 279 |
|
| 280 |
$table = $wpdb->prefix . 'ml_data'; |
| 281 |
|
| 282 |
$tableCreated = get_option('ml_data_table'); |
| 283 |
|
| 284 |
if ($tableCreated != 1) { |
| 285 |
|
| 286 |
MailerLiteSettings::getInstance()->createMailerDataTable(); |
| 287 |
} |
| 288 |
|
| 289 |
$updateQuery = $wpdb->prepare(" |
| 290 |
INSERT INTO $table (data_name, data_value) VALUES ('products', %s) ON DUPLICATE KEY UPDATE data_value = %s |
| 291 |
", json_encode($products), json_encode($products)); |
| 292 |
|
| 293 |
return $wpdb->query($updateQuery); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Save ignored products to WooCommerce Integration and ml_data table |
| 298 |
* woo_ml_save_local_ignore_products |
| 299 |
*/ |
| 300 |
public function saveLocalIgnoreProducts($products) |
| 301 |
{ |
| 302 |
|
| 303 |
$ignore_map = MailerLiteSettings::getInstance()->remapList($products); |
| 304 |
|
| 305 |
if (ShopSettings::getInstance()->updateIgnoreProductList($ignore_map) === true) { |
| 306 |
|
| 307 |
// save updated ignore product list to WooCommerce Integration |
| 308 |
$settings = get_option('woocommerce_mailerlite_settings'); |
| 309 |
|
| 310 |
if (!isset($settings['ignore_product_list'])) { |
| 311 |
$settings['ignore_product_list'] = array(); |
| 312 |
} |
| 313 |
|
| 314 |
$settings['ignore_product_list'] = $ignore_map; |
| 315 |
|
| 316 |
update_option('woocommerce_mailerlite_settings', $settings); |
| 317 |
|
| 318 |
//save product ignore list to ml_data |
| 319 |
$this->updateData($products); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Remove product from product ignore list for ml_data |
| 325 |
* woo_ml_remove_product_from_list |
| 326 |
* @return array |
| 327 |
*/ |
| 328 |
public function removeProductFromList($products, $remove_list) |
| 329 |
{ |
| 330 |
|
| 331 |
return array_filter($products, function ($k) use ($remove_list) { |
| 332 |
return !in_array($k, $remove_list); |
| 333 |
}, ARRAY_FILTER_USE_KEY); |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* |
| 338 |
* woo_ml_get_customers_count |
| 339 |
* @return mixed |
| 340 |
*/ |
| 341 |
public function getCustomersCount() |
| 342 |
{ |
| 343 |
$customer_query = new \WP_User_Query( |
| 344 |
[ |
| 345 |
'fields' => 'ID', |
| 346 |
'role__in' => ['customer', 'administrator'], |
| 347 |
'meta_query' => [ |
| 348 |
'relation' => 'OR', |
| 349 |
[ |
| 350 |
'key' => 'last_order_date', |
| 351 |
'compare' => 'EXISTS', |
| 352 |
], |
| 353 |
[ |
| 354 |
'key' => 'billing_last_name', |
| 355 |
'compare' => 'EXISTS', |
| 356 |
], |
| 357 |
], |
| 358 |
'number' => 1, |
| 359 |
] |
| 360 |
); |
| 361 |
|
| 362 |
return $customer_query->get_total(); |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* |
| 367 |
* woo_ml_get_all_customers_count |
| 368 |
* @return int |
| 369 |
* @throws \Exception |
| 370 |
*/ |
| 371 |
public function getAllCustomersCount() |
| 372 |
{ |
| 373 |
$data_store = \WC_Data_Store::load('report-customers'); |
| 374 |
|
| 375 |
$customers = $data_store->get_data([ |
| 376 |
'per_page' => 1, |
| 377 |
'page' => 1, |
| 378 |
'order_before' => null, |
| 379 |
'order_after' => null, |
| 380 |
]); |
| 381 |
|
| 382 |
return $customers->total ?? 0; |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* |
| 387 |
* woo_ml_get_all_customers |
| 388 |
* |
| 389 |
* @param $page |
| 390 |
* |
| 391 |
* @return array |
| 392 |
* @throws \Exception |
| 393 |
*/ |
| 394 |
public function getAllCustomers($page = 1) |
| 395 |
{ |
| 396 |
$data_store = \WC_Data_Store::load('report-customers'); |
| 397 |
|
| 398 |
$customers = $data_store->get_data([ |
| 399 |
'per_page' => 100, |
| 400 |
'page' => $page, |
| 401 |
'order_before' => null, |
| 402 |
'order_after' => null, |
| 403 |
]); |
| 404 |
|
| 405 |
if (isset($customers->pages)) { |
| 406 |
|
| 407 |
if ($customers->pages >= $page) { |
| 408 |
|
| 409 |
WC()->session->set('untracked_customer_page', $page + 1); |
| 410 |
} else { |
| 411 |
|
| 412 |
return []; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
return $customers->data ?? []; |
| 417 |
} |
| 418 |
} |