| 1 |
<?php |
| 2 |
|
| 3 |
class RewriteAPI |
| 4 |
{ |
| 5 |
|
| 6 |
private $url = 'https://connect.mailerlite.com/api'; |
| 7 |
private $client; |
| 8 |
private $response; |
| 9 |
private $response_code; |
| 10 |
|
| 11 |
/** |
| 12 |
* RewriteAPI constructor |
| 13 |
* |
| 14 |
* @access public |
| 15 |
* @return void |
| 16 |
* @since 1.6.0 |
| 17 |
*/ |
| 18 |
public function __construct($api_key) |
| 19 |
{ |
| 20 |
$this->client = new MailerLiteClient($this->url, [ |
| 21 |
'Authorization' => 'Bearer ' . $api_key, |
| 22 |
'Content-Type' => 'application/json', |
| 23 |
'Accept' => 'application/json' |
| 24 |
]); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Validate API Key |
| 29 |
* |
| 30 |
* @access public |
| 31 |
* @return mixed |
| 32 |
* @since 1.6.0 |
| 33 |
*/ |
| 34 |
public function validateKey() |
| 35 |
{ |
| 36 |
$response = $this->client->remote_get('/account'); |
| 37 |
|
| 38 |
$response = self::parseResponse($response); |
| 39 |
|
| 40 |
return $response->data ?? false; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Update subscriber |
| 45 |
* |
| 46 |
* @access public |
| 47 |
* @return mixed |
| 48 |
* @since 1.6.0 |
| 49 |
*/ |
| 50 |
public function updateSubscriber($subscriber_email, $subscriber_data) |
| 51 |
{ |
| 52 |
|
| 53 |
// TODO - implement update subscriber |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Update subscriber status |
| 58 |
* |
| 59 |
* @access public |
| 60 |
* @return mixed |
| 61 |
* @since 1.7.4 |
| 62 |
*/ |
| 63 |
public function updateSubscriberStatus($email, $status) |
| 64 |
{ |
| 65 |
|
| 66 |
$response = $this->client->remote_post('/subscribers', [ |
| 67 |
'email' => $email, |
| 68 |
'status' => $status |
| 69 |
]); |
| 70 |
|
| 71 |
$response = self::parseResponse($response); |
| 72 |
|
| 73 |
return $response->data ?? false; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Search for a subscriber |
| 78 |
* |
| 79 |
* @access public |
| 80 |
* @return mixed |
| 81 |
* @since 1.6.0 |
| 82 |
*/ |
| 83 |
public function searchSubscriber($query) |
| 84 |
{ |
| 85 |
|
| 86 |
// TODO - implement search subscriber |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get groups |
| 91 |
* |
| 92 |
* @access public |
| 93 |
* @return array[] |
| 94 |
* @since 1.6.0 |
| 95 |
*/ |
| 96 |
public function getGroups($params) |
| 97 |
{ |
| 98 |
$response = $this->client->remote_get('/groups', $params); |
| 99 |
$response = self::parseResponse($response); |
| 100 |
|
| 101 |
$groups = []; |
| 102 |
|
| 103 |
if ($response->data) { |
| 104 |
|
| 105 |
foreach ($response->data as $record) { |
| 106 |
|
| 107 |
$group = []; |
| 108 |
$group['id'] = $record->id; |
| 109 |
$group['name'] = $record->name; |
| 110 |
$group['total'] = $record->active_count; |
| 111 |
$group['opened'] = $record->open_rate->float; |
| 112 |
$group['clicked'] = $record->click_rate->float; |
| 113 |
$group['date_created'] = $record->created_at; |
| 114 |
|
| 115 |
$groups[] = $group; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
return $groups; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Check if more groups need to be loaded |
| 124 |
* |
| 125 |
* @access public |
| 126 |
* @return mixed |
| 127 |
* @since 1.6.0 |
| 128 |
*/ |
| 129 |
public function checkMoreGroups($limit, $offset) |
| 130 |
{ |
| 131 |
|
| 132 |
$response = $this->client->remote_get('/groups', [ |
| 133 |
'limit' => $limit, |
| 134 |
'page' => $offset |
| 135 |
]); |
| 136 |
|
| 137 |
$response = self::parseResponse($response); |
| 138 |
|
| 139 |
return count( $response->data ?? [] ) > 0; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get more groups |
| 144 |
* |
| 145 |
* @access public |
| 146 |
* @return mixed |
| 147 |
* @since 1.6.0 |
| 148 |
*/ |
| 149 |
public function getMoreGroups($limit, $offset) |
| 150 |
{ |
| 151 |
|
| 152 |
$response = $this->client->remote_get('/groups', [ |
| 153 |
'limit' => $limit, |
| 154 |
'page' => $offset |
| 155 |
]); |
| 156 |
|
| 157 |
$response = self::parseResponse($response); |
| 158 |
|
| 159 |
return $response->data ?? []; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get Double opt-in setting |
| 164 |
* |
| 165 |
* @access public |
| 166 |
* @return bool |
| 167 |
* @since 1.6.0 |
| 168 |
*/ |
| 169 |
public function getDoubleOptin() |
| 170 |
{ |
| 171 |
$response = $this->client->remote_get('/subscribe-settings/double-optin'); |
| 172 |
|
| 173 |
if( isset( $response ) ) { |
| 174 |
$response = self::parseResponse($response); |
| 175 |
|
| 176 |
if (isset($response->double_optin) && $response->double_optin) { |
| 177 |
|
| 178 |
return true; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Toggle Double opt-in setting |
| 187 |
* |
| 188 |
* @access public |
| 189 |
* @return bool |
| 190 |
* @since 1.6.0 |
| 191 |
*/ |
| 192 |
public function setDoubleOptin() |
| 193 |
{ |
| 194 |
$this->client->remote_post('/subscribe-settings/double-optin/toggle'); |
| 195 |
|
| 196 |
return $this->getDoubleOptin(); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Get Fields |
| 201 |
* |
| 202 |
* @access public |
| 203 |
* @param $params |
| 204 |
* @return array[] |
| 205 |
* @since 1.6.0 |
| 206 |
*/ |
| 207 |
public function getFields($params): array |
| 208 |
{ |
| 209 |
$response = $this->client->remote_get('/fields', $params); |
| 210 |
$response = self::parseResponse($response); |
| 211 |
|
| 212 |
$fields = []; |
| 213 |
|
| 214 |
if (isset($response->data)) { |
| 215 |
foreach ($response->data as $field) { |
| 216 |
$ml_field = []; |
| 217 |
$ml_field['id'] = $field->id; |
| 218 |
$ml_field['title'] = $field->name; |
| 219 |
$ml_field['key'] = $field->key; |
| 220 |
$ml_field['type'] = $field->type; |
| 221 |
|
| 222 |
$fields[] = $ml_field; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
return $fields; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Create Custom Field |
| 231 |
* |
| 232 |
* @access public |
| 233 |
* @return mixed |
| 234 |
* @since 1.6.0 |
| 235 |
*/ |
| 236 |
public function createField($title, $type) |
| 237 |
{ |
| 238 |
|
| 239 |
$response = $this->client->remote_post('/fields', [ |
| 240 |
'name' => $title, |
| 241 |
'type' => $type |
| 242 |
] ); |
| 243 |
|
| 244 |
$response = self::parseResponse($response); |
| 245 |
|
| 246 |
return $response->data ?? false; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Update Custom Field |
| 251 |
* |
| 252 |
* @access public |
| 253 |
* @return mixed |
| 254 |
* @since 1.7.11 |
| 255 |
*/ |
| 256 |
public function updateField($id, $name) |
| 257 |
{ |
| 258 |
|
| 259 |
$response = $this->client->remote_put('/fields/' . $id, [ |
| 260 |
'name' => $name |
| 261 |
]); |
| 262 |
|
| 263 |
return self::parseResponse($response); |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Sync WooCommerce Customer |
| 268 |
* |
| 269 |
* @access public |
| 270 |
* @return mixed |
| 271 |
* @since 1.6.0 |
| 272 |
*/ |
| 273 |
public function syncCustomerWooCommerce($customer_id, $email, $fields, $shop_id) |
| 274 |
{ |
| 275 |
|
| 276 |
if ( $customer_id !== '0' ) { |
| 277 |
|
| 278 |
$response = $this->client->remote_post('/ecommerce/shops/'.$shop_id.'/customers', [ |
| 279 |
'email' => $email, |
| 280 |
'accepts_marketing' => true, // TODO |
| 281 |
'total_spent' => 0, |
| 282 |
'create_subscriber' => true // TODO |
| 283 |
]); |
| 284 |
} else { |
| 285 |
|
| 286 |
$response = $this->client->remote_put('/ecommerce/shops/'.$shop_id.'/customers/' . $customer_id, [ |
| 287 |
'email' => $email, |
| 288 |
'accepts_marketing' => true, // TODO |
| 289 |
'total_spent' => 0, |
| 290 |
'create_subscriber' => true // TODO |
| 291 |
]); |
| 292 |
} |
| 293 |
|
| 294 |
$response = self::parseResponse($response); |
| 295 |
|
| 296 |
return $response->data; |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Set WooCommerce Consumer Data |
| 301 |
* |
| 302 |
* @access public |
| 303 |
* @return mixed |
| 304 |
* @since 1.6.0 |
| 305 |
*/ |
| 306 |
public function setConsumerData($consumerKey, $consumerSecret, $store, $currency, $group_id, $resubscribe, $ignoreList, $create_segments, $shop_name, $shop_id, $popups_enabled) |
| 307 |
{ |
| 308 |
|
| 309 |
if ( $shop_id !== false ) { |
| 310 |
|
| 311 |
$response = $this->client->remote_put('/ecommerce/shops/' . $shop_id, [ |
| 312 |
'name' => $shop_name, |
| 313 |
'url' => $store, |
| 314 |
'currency' => $currency, |
| 315 |
'platform' => 'woocommerce', |
| 316 |
'group_id' => $group_id, |
| 317 |
'enable_popups' => $popups_enabled, |
| 318 |
'enabled' => true, |
| 319 |
'access_data' => '-' |
| 320 |
]); |
| 321 |
} else { |
| 322 |
|
| 323 |
$response = $this->client->remote_post('/ecommerce/shops', [ |
| 324 |
'name' => $shop_name, |
| 325 |
'url' => $store, |
| 326 |
'currency' => $currency, |
| 327 |
'platform' => 'woocommerce', |
| 328 |
'group_id' => $group_id, |
| 329 |
'enable_popups' => $popups_enabled, |
| 330 |
'enabled' => true, |
| 331 |
'access_data' => '-' |
| 332 |
]); |
| 333 |
} |
| 334 |
|
| 335 |
$response = self::parseResponse($response); |
| 336 |
|
| 337 |
return $response->data ?? false; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Send WooCommerce Subscriber Data |
| 342 |
* |
| 343 |
* @access public |
| 344 |
* @return mixed |
| 345 |
* @since 1.6.0 |
| 346 |
*/ |
| 347 |
public function sendSubscriberData($shop_id, $data) |
| 348 |
{ |
| 349 |
|
| 350 |
$response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/customers', [ |
| 351 |
'email' => $data['email'], |
| 352 |
'accepts_marketing' => $data['checked_sub_to_mailist'], |
| 353 |
'total_spent' => 0, |
| 354 |
'create_subscriber' => $data['checked_sub_to_mailist'] |
| 355 |
] ); |
| 356 |
|
| 357 |
$response = self::parseResponse($response); |
| 358 |
|
| 359 |
return $response->data; |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Save WooCommerce Order |
| 364 |
* |
| 365 |
* @access public |
| 366 |
* @return mixed |
| 367 |
* @since 1.6.0 |
| 368 |
*/ |
| 369 |
public function saveOrder($shop_id, $orderData) |
| 370 |
{ |
| 371 |
|
| 372 |
return true; |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* Send WooCommerce Order Processing |
| 377 |
* |
| 378 |
* @access public |
| 379 |
* @return mixed |
| 380 |
* @since 1.6.0 |
| 381 |
*/ |
| 382 |
public function sendOrderProcessing($shop_id, $data) |
| 383 |
{ |
| 384 |
|
| 385 |
$order_id = $data['order_id']; |
| 386 |
|
| 387 |
$response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/orders/' . $order_id, [ |
| 388 |
'status' => 'processing' |
| 389 |
]); |
| 390 |
|
| 391 |
$response = self::parseResponse($response); |
| 392 |
|
| 393 |
return $response->data; |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* Update WooCommerce Cart |
| 398 |
* |
| 399 |
* @access public |
| 400 |
* @return mixed |
| 401 |
* @since 1.6.0 |
| 402 |
*/ |
| 403 |
public function sendCart($shop_id, $cartData) |
| 404 |
{ |
| 405 |
|
| 406 |
$response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/carts/' . $cartData['cart_id'], [ |
| 407 |
'checkout_url' => $cartData['abandoned_checkout_url'], |
| 408 |
'cart_total' => 0 |
| 409 |
] ); |
| 410 |
|
| 411 |
$response = self::parseResponse($response); |
| 412 |
|
| 413 |
return $response->data; |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Toggle WooCommerce Shop |
| 418 |
* |
| 419 |
* @access public |
| 420 |
* @param $shop |
| 421 |
* @param $activeState |
| 422 |
* @param $shop_id |
| 423 |
* @param $shop_name |
| 424 |
* @return mixed |
| 425 |
* @since 1.6.0 |
| 426 |
*/ |
| 427 |
public function toggleShop($shop, $activeState, $shop_id, $shop_name) |
| 428 |
{ |
| 429 |
|
| 430 |
$response = $this->client->remote_put('/ecommerce/shops/' . $shop_id, [ |
| 431 |
'name' => $shop_name, |
| 432 |
'url' => $shop, |
| 433 |
'enabled' => $activeState |
| 434 |
]); |
| 435 |
|
| 436 |
$response = self::parseResponse($response); |
| 437 |
|
| 438 |
return $response->data; |
| 439 |
} |
| 440 |
|
| 441 |
/** |
| 442 |
* Get WooCommerce Shop Settings |
| 443 |
* |
| 444 |
* @access public |
| 445 |
* @param $shop_id |
| 446 |
* @return mixed |
| 447 |
* @since 1.6.0 |
| 448 |
*/ |
| 449 |
public function getShopSettings($shop_id) |
| 450 |
{ |
| 451 |
|
| 452 |
$response = $this->client->remote_get('/ecommerce/shops/' . $shop_id); |
| 453 |
|
| 454 |
$response = self::parseResponse($response); |
| 455 |
|
| 456 |
return $response->data ?? false; |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* Validate WooCommerce Account |
| 461 |
* |
| 462 |
* @access public |
| 463 |
* @return mixed |
| 464 |
* @since 1.6.0 |
| 465 |
*/ |
| 466 |
public function validateAccount() |
| 467 |
{ |
| 468 |
|
| 469 |
$response = $this->client->remote_get('/account'); |
| 470 |
|
| 471 |
$response = self::parseResponse($response); |
| 472 |
|
| 473 |
return $response->data ?? false; |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* Get list of shops |
| 478 |
* |
| 479 |
* @access public |
| 480 |
* @return array |
| 481 |
* @since 1.6.0 |
| 482 |
*/ |
| 483 |
public function getShops() |
| 484 |
{ |
| 485 |
|
| 486 |
$response = $this->client->remote_get('/ecommerce/shops'); |
| 487 |
|
| 488 |
$response = self::parseResponse($response); |
| 489 |
|
| 490 |
return $response->data ?? []; |
| 491 |
} |
| 492 |
|
| 493 |
/** |
| 494 |
* Get a shop |
| 495 |
* |
| 496 |
* @access public |
| 497 |
* @return mixed |
| 498 |
* @since 1.6.0 |
| 499 |
*/ |
| 500 |
public function getShop($shop_id) |
| 501 |
{ |
| 502 |
|
| 503 |
$response = $this->client->remote_get('/ecommerce/shops/' . $shop_id); |
| 504 |
|
| 505 |
$response = self::parseResponse($response); |
| 506 |
|
| 507 |
return $response->data ?? false; |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
* Create shop |
| 512 |
* |
| 513 |
* @access public |
| 514 |
* @param $name |
| 515 |
* @param $url |
| 516 |
* @param $currency |
| 517 |
* @param $group_id |
| 518 |
* @param $enable_popups |
| 519 |
* @param $access_data |
| 520 |
* @return mixed |
| 521 |
* @since 1.6.0 |
| 522 |
*/ |
| 523 |
public function createShop($name, $url, $currency, $group_id, $enable_popups, $access_data) |
| 524 |
{ |
| 525 |
$response = $this->client->remote_post('/ecommerce/shops', [ |
| 526 |
'name' => $name, |
| 527 |
'url' => $url, |
| 528 |
'currency' => $currency, |
| 529 |
'platform' => 'woocommerce', |
| 530 |
'group_id' => $group_id, |
| 531 |
'enable_popups' => $enable_popups, |
| 532 |
'enabled' => true, |
| 533 |
'access_data' => $access_data |
| 534 |
]); |
| 535 |
|
| 536 |
$response = self::parseResponse($response); |
| 537 |
|
| 538 |
return $response->data ?? false; |
| 539 |
} |
| 540 |
|
| 541 |
/** |
| 542 |
* Delete shop |
| 543 |
* |
| 544 |
* @access public |
| 545 |
* @param $shop |
| 546 |
* @return bool |
| 547 |
* @since 1.6.0 |
| 548 |
*/ |
| 549 |
public function deleteshop($shop) |
| 550 |
{ |
| 551 |
$response = $this->client->remote_delete('/ecommerce/shops/' . $shop); |
| 552 |
|
| 553 |
$response = self::parseResponse($response); |
| 554 |
|
| 555 |
return $response->data ?? false; |
| 556 |
} |
| 557 |
|
| 558 |
/** |
| 559 |
* Fetch order |
| 560 |
* |
| 561 |
* @access public |
| 562 |
* @param $shop_id |
| 563 |
* @param $order_id |
| 564 |
* @return mixed |
| 565 |
* @since 1.6.0 |
| 566 |
*/ |
| 567 |
public function fetchOrder( $shop_id, $order_id ) |
| 568 |
{ |
| 569 |
|
| 570 |
$response = $this->client->remote_get('/ecommerce/shops/' . $shop_id . '/orders/' . $order_id . '?with_resource_id'); |
| 571 |
|
| 572 |
$response = self::parseResponse($response); |
| 573 |
|
| 574 |
return $response->data ?? false; |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* Import orders |
| 579 |
* |
| 580 |
* @access public |
| 581 |
* @param $shop_id |
| 582 |
* @param $orders |
| 583 |
* @return array |
| 584 |
* @since 1.6.0 |
| 585 |
*/ |
| 586 |
public function importOrders( $shop_id, $orders) |
| 587 |
{ |
| 588 |
|
| 589 |
$response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/orders/import?with_resource_id', [ |
| 590 |
'orders' => $orders |
| 591 |
]); |
| 592 |
|
| 593 |
$response = self::parseResponse($response); |
| 594 |
|
| 595 |
return $response->data ?? []; |
| 596 |
} |
| 597 |
|
| 598 |
/** |
| 599 |
* Sync order |
| 600 |
* |
| 601 |
* @access public |
| 602 |
* @param $shop_id |
| 603 |
* @param $order_id |
| 604 |
* @param $customer |
| 605 |
* @param $cart |
| 606 |
* @param $status |
| 607 |
* @param $total_price |
| 608 |
* @param $created_at |
| 609 |
* @return mixed |
| 610 |
* @since 1.6.0 |
| 611 |
*/ |
| 612 |
public function syncOrder( $shop_id, $order_id, $customer, $cart, $status, $total_price, $created_at) |
| 613 |
{ |
| 614 |
|
| 615 |
$order_status = 'pending'; |
| 616 |
|
| 617 |
if ($status == 'completed' || $status == 'processing') { |
| 618 |
$order_status = 'complete'; |
| 619 |
} |
| 620 |
|
| 621 |
if (! isset($cart['resource_id'])) { |
| 622 |
$cart['resource_id'] = (string) $order_id; |
| 623 |
} |
| 624 |
|
| 625 |
$check_order = $this->fetchOrder($shop_id, $order_id); |
| 626 |
|
| 627 |
if ($check_order === false) { |
| 628 |
|
| 629 |
$parameters = [ |
| 630 |
'resource_id' => (string) $order_id, |
| 631 |
'customer' => $customer, |
| 632 |
'cart' => $cart, |
| 633 |
'status' => $order_status, |
| 634 |
'total_price' => $total_price, |
| 635 |
'created_at' => $created_at |
| 636 |
]; |
| 637 |
|
| 638 |
if (isset($customer['resource_id'])) { |
| 639 |
|
| 640 |
$customer_exist = $this->fetchCustomer( $shop_id, $customer['resource_id'] ); |
| 641 |
|
| 642 |
if ($customer_exist !== false) { |
| 643 |
|
| 644 |
$this->deleteCustomer( $shop_id, $customer['resource_id']); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
$response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/orders?with_resource_id', |
| 649 |
$parameters); |
| 650 |
}else{ |
| 651 |
|
| 652 |
if (isset($check_order->customer->resource_id)) { |
| 653 |
|
| 654 |
$this->syncCustomer( $shop_id, $check_order->customer->resource_id, $customer['email'], $customer['subscriber_fields']); |
| 655 |
} |
| 656 |
|
| 657 |
if (isset($check_order->cart)) { |
| 658 |
|
| 659 |
$this->updateCart($shop_id, $check_order->cart->id, $check_order->cart->checkout_url, $total_price, $order_id); |
| 660 |
|
| 661 |
$this->replaceCartItems($shop_id, $order_id, $cart['items']); |
| 662 |
} |
| 663 |
|
| 664 |
$parameters = [ |
| 665 |
'resource_id' => (string) $order_id, |
| 666 |
'status' => $order_status, |
| 667 |
'total_price' => $total_price, |
| 668 |
]; |
| 669 |
|
| 670 |
$response = $this->client->remote_put('/ecommerce/shops/'.$shop_id.'/orders/' . $order_id . '?with_resource_id', |
| 671 |
$parameters); |
| 672 |
} |
| 673 |
|
| 674 |
$response = self::parseResponse($response); |
| 675 |
|
| 676 |
return $response->data ?? false; |
| 677 |
} |
| 678 |
|
| 679 |
/** |
| 680 |
* Delete order |
| 681 |
* |
| 682 |
* @access public |
| 683 |
* @param $shop_id |
| 684 |
* @param $order_id |
| 685 |
* @return mixed |
| 686 |
* @since 1.6.0 |
| 687 |
*/ |
| 688 |
public function deleteOrder( $shop_id, $order_id) |
| 689 |
{ |
| 690 |
|
| 691 |
$response = $this->client->remote_delete('/ecommerce/shops/' . $shop_id . '/orders/' . $order_id . '?with_resource_id'); |
| 692 |
|
| 693 |
$response = self::parseResponse($response); |
| 694 |
|
| 695 |
return $response->data ?? false; |
| 696 |
} |
| 697 |
|
| 698 |
/** |
| 699 |
* Update order |
| 700 |
* |
| 701 |
* @access public |
| 702 |
* @param $shop_id |
| 703 |
* @param $order_id |
| 704 |
* @param $status |
| 705 |
* @param $total_price |
| 706 |
* @return mixed |
| 707 |
* @since 1.6.0 |
| 708 |
*/ |
| 709 |
public function updateOrder( $shop_id, $order_id, $status, $total_price ) |
| 710 |
{ |
| 711 |
|
| 712 |
$response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/orders/' . $order_id . '?with_resource_id', [ |
| 713 |
'status' => $status, |
| 714 |
'total_price' => (float) $total_price |
| 715 |
]); |
| 716 |
|
| 717 |
$response = self::parseResponse($response); |
| 718 |
|
| 719 |
return $response->data ?? false; |
| 720 |
} |
| 721 |
|
| 722 |
/** |
| 723 |
* Sync product |
| 724 |
* |
| 725 |
* @access public |
| 726 |
* @param $shop_id |
| 727 |
* @param $product_id |
| 728 |
* @param $name |
| 729 |
* @param $price |
| 730 |
* @param $exclude_automation |
| 731 |
* @param $url |
| 732 |
* @param $image |
| 733 |
* @param $categories |
| 734 |
* @return mixed |
| 735 |
* @since 1.6.0 |
| 736 |
*/ |
| 737 |
public function syncProduct( $shop_id, $product_id, $name, $price, $exclude_automation, $url, $image, $categories ) |
| 738 |
{ |
| 739 |
|
| 740 |
$parameters = [ |
| 741 |
'resource_id' => (string) $product_id, |
| 742 |
'name' => $name, |
| 743 |
'price' => $price, |
| 744 |
'exclude_from_automations' => $exclude_automation, |
| 745 |
'url' => $url, |
| 746 |
'categories' => $categories |
| 747 |
]; |
| 748 |
|
| 749 |
if (! empty($image)) |
| 750 |
$parameters['image'] = (string) $image; |
| 751 |
|
| 752 |
$response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/products?with_resource_id', $parameters); |
| 753 |
|
| 754 |
$response = self::parseResponse($response); |
| 755 |
|
| 756 |
return $response->data ?? false; |
| 757 |
} |
| 758 |
|
| 759 |
/** |
| 760 |
* Replace product categories |
| 761 |
* |
| 762 |
* @access public |
| 763 |
* @param $shop_id |
| 764 |
* @param $product_id |
| 765 |
* @param $categories |
| 766 |
* @return mixed |
| 767 |
* @since 1.6.0 |
| 768 |
*/ |
| 769 |
public function replaceProductCategories( $shop_id, $product_id, $categories) |
| 770 |
{ |
| 771 |
|
| 772 |
$response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/products/' . $product_id . '/categories/multiple?with_resource_id', [ |
| 773 |
'replace' => true, |
| 774 |
'categories' => $categories |
| 775 |
]); |
| 776 |
|
| 777 |
$response = self::parseResponse($response); |
| 778 |
|
| 779 |
return $response->data ?? false; |
| 780 |
} |
| 781 |
|
| 782 |
/** |
| 783 |
* Import products |
| 784 |
* |
| 785 |
* @access public |
| 786 |
* @param @shop_id |
| 787 |
* @param @products |
| 788 |
* @return array |
| 789 |
* @since 1.6.0 |
| 790 |
*/ |
| 791 |
public function importProducts( $shop_id, $products ) |
| 792 |
{ |
| 793 |
|
| 794 |
$response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/products/import?with_resource_id&replace_categories', $products); |
| 795 |
|
| 796 |
$response = self::parseResponse($response); |
| 797 |
|
| 798 |
return $response->data ?? []; |
| 799 |
} |
| 800 |
|
| 801 |
/** |
| 802 |
* Delete product |
| 803 |
* |
| 804 |
* @access public |
| 805 |
* @param $shop_id |
| 806 |
* @param $product_id |
| 807 |
* @return mixed |
| 808 |
* @since 1.6.0 |
| 809 |
*/ |
| 810 |
public function deleteProduct( $shop_id, $product_id) |
| 811 |
{ |
| 812 |
|
| 813 |
$response = $this->client->remote_delete('/ecommerce/shops/' . $shop_id . '/products/' . $product_id . '?with_resource_id'); |
| 814 |
|
| 815 |
$response = self::parseResponse($response); |
| 816 |
|
| 817 |
return $response->data ?? false; |
| 818 |
} |
| 819 |
|
| 820 |
/** |
| 821 |
* Create product category |
| 822 |
* |
| 823 |
* @access public |
| 824 |
* @param $shop_id |
| 825 |
* @param $category_id |
| 826 |
* @param $name |
| 827 |
* @return mixed |
| 828 |
* @since 1.6.0 |
| 829 |
*/ |
| 830 |
public function syncCategory( $shop_id, $category_id, $name ) |
| 831 |
{ |
| 832 |
|
| 833 |
$response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/categories?with_resource_id', [ |
| 834 |
"resource_id" => (string) $category_id, |
| 835 |
"name" => $name |
| 836 |
]); |
| 837 |
|
| 838 |
$response = self::parseResponse($response); |
| 839 |
|
| 840 |
return $response->data ?? false; |
| 841 |
} |
| 842 |
|
| 843 |
/** |
| 844 |
* Import categories |
| 845 |
* |
| 846 |
* @access public |
| 847 |
* @param @shop_id |
| 848 |
* @param @categories |
| 849 |
* @return array |
| 850 |
* @since 1.6.0 |
| 851 |
*/ |
| 852 |
public function importCategories( $shop_id, $categories ) |
| 853 |
{ |
| 854 |
|
| 855 |
$response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/categories/import?with_resource_id', $categories); |
| 856 |
|
| 857 |
$response = self::parseResponse($response); |
| 858 |
|
| 859 |
return $response->data ?? []; |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
/** |
| 864 |
* Delete product category |
| 865 |
* |
| 866 |
* @access public |
| 867 |
* @param $shop_id |
| 868 |
* @param $category_id |
| 869 |
* @return mixed |
| 870 |
* @since 1.6.0 |
| 871 |
*/ |
| 872 |
public function deleteCategory( $shop_id, $category_id) |
| 873 |
{ |
| 874 |
|
| 875 |
$response = $this->client->remote_delete('/ecommerce/shops/' . $shop_id . '/categories/' . $category_id . '?with_resource_id'); |
| 876 |
|
| 877 |
$response = self::parseResponse($response); |
| 878 |
|
| 879 |
return $response->data ?? false; |
| 880 |
} |
| 881 |
|
| 882 |
/** |
| 883 |
* Sync customer |
| 884 |
* |
| 885 |
* @access public |
| 886 |
* @param $shop_id |
| 887 |
* @param $customer_id |
| 888 |
* @param $email |
| 889 |
* @param $fields |
| 890 |
* @return mixed |
| 891 |
* @since 1.6.0 |
| 892 |
*/ |
| 893 |
public function syncCustomer( $shop_id, $customer_id, $email, $fields ) |
| 894 |
{ |
| 895 |
|
| 896 |
if (strval($customer_id) === "0") |
| 897 |
return false; |
| 898 |
|
| 899 |
$response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/customers' . '?with_resource_id', [ |
| 900 |
'resource_id' => (string) $customer_id, |
| 901 |
'email' => $email, |
| 902 |
'subscriber_fields' => $fields |
| 903 |
]); |
| 904 |
|
| 905 |
$response = self::parseResponse($response); |
| 906 |
|
| 907 |
return $response->data ?? false; |
| 908 |
} |
| 909 |
|
| 910 |
/** |
| 911 |
* Update customer |
| 912 |
* |
| 913 |
* @access public |
| 914 |
* @param $shop_id |
| 915 |
* @param $customer_id |
| 916 |
* @param $email |
| 917 |
* @param $accepts_marketing |
| 918 |
* @param $create_subscriber |
| 919 |
* @return mixed |
| 920 |
* @since 1.6.0 |
| 921 |
*/ |
| 922 |
public function updateCustomer( $shop_id, $customer_id, $email, $accepts_marketing, $create_subscriber ) |
| 923 |
{ |
| 924 |
|
| 925 |
$response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/customers/' . $customer_id, [ |
| 926 |
'email' => $email, |
| 927 |
'accepts_marketing' => $accepts_marketing, |
| 928 |
'create_subscriber' => $create_subscriber |
| 929 |
]); |
| 930 |
|
| 931 |
$response = self::parseResponse($response); |
| 932 |
|
| 933 |
return $response->data ?? false; |
| 934 |
} |
| 935 |
|
| 936 |
/** |
| 937 |
* Delete customer |
| 938 |
* |
| 939 |
* @access public |
| 940 |
* @param $shop_id |
| 941 |
* @param $customer_id |
| 942 |
* @return bool |
| 943 |
* @since 1.6.0 |
| 944 |
*/ |
| 945 |
public function deleteCustomer($shop_id, $customer_id) |
| 946 |
{ |
| 947 |
$response = $this->client->remote_delete('/ecommerce/shops/' . $shop_id . '/customers/' . $customer_id . '?with_resource_id'); |
| 948 |
|
| 949 |
$response = self::parseResponse($response); |
| 950 |
|
| 951 |
return $response->data ?? false; |
| 952 |
} |
| 953 |
|
| 954 |
/** |
| 955 |
* Fetch order |
| 956 |
* |
| 957 |
* @access public |
| 958 |
* @param $shop_id |
| 959 |
* @param $customer_id |
| 960 |
* @return mixed |
| 961 |
* @since 1.6.0 |
| 962 |
*/ |
| 963 |
public function fetchCustomer( $shop_id, $customer_id) |
| 964 |
{ |
| 965 |
|
| 966 |
$response = $this->client->remote_get('/ecommerce/shops/' . $shop_id . '/customers/' . $customer_id . '?with_resource_id'); |
| 967 |
|
| 968 |
$response = self::parseResponse($response); |
| 969 |
|
| 970 |
return $response->data ?? false; |
| 971 |
} |
| 972 |
|
| 973 |
/** |
| 974 |
* Update cart |
| 975 |
* |
| 976 |
* @access public |
| 977 |
* @param $shop_id |
| 978 |
* @param $cart_id |
| 979 |
* @param $checkout_url |
| 980 |
* @param $cart_total |
| 981 |
* @return mixed |
| 982 |
* @since 1.6.0 |
| 983 |
*/ |
| 984 |
public function updateCart( $shop_id, $cart_id, $checkout_url, $cart_total, $order_id ) |
| 985 |
{ |
| 986 |
|
| 987 |
$parameters = [ |
| 988 |
'resource_id' => (string) $order_id, |
| 989 |
'cart_total' => $cart_total |
| 990 |
]; |
| 991 |
|
| 992 |
if (! empty($checkout_url)) |
| 993 |
$parameters['checkout_url'] = (string) $checkout_url; |
| 994 |
|
| 995 |
$response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/carts/' . $cart_id, $parameters); |
| 996 |
|
| 997 |
$response = self::parseResponse($response); |
| 998 |
|
| 999 |
return $response->data ?? false; |
| 1000 |
} |
| 1001 |
|
| 1002 |
/** |
| 1003 |
* Add cart items |
| 1004 |
* |
| 1005 |
* @access public |
| 1006 |
* @param $shop_id |
| 1007 |
* @param $cart_id |
| 1008 |
* @param $item |
| 1009 |
* @return mixed |
| 1010 |
* @since 1.6.0 |
| 1011 |
*/ |
| 1012 |
public function addCartItem( $shop_id, $cart_id, $item) |
| 1013 |
{ |
| 1014 |
|
| 1015 |
$response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/carts/' . $cart_id . '/items', $item); |
| 1016 |
|
| 1017 |
$response = self::parseResponse($response); |
| 1018 |
|
| 1019 |
return $response->data ?? false; |
| 1020 |
} |
| 1021 |
|
| 1022 |
/** |
| 1023 |
* Replace cart items |
| 1024 |
* |
| 1025 |
* @access public |
| 1026 |
* @param $shop_id |
| 1027 |
* @param $cart_id |
| 1028 |
* @param $items |
| 1029 |
* @return mixed |
| 1030 |
* @since 1.6.0 |
| 1031 |
*/ |
| 1032 |
public function replaceCartItems( $shop_id, $cart_id, $items ) |
| 1033 |
{ |
| 1034 |
|
| 1035 |
$response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/carts/' . $cart_id . '/items/replace?with_resource_id', [ |
| 1036 |
'items' => $items |
| 1037 |
]); |
| 1038 |
|
| 1039 |
$response = self::parseResponse($response); |
| 1040 |
|
| 1041 |
return $response->data ?? false; |
| 1042 |
} |
| 1043 |
|
| 1044 |
/** |
| 1045 |
* Get cart items |
| 1046 |
* |
| 1047 |
* @access public |
| 1048 |
* @param $shop_id |
| 1049 |
* @param $cart_id |
| 1050 |
* @return mixed |
| 1051 |
* @since 1.6.0 |
| 1052 |
*/ |
| 1053 |
public function getCartItems($shop_id, $cart_id) |
| 1054 |
{ |
| 1055 |
|
| 1056 |
$response = $this->client->remote_get('/ecommerce/shops/' . $shop_id . '/carts/' . $cart_id . '/items'); |
| 1057 |
|
| 1058 |
$response = self::parseResponse($response); |
| 1059 |
|
| 1060 |
return $response->data ?? false; |
| 1061 |
} |
| 1062 |
|
| 1063 |
/** |
| 1064 |
* Delete cart items |
| 1065 |
* |
| 1066 |
* @access public |
| 1067 |
* @param $shop_id |
| 1068 |
* @param $cart_id |
| 1069 |
* @param $item_id |
| 1070 |
* @return mixed |
| 1071 |
* @since 1.6.0 |
| 1072 |
*/ |
| 1073 |
public function deleteCartItem($shop_id, $cart_id, $item_id) |
| 1074 |
{ |
| 1075 |
|
| 1076 |
$response = $this->client->remote_delete('/ecommerce/shops/' . $shop_id . '/carts/' . $cart_id . '/items/' . $item_id); |
| 1077 |
|
| 1078 |
$response = self::parseResponse($response); |
| 1079 |
|
| 1080 |
return $response->data ?? false; |
| 1081 |
} |
| 1082 |
|
| 1083 |
/** |
| 1084 |
* Batch process requests |
| 1085 |
* |
| 1086 |
* @access public |
| 1087 |
* @param $requests |
| 1088 |
* @return mixed |
| 1089 |
* @since 1.6.6 |
| 1090 |
*/ |
| 1091 |
public function batch($requests) |
| 1092 |
{ |
| 1093 |
$response = $this->client->remote_post('/batch', $requests); |
| 1094 |
|
| 1095 |
$response = self::parseResponse($response); |
| 1096 |
|
| 1097 |
return $response->responses ?? false; |
| 1098 |
} |
| 1099 |
|
| 1100 |
/** |
| 1101 |
* Get raw response body |
| 1102 |
* |
| 1103 |
* @access public |
| 1104 |
* @return string |
| 1105 |
* @since 1.6.0 |
| 1106 |
*/ |
| 1107 |
public function getResponseBody() |
| 1108 |
{ |
| 1109 |
return $this->response; |
| 1110 |
} |
| 1111 |
|
| 1112 |
/** |
| 1113 |
* Get response code |
| 1114 |
* |
| 1115 |
* @access public |
| 1116 |
* @return int |
| 1117 |
* @since 1.6.0 |
| 1118 |
*/ |
| 1119 |
public function responseCode() |
| 1120 |
{ |
| 1121 |
return $this->response_code; |
| 1122 |
} |
| 1123 |
|
| 1124 |
/** |
| 1125 |
* Get response code and body |
| 1126 |
* |
| 1127 |
* @access private |
| 1128 |
* @return mixed |
| 1129 |
* @since 1.6.0 |
| 1130 |
*/ |
| 1131 |
private function parseResponse($response) |
| 1132 |
{ |
| 1133 |
if ( ! is_wp_error($response) ) { |
| 1134 |
|
| 1135 |
$this->response = wp_remote_retrieve_body($response); |
| 1136 |
$this->response_code = wp_remote_retrieve_response_code($response); |
| 1137 |
|
| 1138 |
if ( ! is_wp_error($this->response)) { |
| 1139 |
$response = json_decode($this->response); |
| 1140 |
|
| 1141 |
if (json_last_error() == JSON_ERROR_NONE) { |
| 1142 |
|
| 1143 |
if ( ! isset($response->error)) { |
| 1144 |
return $response; |
| 1145 |
} |
| 1146 |
} |
| 1147 |
} |
| 1148 |
}else{ |
| 1149 |
$this->response = $response->get_error_message(); |
| 1150 |
$this->response_code = $response->get_error_code(); |
| 1151 |
} |
| 1152 |
|
| 1153 |
return false; |
| 1154 |
} |
| 1155 |
} |