| 1 |
<?php |
| 2 |
|
| 3 |
namespace MailerLite\Includes\Classes\Process; |
| 4 |
|
| 5 |
use MailerLite\Includes\Classes\Settings\MailerLiteSettings; |
| 6 |
use MailerLite\Includes\Classes\Singleton; |
| 7 |
use MailerLite\Includes\Shared\Api\ApiType; |
| 8 |
use MailerLite\Includes\Shared\Api\PlatformAPI; |
| 9 |
|
| 10 |
class ProductProcess extends Singleton |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Retrieves the url of the image of the given product |
| 14 |
* woo_ml_product_image |
| 15 |
* |
| 16 |
* @param $product |
| 17 |
* @param string $size |
| 18 |
* |
| 19 |
* @return mixed|null |
| 20 |
*/ |
| 21 |
public function productImage($product, $size = 'large') |
| 22 |
{ |
| 23 |
if ($product->get_image_id()) { |
| 24 |
|
| 25 |
$image = wp_get_attachment_image_src($product->get_image_id(), $size, false); |
| 26 |
|
| 27 |
list($src, $width, $height) = $image; |
| 28 |
|
| 29 |
return $src; |
| 30 |
} elseif ($product->get_parent_id()) { |
| 31 |
|
| 32 |
$parentProduct = wc_get_product($product->get_parent_id()); |
| 33 |
if ($parentProduct) { |
| 34 |
|
| 35 |
return $this->productImage($parentProduct, $size); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
return ''; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get Product List |
| 44 |
* woo_ml_get_product_list |
| 45 |
* @return array|mixed |
| 46 |
*/ |
| 47 |
public function getIgnoredProductList() |
| 48 |
{ |
| 49 |
global $wpdb; |
| 50 |
|
| 51 |
$table = $wpdb->prefix . 'ml_data'; |
| 52 |
|
| 53 |
$tableCreated = get_option('ml_data_table'); |
| 54 |
|
| 55 |
if ($tableCreated != 1) { |
| 56 |
|
| 57 |
MailerLiteSettings::getInstance()->createMailerDataTable(); |
| 58 |
} |
| 59 |
|
| 60 |
$data = $wpdb->get_row("SELECT * FROM $table WHERE data_name = 'products'"); |
| 61 |
|
| 62 |
if (!empty($data)) { |
| 63 |
|
| 64 |
$products = json_decode($data->data_value, true); |
| 65 |
|
| 66 |
if ($products !== false) { |
| 67 |
|
| 68 |
return $products; |
| 69 |
} else { |
| 70 |
|
| 71 |
return []; |
| 72 |
} |
| 73 |
} else { |
| 74 |
|
| 75 |
return []; |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* |
| 82 |
* mailerlite_wp_delete_product_category |
| 83 |
* |
| 84 |
* @param $category_id |
| 85 |
* |
| 86 |
* @return false|void |
| 87 |
*/ |
| 88 |
public function deleteProductCategory($category_id) |
| 89 |
{ |
| 90 |
|
| 91 |
$shop = get_option('woo_ml_shop_id', false); |
| 92 |
|
| 93 |
if ($shop === false) { |
| 94 |
|
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 99 |
|
| 100 |
if ($mailerliteClient->getApiType() !== ApiType::CURRENT) { |
| 101 |
|
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
$mailerliteClient->deleteCategory($shop, $category_id); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* |
| 110 |
* mailerlite_wp_sync_product_category |
| 111 |
* |
| 112 |
* @param $category_id |
| 113 |
* |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
public function syncProductCategory($category_id) |
| 117 |
{ |
| 118 |
|
| 119 |
$category = get_term($category_id); |
| 120 |
|
| 121 |
if (!isset($category->term_id)) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
$shop = get_option('woo_ml_shop_id', false); |
| 126 |
|
| 127 |
if ($shop === false) { |
| 128 |
|
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 133 |
|
| 134 |
if ($mailerliteClient->getApiType() !== ApiType::CURRENT) { |
| 135 |
|
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
$result = $mailerliteClient->syncCategory($shop, $category->term_id, $category->name); |
| 140 |
|
| 141 |
if ($mailerliteClient->responseCode() === 200 || $mailerliteClient->responseCode() === 201) { |
| 142 |
|
| 143 |
MailerLiteSettings::getInstance()->completeCategoryTracking($result->resource_id, $result->id); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* |
| 149 |
* mailerlite_sync_product |
| 150 |
* |
| 151 |
* @param $product_id |
| 152 |
* |
| 153 |
* @return bool |
| 154 |
*/ |
| 155 |
public function syncProduct($product_id) |
| 156 |
{ |
| 157 |
|
| 158 |
$shop = get_option('woo_ml_shop_id', false); |
| 159 |
|
| 160 |
if ($shop === false) { |
| 161 |
|
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
$product = wc_get_product($product_id); |
| 166 |
|
| 167 |
$productID = $product->get_id(); |
| 168 |
$productName = $product->get_name() ?: 'Untitled product'; |
| 169 |
$productPrice = floatval($product->get_price('edit')); |
| 170 |
$productImage = $this->productImage($product); |
| 171 |
$productURL = $product->get_permalink(); |
| 172 |
|
| 173 |
$categories = get_the_terms($productID, 'product_cat'); |
| 174 |
|
| 175 |
$productCategories = []; |
| 176 |
|
| 177 |
foreach ($categories as $category) { |
| 178 |
|
| 179 |
if (isset($category->term_id) && is_numeric($category->term_id)) { |
| 180 |
|
| 181 |
$productCategories[] = (string)$category->term_id; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
$exclude_automation = get_post_meta($productID, '_woo_ml_product_ignored', true) === "1"; |
| 186 |
|
| 187 |
$mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); |
| 188 |
|
| 189 |
if ($mailerliteClient->getApiType() !== ApiType::CURRENT) { |
| 190 |
|
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
$result = $mailerliteClient->syncProduct( |
| 195 |
$shop, |
| 196 |
$productID, |
| 197 |
$productName, |
| 198 |
$productPrice, |
| 199 |
$exclude_automation, |
| 200 |
$productURL, |
| 201 |
$productImage, |
| 202 |
[] |
| 203 |
); |
| 204 |
|
| 205 |
if ($result !== false && ($mailerliteClient->responseCode() == 201 || $mailerliteClient->responseCode() == 200)) { |
| 206 |
$mailerLiteSettings = MailerLiteSettings::getInstance(); |
| 207 |
if (!$mailerLiteSettings->checkProductTracking($productID)) { |
| 208 |
$mailerLiteSettings->completeProductTracking($productID); |
| 209 |
} |
| 210 |
|
| 211 |
$mailerliteClient->replaceProductCategories($shop, $productID, $productCategories); |
| 212 |
|
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* |
| 221 |
* mailerlite_wp_sync_product |
| 222 |
* |
| 223 |
* @param $product_id |
| 224 |
* |
| 225 |
* @return void |
| 226 |
*/ |
| 227 |
public function wpSyncProduct($product_id) |
| 228 |
{ |
| 229 |
if (did_action('woocommerce_update_product') === 1) { |
| 230 |
|
| 231 |
$this->syncProduct($product_id); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
} |