PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / 1.10.20
ووسلام – همگام سازی ووکامرس و باسلام v1.10.20
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 / elements / ProductList / BulkEdit.php

BulkEdit.php in ووسلام – همگام سازی ووکامرس و باسلام 1.10.20, at includes/Admin/Product/elements/ProductList/BulkEdit.php

145 lines 4.8 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\elements\ProductList;
4
5 use SyncBasalam\Admin\Product\elements\SingleProduct\PriceChangeField;
6 use SyncBasalam\Admin\Product\Utils\ProductUnits;
7 use SyncBasalam\Utilities\PriceAdjustment;
8
9 defined('ABSPATH') || exit;
10
11 class BulkEdit
12 {
13 public const TYPE_META_KEY = '_sync_basalam_is_product_type_checkbox';
14 public const UNIT_META_KEY = '_sync_basalam_product_unit';
15 public const QUANTITY_META_KEY = '_sync_basalam_product_value';
16 public const WHOLESALE_META_KEY = '_sync_basalam_is_wholesale';
17
18 public function renderFields(string $columnName, string $postType): void
19 {
20 if ($postType !== 'product' || $columnName !== 'name') {
21 return;
22 }
23
24 $units = ProductUnits::all();
25
26 require __DIR__ . '/views/BulkEditFields.php';
27 }
28
29 /**
30 * Runs on admin_action_sync_basalam_bulk_edit, which passes no arguments,
31 * so the selected products are read from the bulk edit request itself.
32 */
33 public function save(): void
34 {
35 if (!isset($_REQUEST['post']) || !is_array($_REQUEST['post'])) {
36 return;
37 }
38
39 if (
40 !isset($_REQUEST['_wpnonce'])
41 || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])), 'bulk-posts')
42 ) {
43 wp_die('درخواست نا�
44 عتبر است. لطفاً دوباره تلاش کنید.');
45 }
46
47 if (!isset($_REQUEST['sync_basalam_bulk_product_type_action']) && !isset($_REQUEST['sync_basalam_bulk_price_change_action'])) {
48 return;
49 }
50
51 $postIds = array_map('intval', wp_unslash($_REQUEST['post'])); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce verified above; each ID is cast with intval().
52
53 foreach ($postIds as $postId) {
54 if ($postId <= 0 || get_post_type($postId) !== 'product') {
55 continue;
56 }
57
58 if (!current_user_can('edit_post', $postId)) {
59 continue;
60 }
61
62 $this->saveProductType($postId);
63 $this->saveWholesale($postId);
64 $this->savePriceChange($postId);
65 }
66 }
67
68 private function saveProductType(int $postId): void
69 {
70 $action = isset($_REQUEST['sync_basalam_bulk_product_type_action'])
71 ? sanitize_text_field(wp_unslash($_REQUEST['sync_basalam_bulk_product_type_action']))
72 : 'keep';
73
74 if ($action === 'keep') {
75 return;
76 }
77
78 if ($action === 'yes') {
79 update_post_meta($postId, self::TYPE_META_KEY, 'yes');
80
81 $unit = isset($_REQUEST['sync_basalam_bulk_product_unit'])
82 ? sanitize_text_field(wp_unslash($_REQUEST['sync_basalam_bulk_product_unit']))
83 : '6304';
84 $quantity = isset($_REQUEST['sync_basalam_bulk_product_value'])
85 ? sanitize_text_field(wp_unslash($_REQUEST['sync_basalam_bulk_product_value']))
86 : '1';
87
88 $unitValue = is_numeric($unit) ? (string) intval($unit) : '6304';
89 $quantityValue = (is_numeric($quantity) && intval($quantity) > 0) ? (string) intval($quantity) : '1';
90
91 update_post_meta($postId, self::UNIT_META_KEY, $unitValue);
92 update_post_meta($postId, self::QUANTITY_META_KEY, $quantityValue);
93
94 return;
95 }
96
97 update_post_meta($postId, self::TYPE_META_KEY, 'no');
98 delete_post_meta($postId, self::UNIT_META_KEY);
99 delete_post_meta($postId, self::QUANTITY_META_KEY);
100 }
101
102 private function saveWholesale(int $postId): void
103 {
104 $action = isset($_REQUEST['sync_basalam_bulk_wholesale_action'])
105 ? sanitize_text_field(wp_unslash($_REQUEST['sync_basalam_bulk_wholesale_action']))
106 : 'keep';
107
108 if ($action === 'keep') {
109 return;
110 }
111
112 $wholesaleValue = $action === 'yes' ? 'yes' : 'no';
113 update_post_meta($postId, self::WHOLESALE_META_KEY, $wholesaleValue);
114 }
115
116 private function savePriceChange(int $postId): void
117 {
118 $action = isset($_REQUEST['sync_basalam_bulk_price_change_action'])
119 ? sanitize_text_field(wp_unslash($_REQUEST['sync_basalam_bulk_price_change_action']))
120 : 'keep';
121
122 if ($action === 'keep') {
123 return;
124 }
125
126 if ($action === 'clear') {
127 delete_post_meta($postId, PriceChangeField::META_KEY);
128
129 return;
130 }
131
132 $value = isset($_REQUEST['sync_basalam_bulk_price_change_value'])
133 ? sanitize_text_field(wp_unslash($_REQUEST['sync_basalam_bulk_price_change_value']))
134 : '';
135
136 $priceChangeValue = PriceAdjustment::normalize($value);
137
138 if ($priceChangeValue === null) {
139 return;
140 }
141
142 update_post_meta($postId, PriceChangeField::META_KEY, $priceChangeValue);
143 }
144 }
145