PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.9
ووسلام – همگام سازی ووکامرس و باسلام v1.10.9
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.9, at includes/Admin/Product/Operations/ConnectProduct.php

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