| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of CommonSettingService |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace AliNext_Lite;; |
| 10 |
|
| 11 |
use Pages; |
| 12 |
|
| 13 |
class CommonSettingService |
| 14 |
{ |
| 15 |
public const PARAM_ALLOW_SHOP_MANAGER = 'a2wl_allow_shop_manager'; |
| 16 |
public const PARAM_HIDDEN_PAGES = 'a2wl_hidden_pages'; |
| 17 |
|
| 18 |
|
| 19 |
protected AliexpressRegionRepository $AliexpressRegionRepository; |
| 20 |
|
| 21 |
public function __construct(AliexpressRegionRepository $AliexpressRegionRepository) { |
| 22 |
$this->AliexpressRegionRepository = $AliexpressRegionRepository; |
| 23 |
} |
| 24 |
|
| 25 |
public function handle(): void |
| 26 |
{ |
| 27 |
settings()->auto_commit(false); |
| 28 |
set_setting('item_purchase_code', isset($_POST['a2wl_item_purchase_code']) ? wp_unslash($_POST['a2wl_item_purchase_code']) : ''); |
| 29 |
|
| 30 |
set_setting('import_language', isset($_POST['a2w_import_language']) ? wp_unslash($_POST['a2w_import_language']) : 'en'); |
| 31 |
set_setting( |
| 32 |
Settings::SETTING_ALIEXPRESS_REGION, |
| 33 |
isset($_POST['a2wl_aliexpress_region']) ? wp_unslash($_POST['a2wl_aliexpress_region']) : 'US' |
| 34 |
); |
| 35 |
|
| 36 |
if (isset($_POST['a2w_local_currency'])) { |
| 37 |
$currency = isset($_POST['a2w_local_currency']) ? wp_unslash($_POST['a2w_local_currency']) : 'USD'; |
| 38 |
set_setting('local_currency', $currency); |
| 39 |
update_option('woocommerce_currency', $currency); |
| 40 |
} |
| 41 |
|
| 42 |
set_setting('default_product_type', isset($_POST['a2wl_default_product_type']) ? wp_unslash($_POST['a2wl_default_product_type']) : 'simple'); |
| 43 |
set_setting('default_product_status', isset($_POST['a2wl_default_product_status']) ? wp_unslash($_POST['a2wl_default_product_status']) : 'publish'); |
| 44 |
|
| 45 |
set_setting('delivered_order_status', isset($_POST['a2wl_delivered_order_status']) ? wp_unslash($_POST['a2wl_delivered_order_status']) : ''); |
| 46 |
|
| 47 |
set_setting('tracking_code_order_status', isset($_POST['a2wl_tracking_code_order_status']) ? wp_unslash($_POST['a2wl_tracking_code_order_status']) : ''); |
| 48 |
|
| 49 |
set_setting('placed_order_status', isset($_POST['a2wl_placed_order_status']) ? wp_unslash($_POST['a2wl_placed_order_status']) : ''); |
| 50 |
|
| 51 |
set_setting('currency_conversion_factor', isset($_POST['a2wl_currency_conversion_factor']) ? wp_unslash($_POST['a2wl_currency_conversion_factor']) : '1'); |
| 52 |
set_setting('import_product_images_limit', isset($_POST['a2wl_import_product_images_limit']) && intval($_POST['a2wl_import_product_images_limit']) ? intval($_POST['a2wl_import_product_images_limit']) : ''); |
| 53 |
set_setting('import_extended_attribute', isset($_POST['a2wl_import_extended_attribute']) ? 1 : 0); |
| 54 |
|
| 55 |
set_setting('background_import', isset($_POST['a2wl_background_import']) ? 1 : 0); |
| 56 |
set_setting('allow_product_duplication', isset($_POST['a2wl_allow_product_duplication']) ? 1 : 0); |
| 57 |
set_setting('convert_attr_case', isset($_POST['a2wl_convert_attr_case']) ? wp_unslash($_POST['a2wl_convert_attr_case']) : 'original'); |
| 58 |
|
| 59 |
set_setting('remove_ship_from', isset($_POST['a2wl_remove_ship_from']) ? 1 : 0); |
| 60 |
set_setting('default_ship_from', isset($_POST['a2wl_default_ship_from']) ? wp_unslash($_POST['a2wl_default_ship_from']) : 'CN'); |
| 61 |
|
| 62 |
set_setting('use_external_image_urls', isset($_POST['a2wl_use_external_image_urls'])); |
| 63 |
set_setting('not_import_attributes', isset($_POST['a2wl_not_import_attributes'])); |
| 64 |
set_setting('not_import_description', isset($_POST['a2wl_not_import_description'])); |
| 65 |
set_setting('not_import_description_images', isset($_POST['a2wl_not_import_description_images'])); |
| 66 |
|
| 67 |
set_setting('use_random_stock', isset($_POST['a2wl_use_random_stock'])); |
| 68 |
if (isset($_POST['a2wl_use_random_stock'])) { |
| 69 |
$min_stock = (!empty($_POST['a2wl_use_random_stock_min']) && intval($_POST['a2wl_use_random_stock_min']) > 0) ? intval($_POST['a2wl_use_random_stock_min']) : 1; |
| 70 |
$max_stock = (!empty($_POST['a2wl_use_random_stock_max']) && intval($_POST['a2wl_use_random_stock_max']) > 0) ? intval($_POST['a2wl_use_random_stock_max']) : 1; |
| 71 |
|
| 72 |
if ($min_stock > $max_stock) { |
| 73 |
$min_stock = $min_stock + $max_stock; |
| 74 |
$max_stock = $min_stock - $max_stock; |
| 75 |
$min_stock = $min_stock - $max_stock; |
| 76 |
} |
| 77 |
set_setting('use_random_stock_min', $min_stock); |
| 78 |
set_setting('use_random_stock_max', $max_stock); |
| 79 |
} |
| 80 |
|
| 81 |
set_setting('auto_update', isset($_POST['a2wl_auto_update'])); |
| 82 |
|
| 83 |
set_setting('on_not_available_product', isset($_POST['a2wl_on_not_available_product']) ? wp_unslash($_POST['a2wl_on_not_available_product']) : 'trash'); |
| 84 |
set_setting('on_not_available_variation', isset($_POST['a2wl_on_not_available_variation']) ? wp_unslash($_POST['a2wl_on_not_available_variation']) : 'trash'); |
| 85 |
set_setting('on_new_variation_appearance', isset($_POST['a2wl_on_new_variation_appearance']) ? wp_unslash($_POST['a2wl_on_new_variation_appearance']) : 'add'); |
| 86 |
set_setting('on_price_changes', isset($_POST['a2wl_on_price_changes']) ? wp_unslash($_POST['a2wl_on_price_changes']) : 'update'); |
| 87 |
set_setting('on_stock_changes', isset($_POST['a2wl_on_stock_changes']) ? wp_unslash($_POST['a2wl_on_stock_changes']) : 'update'); |
| 88 |
set_setting('untrash_product', isset($_POST['a2wl_untrash_product'])); |
| 89 |
set_setting('email_alerts', isset($_POST['a2wl_email_alerts'])); |
| 90 |
set_setting('email_alerts_email', isset($_POST['a2wl_email_alerts_email']) ? wp_unslash($_POST['a2wl_email_alerts_email']) : ''); |
| 91 |
|
| 92 |
set_setting('fulfillment_prefship', isset($_POST['a2w_fulfillment_prefship']) ? wp_unslash($_POST['a2w_fulfillment_prefship']) : 'ePacket'); |
| 93 |
set_setting('fulfillment_phone_code', isset($_POST['a2wl_fulfillment_phone_code']) ? wp_unslash($_POST['a2wl_fulfillment_phone_code']) : ''); |
| 94 |
set_setting('fulfillment_phone_number', isset($_POST['a2wl_fulfillment_phone_number']) ? wp_unslash($_POST['a2wl_fulfillment_phone_number']) : ''); |
| 95 |
set_setting('fulfillment_custom_note', isset($_POST['a2wl_fulfillment_custom_note']) ? wp_unslash($_POST['a2wl_fulfillment_custom_note']) : ''); |
| 96 |
set_setting('fulfillment_cpf_meta_key', isset($_POST['a2wl_fulfillment_cpf_meta_key']) ? wp_unslash($_POST['a2wl_fulfillment_cpf_meta_key']) : ''); |
| 97 |
set_setting('fulfillment_rut_meta_key', isset($_POST['a2wl_fulfillment_rut_meta_key']) ? wp_unslash($_POST['a2wl_fulfillment_rut_meta_key']) : ''); |
| 98 |
|
| 99 |
set_setting('order_translitirate', isset($_POST['a2wl_order_translitirate'])); |
| 100 |
set_setting('order_third_name', isset($_POST['a2wl_order_third_name'])); |
| 101 |
|
| 102 |
set_setting( |
| 103 |
Settings::SETTING_FULFILLMENT_IS_FOREIGNER, |
| 104 |
isset($_POST['a2wl_fulfillment_is_foreigner']) |
| 105 |
); |
| 106 |
set_setting(Settings::SETTING_FULFILLMENT_FOREIGN_TAX_ID, |
| 107 |
isset($_POST['a2wl_fulfillment_foreign_tax_id']) ? wp_unslash($_POST['a2wl_fulfillment_foreign_tax_id']) : '' |
| 108 |
); |
| 109 |
set_setting(Settings::SETTING_FULFILLMENT_PASSPORT_NUMBER, |
| 110 |
isset($_POST['a2wl_fulfillment_passport_number']) ? wp_unslash($_POST['a2wl_fulfillment_passport_number']) : '' |
| 111 |
); |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
settings()->commit(); |
| 116 |
settings()->auto_commit(true); |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
public function collectModel(): array |
| 121 |
{ |
| 122 |
$Localizator = AliexpressLocalizator::getInstance(); |
| 123 |
$countryModel = new Country(); |
| 124 |
$languageModel = new Language(); |
| 125 |
|
| 126 |
return [ |
| 127 |
'aliexpressRegion' => $this->AliexpressRegionRepository->get(), |
| 128 |
'aliexpressRegions' => $this->AliexpressRegionRepository->getAllWithLabels(), |
| 129 |
'upgradeTariffUrl' => $this->buildUpgradeTariffUrl(), |
| 130 |
'shipping_options' => Utils::get_aliexpress_shipping_options(), |
| 131 |
'currencies' => $Localizator->getCurrencies(false), |
| 132 |
'custom_currencies' => $Localizator->getCurrencies(true), |
| 133 |
'order_statuses' => function_exists('wc_get_order_statuses') ? wc_get_order_statuses() : [], |
| 134 |
'shipping_countries' => $countryModel->get_countries(), |
| 135 |
'languages' => $languageModel->get_languages(), |
| 136 |
|
| 137 |
// Access Control settings |
| 138 |
'isShopManagerAllowed' => get_setting(Settings::SETTING_ALLOW_SHOP_MANAGER, false), |
| 139 |
'hiddenPages' => get_setting(Settings::SETTING_HIDDEN_PAGES, []), |
| 140 |
'menuPages' => $this->getMenuPages(), |
| 141 |
]; |
| 142 |
} |
| 143 |
|
| 144 |
private function getMenuPages(): array |
| 145 |
{ |
| 146 |
$pageLabels = Pages::getLabels(); |
| 147 |
|
| 148 |
//todo: need to move shipping list to own template to support it |
| 149 |
unset($pageLabels[Pages::SHIPPING]); |
| 150 |
unset($pageLabels[Pages::HELP]); |
| 151 |
|
| 152 |
return $pageLabels; |
| 153 |
} |
| 154 |
|
| 155 |
private function buildUpgradeTariffUrl(): string |
| 156 |
{ |
| 157 |
$url = 'https://ali2woo.com/pricing/'; |
| 158 |
$purchaseCode = get_setting('item_purchase_code'); |
| 159 |
|
| 160 |
$urlComponents = []; |
| 161 |
|
| 162 |
if (!a2wl_check_defined('A2WL_HIDE_KEY_FIELDS') && $purchaseCode){ |
| 163 |
$urlComponents[] = 'purchase_code=' . esc_attr($purchaseCode); |
| 164 |
} |
| 165 |
|
| 166 |
$urlComponents[] = 'utm_source=lite&utm_medium=upgrade&utm_campaign=' . A2WL()->plugin_slug; |
| 167 |
|
| 168 |
return $url . "?" . implode("&", $urlComponents); |
| 169 |
} |
| 170 |
|
| 171 |
} |
| 172 |
|