| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of ProductImport |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace AliNext_Lite;; |
| 10 |
|
| 11 |
class ProductImport { |
| 12 |
|
| 13 |
private $order; |
| 14 |
private $order_dir; |
| 15 |
|
| 16 |
public function add_product($product) { |
| 17 |
if (!isset($product['import_id']) || !$product['import_id']) { |
| 18 |
// 1.7.0 for compatibility with older versions |
| 19 |
$product['import_id'] = $product[ImportedProductService::FIELD_EXTERNAL_PRODUCT_ID]; |
| 20 |
} |
| 21 |
|
| 22 |
if (!isset($product['date_add'])) { |
| 23 |
$product['date_add'] = gmdate('Y-m-d H:i:s'); |
| 24 |
} |
| 25 |
|
| 26 |
$this->save_product($product['import_id'], $product); |
| 27 |
|
| 28 |
return "ok"; |
| 29 |
} |
| 30 |
|
| 31 |
public function upd_product($product, $merge=true) { |
| 32 |
if(!isset($product['import_id']) || !$product['import_id']){ |
| 33 |
// 1.7.0 for compatibility with older versions |
| 34 |
$product['import_id'] = $product[ImportedProductService::FIELD_EXTERNAL_PRODUCT_ID]; |
| 35 |
} |
| 36 |
|
| 37 |
$product_id_list = $this->get_product_id_list(); |
| 38 |
|
| 39 |
if (in_array($product['import_id'], $product_id_list)) { |
| 40 |
$product = $merge?array_merge($this->get_product($product['import_id']), $product):$product; |
| 41 |
$this->save_product($product['import_id'], $product); |
| 42 |
} |
| 43 |
|
| 44 |
return $product; |
| 45 |
} |
| 46 |
|
| 47 |
public function del_product($product_id, $processing = false) { |
| 48 |
if (!is_array($product_id)) { |
| 49 |
$to_del = array($product_id); |
| 50 |
} else { |
| 51 |
$to_del = $product_id; |
| 52 |
} |
| 53 |
|
| 54 |
if ($to_del) { |
| 55 |
foreach ($to_del as $did) { |
| 56 |
a2wl_delete_transient('a2wl_'.($processing?'processing_':'').'product#' . strval($did)); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public function move_to_processing($product_id) { |
| 62 |
$product = $this->get_product($product_id); |
| 63 |
if($product) { |
| 64 |
$this->save_product($product_id, $product, true); |
| 65 |
$this->del_product($product_id); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
public function save_product($product_id, $product, $processing = false): void |
| 70 |
{ |
| 71 |
$transientName = 'a2wl_' . ($processing ? 'processing_' : '') . 'product#' . strval($product_id); |
| 72 |
a2wl_set_transient($transientName, $product); |
| 73 |
} |
| 74 |
|
| 75 |
public function get_product($product_id, $processing = false) { |
| 76 |
$product = a2wl_get_transient('a2wl_' . ($processing ? 'processing_' : '') . 'product#' . strval($product_id)); |
| 77 |
if ($product) { |
| 78 |
/* if (!isset($product[ImportedProductService::FIELD_EXTERNAL_PRODUCT_ID])) { |
| 79 |
return []; |
| 80 |
}*/ |
| 81 |
if (!isset($product['import_id']) || !$product['import_id']) { |
| 82 |
// 1.7.0 for compatibility with older versions |
| 83 |
$product['import_id'] = $product[ImportedProductService::FIELD_EXTERNAL_PRODUCT_ID]; |
| 84 |
} |
| 85 |
if (!isset($product['product_type'])) { |
| 86 |
$product['product_type'] = get_setting('default_product_type'); |
| 87 |
} |
| 88 |
if (!isset($product['product_status'])) { |
| 89 |
$product['product_status'] = get_setting('default_product_status'); |
| 90 |
} |
| 91 |
if (!isset($product['tags'])) { |
| 92 |
$product['tags'] = []; |
| 93 |
} |
| 94 |
if (!isset($product['categories'])) { |
| 95 |
$product['categories'] = []; |
| 96 |
} |
| 97 |
if (!isset($product['skip_images'])) { |
| 98 |
$product['skip_images'] = []; |
| 99 |
} |
| 100 |
if (!isset($product['skip_vars'])) { |
| 101 |
$product['skip_vars'] = []; |
| 102 |
} |
| 103 |
if (empty($product['images'])) { |
| 104 |
$product['images'] = []; |
| 105 |
} |
| 106 |
if (empty($product['original_attr_cache'])) { |
| 107 |
$product['original_attr_cache'] = []; |
| 108 |
} |
| 109 |
if (!isset($product['disable_sync'])) { |
| 110 |
$product['disable_sync'] = false; |
| 111 |
} |
| 112 |
if (!isset($product['disable_var_price_change'])) { |
| 113 |
$product['disable_var_price_change'] = false; |
| 114 |
} |
| 115 |
if (!isset($product['disable_var_quantity_change'])) { |
| 116 |
$product['disable_var_quantity_change'] = false; |
| 117 |
} |
| 118 |
if (!isset($product['date_add'])) { |
| 119 |
$product['date_add'] = gmdate('1981-12-29'); |
| 120 |
} |
| 121 |
if (!isset($product['tmp_move_images'])) { |
| 122 |
$product['tmp_move_images'] = []; |
| 123 |
} |
| 124 |
if (!isset($product['tmp_copy_images'])) { |
| 125 |
$product['tmp_copy_images'] = []; |
| 126 |
} |
| 127 |
if (!isset($product['tmp_edit_images'])) { |
| 128 |
$product['tmp_edit_images'] = []; |
| 129 |
} |
| 130 |
if (!isset($product[ImportedProductService::FIELD_SHIPPING_INFO])) { |
| 131 |
$product[ImportedProductService::FIELD_SHIPPING_INFO] = []; |
| 132 |
} |
| 133 |
|
| 134 |
return $product; |
| 135 |
} |
| 136 |
|
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @throws ServiceException |
| 142 |
*/ |
| 143 |
public function getProduct($product_id, $processing = false): array |
| 144 |
{ |
| 145 |
$product = $this->get_product($product_id, $processing); |
| 146 |
|
| 147 |
if ($product !== false && is_array($product)) { |
| 148 |
|
| 149 |
return $product; |
| 150 |
} |
| 151 |
|
| 152 |
throw new ServiceException( |
| 153 |
__('Invalid external product ID provided.', 'ali2woo') |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
public function get_product_list($with_html = true, $search = '', $sort = '', $limit = null, $ofset = null): array |
| 159 |
{ |
| 160 |
$product_id_list = $this->get_product_id_list($limit, $ofset); |
| 161 |
$products = array(); |
| 162 |
|
| 163 |
foreach ($product_id_list as $product_id) { |
| 164 |
$product = $this->get_product($product_id); |
| 165 |
if ($product) { |
| 166 |
if (isset($product['html']) && $product['html']) { |
| 167 |
if (!$with_html) { |
| 168 |
$product['html'] = "#hidden#"; |
| 169 |
} |
| 170 |
} else { |
| 171 |
$product['html'] = "#needload#"; |
| 172 |
} |
| 173 |
|
| 174 |
if (empty($search) || str_contains($product['title'], strval($search)) || str_contains($product[ImportedProductService::FIELD_EXTERNAL_PRODUCT_ID], strval($search))) { |
| 175 |
$products[$product_id] = $product; |
| 176 |
} |
| 177 |
unset($product); |
| 178 |
} |
| 179 |
} |
| 180 |
unset($product_id_list); |
| 181 |
|
| 182 |
$this->init_sort($sort); |
| 183 |
uasort($products, array($this, 'custom_sort')); |
| 184 |
|
| 185 |
return $products; |
| 186 |
} |
| 187 |
|
| 188 |
public function get_product_id_list($limit=null, $ofset=null): array |
| 189 |
{ |
| 190 |
global $wpdb; |
| 191 |
|
| 192 |
$limitStr = ""; |
| 193 |
if (isset($limit)) { |
| 194 |
$offsetStr = ''; |
| 195 |
if ($ofset) { |
| 196 |
$offsetStr = $wpdb->prepare("%d, ", $ofset); |
| 197 |
} |
| 198 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 199 |
$limitStr = $wpdb->prepare("LIMIT " . $offsetStr . "%d", $limit); |
| 200 |
} |
| 201 |
if (a2wl_check_defined('A2WL_SAVE_TRANSIENT_AS_OPTION')) { |
| 202 |
$results = $wpdb->get_results($wpdb->prepare( |
| 203 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 204 |
"SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s ORDER BY option_id $limitStr", |
| 205 |
$wpdb->esc_like('a2wl_product#') . '%'), |
| 206 |
ARRAY_A); |
| 207 |
} else { |
| 208 |
$results = $wpdb->get_results($wpdb->prepare( |
| 209 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 210 |
"select option_name from {$wpdb->options} where option_name like %s order by option_id $limitStr", |
| 211 |
$wpdb->esc_like('_transient_a2wl_product#') . '%'), |
| 212 |
ARRAY_A); |
| 213 |
} |
| 214 |
$ids = array(); |
| 215 |
foreach ($results as $r) { |
| 216 |
$tmp = explode("#", $r['option_name']); |
| 217 |
if (count($tmp) == 2) { |
| 218 |
$ids[] = $tmp[1]; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
return $ids; |
| 223 |
} |
| 224 |
|
| 225 |
public function get_products_count(): int |
| 226 |
{ |
| 227 |
global $wpdb; |
| 228 |
|
| 229 |
if (a2wl_check_defined('A2WL_SAVE_TRANSIENT_AS_OPTION')) { |
| 230 |
$result = $wpdb->get_var("select count(option_id) from {$wpdb->options} where option_name like 'a2wl_product#%'"); |
| 231 |
} else { |
| 232 |
$result = $wpdb->get_var("select count(option_id) from {$wpdb->options} where option_name like '_transient_a2wl_product#%'"); |
| 233 |
} |
| 234 |
|
| 235 |
if (!$result) { |
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
return (int) $result; |
| 240 |
} |
| 241 |
|
| 242 |
public function default_sort(): string |
| 243 |
{ |
| 244 |
return 'date_add-asc'; |
| 245 |
} |
| 246 |
|
| 247 |
public function sort_list(): array |
| 248 |
{ |
| 249 |
return [ |
| 250 |
'id-asc' => esc_html__('Sort by External Id', 'ali2woo'), |
| 251 |
'title-asc' => esc_html__('Sort by Product title', 'ali2woo'), |
| 252 |
'date_add-asc' => esc_html__('Sort by Date add (old first)', 'ali2woo'), |
| 253 |
'date_add-desc' => esc_html__('Sort by Date add (new first)', 'ali2woo'), |
| 254 |
|
| 255 |
]; |
| 256 |
} |
| 257 |
|
| 258 |
public function init_sort($sort): void |
| 259 |
{ |
| 260 |
if (empty($sort)) { |
| 261 |
$sort = $this->default_sort(); |
| 262 |
} |
| 263 |
|
| 264 |
if (in_array($sort, array_keys($this->sort_list()))) { |
| 265 |
$sv = explode("-", strtolower($sort)); |
| 266 |
$this->order = $sv[0]; |
| 267 |
$this->order_dir = $sv[1]; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
private function custom_sort($a, $b): int |
| 272 |
{ |
| 273 |
if ($a[$this->order] == $b[$this->order]) { |
| 274 |
return 0; |
| 275 |
} |
| 276 |
return (($a[$this->order] < $b[$this->order]) ? -1 : 1) * ($this->order_dir == 'asc' ? 1 : -1); |
| 277 |
} |
| 278 |
|
| 279 |
} |
| 280 |
|