| 1 |
<?php |
| 2 |
/** |
| 3 |
* Description of ProductDataTabController |
| 4 |
* |
| 5 |
* @author Ali2Woo Team |
| 6 |
* |
| 7 |
* @autoload: a2wl_admin_init |
| 8 |
* |
| 9 |
* @ajax: true |
| 10 |
*/ |
| 11 |
|
| 12 |
// phpcs:ignoreFile WordPress.Security.NonceVerification.Recommended |
| 13 |
namespace AliNext_Lite;; |
| 14 |
|
| 15 |
use Pages; |
| 16 |
|
| 17 |
class ProductDataTabController extends AbstractController |
| 18 |
{ |
| 19 |
|
| 20 |
public $tab_class = ''; |
| 21 |
public $tab_id = ''; |
| 22 |
public $tab_title = ''; |
| 23 |
public $tab_icon = ''; |
| 24 |
|
| 25 |
protected ProductShippingDataRepository $ProductShippingDataRepository; |
| 26 |
protected ProductShippingDataService $ProductShippingDataService; |
| 27 |
protected Country $CountryModel; |
| 28 |
protected Woocommerce $WoocommerceModel; |
| 29 |
protected WoocommerceService $WoocommerceService; |
| 30 |
|
| 31 |
public function __construct( |
| 32 |
ProductShippingDataRepository $ProductShippingDataRepository, |
| 33 |
ProductShippingDataService $ProductShippingDataService, |
| 34 |
Country $CountryModel, |
| 35 |
Woocommerce $WoocommerceModel, |
| 36 |
WoocommerceService $WoocommerceService |
| 37 |
) { |
| 38 |
parent::__construct(); |
| 39 |
|
| 40 |
$this->ProductShippingDataRepository = $ProductShippingDataRepository; |
| 41 |
$this->ProductShippingDataService = $ProductShippingDataService; |
| 42 |
$this->CountryModel = $CountryModel; |
| 43 |
$this->WoocommerceModel = $WoocommerceModel; |
| 44 |
$this->WoocommerceService = $WoocommerceService; |
| 45 |
|
| 46 |
|
| 47 |
$this->tab_class = 'a2wl_product_data'; |
| 48 |
$this->tab_id = 'a2wl_product_data'; |
| 49 |
$this->tab_title = 'A2WL Data'; |
| 50 |
|
| 51 |
add_action('admin_head', array(&$this, 'on_admin_head')); |
| 52 |
add_action('woocommerce_product_write_panel_tabs', array(&$this, 'product_write_panel_tabs'), 99); |
| 53 |
add_action('woocommerce_product_data_panels', [$this, 'product_data_panel_wrap'], 99); |
| 54 |
add_action('woocommerce_process_product_meta', [$this, 'process_product_meta'], 1, 2); |
| 55 |
add_action('woocommerce_variation_options_pricing', [$this, 'variation_options_pricing'], 20, 3); |
| 56 |
|
| 57 |
add_action('wp_ajax_a2wl_data_remove_deleted_attribute', [$this, 'ajax_remove_deleted_attribute']); |
| 58 |
add_action('wp_ajax_a2wl_data_remove_deleted_variation', [$this, 'ajax_remove_deleted_variation']); |
| 59 |
add_action('wp_ajax_a2wl_data_last_update_clean', [$this, 'ajax_last_update_clean']); |
| 60 |
add_action('wp_ajax_a2wl_update_product_shipping_info_cache', [$this, 'ajax_update_product_shipping_info_cache']); |
| 61 |
add_action('wp_ajax_a2wl_remove_product_shipping_info', [$this, 'ajaxRemoveProductDefaultShipping']); |
| 62 |
} |
| 63 |
|
| 64 |
public function on_admin_head() { |
| 65 |
echo '<style type="text/css">#woocommerce-product-data ul.wc-tabs li.' . $this->tab_class . ' a::before {content: \'\f163\';}</style>'; |
| 66 |
} |
| 67 |
|
| 68 |
public function product_write_panel_tabs() { |
| 69 |
?> |
| 70 |
<li class="<?php echo $this->tab_class; ?>"><a href="#<?php echo $this->tab_id; ?>"><span><?php echo $this->tab_title; ?></span></a></li> |
| 71 |
<?php |
| 72 |
} |
| 73 |
|
| 74 |
public function product_data_panel_wrap(): void |
| 75 |
{ |
| 76 |
?> |
| 77 |
<div id="<?php echo $this->tab_id; ?>" class="panel <?php echo $this->tab_class; ?> woocommerce_options_panel wc-metaboxes-wrapper" style="display:none"> |
| 78 |
<?php $this->render_product_tab_content(); ?> |
| 79 |
</div> |
| 80 |
<?php |
| 81 |
} |
| 82 |
|
| 83 |
private function render_product_tab_content(): void |
| 84 |
{ |
| 85 |
$productId = $_REQUEST['post'] ?? null; |
| 86 |
|
| 87 |
if (!$productId) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
try { |
| 92 |
$ProductShippingData = $this->ProductShippingDataRepository->get($productId); |
| 93 |
} catch (RepositoryException $RepositoryException) { |
| 94 |
error_log($RepositoryException->getMessage()); |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
try { |
| 99 |
$product = $this->WoocommerceService->getProductWithVariations($productId); |
| 100 |
} catch (RepositoryException|ServiceException $Exception) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
$variationList = []; |
| 105 |
if (!empty($product['sku_products']['variations'])) { |
| 106 |
foreach ($product['sku_products']['variations'] as $variation) { |
| 107 |
$variationList[] = [ |
| 108 |
'id' => $variation[ImportedProductService::FIELD_EXTERNAL_SKU_ID], |
| 109 |
'title' => $variation['title'], |
| 110 |
]; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
$variationExternalId = !empty($product[ImportedProductService::FIELD_VARIATION_KEY]) ? |
| 115 |
$product[ImportedProductService::FIELD_VARIATION_KEY] : ''; |
| 116 |
|
| 117 |
|
| 118 |
$shipping_country_from_list = $this->ProductShippingDataService->getCountryFromList($productId); |
| 119 |
|
| 120 |
$this->model_put('shipping_country_from_list', $shipping_country_from_list); |
| 121 |
$this->model_put('ProductShippingData', $ProductShippingData); |
| 122 |
$this->model_put('post_id', $productId); |
| 123 |
$this->model_put('countries', $this->CountryModel->get_countries()); |
| 124 |
$this->model_put('variationExternalId', $variationExternalId); |
| 125 |
$this->model_put('variationList', $variationList); |
| 126 |
|
| 127 |
$this->include_view("product_data_tab.php"); |
| 128 |
} |
| 129 |
|
| 130 |
public function process_product_meta($post_id, $post): void |
| 131 |
{ |
| 132 |
if (isset($_POST['_a2w_external_id'])) { |
| 133 |
update_post_meta($post_id, '_a2w_external_id', $_POST['_a2w_external_id']); |
| 134 |
} else { |
| 135 |
delete_post_meta($post_id, '_a2w_external_id'); |
| 136 |
} |
| 137 |
|
| 138 |
if (isset($_POST['_a2w_orders_count'])) { |
| 139 |
update_post_meta($post_id, '_a2w_orders_count', $_POST['_a2w_orders_count']); |
| 140 |
} else { |
| 141 |
delete_post_meta($post_id, '_a2w_orders_count'); |
| 142 |
} |
| 143 |
|
| 144 |
update_post_meta($post_id, '_a2w_disable_sync', !empty($_POST['_a2w_disable_sync']) ? 1 : 0); |
| 145 |
|
| 146 |
update_post_meta($post_id, '_a2w_disable_var_price_change', !empty($_POST['_a2w_disable_var_price_change']) ? 1 : 0); |
| 147 |
|
| 148 |
update_post_meta($post_id, '_a2w_disable_var_quantity_change', !empty($_POST['_a2w_disable_var_quantity_change']) ? 1 : 0); |
| 149 |
|
| 150 |
update_post_meta($post_id, '_a2w_disable_add_new_variants', !empty($_POST['_a2w_disable_add_new_variants']) ? 1 : 0); |
| 151 |
|
| 152 |
if (!empty($_POST['_a2w_last_update'])) { |
| 153 |
update_post_meta($post_id, '_a2w_last_update', $_POST['_a2w_last_update']); |
| 154 |
} else { |
| 155 |
delete_post_meta($post_id, '_a2w_last_update'); |
| 156 |
} |
| 157 |
if (!empty($_POST['_a2w_reviews_last_update'])) { |
| 158 |
update_post_meta($post_id, '_a2w_reviews_last_update', $_POST['_a2w_reviews_last_update']); |
| 159 |
} else { |
| 160 |
delete_post_meta($post_id, '_a2w_reviews_last_update'); |
| 161 |
} |
| 162 |
if (!empty($_POST['_a2w_review_page'])) { |
| 163 |
update_post_meta($post_id, '_a2w_review_page', $_POST['_a2w_review_page']); |
| 164 |
} else { |
| 165 |
delete_post_meta($post_id, '_a2w_review_page'); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
public function variation_options_pricing($loop, $variation_data, $variation): void |
| 170 |
{ |
| 171 |
if (!empty($variation_data['_aliexpress_regular_price']) || !empty($variation_data['_aliexpress_price'])) { |
| 172 |
|
| 173 |
$helpTip = esc_html__('Source Regular price loaded from Aliexpress', 'ali2woo'); |
| 174 |
$label = sprintf(esc_html__('Aliexpress Regular price (%s)', 'ali2woo'), get_woocommerce_currency_symbol()); |
| 175 |
$value = wc_format_localized_price(is_array($variation_data['_aliexpress_regular_price']) ? $variation_data['_aliexpress_regular_price'][0] : $variation_data['_aliexpress_regular_price']); |
| 176 |
$this->outputSettingRow($label, $value, "form-row-first", $helpTip); |
| 177 |
|
| 178 |
$helpTip = esc_html__('Source Sale price loaded from Aliexpress', 'ali2woo'); |
| 179 |
$label = sprintf(esc_html__('Aliexpress Sale price (%s)', 'ali2woo'), get_woocommerce_currency_symbol()); |
| 180 |
$value = wc_format_localized_price(is_array($variation_data['_aliexpress_price']) ? $variation_data['_aliexpress_price'][0] : $variation_data['_aliexpress_price']); |
| 181 |
$this->outputSettingRow($label, $value, "form-row-last", $helpTip); |
| 182 |
|
| 183 |
$notLoadedText = esc_html__('not loaded', 'ali2woo'); |
| 184 |
|
| 185 |
$helpTip = esc_html__('Source Sku ID loaded from Aliexpress', 'ali2woo'); |
| 186 |
$label = esc_html__('Aliexpress Sku ID', 'ali2woo'); |
| 187 |
$value = !empty($variation_data['_a2w_ali_sku_id'][0]) ? $variation_data['_a2w_ali_sku_id'][0] : $notLoadedText; |
| 188 |
$this->outputSettingRow($label, $value, "form-row-first", $helpTip); |
| 189 |
|
| 190 |
$helpTip = esc_html__('Source Sku properties loaded from Aliexpress', 'ali2woo'); |
| 191 |
$label = esc_html__('Aliexpress Sku Props', 'ali2woo'); |
| 192 |
$value = !empty($variation_data['_aliexpress_sku_props'][0]) ? $variation_data['_aliexpress_sku_props'][0] : $notLoadedText; |
| 193 |
$this->outputSettingRow($label, $value, "form-row-last", $helpTip); |
| 194 |
|
| 195 |
$helpTip = esc_html__('All attributes included to this variation', 'ali2woo'); |
| 196 |
$label = esc_html__('Attributes', 'ali2woo'); |
| 197 |
$values = []; |
| 198 |
foreach ($variation_data as $itemKey => $dataItem) { |
| 199 |
if (str_starts_with($itemKey, 'attribute_pa_')) { |
| 200 |
$attrLabel = str_replace('attribute_pa_', '', $itemKey); |
| 201 |
$values[] = $attrLabel . ': ' . (!empty($dataItem[0]) ? $dataItem[0] : ''); |
| 202 |
} |
| 203 |
} |
| 204 |
$value = implode('; ', $values); |
| 205 |
$this->outputSettingRow($label, $value, "", $helpTip); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
public function ajax_remove_deleted_attribute(): void |
| 210 |
{ |
| 211 |
check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE); |
| 212 |
|
| 213 |
if (!PageGuardHelper::canAccessPage(Pages::IMPORT_LIST)) { |
| 214 |
$result = ResultBuilder::buildError($this->getErrorTextNoPermissions()); |
| 215 |
echo wp_json_encode($result); |
| 216 |
wp_die(); |
| 217 |
} |
| 218 |
|
| 219 |
if (!empty($_POST['post_id']) && !empty($_POST['id'])) { |
| 220 |
$deleted_variations_attributes = get_post_meta($_POST['post_id'], '_a2w_deleted_variations_attributes', true); |
| 221 |
if ($deleted_variations_attributes) { |
| 222 |
foreach ($deleted_variations_attributes as $k => $a) { |
| 223 |
if ($_POST['id'] == 'all' || $k == sanitize_title($_POST['id'])) { |
| 224 |
unset($deleted_variations_attributes[$k]); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
update_post_meta($_POST['post_id'], '_a2w_deleted_variations_attributes', $deleted_variations_attributes); |
| 229 |
} |
| 230 |
|
| 231 |
echo wp_json_encode(ResultBuilder::buildOk()); |
| 232 |
wp_die(); |
| 233 |
} |
| 234 |
|
| 235 |
public function ajax_remove_deleted_variation(): void |
| 236 |
{ |
| 237 |
check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE); |
| 238 |
|
| 239 |
if (!PageGuardHelper::canAccessPage(Pages::IMPORT_LIST)) { |
| 240 |
$result = ResultBuilder::buildError($this->getErrorTextNoPermissions()); |
| 241 |
echo wp_json_encode($result); |
| 242 |
wp_die(); |
| 243 |
} |
| 244 |
|
| 245 |
if (!empty($_POST['post_id'])) { |
| 246 |
$a2wl_skip_meta = get_post_meta($_POST['post_id'], "_a2w_skip_meta", true); |
| 247 |
$a2wl_skip_meta = $a2wl_skip_meta?$a2wl_skip_meta:array('skip_vars' => array(), 'skip_images' => array()); |
| 248 |
if ($_POST['id']=='all') { |
| 249 |
$a2wl_skip_meta['skip_vars'] = array(); |
| 250 |
} else { |
| 251 |
$a2wl_skip_meta['skip_vars'] = array_filter(array_diff($a2wl_skip_meta['skip_vars'], array($_POST['id']))); |
| 252 |
} |
| 253 |
update_post_meta($_POST['post_id'], "_a2w_skip_meta", $a2wl_skip_meta); |
| 254 |
} |
| 255 |
|
| 256 |
echo wp_json_encode(ResultBuilder::buildOk()); |
| 257 |
wp_die(); |
| 258 |
} |
| 259 |
|
| 260 |
public function ajax_last_update_clean(): void |
| 261 |
{ |
| 262 |
check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE); |
| 263 |
|
| 264 |
if (!PageGuardHelper::canAccessPage(Pages::IMPORT_LIST)) { |
| 265 |
$result = ResultBuilder::buildError($this->getErrorTextNoPermissions()); |
| 266 |
echo wp_json_encode($result); |
| 267 |
wp_die(); |
| 268 |
} |
| 269 |
|
| 270 |
if (!empty($_POST['post_id']) && !empty($_POST['type'])) { |
| 271 |
if ($_POST['type'] === 'product') { |
| 272 |
delete_post_meta($_POST['post_id'], '_a2w_last_update'); |
| 273 |
} else if($_POST['type'] === 'review') { |
| 274 |
delete_post_meta($_POST['post_id'], '_a2w_reviews_last_update'); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
echo wp_json_encode(ResultBuilder::buildOk()); |
| 279 |
wp_die(); |
| 280 |
} |
| 281 |
|
| 282 |
public function ajax_update_product_shipping_info_cache(): void |
| 283 |
{ |
| 284 |
check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE); |
| 285 |
|
| 286 |
if (!PageGuardHelper::canAccessPage(Pages::IMPORT_LIST)) { |
| 287 |
$result = ResultBuilder::buildError($this->getErrorTextNoPermissions()); |
| 288 |
echo wp_json_encode($result); |
| 289 |
wp_die(); |
| 290 |
} |
| 291 |
|
| 292 |
$isValidOperation = !empty($_POST['id']) && isset($_POST['cost']) && !empty($_POST['country_to']) && |
| 293 |
!empty($_POST['method']) && !empty($_POST['items']); |
| 294 |
|
| 295 |
if ($isValidOperation) { |
| 296 |
$wc_product_id = intval($_POST['id']); |
| 297 |
|
| 298 |
$cost = floatval($_POST['cost']); |
| 299 |
$method = sanitize_text_field($_POST['method']); |
| 300 |
$countryTo = sanitize_text_field($_POST['country_to']); |
| 301 |
$countryFrom = isset($_POST['country_from']) ? sanitize_text_field($_POST['country_from']) : null; |
| 302 |
$items = rest_sanitize_array($_POST['items']); |
| 303 |
$variationKey = isset($_POST['variation_key']) ? sanitize_text_field($_POST['variation_key']) : null; |
| 304 |
|
| 305 |
try { |
| 306 |
$ProductShippingData = $this->ProductShippingDataRepository->get($wc_product_id); |
| 307 |
$ProductShippingData |
| 308 |
->setCost($cost) |
| 309 |
->setMethod($method) |
| 310 |
->setCountryTo($countryTo) |
| 311 |
->setCountryFrom($countryFrom) |
| 312 |
->setItems(1, $countryFrom, $countryTo, $items) |
| 313 |
->setVariationKey($variationKey); |
| 314 |
|
| 315 |
$this->ProductShippingDataRepository->save($wc_product_id, $ProductShippingData); |
| 316 |
} catch (RepositoryException $RepositoryException) { |
| 317 |
error_log($RepositoryException->getMessage()); |
| 318 |
$errorText = esc_html__( |
| 319 |
'Repository error: can`t update product shipping cache', 'ali2woo' |
| 320 |
); |
| 321 |
echo wp_json_encode(ResultBuilder::buildError($errorText)); |
| 322 |
wp_die(); |
| 323 |
} |
| 324 |
|
| 325 |
echo wp_json_encode(ResultBuilder::buildOk()); |
| 326 |
} else { |
| 327 |
$errorText = esc_html__( |
| 328 |
'ProductDataTabController error: can`t update product shipping cache', 'ali2woo' |
| 329 |
); |
| 330 |
echo wp_json_encode(ResultBuilder::buildError($errorText)); |
| 331 |
} |
| 332 |
|
| 333 |
wp_die(); |
| 334 |
} |
| 335 |
|
| 336 |
public function ajaxRemoveProductDefaultShipping(): void |
| 337 |
{ |
| 338 |
check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE); |
| 339 |
|
| 340 |
if (!PageGuardHelper::canAccessPage(Pages::IMPORT_LIST)) { |
| 341 |
$result = ResultBuilder::buildError($this->getErrorTextNoPermissions()); |
| 342 |
echo wp_json_encode($result); |
| 343 |
wp_die(); |
| 344 |
} |
| 345 |
|
| 346 |
if (empty($_POST['id'])) { |
| 347 |
$errorText = esc_html__( |
| 348 |
'Can`t reset product shipping data: wrong params', |
| 349 |
'ali2woo' |
| 350 |
); |
| 351 |
$result = ResultBuilder::buildError($errorText); |
| 352 |
echo wp_json_encode($result); |
| 353 |
wp_die(); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
$productId = intval($_POST['id']); |
| 358 |
|
| 359 |
try { |
| 360 |
$this->ProductShippingDataService->resetProductDefaultShipping($productId); |
| 361 |
} |
| 362 |
catch (RepositoryException $RepositoryException) { |
| 363 |
error_log($RepositoryException->getMessage()); |
| 364 |
|
| 365 |
$errorText = esc_html__( |
| 366 |
'Can`t reset product shipping data: repository error', |
| 367 |
'ali2woo' |
| 368 |
); |
| 369 |
|
| 370 |
$result = ResultBuilder::buildError($errorText); |
| 371 |
echo wp_json_encode($result); |
| 372 |
wp_die(); |
| 373 |
} |
| 374 |
|
| 375 |
echo wp_json_encode(ResultBuilder::buildOk()); |
| 376 |
wp_die(); |
| 377 |
} |
| 378 |
|
| 379 |
private function outputSettingRow(string $label, string $value, string $class = "", string $helpTip = ""): void |
| 380 |
{ |
| 381 |
echo '<p class="form-field form-row ' . $class . '">'; |
| 382 |
if (!empty($value)) { |
| 383 |
echo '<label style="cursor: inherit;">' . $label . ':</label> <label style="cursor: inherit;">' . $value . '</label>'; |
| 384 |
} |
| 385 |
if (!empty($helpTip)) { |
| 386 |
echo '<span class="woocommerce-help-tip" data-tip="' . $helpTip . '"></span>'; |
| 387 |
} |
| 388 |
echo '</p>'; |
| 389 |
} |
| 390 |
} |
| 391 |
|