| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of ShippingSettingService |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace AliNext_Lite;; |
| 10 |
|
| 11 |
class ShippingSettingService |
| 12 |
{ |
| 13 |
public function handle(): void |
| 14 |
{ |
| 15 |
settings()->auto_commit(false); |
| 16 |
set_setting('aliship_shipto', isset($_POST['a2w_aliship_shipto']) ? wp_unslash($_POST['a2w_aliship_shipto']) : 'US'); |
| 17 |
set_setting( |
| 18 |
Settings::SETTING_ALLOW_SHIPPING_FRONTEND, |
| 19 |
isset($_POST['a2wl_' . Settings::SETTING_ALLOW_SHIPPING_FRONTEND]) |
| 20 |
); |
| 21 |
set_setting('default_shipping_class', !empty($_POST['a2wl_default_shipping_class']) ? $_POST['a2wl_default_shipping_class'] : false); |
| 22 |
|
| 23 |
if (isset($_POST['a2wl_' . Settings::SETTING_ALLOW_SHIPPING_FRONTEND])) { |
| 24 |
set_setting( |
| 25 |
Settings::SETTING_ASSIGN_SHIPPING_ON_IMPORT, |
| 26 |
isset($_POST['a2wl_' . Settings::SETTING_ASSIGN_SHIPPING_ON_IMPORT]) |
| 27 |
); |
| 28 |
|
| 29 |
$syncProductShipping = isset($_POST['a2wl_' . Settings::SETTING_SYNC_PRODUCT_SHIPPING]); |
| 30 |
set_setting(Settings::SETTING_SYNC_PRODUCT_SHIPPING, $syncProductShipping); |
| 31 |
|
| 32 |
$this->saveDeliveryInfoDisplayMode(); |
| 33 |
|
| 34 |
if (isset($_POST['default_rule'])) { |
| 35 |
ShippingPriceFormula::set_default_formula(new ShippingPriceFormula($_POST['default_rule'])); |
| 36 |
} |
| 37 |
|
| 38 |
set_setting('aliship_selection_type', isset($_POST['a2wl_aliship_selection_type']) ? wp_unslash($_POST['a2wl_aliship_selection_type']) : 'popup'); |
| 39 |
|
| 40 |
set_setting('aliship_shipping_type', isset($_POST['a2wl_aliship_shipping_type']) ? wp_unslash($_POST['a2wl_aliship_shipping_type']) : 'new'); |
| 41 |
|
| 42 |
set_setting('aliship_shipping_option_text', |
| 43 |
(isset($_POST['a2wl_aliship_shipping_option_text']) && !empty($_POST['a2wl_aliship_shipping_option_text'])) ? |
| 44 |
wp_unslash($_POST['a2wl_aliship_shipping_option_text']) : '[{shipping_cost}] {shipping_company} ({delivery_time}) - {country}'); |
| 45 |
|
| 46 |
set_setting('aliship_shipping_label', isset($_POST['a2wl_aliship_shipping_label']) ? wp_unslash($_POST['a2wl_aliship_shipping_label']) : 'Shipping'); |
| 47 |
set_setting('aliship_free_shipping_label', isset($_POST['a2wl_aliship_free_shipping_label']) ? wp_unslash($_POST['a2wl_aliship_free_shipping_label']) : 'Free Shipping'); |
| 48 |
|
| 49 |
set_setting( |
| 50 |
Settings::SETTING_SHIPPING_ON_PRODUCT_PAGE, |
| 51 |
isset($_POST['a2wl_' . Settings::SETTING_SHIPPING_ON_PRODUCT_PAGE]) |
| 52 |
); |
| 53 |
|
| 54 |
if (isset($_POST['a2wl_' . Settings::SETTING_SHIPPING_ON_PRODUCT_PAGE])) { |
| 55 |
set_setting('aliship_product_position', isset($_POST['a2wl_aliship_product_position']) ? wp_unslash($_POST['a2wl_aliship_product_position']) : 'after_cart'); |
| 56 |
|
| 57 |
set_setting('aliship_product_not_available_message', |
| 58 |
(isset($_POST['a2wl_aliship_product_not_available_message']) && !empty($_POST['a2wl_aliship_product_not_available_message'])) ? |
| 59 |
wp_unslash($_POST['a2wl_aliship_product_not_available_message']) : 'This product can not be delivered to {country}.'); |
| 60 |
} |
| 61 |
|
| 62 |
set_setting('aliship_not_available_remove', isset($_POST['a2wl_aliship_not_available_remove'])); |
| 63 |
|
| 64 |
set_setting('aliship_not_available_message', |
| 65 |
(isset($_POST['a2wl_aliship_not_available_message']) && !empty($_POST['a2wl_aliship_not_available_message'])) ? |
| 66 |
wp_unslash($_POST['a2wl_aliship_not_available_message']) : '[{shipping_cost}] {delivery_time} - {country}'); |
| 67 |
|
| 68 |
$not_available_shipping_cost = (isset($_POST['a2wl_aliship_not_available_cost']) && floatval($_POST['a2wl_aliship_not_available_cost']) >= 0) ? floatval($_POST['a2wl_aliship_not_available_cost']) : 10; |
| 69 |
|
| 70 |
set_setting('aliship_not_available_cost', $not_available_shipping_cost); |
| 71 |
|
| 72 |
$min_time = (isset($_POST['a2wl_aliship_not_available_time_min']) && intval($_POST['a2wl_aliship_not_available_time_min']) > 0) ? intval($_POST['a2wl_aliship_not_available_time_min']) : 20; |
| 73 |
$max_time = (isset($_POST['a2wl_aliship_not_available_time_max']) && intval($_POST['a2wl_aliship_not_available_time_max']) > 0) ? intval($_POST['a2wl_aliship_not_available_time_max']) : 30; |
| 74 |
|
| 75 |
set_setting('aliship_not_available_time_min', $min_time); |
| 76 |
set_setting('aliship_not_available_time_max', $max_time); |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
settings()->commit(); |
| 81 |
settings()->auto_commit(true); |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
public function collectModel(): array |
| 86 |
{ |
| 87 |
$countryModel = new Country(); |
| 88 |
$shipping_class = get_terms([ |
| 89 |
'taxonomy' => 'product_shipping_class', |
| 90 |
'hide_empty' => false |
| 91 |
]); |
| 92 |
|
| 93 |
return [ |
| 94 |
"shipping_countries" => $countryModel->get_countries(), |
| 95 |
"shipping_selection_types" => Shipping::get_selection_types(), |
| 96 |
"shipping_types" => Shipping::get_shipping_types(), |
| 97 |
"selection_position_types" => Shipping::get_selection_position_types(), |
| 98 |
"default_formula" => ShippingPriceFormula::get_default_formula(), |
| 99 |
"shipping_class" => $shipping_class ?: [], |
| 100 |
"deliveryInfoDisplayModes" => $this->getDeliveryInfoDisplayModes(), |
| 101 |
"shippingOnProductPage" => get_setting(Settings::SETTING_SHIPPING_ON_PRODUCT_PAGE), |
| 102 |
"deliveryTimeTextFormat" => get_setting( |
| 103 |
Settings::SETTING_DELIVERY_TIME_TEXT_FORMAT, |
| 104 |
Settings::DEFAULTS[Settings::SETTING_DELIVERY_TIME_TEXT_FORMAT] |
| 105 |
), |
| 106 |
"deliveryTimeFallbackMin" => get_setting( |
| 107 |
Settings::SETTING_DELIVERY_TIME_FALLBACK_MIN, |
| 108 |
Settings::DEFAULTS[Settings::SETTING_DELIVERY_TIME_FALLBACK_MIN] |
| 109 |
), |
| 110 |
"deliveryTimeFallbackMax" => get_setting( |
| 111 |
Settings::SETTING_DELIVERY_TIME_FALLBACK_MAX, |
| 112 |
Settings::DEFAULTS[Settings::SETTING_DELIVERY_TIME_FALLBACK_MAX] |
| 113 |
), |
| 114 |
"isDeliveryTimeOnlyMode" => |
| 115 |
get_setting(Settings::SETTING_DELIVERY_INFO_DISPLAY_MODE) === 'delivery_time_only', |
| 116 |
]; |
| 117 |
} |
| 118 |
|
| 119 |
private function saveDeliveryInfoDisplayMode(): void |
| 120 |
{ |
| 121 |
$allowed = array_keys($this->getDeliveryInfoDisplayModes()); |
| 122 |
$value = isset($_POST['a2wl_' . Settings::SETTING_DELIVERY_INFO_DISPLAY_MODE]) |
| 123 |
? sanitize_text_field(wp_unslash($_POST['a2wl_' . Settings::SETTING_DELIVERY_INFO_DISPLAY_MODE])) |
| 124 |
: Settings::DEFAULTS[Settings::SETTING_DELIVERY_INFO_DISPLAY_MODE]; |
| 125 |
|
| 126 |
if (!in_array($value, $allowed, true)) { |
| 127 |
$value = Settings::DEFAULTS[Settings::SETTING_DELIVERY_INFO_DISPLAY_MODE]; |
| 128 |
} |
| 129 |
|
| 130 |
set_setting(Settings::SETTING_DELIVERY_INFO_DISPLAY_MODE, $value); |
| 131 |
|
| 132 |
if ($value === 'delivery_time_only') { |
| 133 |
set_setting( |
| 134 |
Settings::SETTING_DELIVERY_TIME_TEXT_FORMAT, |
| 135 |
sanitize_text_field($_POST['a2wl_' . Settings::SETTING_DELIVERY_TIME_TEXT_FORMAT] ?? |
| 136 |
Settings::DEFAULTS[Settings::SETTING_DELIVERY_TIME_TEXT_FORMAT]) |
| 137 |
); |
| 138 |
|
| 139 |
set_setting( |
| 140 |
Settings::SETTING_DELIVERY_TIME_FALLBACK_MIN, |
| 141 |
intval($_POST['a2wl_' . Settings::SETTING_DELIVERY_TIME_FALLBACK_MIN] ?? |
| 142 |
Settings::DEFAULTS[Settings::SETTING_DELIVERY_TIME_FALLBACK_MIN]) |
| 143 |
); |
| 144 |
|
| 145 |
set_setting( |
| 146 |
Settings::SETTING_DELIVERY_TIME_FALLBACK_MAX, |
| 147 |
intval($_POST['a2wl_' . Settings::SETTING_DELIVERY_TIME_FALLBACK_MAX] ?? |
| 148 |
Settings::DEFAULTS[Settings::SETTING_DELIVERY_TIME_FALLBACK_MAX]) |
| 149 |
); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** @return string[] */ |
| 154 |
private function getDeliveryInfoDisplayModes(): array |
| 155 |
{ |
| 156 |
return [ |
| 157 |
'default' => esc_html_x( |
| 158 |
'Full shipping options', |
| 159 |
'Delivery info display mode', |
| 160 |
'ali2woo' |
| 161 |
), |
| 162 |
'delivery_time_only' => esc_html_x( |
| 163 |
'Delivery time only', |
| 164 |
'Delivery info display mode', |
| 165 |
'ali2woo' |
| 166 |
), |
| 167 |
]; |
| 168 |
} |
| 169 |
|
| 170 |
} |
| 171 |
|