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