PluginProbe
MailerLite – WooCommerce integration / 2.0.8
MailerLite – WooCommerce integration v2.0.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 / ResetSettings.php

ResetSettings.php in MailerLite – WooCommerce integration 2.0.8, at includes/classes/settings/ResetSettings.php

163 lines 4.0 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
7 class ResetSettings extends Singleton
8 {
9 /**
10 * Class instance
11 * @var $instance
12 */
13 protected static $instance;
14
15 /**
16 * Resets the tracked resources so that they can be re-synced
17 * woo_ml_reset_tracked_resources
18 */
19 public function resetTrackedResources($synchronous = false)
20 {
21 $finished = $this->processResetTrackedResources();
22 delete_option('woo_ml_guests_sync_count');
23 delete_option('woo_ml_last_synced_guest_id');
24 delete_option('woo_ml_last_synced_customer');
25 if($synchronous) {
26 return $finished;
27 }
28 echo json_encode([
29 'allDone' => $finished
30 ]);
31 // issue !!
32 exit;
33 }
34
35 /**
36 * Actual process to reset tracked resources and recreate shop if needed
37 * woo_ml_reset_tracked_resources_process
38 */
39 public function processResetTrackedResources()
40 {
41 global $wpdb;
42 set_time_limit(1800);
43
44 if ( ! $this->resetTrackedCategories()) {
45
46 return false;
47 }
48
49 if ( ! $this->resetTrackedProducts()) {
50
51 return false;
52 }
53
54 $query = "SELECT DISTINCT
55 (wcl.user_id)
56 FROM
57 {$wpdb->prefix}wc_customer_lookup wcl
58 INNER JOIN {$wpdb->prefix}usermeta wum ON wum.user_id = wcl.user_id
59 WHERE
60 meta_key = '_woo_ml_customer_tracked'
61 LIMIT 100;";
62
63 $customers = $wpdb->get_results($query, 'ARRAY_A');
64
65 if ($customers > 0) {
66
67 foreach ($customers as $customer) {
68 $wc_customer = new \WC_Customer($customer['user_id']);
69
70 if ( ! empty($wc_customer->get_email())) {
71
72 $wc_customer->delete_meta_data('_woo_ml_customer_tracked');
73 $wc_customer->save_meta_data();
74 }
75 }
76 }
77
78 return (count($customers) == 0);
79 }
80
81 /**
82 * Resets the tracked products so that they can be re-synced
83 * woo_ml_reset_tracked_products
84 */
85 public function resetTrackedProducts()
86 {
87 set_time_limit(1800);
88
89 $defaults = array(
90 'post_type' => 'product',
91 'posts_per_page' => 100,
92 'meta_key' => '_woo_ml_product_tracked',
93 'meta_compare' => 'EXISTS'
94 );
95
96 $args = wp_parse_args($defaults);
97 $product_posts_query = new \WP_Query($args);
98
99 $product_posts = [];
100
101 if ($product_posts_query->have_posts()) {
102 $product_posts = $product_posts_query->get_posts();
103 }
104
105 $finished = count($product_posts) == 0;
106
107 foreach ($product_posts as $post) {
108
109 if ( ! isset($post->ID)) {
110
111 continue;
112 }
113
114 delete_post_meta($post->ID, '_woo_ml_product_tracked');
115 }
116
117 return $finished;
118 }
119
120 /**
121 * Resets the tracked categories so that they can be re-synced
122 * woo_ml_reset_tracked_categories
123 */
124 public function resetTrackedCategories()
125 {
126 set_time_limit(1800);
127
128 $term_args = array(
129 'taxonomy' => 'product_cat',
130 'hide_empty' => false,
131 'orderby' => 'none',
132 'meta_key' => '_woo_ml_category_tracked',
133 'meta_compare' => 'EXISTS'
134 );
135
136 $categories = get_terms($term_args);
137
138 $finished = count($categories) == 0;
139
140 foreach ($categories as $category) {
141
142 if ( ! isset($category->term_id)) {
143
144 continue;
145 }
146
147 delete_term_meta($category->term_id, '_woo_ml_category_tracked');
148 }
149
150 return $finished;
151 }
152
153 /**
154 * Reset shop sync on platform change
155 * mailerlite_reset_shop
156 * @return bool|void
157 */
158 public function resetShop()
159 {
160 $this->processResetTrackedResources();
161 }
162
163 }