| 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 |
|