| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services\Products; |
| 4 |
|
| 5 |
use SyncBasalam\Logger\Logger; |
| 6 |
use SyncBasalam\Jobs\Exceptions\RetryableException; |
| 7 |
use SyncBasalam\Jobs\Exceptions\NonRetryableException; |
| 8 |
use SyncBasalam\Utilities\ProductMetaKey; |
| 9 |
|
| 10 |
defined('ABSPATH') || exit; |
| 11 |
|
| 12 |
class AutoConnectProducts |
| 13 |
{ |
| 14 |
public function checkSameProduct($title = null, $cursor = null) |
| 15 |
{ |
| 16 |
try { |
| 17 |
$getProductData = new FetchProductsData(); |
| 18 |
if ($title) { |
| 19 |
$title = mb_substr($title, 0, 120); |
| 20 |
$syncBasalamProducts = $getProductData->getProductData($title); |
| 21 |
} else { |
| 22 |
$syncBasalamProducts = $getProductData->getProductData(null, $cursor); |
| 23 |
} |
| 24 |
|
| 25 |
if (!is_array($syncBasalamProducts) || !isset($syncBasalamProducts['data'])) { |
| 26 |
return $title ? [] : [ |
| 27 |
'error' => true, |
| 28 |
'message' => 'خطا در دریافت اطلاعات � |
| 29 |
حصولات', |
| 30 |
'status_code' => 400, |
| 31 |
'has_more' => false, |
| 32 |
'next_cursor' => null, |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
if ($title) { |
| 37 |
return $syncBasalamProducts['data']; |
| 38 |
} |
| 39 |
|
| 40 |
global $wpdb; |
| 41 |
|
| 42 |
$matchedProducts = []; |
| 43 |
$productIdMetaKey = ProductMetaKey::basalamProductId(); |
| 44 |
|
| 45 |
foreach ($syncBasalamProducts['data'] as $syncBasalamProduct) { |
| 46 |
$normalizedTitle = trim($syncBasalamProduct['title']); |
| 47 |
|
| 48 |
if (mb_strlen($normalizedTitle) >= 120) { |
| 49 |
$likeTitle = $normalizedTitle . '%'; |
| 50 |
} else { |
| 51 |
$likeTitle = $normalizedTitle; |
| 52 |
} |
| 53 |
|
| 54 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Direct lookup on core posts/postmeta tables; no cache key available for this title match. |
| 55 |
$productId = $wpdb->get_var( |
| 56 |
$wpdb->prepare(" |
| 57 |
SELECT p.ID |
| 58 |
FROM {$wpdb->posts} p |
| 59 |
LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = %s |
| 60 |
WHERE p.post_type = 'product' |
| 61 |
AND p.post_status = 'publish' |
| 62 |
AND pm.post_id IS NULL |
| 63 |
AND LOWER(p.post_title) LIKE LOWER(%s) |
| 64 |
LIMIT 1 |
| 65 |
", $productIdMetaKey, $likeTitle) |
| 66 |
); |
| 67 |
|
| 68 |
if ($productId) { |
| 69 |
$connectProductService = new ConnectSingleProductService(); |
| 70 |
$result = $connectProductService->connectProductById($productId, $syncBasalamProduct['id']); |
| 71 |
|
| 72 |
if ($result) { |
| 73 |
Logger::info($syncBasalamProduct['title'] . ' به � |
| 74 |
حصول � |
| 75 |
شابه خود در باسلا� |
| 76 |
� |
| 77 |
تصل شد', [ |
| 78 |
'product_id' => $productId, |
| 79 |
'ع� |
| 80 |
لیات' => "اتصال اتو� |
| 81 |
اتیک � |
| 82 |
حصولات ووکا� |
| 83 |
رس و باسلا� |
| 84 |
", |
| 85 |
]); |
| 86 |
} |
| 87 |
|
| 88 |
$matchedProducts[] = $syncBasalamProduct; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
$hasMore = !empty($syncBasalamProducts['has_more']); |
| 93 |
$nextCursor = $syncBasalamProducts['next_cursor'] ?? null; |
| 94 |
|
| 95 |
if ($hasMore && !empty($nextCursor)) { |
| 96 |
return [ |
| 97 |
'success' => true, |
| 98 |
'message' => '� |
| 99 |
حصولات با � |
| 100 |
وفقیت به صف اتصال افزوده شدند.', |
| 101 |
'status_code' => 200, |
| 102 |
'has_more' => true, |
| 103 |
'next_cursor' => $nextCursor, |
| 104 |
]; |
| 105 |
} else { |
| 106 |
if (!empty($matchedProducts)) { |
| 107 |
return [ |
| 108 |
'success' => true, |
| 109 |
'message' => 'اتصال � |
| 110 |
حصولات کا� |
| 111 |
ل شد.', |
| 112 |
'status_code' => 200, |
| 113 |
'has_more' => false, |
| 114 |
'next_cursor' => null, |
| 115 |
]; |
| 116 |
} else { |
| 117 |
return [ |
| 118 |
'error' => true, |
| 119 |
'message' => '� |
| 120 |
حصول � |
| 121 |
شابهی یافت نشد.', |
| 122 |
'status_code' => 404, |
| 123 |
'has_more' => false, |
| 124 |
'next_cursor' => null, |
| 125 |
]; |
| 126 |
} |
| 127 |
} |
| 128 |
} catch (RetryableException $e) { |
| 129 |
Logger::error("خطا در اتصال خودکار � |
| 130 |
حصولات: " . $e->getMessage(), [ |
| 131 |
'operation' => 'اتصال خودکار � |
| 132 |
حصولات', |
| 133 |
]); |
| 134 |
throw $e; |
| 135 |
} catch (NonRetryableException $e) { |
| 136 |
Logger::error("خطا در اتصال خودکار � |
| 137 |
حصولات: " . $e->getMessage(), [ |
| 138 |
'operation' => 'اتصال خودکار � |
| 139 |
حصولات', |
| 140 |
]); |
| 141 |
throw $e; |
| 142 |
} catch (\Exception $e) { |
| 143 |
Logger::error("خطا در اتصال خودکار � |
| 144 |
حصولات: " . $e->getMessage(), [ |
| 145 |
'operation' => 'اتصال خودکار � |
| 146 |
حصولات', |
| 147 |
]); |
| 148 |
throw $e; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|