| 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() |
| 20 |
{ |
| 21 |
|
| 22 |
$finished = $this->processResetTrackedResources(); |
| 23 |
delete_option('woo_ml_guests_sync_count'); |
| 24 |
echo json_encode([ |
| 25 |
'allDone' => $finished |
| 26 |
]); |
| 27 |
// issue !! |
| 28 |
exit; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Actual process to reset tracked resources and recreate shop if needed |
| 33 |
* woo_ml_reset_tracked_resources_process |
| 34 |
*/ |
| 35 |
public function processResetTrackedResources() |
| 36 |
{ |
| 37 |
|
| 38 |
set_time_limit(1800); |
| 39 |
|
| 40 |
if ( ! $this->resetTrackedCategories()) { |
| 41 |
|
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
if ( ! $this->resetTrackedProducts()) { |
| 46 |
|
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
$customer_query = new \WP_User_Query( |
| 51 |
[ |
| 52 |
'fields' => 'ID', |
| 53 |
'role__in' => ['customer', 'administrator'], |
| 54 |
'meta_query' => [ |
| 55 |
'relation' => 'AND', |
| 56 |
[ |
| 57 |
'relation' => 'OR', |
| 58 |
[ |
| 59 |
'key' => 'last_order_date', |
| 60 |
'compare' => 'EXISTS', |
| 61 |
], |
| 62 |
[ |
| 63 |
'key' => 'billing_last_name', |
| 64 |
'compare' => 'EXISTS', |
| 65 |
], |
| 66 |
], |
| 67 |
[ |
| 68 |
'key' => '_woo_ml_customer_tracked', |
| 69 |
'compare' => 'EXISTS' |
| 70 |
] |
| 71 |
], |
| 72 |
'number' => 100, |
| 73 |
] |
| 74 |
); |
| 75 |
|
| 76 |
$customers = $customer_query->get_results(); |
| 77 |
|
| 78 |
if ($customers > 0) { |
| 79 |
|
| 80 |
foreach ($customers as $customer) { |
| 81 |
|
| 82 |
$wc_customer = new \WC_Customer($customer); |
| 83 |
|
| 84 |
if ( ! empty($wc_customer->get_email())) { |
| 85 |
|
| 86 |
$wc_customer->delete_meta_data('_woo_ml_customer_tracked'); |
| 87 |
$wc_customer->save_meta_data(); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return (count($customers) == 0); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Resets the tracked products so that they can be re-synced |
| 97 |
* woo_ml_reset_tracked_products |
| 98 |
*/ |
| 99 |
public function resetTrackedProducts() |
| 100 |
{ |
| 101 |
set_time_limit(1800); |
| 102 |
|
| 103 |
$defaults = array( |
| 104 |
'post_type' => 'product', |
| 105 |
'posts_per_page' => 100, |
| 106 |
'meta_key' => '_woo_ml_product_tracked', |
| 107 |
'meta_compare' => 'EXISTS' |
| 108 |
); |
| 109 |
|
| 110 |
$args = wp_parse_args($defaults); |
| 111 |
$product_posts_query = new \WP_Query($args); |
| 112 |
|
| 113 |
$product_posts = []; |
| 114 |
|
| 115 |
if ($product_posts_query->have_posts()) { |
| 116 |
$product_posts = $product_posts_query->get_posts(); |
| 117 |
} |
| 118 |
|
| 119 |
$finished = count($product_posts) == 0; |
| 120 |
|
| 121 |
foreach ($product_posts as $post) { |
| 122 |
|
| 123 |
if ( ! isset($post->ID)) { |
| 124 |
|
| 125 |
continue; |
| 126 |
} |
| 127 |
|
| 128 |
delete_post_meta($post->ID, '_woo_ml_product_tracked'); |
| 129 |
} |
| 130 |
|
| 131 |
return $finished; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Resets the tracked categories so that they can be re-synced |
| 136 |
* woo_ml_reset_tracked_categories |
| 137 |
*/ |
| 138 |
public function resetTrackedCategories() |
| 139 |
{ |
| 140 |
set_time_limit(1800); |
| 141 |
|
| 142 |
$term_args = array( |
| 143 |
'taxonomy' => 'product_cat', |
| 144 |
'hide_empty' => false, |
| 145 |
'orderby' => 'none', |
| 146 |
'meta_key' => '_woo_ml_category_tracked', |
| 147 |
'meta_compare' => 'EXISTS' |
| 148 |
); |
| 149 |
|
| 150 |
$categories = get_terms($term_args); |
| 151 |
|
| 152 |
$finished = count($categories) == 0; |
| 153 |
|
| 154 |
foreach ($categories as $category) { |
| 155 |
|
| 156 |
if ( ! isset($category->term_id)) { |
| 157 |
|
| 158 |
continue; |
| 159 |
} |
| 160 |
|
| 161 |
delete_term_meta($category->term_id, '_woo_ml_category_tracked'); |
| 162 |
} |
| 163 |
|
| 164 |
return $finished; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Reset shop sync on platform change |
| 169 |
* mailerlite_reset_shop |
| 170 |
* @return bool|void |
| 171 |
*/ |
| 172 |
public function resetShop() |
| 173 |
{ |
| 174 |
$this->processResetTrackedResources(); |
| 175 |
} |
| 176 |
|
| 177 |
} |