product_import_model = new A2WL_ProductImport();
$this->account = A2WL_Account::getInstance();
}
public function load_products($filter, $page = 1, $per_page = 20, $params = array()) {
/** @var wpdb $wpdb */
global $wpdb;
$products_in_import = $this->product_import_model->get_product_id_list();
$request_url = A2WL_RequestHelper::build_request('get_products', array_merge(array('page' => $page, 'per_page' => $per_page), $filter));
$request = a2wl_remote_get($request_url);
if (is_wp_error($request)) {
$result = A2WL_ResultBuilder::buildError($request->get_error_message());
} else if (intval($request['response']['code']) != 200) {
$result = A2WL_ResultBuilder::buildError($request['response']['code'] . " " . $request['response']['message']);
} else {
$result = json_decode($request['body'], true);
if (isset($result['state']) && $result['state'] !== 'error') {
$default_type = a2wl_get_setting('default_product_type');
$default_status = a2wl_get_setting('default_product_status');
$tmp_urls = array();
foreach ($result['products'] as &$product) {
$product['post_id'] = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_a2wl_external_id' AND meta_value='%s' LIMIT 1", $product['id']));
$product['import_id'] = in_array($product['id'], $products_in_import) ? $product['id'] : 0;
$product['product_type'] = $default_type;
$product['product_status'] = $default_status;
$product['is_affiliate'] = true;
if (isset($filter['country']) && $filter['country']) {
$product['shipping_to_country'] = $filter['country'];
}
$tmp_urls[] = $product['url'];
}
}
}
return $result;
}
public function load_product($product_id, $params = array()) {
/** @var wpdb $wpdb */
global $wpdb;
$products_in_import = $this->product_import_model->get_product_id_list();
$request_url = A2WL_RequestHelper::build_request('get_product', array('product_id' => $product_id));
$request = a2wl_remote_get($request_url);
if (is_wp_error($request)) {
$result = A2WL_ResultBuilder::buildError($request->get_error_message());
} else {
$result = json_decode($request['body'], true);
if ($result['state'] !== 'error') {
$result['product']['post_id'] = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_a2wl_external_id' AND meta_value='%s' LIMIT 1", $result['product']['id']));
$result['product']['import_id'] = in_array($result['product']['id'], $products_in_import) ? $result['product']['id'] : 0;
if (a2wl_get_setting('use_random_stock')) {
$result['product']['disable_var_quantity_change'] = true;
foreach ($result['product']['sku_products']['variations'] as &$variation) {
$variation['original_quantity'] = intval($variation['quantity']);
$tmp_quantity = rand(intval(a2wl_get_setting('use_random_stock_min')), intval(a2wl_get_setting('use_random_stock_max')));
$tmp_quantity = ($tmp_quantity > $variation['original_quantity']) ? $variation['original_quantity'] : $tmp_quantity;
$variation['quantity'] = $tmp_quantity;
}
}
$request_url = 'https://m.aliexpress.com/ajaxapi/productItemDescripitonAjax.do?productId=' . $result['product']['id'] . '&lang=' . A2WL_AliexpressLocalizator::getInstance()->language;
$response = a2wl_remote_get($request_url, array('cookies' => A2WL_AliexpressLocalizator::getInstance()->getLocaleCookies()));
if (!is_wp_error($response)) {
try {
$item = json_decode($response['body'], true);
if (!empty($item['descResult']['props'])) {
$split_attribute_values = a2wl_get_setting('split_attribute_values');
$attribute_values_separator = a2wl_get_setting('attribute_values_separator');
$result['product']['attribute'] = array();
foreach ($item['descResult']['props'] as $prop) {
if ($split_attribute_values) {
$result['product']['attribute'][] = array('name' => $prop['attrName'], 'value' => array_map('a2wl_phrase_apply_filter_to_text', array_map('trim', explode($attribute_values_separator, $prop['attrValue']))));
} else {
$result['product']['attribute'][] = array('name' => $prop['attrName'], 'value' => a2wl_phrase_apply_filter_to_text($prop['attrValue']));
}
}
}
} catch (Exception $e) {
error_log($e->getMessage());
}
} else {
error_log("Load product attribute error: " . $response->get_error_message($response->get_error_code()));
}
$result['product']['description'] = '';
if (a2wl_check_defined('A2WL_SAVE_ATTRIBUTE_AS_DESCRIPTION')) {
if ($result['product']['attribute'] && count($result['product']['attribute']) > 0) {
$result['product']['description'] .= '
';
foreach ($result['product']['attribute'] as $attribute) {
$result['product']['description'] .= '| ' . $attribute['name'] . ' | ' . (is_array($attribute['value']) ? implode(", ", $attribute['value']) : $attribute['value']) . " |
";
}
$result['product']['description'] .= '
';
}
// Uncoment if need empty attribute list
//$result['product']['attribute'] = array();
}
if (!a2wl_get_setting('not_import_description')) {
// alternativ method
//https://m.ru.aliexpress.com/item-desc/32797801052.html
$request_url = "https://" . (A2WL_AliexpressLocalizator::getInstance()->language === 'en' ? "www" : A2WL_AliexpressLocalizator::getInstance()->language) . ".aliexpress.com/getDescModuleAjax.htm?productId=" . $result['product']['id'] . "&t=" . (round(microtime(true), 3) * 1000);
$response = a2wl_remote_get($request_url, array('cookies' => A2WL_AliexpressLocalizator::getInstance()->getLocaleCookies()));
if (!is_wp_error($response)) {
$body = $response['body'];
$desc_content = str_replace(array("window.productDescription='", "';"), '', $body);
$result['product']['description'] .= $this->clean_description($desc_content);
} else {
error_log("Load product description error: " . $response->get_error_message($response->get_error_code()));
}
}
$result['product']['description'] = A2WL_PhraseFilter::apply_filter_to_text($result['product']['description']);
$tmp_all_images = A2WL_Utils::get_all_images_from_product($result['product']);
$not_import_gallery_images = false;
$not_import_variant_images = false;
$not_import_description_images = a2wl_get_setting('not_import_description_images');
$result['product']['skip_images'] = array();
foreach ($tmp_all_images as $img_id => $img) {
if (!in_array($img_id, $result['product']['skip_images']) && (($not_import_gallery_images && $img['type'] === 'gallery') || ($not_import_variant_images && $img['type'] === 'variant') || ($not_import_description_images && $img['type'] === 'description'))) {
$result['product']['skip_images'][] = $img_id;
}
}
}
}
return $result;
}
public function sync_products($product_ids, $params = array()) {
$request_params = array('product_id' => implode(',', is_array($product_ids) ? $product_ids : array($product_ids)));
if (!empty($params['manual_update'])) {
$request_params['manual_update'] = 1;
}
if (!empty($params['pc'])) {
$request_params['pc'] = $params['pc'];
}
$request_url = A2WL_RequestHelper::build_request('sync_products', $request_params);
$request = a2wl_remote_get($request_url);
if (is_wp_error($request)) {
$result = A2WL_ResultBuilder::buildError($request->get_error_message());
} else {
$result = json_decode($request['body'], true);
$use_random_stock = a2wl_get_setting('use_random_stock');
if ($use_random_stock) {
$random_stock_min = intval(a2wl_get_setting('use_random_stock_min'));
$random_stock_max = intval(a2wl_get_setting('use_random_stock_max'));
foreach ($result['products'] as &$product) {
foreach ($product['sku_products']['variations'] as &$variation) {
$variation['original_quantity'] = intval($variation['quantity']);
$tmp_quantity = rand($random_stock_min, $random_stock_max);
$tmp_quantity = ($tmp_quantity > $variation['original_quantity']) ? $variation['original_quantity'] : $tmp_quantity;
$variation['quantity'] = $tmp_quantity;
}
}
}
if (isset($params['manual_update']) && $params['manual_update'] && a2wl_check_defined('A2WL_FIX_RELOAD_DESCRIPTION') && !a2wl_get_setting('not_import_description')) {
foreach ($result["products"] as &$product) {
$request_url = "https://" . (A2WL_AliexpressLocalizator::getInstance()->language === 'en' ? "www" : A2WL_AliexpressLocalizator::getInstance()->language) . ".aliexpress.com/getDescModuleAjax.htm?productId=" . $product['id'] . "&t=" . (round(microtime(true), 3) * 1000);
$response = a2wl_remote_get($request_url, array('cookies' => A2WL_AliexpressLocalizator::getInstance()->getLocaleCookies()));
if (!is_wp_error($response)) {
$body = $response['body'];
$desc_content = str_replace(array("window.productDescription='", "';"), '', $body);
$product['description'] .= $this->clean_description($desc_content);
} else {
error_log("Load product description error: " . $response->get_error_message($response->get_error_code()));
}
$product['description'] = A2WL_PhraseFilter::apply_filter_to_text($product['description']);
}
}
}
return $result;
}
public static function clean_description($description) {
$html = $description;
if (function_exists('mb_convert_encoding')) {
$html = trim(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
} else {
$html = htmlspecialchars_decode(utf8_decode(htmlentities($html, ENT_COMPAT, 'UTF-8', false)));
}
if (function_exists('libxml_use_internal_errors')) {
libxml_use_internal_errors(true);
}
$dom = new DOMDocument();
@$dom->loadHTML($html);
$dom->formatOutput = true;
$tags = apply_filters('a2wl_clean_description_tags', array('script', 'head', 'meta', 'style', 'map', 'noscript', 'object', 'iframe'));
foreach ($tags as $tag) {
$elements = $dom->getElementsByTagName($tag);
for ($i = $elements->length; --$i >= 0;) {
$e = $elements->item($i);
if ($tag == 'a') {
while ($e->hasChildNodes()) {
$child = $e->removeChild($e->firstChild);
$e->parentNode->insertBefore($child, $e);
}
$e->parentNode->removeChild($e);
} else {
$e->parentNode->removeChild($e);
}
}
}
if (!in_array('img', $tags)) {
$elements = $dom->getElementsByTagName('img');
for ($i = $elements->length; --$i >= 0;) {
$e = $elements->item($i);
$e->setAttribute('src', add_query_arg('descimg', '1', A2WL_Utils::clear_image_url($e->getAttribute('src'))));
}
}
$html = preg_replace('~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', '', $dom->saveHTML());
$html = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $html);
$html = preg_replace('/(<[^>]+) class=".*?"/i', '$1', $html);
$html = preg_replace('/(<[^>]+) width=".*?"/i', '$1', $html);
$html = preg_replace('/(<[^>]+) height=".*?"/i', '$1', $html);
$html = preg_replace('/(<[^>]+) alt=".*?"/i', '$1', $html);
$html = preg_replace('/^/', '$1', str_replace(array('', '', '', ''), '', $html));
$html = preg_replace("/<\/?div[^>]*\>/i", "", $html);
$html = preg_replace('/]*>(.*)<\/a>/iU', '', $html);
$html = preg_replace('/]*><\/a>/iU', '', $html); //delete empty A tags
$html = preg_replace("/<\/?h1[^>]*\>/i", "", $html);
$html = preg_replace("/<\/?strong[^>]*\>/i", "", $html);
$html = preg_replace("/<\/?span[^>]*\>/i", "", $html);
//$html = str_replace(' ', '', $html);
$html = str_replace(' ', ' ', $html);
$html = str_replace('\t', ' ', $html);
$html = str_replace(' ', ' ', $html);
$html = preg_replace("/http:\/\/g(\d+)\.a\./i", "https://ae$1.", $html);
$html = preg_replace("/<[^\/>]*[^td]>([\s]?| )*<\/[^>]*[^td]>/", '', $html); //delete ALL empty tags
$html = preg_replace('/]*><\/td>/iU', '', $html); //delete empty TD tags
$html = str_replace(array('![]() |