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