PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.6
ووسلام – همگام سازی ووکامرس و باسلام v1.10.6
1.10.19 1.10.20 1.10.18 1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 1.8.8 1.8.5 1.8.6 All 53 releases
sync-basalam / includes / Services / Products / AutoConnectProducts.php

AutoConnectProducts.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.6, at includes/Services/Products/AutoConnectProducts.php

152 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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