| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of ImportPageController |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
* |
| 8 |
* @autoload: a2wl_admin_init |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace AliNext_Lite;; |
| 12 |
|
| 13 |
use Pages; |
| 14 |
|
| 15 |
class ImportPageController extends AbstractAdminPage |
| 16 |
{ |
| 17 |
|
| 18 |
public const COOKIE_IMPORT_SORT = 'a2wl_import_sort'; |
| 19 |
|
| 20 |
|
| 21 |
public function __construct( |
| 22 |
protected Woocommerce $WoocommerceModel, |
| 23 |
protected ImportListService $ImportListService, |
| 24 |
protected AliexpressRegionRepository $AliexpressRegionRepository, |
| 25 |
protected PermanentAlertService $PermanentAlertService, |
| 26 |
protected ProductImport $ProductImport, |
| 27 |
protected Country $Country, |
| 28 |
protected Override $Override, |
| 29 |
protected TipOfDayService $TipOfDayService, |
| 30 |
|
| 31 |
protected PromoService $PromoService, |
| 32 |
|
| 33 |
) { |
| 34 |
|
| 35 |
parent::__construct( |
| 36 |
Pages::getLabel(Pages::IMPORT_LIST), |
| 37 |
Pages::getLabel(Pages::IMPORT_LIST) . ' ' . $this->getImportListItemCountHtml(), |
| 38 |
Capability::pluginAccess(), |
| 39 |
Pages::IMPORT_LIST, |
| 40 |
20 |
| 41 |
); |
| 42 |
|
| 43 |
add_filter('tiny_mce_before_init', array($this, 'tiny_mce_before_init'), 30); |
| 44 |
add_filter('a2wl_configure_lang_data', array($this, 'configure_lang_data'), 30); |
| 45 |
add_action('admin_enqueue_scripts', [$this, 'assets']); |
| 46 |
} |
| 47 |
|
| 48 |
public function assets(): void |
| 49 |
{ |
| 50 |
if ($this->is_current_page()) { |
| 51 |
wp_enqueue_script( |
| 52 |
'a2wl-fancybox', |
| 53 |
A2WL()->plugin_url() . '/assets/js/fancybox/fancybox.umd.js', |
| 54 |
[], A2WL()->version, true |
| 55 |
); |
| 56 |
|
| 57 |
wp_enqueue_style( |
| 58 |
'a2wl-fancybox-style', |
| 59 |
A2WL()->plugin_url() . '/assets/css/fancybox/fancybox.css', |
| 60 |
[], A2WL()->version |
| 61 |
); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public function configure_lang_data($data) { |
| 66 |
$data['attr_new_name'] = esc_html__('New name', 'ali2woo'); |
| 67 |
$data['attr_name_duplicate_error'] = esc_html__('this name is already used', 'ali2woo'); |
| 68 |
|
| 69 |
return $data; |
| 70 |
} |
| 71 |
|
| 72 |
public function before_admin_render(): void |
| 73 |
{ |
| 74 |
if (!empty($_REQUEST['delete_id']) || !empty($_REQUEST['action']) || !empty($_REQUEST['action2'])) { |
| 75 |
check_admin_referer(self::PAGE_NONCE_ACTION, self::NONCE); |
| 76 |
} |
| 77 |
|
| 78 |
if (!PageGuardHelper::canAccessPage(Pages::IMPORT_LIST)) { |
| 79 |
wp_die($this->getErrorTextNoPermissions()); |
| 80 |
} |
| 81 |
|
| 82 |
if (isset($_REQUEST['delete_id']) && $_REQUEST['delete_id']) { |
| 83 |
if ($product = $this->ProductImport->get_product($_REQUEST['delete_id'])) { |
| 84 |
foreach ($product['tmp_edit_images'] as $edit_image) { |
| 85 |
if (isset($edit_image['attachment_id'])) { |
| 86 |
Utils::delete_attachment($edit_image['attachment_id'], true); |
| 87 |
} |
| 88 |
} |
| 89 |
$this->ProductImport->del_product($_REQUEST['delete_id']); |
| 90 |
} |
| 91 |
wp_redirect(admin_url('admin.php?page=a2wl_import')); |
| 92 |
} else if ((isset($_REQUEST['action']) && $_REQUEST['action'] == "delete_all") || (isset($_REQUEST['action2']) && $_REQUEST['action2'] == "delete_all")) { |
| 93 |
$product_ids = $this->ProductImport->get_product_id_list(); |
| 94 |
|
| 95 |
foreach ($product_ids as $product_id) { |
| 96 |
if ($product = $this->ProductImport->get_product($product_id)) { |
| 97 |
foreach ($product['tmp_edit_images'] as $edit_image) { |
| 98 |
if (isset($edit_image['attachment_id'])) { |
| 99 |
Utils::delete_attachment($edit_image['attachment_id'], true); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
$this->ProductImport->del_product($product_ids); |
| 106 |
|
| 107 |
wp_redirect(admin_url('admin.php?page=a2wl_import')); |
| 108 |
} else if ((isset($_REQUEST['action']) && $_REQUEST['action'] == "push_all") || (isset($_REQUEST['action2']) && $_REQUEST['action2'] == "push_all")) { |
| 109 |
// push all |
| 110 |
wp_redirect(admin_url('admin.php?page=a2wl_import')); |
| 111 |
} else if (((isset($_REQUEST['action']) && $_REQUEST['action'] == "delete") || (isset($_REQUEST['action2']) && $_REQUEST['action2'] == "delete")) && isset($_REQUEST['gi']) && is_array($_REQUEST['gi']) && $_REQUEST['gi']) { |
| 112 |
$this->ProductImport->del_product($_REQUEST['gi']); |
| 113 |
|
| 114 |
wp_redirect(admin_url('admin.php?page=a2wl_import')); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
public function render($params = []): void |
| 119 |
{ |
| 120 |
if (!empty($_REQUEST['s']) || !empty($_REQUEST['o']) || !empty($_REQUEST['cur_page'])) { |
| 121 |
check_admin_referer(self::PAGE_NONCE_ACTION, self::NONCE); |
| 122 |
} |
| 123 |
|
| 124 |
if (!PageGuardHelper::canAccessPage(Pages::IMPORT_LIST)) { |
| 125 |
wp_die($this->getErrorTextNoPermissions()); |
| 126 |
} |
| 127 |
|
| 128 |
$search_query = !empty($_REQUEST['s']) ? sanitize_text_field($_REQUEST['s']) : ''; |
| 129 |
|
| 130 |
$sort_query = $this->resolveSortQuery(); |
| 131 |
|
| 132 |
$products_cnt = $this->ProductImport->get_products_count(); |
| 133 |
$paginator = Paginator::build($products_cnt); |
| 134 |
|
| 135 |
if (a2wl_check_defined('A2WL_SKIP_IMPORT_SORTING')) { |
| 136 |
$product_list = $this->ProductImport->get_product_list( |
| 137 |
true, $search_query, |
| 138 |
$sort_query, $paginator['per_page'], |
| 139 |
($paginator['cur_page'] - 1) * $paginator['per_page'] |
| 140 |
); |
| 141 |
} else { |
| 142 |
$product_list_all = $this->ProductImport->get_product_list(true, $search_query, $sort_query); |
| 143 |
$product_list = array_slice( |
| 144 |
$product_list_all, |
| 145 |
$paginator['per_page'] * ($paginator['cur_page'] - 1), |
| 146 |
$paginator['per_page'] |
| 147 |
); |
| 148 |
unset($product_list_all); |
| 149 |
} |
| 150 |
foreach ($product_list as &$product) { |
| 151 |
$this->prepareProduct($product); |
| 152 |
} |
| 153 |
|
| 154 |
$productShippingFromList = $this->prepareShippingFromList($product_list); |
| 155 |
|
| 156 |
$page = !empty($_REQUEST['page']) ? sanitize_text_field($_REQUEST['page']) : ''; |
| 157 |
|
| 158 |
$product_links = $this->prepareProductLinks($product_list, $page); |
| 159 |
|
| 160 |
$links = [ |
| 161 |
'remove_all_products_link' => |
| 162 |
$this->getRemoveAllProductsLink($page) |
| 163 |
]; |
| 164 |
|
| 165 |
$this->modelPutArray([ |
| 166 |
'paginator' => $paginator, |
| 167 |
'search_query' => $search_query, |
| 168 |
'sort_query' => $sort_query, |
| 169 |
'sort_list' => $this->ProductImport->sort_list(), |
| 170 |
'product_list' => $product_list, |
| 171 |
'product_links' => $product_links, |
| 172 |
'links' => $links, |
| 173 |
'productShippingFromList'=> $productShippingFromList, |
| 174 |
'localizator' => AliexpressLocalizator::getInstance(), |
| 175 |
'categories' => $this->WoocommerceModel->get_categories(), |
| 176 |
'countries' => $this->Country->get_countries(), |
| 177 |
'override_model' => $this->Override, |
| 178 |
'TipOfDay' => $this->TipOfDayService->getNextTip(), |
| 179 |
|
| 180 |
'promo_data' => $this->PromoService->getPromoData(), |
| 181 |
|
| 182 |
'PermanentAlerts' => $this->PermanentAlertService->getAll(), |
| 183 |
|
| 184 |
]); |
| 185 |
|
| 186 |
if (A2WL()->isFreePlugin()) { |
| 187 |
$this->modelPutArray([ |
| 188 |
'aliexpressRegion' => 'US', |
| 189 |
'aliexpressRegions' => ['US' => 'United States'], |
| 190 |
'defaultShippingLabel' => $this->getDefaultShippingLabel(), |
| 191 |
'countryToCode' => get_setting('aliship_shipto', 'US'), |
| 192 |
'applyShippingScopes' => [], |
| 193 |
]); |
| 194 |
} |
| 195 |
|
| 196 |
$this->include_view("import.php"); |
| 197 |
} |
| 198 |
|
| 199 |
public function tiny_mce_before_init($initArray) |
| 200 |
{ |
| 201 |
if ($this->is_current_page()) { |
| 202 |
$initArray['setup'] = 'function(ed) {ed.on("change", function(e) { a2wl_update_product(e.target.id, { description:encodeURIComponent(e.target.getContent())}); });}'; |
| 203 |
} |
| 204 |
return $initArray; |
| 205 |
} |
| 206 |
|
| 207 |
private function prepareProduct(array &$product): void |
| 208 |
{ |
| 209 |
if (empty($product['sku_products'])) { |
| 210 |
$product['sku_products'] = [ |
| 211 |
'variations' => [], |
| 212 |
'attributes' => [] |
| 213 |
]; |
| 214 |
} |
| 215 |
|
| 216 |
$tmp_all_images = Utils::get_all_images_from_product($product); |
| 217 |
|
| 218 |
if (empty($product['description'])) { |
| 219 |
$product['description'] = ''; |
| 220 |
} |
| 221 |
|
| 222 |
$product['gallery_images'] = []; |
| 223 |
$product['variant_images'] = []; |
| 224 |
$product['description_images'] = []; |
| 225 |
|
| 226 |
foreach ($tmp_all_images as $img_id => $img) { |
| 227 |
if ($img['type'] === 'gallery') { |
| 228 |
$product['gallery_images'][$img_id] = $img['image']; |
| 229 |
} else if ($img['type'] === 'variant') { |
| 230 |
$product['variant_images'][$img_id] = $img['image']; |
| 231 |
} else if ($img['type'] === 'description') { |
| 232 |
$product['description_images'][$img_id] = $img['image']; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
$this->mergeTmpImages($product, $tmp_all_images, 'tmp_copy_images'); |
| 237 |
$this->mergeTmpImages($product, $tmp_all_images, 'tmp_move_images'); |
| 238 |
|
| 239 |
if (!isset($product['thumb_id']) && $product['gallery_images']) { |
| 240 |
$k = array_keys($product['gallery_images']); |
| 241 |
$product['thumb_id'] = $k[0]; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
private function prepareShippingFromList(array $productList): array |
| 246 |
{ |
| 247 |
return array_map(function ($item) { |
| 248 |
return $this->ImportListService->getCountryFromList($item); |
| 249 |
}, $productList); |
| 250 |
} |
| 251 |
|
| 252 |
private function prepareProductLinks($productList, $page): array |
| 253 |
{ |
| 254 |
$result = []; |
| 255 |
|
| 256 |
foreach ($productList as $index => $item) { |
| 257 |
$result[$index]['remove_product_link'] = |
| 258 |
$this->getRemoveProductLink($page, $item); |
| 259 |
|
| 260 |
if (!empty($item['store_id']) && !empty($item['seller_id'])) { |
| 261 |
$result[$index]['find_all_products_in_store_link'] = |
| 262 |
$this->getFindAllProductInStoreLink($item); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
return $result; |
| 267 |
} |
| 268 |
|
| 269 |
private function mergeTmpImages(array &$product, array $tmp_all_images, string $field): void |
| 270 |
{ |
| 271 |
if (empty($product[$field]) || !is_array($product[$field])) { |
| 272 |
return; |
| 273 |
} |
| 274 |
|
| 275 |
foreach ($product[$field] as $img_id => $source) { |
| 276 |
if (isset($tmp_all_images[$img_id])) { |
| 277 |
$product['gallery_images'][$img_id] = $tmp_all_images[$img_id]['image']; |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
private function resolveSortQuery(): string |
| 283 |
{ |
| 284 |
$sort_query = null; |
| 285 |
|
| 286 |
if (!empty($_REQUEST['o'])) { |
| 287 |
$sort_query = sanitize_text_field($_REQUEST['o']); |
| 288 |
$sort_query = $this->normalizeSortValue($sort_query); |
| 289 |
Utils::setAdminCookie(self::COOKIE_IMPORT_SORT, $sort_query); |
| 290 |
} elseif (!empty($_COOKIE[self::COOKIE_IMPORT_SORT])) { |
| 291 |
$sort_query = sanitize_text_field($_COOKIE[self::COOKIE_IMPORT_SORT]); |
| 292 |
$sort_query = $this->normalizeSortValue($sort_query); |
| 293 |
} |
| 294 |
|
| 295 |
return $sort_query ?: $this->ProductImport->default_sort(); |
| 296 |
} |
| 297 |
|
| 298 |
private function normalizeSortValue(string $sortValue): string |
| 299 |
{ |
| 300 |
$allowed_sorts = array_keys($this->ProductImport->sort_list()); |
| 301 |
if (!in_array($sortValue, $allowed_sorts, true)) { |
| 302 |
$sortValue = $this->ProductImport->default_sort(); |
| 303 |
} |
| 304 |
|
| 305 |
return $sortValue; |
| 306 |
} |
| 307 |
|
| 308 |
private function getDefaultShippingLabel(): string |
| 309 |
{ |
| 310 |
$shippingOptions = Utils::get_aliexpress_shipping_options(); |
| 311 |
$currentShippingCode = get_setting('fulfillment_prefship', 'CAINIAO_PREMIUM'); |
| 312 |
foreach ($shippingOptions as $shipping_option) { |
| 313 |
if ($currentShippingCode === $shipping_option['value']) { |
| 314 |
return $shipping_option['label']; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
return ''; |
| 319 |
} |
| 320 |
|
| 321 |
private function getFindAllProductInStoreLink(array $product): string |
| 322 |
{ |
| 323 |
$url = admin_url('admin.php?page=a2wl_store') . |
| 324 |
'&a2wl_store_id=' . $product['store_id'] . |
| 325 |
'&a2wl_seller_id=' . $product['seller_id'] . |
| 326 |
'&a2wl_search=1'; |
| 327 |
|
| 328 |
return wp_nonce_url($url, self::PAGE_NONCE_ACTION, self::NONCE); |
| 329 |
} |
| 330 |
|
| 331 |
private function getRemoveProductLink(string $page, array $product): string |
| 332 |
{ |
| 333 |
$url = admin_url('admin.php?page=' . $page) . |
| 334 |
'&delete_id=' . $product['import_id']; |
| 335 |
|
| 336 |
return wp_nonce_url($url, self::PAGE_NONCE_ACTION, self::NONCE); |
| 337 |
} |
| 338 |
|
| 339 |
private function getRemoveAllProductsLink(string $page): string |
| 340 |
{ |
| 341 |
$url = admin_url('admin.php?page=' . $page) . |
| 342 |
'&action=delete_all'; |
| 343 |
|
| 344 |
return wp_nonce_url($url, self::PAGE_NONCE_ACTION, self::NONCE); |
| 345 |
} |
| 346 |
|
| 347 |
private function getImportListItemCountHtml(): string |
| 348 |
{ |
| 349 |
$products_cnt = 0; |
| 350 |
if (is_admin()) { |
| 351 |
$products_cnt = $this->ProductImport->get_products_count(); |
| 352 |
} |
| 353 |
|
| 354 |
$itemCountText = ''; |
| 355 |
if ($products_cnt) { |
| 356 |
$itemCountText = ' <span class="update-plugins count-' . |
| 357 |
$products_cnt . '"><span class="plugin-count">' . $products_cnt . '</span></span>'; |
| 358 |
} |
| 359 |
|
| 360 |
return $itemCountText; |
| 361 |
} |
| 362 |
|
| 363 |
} |
| 364 |
|