| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Product\elements\SingleProduct; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
class GoldFields |
| 8 |
{ |
| 9 |
public static function renderCheckbox() |
| 10 |
{ |
| 11 |
$isGoldProduct = get_post_meta(get_the_ID(), '_sync_basalam_is_gold_product_checkbox', true); |
| 12 |
$checked = ($isGoldProduct === 'yes') ? 'yes' : 'no'; |
| 13 |
|
| 14 |
woocommerce_wp_checkbox([ |
| 15 |
'id' => '_sync_basalam_is_gold_product_checkbox', |
| 16 |
'label' => '� |
| 17 |
حصول طلا', |
| 18 |
'description' => 'در صورتی که � |
| 19 |
حصول ش� |
| 20 |
ا طلا است ، این گزینه را فعال ن� |
| 21 |
ایید .', |
| 22 |
'desc_tip' => true, |
| 23 |
'value' => $checked, |
| 24 |
]); |
| 25 |
|
| 26 |
wp_nonce_field('sync_basalam_save_gold_fields_action', '_sync_basalam_gold_fields_nonce'); |
| 27 |
|
| 28 |
self::renderFields($checked); |
| 29 |
} |
| 30 |
|
| 31 |
public static function renderFields($checked) |
| 32 |
{ |
| 33 |
$fields = [ |
| 34 |
'_sync_basalam_gold_purity' => 'عیار طلا (� |
| 35 |
بنای 1000)*', |
| 36 |
'_sync_basalam_gold_weight' => 'وزن طلا (� |
| 37 |
یلی گر� |
| 38 |
)*', |
| 39 |
]; |
| 40 |
|
| 41 |
echo '<div id="basalam_gold_product_fields" class="basalam-mobile-fields' . ($checked === 'yes' ? ' show' : '') . '">'; |
| 42 |
|
| 43 |
foreach ($fields as $id => $label) { |
| 44 |
woocommerce_wp_text_input([ |
| 45 |
'id' => $id, |
| 46 |
'label' => $label, |
| 47 |
'type' => 'text', |
| 48 |
'value' => get_post_meta(get_the_ID(), $id, true), |
| 49 |
]); |
| 50 |
} |
| 51 |
|
| 52 |
echo '</div>'; |
| 53 |
} |
| 54 |
|
| 55 |
public static function saveCheckbox($postId) |
| 56 |
{ |
| 57 |
if ( |
| 58 |
!isset($_POST['_sync_basalam_gold_fields_nonce']) |
| 59 |
|| !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_sync_basalam_gold_fields_nonce'])), 'sync_basalam_save_gold_fields_action') |
| 60 |
) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
$syncBasalamIsGoldCheckboxValue = isset($_POST['_sync_basalam_is_gold_product_checkbox']) ? 'yes' : 'no'; |
| 65 |
update_post_meta($postId, '_sync_basalam_is_gold_product_checkbox', $syncBasalamIsGoldCheckboxValue); |
| 66 |
|
| 67 |
if ($syncBasalamIsGoldCheckboxValue === 'no') { |
| 68 |
self::deleteFields($postId); |
| 69 |
} else { |
| 70 |
self::saveFields($postId); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public static function deleteFields($postId) |
| 75 |
{ |
| 76 |
$fields = [ |
| 77 |
'_sync_basalam_gold_purity', |
| 78 |
'_sync_basalam_gold_weight', |
| 79 |
]; |
| 80 |
|
| 81 |
foreach ($fields as $field) { |
| 82 |
delete_post_meta($postId, $field); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
public static function saveFields($postId) |
| 87 |
{ |
| 88 |
if ( |
| 89 |
!isset($_POST['_sync_basalam_gold_fields_nonce']) |
| 90 |
|| !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_sync_basalam_gold_fields_nonce'])), 'sync_basalam_save_gold_fields_action') |
| 91 |
) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
$fields = [ |
| 96 |
'_sync_basalam_gold_purity' => '', |
| 97 |
'_sync_basalam_gold_weight' => '', |
| 98 |
]; |
| 99 |
|
| 100 |
foreach ($fields as $fieldKey => $default) { |
| 101 |
if (isset($_POST[$fieldKey])) { |
| 102 |
$value = sanitize_text_field(wp_unslash($_POST[$fieldKey])); |
| 103 |
update_post_meta($postId, $fieldKey, $value); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|