| 1 |
<?php |
| 2 |
// phpcs:ignoreFile WordPress.Security.NonceVerification.Recommended |
| 3 |
namespace AliNext_Lite;; |
| 4 |
|
| 5 |
/* |
| 6 |
Controller name: Core |
| 7 |
Controller description: Basic introspection methods |
| 8 |
*/ |
| 9 |
|
| 10 |
use Exception; |
| 11 |
|
| 12 |
class JSON_API_Core_Controller |
| 13 |
{ |
| 14 |
|
| 15 |
protected GlobalSystemMessageService $GlobalSystemMessageService; |
| 16 |
protected ProductImport $ProductImportModel; |
| 17 |
protected Woocommerce $WoocommerceModel; |
| 18 |
protected ProductService $ProductService; |
| 19 |
protected Aliexpress $AliexpressModel; |
| 20 |
protected PriceFormulaService $PriceFormulaService; |
| 21 |
protected ProductImportTransactionService $ProductImportTransactionService; |
| 22 |
protected PurchaseCodeInfoService $PurchaseCodeInfoService; |
| 23 |
|
| 24 |
public function __construct( |
| 25 |
GlobalSystemMessageService $GlobalSystemMessageService, |
| 26 |
ProductImport $ProductImportModel, |
| 27 |
Woocommerce $WoocommerceModel, |
| 28 |
ProductService $ProductService, |
| 29 |
Aliexpress $AliexpressModel, |
| 30 |
PriceFormulaService $PriceFormulaService, |
| 31 |
ProductImportTransactionService $ProductImportTransactionService, |
| 32 |
PurchaseCodeInfoService $PurchaseCodeInfoService |
| 33 |
) { |
| 34 |
|
| 35 |
$this->GlobalSystemMessageService = $GlobalSystemMessageService; |
| 36 |
$this->ProductImportModel = $ProductImportModel; |
| 37 |
$this->WoocommerceModel = $WoocommerceModel; |
| 38 |
$this->ProductService = $ProductService; |
| 39 |
$this->AliexpressModel = $AliexpressModel; |
| 40 |
$this->PriceFormulaService = $PriceFormulaService; |
| 41 |
$this->ProductImportTransactionService = $ProductImportTransactionService; |
| 42 |
$this->PurchaseCodeInfoService = $PurchaseCodeInfoService; |
| 43 |
|
| 44 |
//todo: perhaps it would be better to move this call to some other controller |
| 45 |
$this->GlobalSystemMessageService->clear(); |
| 46 |
} |
| 47 |
|
| 48 |
public function permissions($method) |
| 49 |
{ |
| 50 |
global $a2wl_json_api; |
| 51 |
$protected_methods = array('add_product', 'upd_product', 'del_product', 'get_products', 'get_products_info', 'upd_order', 'get_settings', 'get_orders', 'update_tracking_data'); |
| 52 |
if (in_array($method, $protected_methods)) { |
| 53 |
$a2wl_key = $a2wl_json_api->query->get('a2w-key'); |
| 54 |
if (!empty($a2wl_key)) { |
| 55 |
// new auth method |
| 56 |
return $a2wl_json_api->query->is_valid_api_key($a2wl_key); |
| 57 |
} else { |
| 58 |
// old auth method |
| 59 |
if ($a2wl_json_api->query->cookie && wp_validate_auth_cookie($a2wl_json_api->query->cookie, 'logged_in')) { |
| 60 |
return true; |
| 61 |
} |
| 62 |
return false; |
| 63 |
} |
| 64 |
} |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
public function info() |
| 69 |
{ |
| 70 |
global $a2wl_json_api; |
| 71 |
if (!empty($a2wl_json_api->query->controller)) { |
| 72 |
return $a2wl_json_api->controller_info($a2wl_json_api->query->controller); |
| 73 |
} else { |
| 74 |
$active_controllers = explode(',', get_setting('json_api_controllers')); |
| 75 |
$controllers = array_intersect($a2wl_json_api->get_controllers(), $active_controllers); |
| 76 |
return array( |
| 77 |
'json_api_version' => A2WL_JSON_API_VERSION, |
| 78 |
'controllers' => array_values($controllers), |
| 79 |
); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public function add_product() |
| 84 |
{ |
| 85 |
/** |
| 86 |
* @var JSON_API $a2wl_json_api |
| 87 |
*/ |
| 88 |
global $a2wl_json_api; |
| 89 |
$result = []; |
| 90 |
|
| 91 |
|
| 92 |
$externalId = isset($_REQUEST['id']) ? sanitize_text_field($_REQUEST['id']) : null; |
| 93 |
|
| 94 |
if (!$externalId) { |
| 95 |
$a2wl_json_api->error("No ID specified. Include 'id' var in your request."); |
| 96 |
} |
| 97 |
|
| 98 |
$apdRaw = isset($_REQUEST['apd']) ? sanitize_text_field($_REQUEST['apd']) : ''; |
| 99 |
|
| 100 |
$Product = [ |
| 101 |
ImportedProductService::FIELD_EXTERNAL_PRODUCT_ID => $externalId |
| 102 |
]; |
| 103 |
|
| 104 |
$apd = !empty($apdRaw) |
| 105 |
? ['data' => ['apd' => json_decode(stripslashes($apdRaw))]] |
| 106 |
: []; |
| 107 |
|
| 108 |
a2wl_init_error_handler(); |
| 109 |
try { |
| 110 |
$result = $this->ProductImportTransactionService->executeWithProductData($Product, $apd); |
| 111 |
restore_error_handler(); |
| 112 |
} |
| 113 |
catch (Exception $Exception) { |
| 114 |
$a2wl_json_api->error($Exception->getMessage()); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
return $result; |
| 119 |
} |
| 120 |
|
| 121 |
public function upd_product() |
| 122 |
{ |
| 123 |
global $a2wl_json_api; |
| 124 |
|
| 125 |
$product_id = $_REQUEST['id']; |
| 126 |
if (empty($product_id)) { |
| 127 |
$a2wl_json_api->error("No ID specified. Include 'id' var in your request."); |
| 128 |
} else { |
| 129 |
|
| 130 |
$product = $this->ProductImportModel->get_product($product_id); |
| 131 |
if (!$product) { |
| 132 |
$product = array('id' => $product_id); |
| 133 |
} |
| 134 |
|
| 135 |
if (!empty($_REQUEST['url'])) { |
| 136 |
$product['url'] = $_REQUEST['url']; |
| 137 |
} |
| 138 |
if (!empty($_REQUEST['thumb'])) { |
| 139 |
$product['thumb'] = $_REQUEST['thumb']; |
| 140 |
} |
| 141 |
if (!empty($_REQUEST['price'])) { |
| 142 |
$product['price'] = str_replace(",", ".", $_REQUEST['price']); |
| 143 |
} |
| 144 |
if (!empty($_REQUEST['price_min'])) { |
| 145 |
$product['price_min'] = str_replace(",", ".", $_REQUEST['price_min']); |
| 146 |
} |
| 147 |
if (!empty($_REQUEST['price_max'])) { |
| 148 |
$product['price_max'] = str_replace(",", ".", $_REQUEST['price_max']); |
| 149 |
} |
| 150 |
if (!empty($_REQUEST['title'])) { |
| 151 |
$product['title'] = $_REQUEST['title']; |
| 152 |
} |
| 153 |
if (!empty($_REQUEST['currency'])) { |
| 154 |
$product['currency'] = $_REQUEST['currency']; |
| 155 |
} |
| 156 |
|
| 157 |
$this->ProductImportModel->upd_product($product); |
| 158 |
} |
| 159 |
|
| 160 |
return array(); |
| 161 |
} |
| 162 |
|
| 163 |
public function del_product() |
| 164 |
{ |
| 165 |
global $a2wl_json_api; |
| 166 |
|
| 167 |
$product_id = $_REQUEST['id']; |
| 168 |
if (empty($product_id)) { |
| 169 |
$a2wl_json_api->error("No ID specified. Include 'id' var in your request."); |
| 170 |
} else { |
| 171 |
$this->ProductImportModel->del_product($product_id); |
| 172 |
} |
| 173 |
return array(); |
| 174 |
} |
| 175 |
|
| 176 |
public function get_products() |
| 177 |
{ |
| 178 |
global $a2wl_json_api; |
| 179 |
|
| 180 |
$tmp_products = $this->ProductImportModel->get_product_list(); |
| 181 |
|
| 182 |
if (isset($_REQUEST['html'])) { |
| 183 |
return array('products' => $tmp_products); |
| 184 |
} else { |
| 185 |
$result = array(); |
| 186 |
foreach ($tmp_products as $id => $p) { |
| 187 |
$result[$id] = array('id' => $id); |
| 188 |
} |
| 189 |
return array('products' => $result); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
public function get_products_info() |
| 194 |
{ |
| 195 |
global $a2wl_json_api; |
| 196 |
if (!empty($_REQUEST['id'])) { |
| 197 |
$products = array(); |
| 198 |
$ids = array_map('trim', is_array($_REQUEST['id']) ? $_REQUEST['id'] : explode(",", $_REQUEST['id'])); |
| 199 |
foreach ($ids as $id) { |
| 200 |
$product = array('id' => $id); |
| 201 |
$product['imported'] = !!$this->WoocommerceModel->get_product_id_by_external_id($id) || |
| 202 |
!!$this->ProductImportModel->get_product($id); |
| 203 |
$products[] = $product; |
| 204 |
} |
| 205 |
return array('products' => $products); |
| 206 |
} else { |
| 207 |
$a2wl_json_api->error("You should pass products id."); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
public function upd_order() |
| 212 |
{ |
| 213 |
global $a2wl_json_api; |
| 214 |
|
| 215 |
$order_id = $_REQUEST['id']; |
| 216 |
if (empty($order_id)) { |
| 217 |
$a2wl_json_api->error("No ID specified. Include 'id' var in your request."); |
| 218 |
} else { |
| 219 |
|
| 220 |
$order = wc_get_order($order_id); |
| 221 |
|
| 222 |
if ($order === false) { |
| 223 |
$a2wl_json_api->error("Did not find the appropriate Woocommerce order."); |
| 224 |
} else { |
| 225 |
if (isset($_REQUEST['match_orders']) && !empty($_REQUEST['match_orders'])) { |
| 226 |
$matchOrders = $_REQUEST['match_orders']; |
| 227 |
|
| 228 |
$order_items = $order->get_items(); |
| 229 |
if (count($order_items)) { |
| 230 |
foreach ($matchOrders as $matchOrder) { |
| 231 |
$external_order_id = $matchOrder['orderId']; |
| 232 |
$orderTotal = $matchOrder['orderTotal']; |
| 233 |
if ($orderTotal) { |
| 234 |
//todo: we can save order total in a future plugin release |
| 235 |
} |
| 236 |
$matchProductIds = array_unique($matchOrder['matchProductIds']); |
| 237 |
foreach ($order_items as $item_id => $item) { |
| 238 |
|
| 239 |
$a2wl_order_item = new WooCommerceOrderItem($item); |
| 240 |
|
| 241 |
$external_id = $a2wl_order_item->get_external_product_id(); |
| 242 |
|
| 243 |
if (in_array($external_id, $matchProductIds)) { |
| 244 |
|
| 245 |
//save external order id if payment is done |
| 246 |
// if ( strtolower( trim( $matchOrder['orderStatus'] ) ) === 'wait_seller_send_goods' ) { |
| 247 |
$a2wl_order_item->update_external_order($external_order_id, true); |
| 248 |
// } |
| 249 |
|
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
} else if (!empty($_REQUEST['external_id'])) { |
| 256 |
|
| 257 |
/* todo: $_REQUEST['external_id'] deprecated and it will be removed in a future versions of the chrome extension |
| 258 |
* use $_REQUEST['match_orders'] instead |
| 259 |
*/ |
| 260 |
$data = $_REQUEST['external_id']; |
| 261 |
|
| 262 |
$current_data = $order->get_meta(Constants::old_order_external_order_id(), false); |
| 263 |
|
| 264 |
if (is_array($data)) { |
| 265 |
foreach ($data as $code_value) { |
| 266 |
if (in_array($code_value, $current_data)) { |
| 267 |
continue; |
| 268 |
} |
| 269 |
|
| 270 |
$code_value = trim($code_value); |
| 271 |
if (!empty($code_value)) { |
| 272 |
$order->add_meta_data(Constants::old_order_external_order_id(), $code_value); |
| 273 |
} |
| 274 |
} |
| 275 |
} else { |
| 276 |
$code_value = $data; |
| 277 |
if (!in_array($code_value, $current_data)) { |
| 278 |
$order->add_meta_data(Constants::old_order_external_order_id(), $code_value); |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
$placed_order_status = get_setting('placed_order_status'); |
| 284 |
if ($placed_order_status !== "") { |
| 285 |
$order->update_status($placed_order_status); |
| 286 |
} |
| 287 |
|
| 288 |
$order->save(); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
return []; |
| 293 |
} |
| 294 |
|
| 295 |
public function get_orders() |
| 296 |
{ |
| 297 |
global $a2wl_json_api; |
| 298 |
|
| 299 |
$result = $this->WoocommerceModel->get_fulfilled_orders_data(); |
| 300 |
|
| 301 |
return array('orders' => $result); |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
public function update_tracking_data() |
| 306 |
{ |
| 307 |
global $a2wl_json_api; |
| 308 |
|
| 309 |
if (!isset($_REQUEST['order_array'])) { |
| 310 |
|
| 311 |
$a2wl_json_api->error("No orders array specified. Include 'orders' array var in your request."); |
| 312 |
|
| 313 |
} else { |
| 314 |
|
| 315 |
$orders = json_decode(stripslashes($_REQUEST['order_array']), true); |
| 316 |
|
| 317 |
foreach ($orders as $order) { |
| 318 |
|
| 319 |
$order_id = intval($order['order_id']); |
| 320 |
$ext_id = floatval($order['ext_order_id']); |
| 321 |
|
| 322 |
$tracking_codes = isset($order['tracking_data']['tracking_codes']) ? $order['tracking_data']['tracking_codes'] : array(); |
| 323 |
$carrier_name = isset($order['tracking_data']['carrier_name']) ? $order['tracking_data']['carrier_name'] : ''; |
| 324 |
$carrier_url = isset($order['tracking_data']['carrier_url']) ? $order['tracking_data']['carrier_url'] : ''; |
| 325 |
$tracking_status = isset($order['tracking_data']['tracking_status']) ? $order['tracking_data']['tracking_status'] : ''; |
| 326 |
|
| 327 |
$result = $this->WoocommerceModel->save_tracking_code($order_id, $ext_id, $tracking_codes, $carrier_name, $carrier_url, $tracking_status); |
| 328 |
} |
| 329 |
|
| 330 |
} |
| 331 |
|
| 332 |
return array(); |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
public function get_settings(): array |
| 337 |
{ |
| 338 |
global $a2wl_json_api; |
| 339 |
|
| 340 |
$localizator = AliexpressLocalizator::getInstance(); |
| 341 |
|
| 342 |
$settings = [ |
| 343 |
'a2w_fulfillment_prefship' => get_setting('fulfillment_prefship', 'ePacket'), |
| 344 |
'a2w_aliship_shipto' => get_setting('aliship_shipto', 'US'), |
| 345 |
'a2w_import_language' => $localizator->language, |
| 346 |
'a2w_import_locale' => $localizator->getLangCode(), |
| 347 |
'a2w_local_currency' => $localizator->currency, |
| 348 |
'a2wl_chrome_ext_import' => a2wl_check_defined('A2WL_CHROME_EXT_IMPORT'), |
| 349 |
'a2wl_premium' => false |
| 350 |
]; |
| 351 |
|
| 352 |
if (EditionHelper::isFull()) { |
| 353 |
$PurchaseCodeInfo = $this->PurchaseCodeInfoService->getFromCache(); |
| 354 |
$supportedUntilTimestamp = $PurchaseCodeInfo->getSupportedUntilStamp(); |
| 355 |
if ($supportedUntilTimestamp && $supportedUntilTimestamp >= time()) { |
| 356 |
$settings['a2wl_premium'] = true; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
return ['settings' => $settings]; |
| 361 |
} |
| 362 |
|
| 363 |
} |
| 364 |
|