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