PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.7
ووسلام – همگام سازی ووکامرس و باسلام v1.10.7
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 / Admin / Product / Operations / ConnectProduct.php

ConnectProduct.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.7, at includes/Admin/Product/Operations/ConnectProduct.php

130 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SyncBasalam\Admin\Product\Operations;
4
5 use SyncBasalam\Services\Products\ConnectSingleProductService;
6 use SyncBasalam\Services\Products\AutoConnectProducts;
7 use SyncBasalam\JobManager;
8 use SyncBasalam\Admin\Components\SingleProductPageComponents;
9
10 defined('ABSPATH') || exit;
11
12 class ConnectProduct
13 {
14 public function handleConnectProduct(): array
15 {
16 $nonce = isset($_POST['_wpnonce']) ? sanitize_text_field(wp_unslash($_POST['_wpnonce'])) : '';
17
18 if (!wp_verify_nonce($nonce, 'basalam_connect_product_nonce')) {
19 return [
20 'success' => false,
21 'message' => 'دسترسی غیر�
22 جاز!',
23 'status_code' => 403,
24 ];
25 }
26
27 if (!current_user_can('manage_woocommerce')) {
28 return [
29 'success' => false,
30 'message' => 'تنها �
31 دیر کل ا�
32 کان تغییر وضعیت سفارش را دارد.',
33 'status_code' => 400,
34 ];
35 }
36
37 $woo_product_id = isset($_POST['woo_product_id']) ? intval($_POST['woo_product_id']) : '';
38 $sync_basalam_product_id = isset($_POST['basalam_product_id']) ? sanitize_text_field(wp_unslash($_POST['basalam_product_id'])) : '';
39
40 if (!$woo_product_id || !$sync_basalam_product_id) {
41 return [
42 'success' => false,
43 'message' => 'داده های ورودی ناقص است.',
44 'status_code' => 400,
45 ];
46 }
47
48 $connect_status = ConnectSingleProductService::connectProductById($woo_product_id, $sync_basalam_product_id);
49
50 $job_manager = syncBasalamContainer()->get(JobManager::class);
51 $job_manager->createJob(
52 'sync_basalam_update_single_product',
53 'pending',
54 $woo_product_id,
55 );
56
57 if ($connect_status) {
58 return [
59 'success' => true,
60 'message' => 'اتصال �
61 حصولات با �
62 وفقیت انجا�
63 شد.',
64 'status_code' => 200,
65 ];
66 } else {
67 return [
68 'success' => false,
69 'message' => 'این �
70 حصول به �
71 حصول دیگری �
72 تصل شده است.',
73 'status_code' => 400,
74 ];
75 }
76 }
77
78 public function handleSearchProducts(): void
79 {
80 check_ajax_referer('basalam_search_products_nonce', '_wpnonce');
81
82 if (!current_user_can('edit_posts')) {
83 wp_send_json_error("دسترسی غیر�
84 جاز.");
85 }
86
87 $title = isset($_POST['title']) ? sanitize_text_field(wp_unslash($_POST['title'])) : '';
88
89 if (empty($title)) {
90 echo '<p>عنوان �
91 حصول وارد نشده است.</p>';
92 wp_die();
93 }
94
95 $productId = isset($_POST['woo_product_id']) ? intval($_POST['woo_product_id']) : 0;
96 $this->renderProductsByTitle($title, $productId);
97 wp_die();
98 }
99
100 public function renderProductsByTitle(string $title, int $productId): void
101 {
102 $products = $this->getProductsByTitle($title);
103 $this->renderProductCards($products, $productId);
104 }
105
106 private function getProductsByTitle(string $title): array
107 {
108 $title = trim($title);
109 if ($title === '') return [];
110
111 $checker = new AutoConnectProducts();
112 $result = $checker->checkSameProduct($title);
113
114 return is_array($result) && !isset($result['error']) ? $result : [];
115 }
116
117 private function renderProductCards(array $products, int $productId): void
118 {
119 if (!empty($products)) {
120 foreach ($products as $product) {
121 if (!is_array($product)) continue;
122 SingleProductPageComponents::renderProductCard($product, $productId);
123 }
124 } else {
125 echo '<p class="basalam--no-match">�
126 حصولی با این عنوان پیدا نشد.</p>';
127 }
128 }
129 }
130