| 1 |
<?php |
| 2 |
|
| 3 |
class WooMailerLiteRewriteApi extends WooMailerLiteApi |
| 4 |
{ |
| 5 |
const DEFAULT_BASE_URL = 'https://connect.mailerlite.com/api'; |
| 6 |
|
| 7 |
public function __construct() |
| 8 |
{ |
| 9 |
$baseUrl = $this->getBaseUrl(); |
| 10 |
parent::__construct($baseUrl); |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Base URL for the Rewrite API. Override for tests via MAILERLITE_API_URL env var |
| 15 |
* or by defining MAILERLITE_API_URL in wp-config (e.g. wp-env config in .wp-env.json). |
| 16 |
* |
| 17 |
* @return string |
| 18 |
*/ |
| 19 |
private function getBaseUrl() |
| 20 |
{ |
| 21 |
if (defined('MAILERLITE_API_URL')) { |
| 22 |
return MAILERLITE_API_URL; |
| 23 |
} |
| 24 |
return self::DEFAULT_BASE_URL; |
| 25 |
} |
| 26 |
|
| 27 |
public function validateKey($key) |
| 28 |
{ |
| 29 |
$this->setApiKey($key); |
| 30 |
return $this->get('/account'); |
| 31 |
} |
| 32 |
|
| 33 |
public function getGroups($args = []) |
| 34 |
{ |
| 35 |
return $this->get('/groups', $args); |
| 36 |
} |
| 37 |
|
| 38 |
public function createGroup($group) |
| 39 |
{ |
| 40 |
return $this->post('/groups', ['name' => $group]); |
| 41 |
} |
| 42 |
|
| 43 |
public function getShops() |
| 44 |
{ |
| 45 |
return $this->get('/ecommerce/shops'); |
| 46 |
} |
| 47 |
|
| 48 |
public function setConsumerData($data) |
| 49 |
{ |
| 50 |
$shopId = WooMailerLiteOptions::get('shopId'); |
| 51 |
if ($shopId) { |
| 52 |
return $this->put('/ecommerce/shops/' . $shopId, $data); |
| 53 |
} |
| 54 |
return $this->post('/ecommerce/shops', $data); |
| 55 |
} |
| 56 |
|
| 57 |
public function getGroupById($id) |
| 58 |
{ |
| 59 |
return $this->get('/groups/'.$id); |
| 60 |
} |
| 61 |
|
| 62 |
public function importProducts($products) |
| 63 |
{ |
| 64 |
|
| 65 |
return $this->post('/ecommerce/shops/' . WooMailerLiteOptions::get('shopId') . '/products/import?with_resource_id&replace_categories', |
| 66 |
$products); |
| 67 |
} |
| 68 |
|
| 69 |
public function importCategories($categories) |
| 70 |
{ |
| 71 |
return $this->post('/ecommerce/shops/' . WooMailerLiteOptions::get('shopId') . '/categories/import?with_resource_id', $categories); |
| 72 |
} |
| 73 |
|
| 74 |
public function syncProduct($shopId, $data, $replaceCategories = false) |
| 75 |
{ |
| 76 |
$response = $this->post('/ecommerce/shops/' . $shopId . '/products?with_resource_id', |
| 77 |
$data); |
| 78 |
if ($replaceCategories && isset($data['resource_id']) && isset($data['category_ids'])) { |
| 79 |
$response = $this->put('/ecommerce/shops/' . $shopId . '/products/' . $data['resource_id'] . '/categories/multiple?with_resource_id', |
| 80 |
[ |
| 81 |
'replace' => true, |
| 82 |
'categories' => $data['category_ids'] |
| 83 |
]); |
| 84 |
} |
| 85 |
return $response; |
| 86 |
} |
| 87 |
|
| 88 |
public function syncCustomers($customers) |
| 89 |
{ |
| 90 |
return $this->post('/ecommerce/shops/' . WooMailerLiteOptions::get('shopId') . '/customers/import?with_resource_id', |
| 91 |
$customers); |
| 92 |
} |
| 93 |
|
| 94 |
public function syncCategory($category) |
| 95 |
{ |
| 96 |
return $this->post('/ecommerce/shops/' . WooMailerLiteOptions::get('shopId') . '/categories?with_resource_id', $category); |
| 97 |
} |
| 98 |
|
| 99 |
public function deleteCategory($category) |
| 100 |
{ |
| 101 |
return $this->delete('/ecommerce/shops/' . WooMailerLiteOptions::get('shopId') . '/categories/' . $category . '?with_resource_id'); |
| 102 |
} |
| 103 |
|
| 104 |
public function syncOrder($shopId, $orderId, $customer, $cart, $status, $totalPrice, $createdAt, $stage = null) |
| 105 |
{ |
| 106 |
$orderStatus = WooMailerLiteOptions::isCompleteOrderStatus($status) ? 'complete' : 'pending'; |
| 107 |
if ( ! isset($cart['resource_id'])) { |
| 108 |
$cart['resource_id'] = (string)$orderId; |
| 109 |
} |
| 110 |
|
| 111 |
if ( ! isset($cart['cart_total'])) { |
| 112 |
$cart['cart_total'] = $totalPrice; |
| 113 |
} |
| 114 |
|
| 115 |
$parameters = [ |
| 116 |
'resource_id' => (string)$orderId, |
| 117 |
'customer' => $customer, |
| 118 |
'cart' => $cart, |
| 119 |
'status' => $orderStatus, |
| 120 |
'total_price' => $totalPrice, |
| 121 |
'created_at' => $createdAt |
| 122 |
]; |
| 123 |
|
| 124 |
if ($stage !== null) { |
| 125 |
$parameters['stage'] = $stage; |
| 126 |
} |
| 127 |
|
| 128 |
// check if wpml is enabled and current language is not english |
| 129 |
if ( defined( 'ICL_SITEPRESS_VERSION' )) { |
| 130 |
foreach($parameters['cart']['items'] as &$product) { |
| 131 |
|
| 132 |
// send english product id |
| 133 |
$args = array('element_id' => $product['product_resource_id'], 'element_type' => 'post' ); |
| 134 |
$productLang = apply_filters( 'wpml_element_language_details', null, $args ); |
| 135 |
|
| 136 |
if ($productLang->source_language_code) { |
| 137 |
$product['product_resource_id'] = (string)apply_filters( 'wpml_object_id', $product['product_resource_id'], 'post', FALSE, $productLang->source_language_code ) ?? $product['product_resource_id']; |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
return $this->post( |
| 142 |
sprintf('/ecommerce/shops/%s/orders/queue-order-sync?with_resource_id', $shopId), |
| 143 |
$parameters |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
public function deleteOrder($orderId) |
| 148 |
{ |
| 149 |
return $this->delete('/ecommerce/shops/' . WooMailerLiteOptions::get('shopId') . '/orders/' . $orderId . '?with_resource_id'); |
| 150 |
} |
| 151 |
|
| 152 |
public function getDoubleOptin() |
| 153 |
{ |
| 154 |
return $this->get('/subscribe-settings/double-optin'); |
| 155 |
} |
| 156 |
|
| 157 |
public function setDoubleOptin() |
| 158 |
{ |
| 159 |
return $this->post('/subscribe-settings/double-optin/toggle'); |
| 160 |
} |
| 161 |
|
| 162 |
public function getFields($params = []) |
| 163 |
{ |
| 164 |
return $this->get('/fields', $params); |
| 165 |
} |
| 166 |
|
| 167 |
public function createField($params) |
| 168 |
{ |
| 169 |
return $this->post('/fields', $params); |
| 170 |
} |
| 171 |
|
| 172 |
public function updateField($id, $params) |
| 173 |
{ |
| 174 |
return $this->put('/fields/' . $id, $params); |
| 175 |
} |
| 176 |
|
| 177 |
public function ping() |
| 178 |
{ |
| 179 |
return $this->get('/ecommerce/shops'); |
| 180 |
} |
| 181 |
|
| 182 |
public function toggleShop($shop, $state) |
| 183 |
{ |
| 184 |
|
| 185 |
return $this->put('/ecommerce/shops/' . WooMailerLiteOptions::get('shopId'), [ |
| 186 |
'name' => get_bloginfo('name'), |
| 187 |
'url' => $shop, |
| 188 |
'enabled' => $state |
| 189 |
]); |
| 190 |
} |
| 191 |
|
| 192 |
public function deleteProduct($productId) |
| 193 |
{ |
| 194 |
return $this->delete('/ecommerce/shops/' . WooMailerLiteOptions::get('shopId') . '/products/' . $productId . '?with_resource_id'); |
| 195 |
} |
| 196 |
} |
| 197 |
|