| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.2.6 |
| 5 |
* |
| 6 |
* @author FirePlugins <[email protected]> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FPFramework\Base\Integrations; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Brevo extends Integration |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Create a new instance |
| 23 |
* @param array $options The service's required options |
| 24 |
* @throws \Exception |
| 25 |
*/ |
| 26 |
public function __construct($options) |
| 27 |
{ |
| 28 |
$this->setKey($options['api']); |
| 29 |
$this->setEndpoint('https://api.brevo.com/v3'); |
| 30 |
|
| 31 |
$this->headers = array_merge($this->headers, [ |
| 32 |
'Accept' => 'application/json', |
| 33 |
'Content-Type' => 'application/json', |
| 34 |
'API-Key' => $this->key |
| 35 |
]); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Subscribes a user to a SendinBlue Account |
| 40 |
* |
| 41 |
* API Reference: |
| 42 |
* https://developers.brevo.com/reference/createcontact |
| 43 |
* |
| 44 |
* @param string $email The user's email |
| 45 |
* @param array $params All the form fields |
| 46 |
* @param string $listid The List ID. |
| 47 |
* @param boolean $update_existing Whether to update the existing contact. |
| 48 |
* @param boolean $double_optin Whether to subscribe the user via double opt in. |
| 49 |
* @param boolean $double_optin_redirect_url Enter the URL to redirect the user upon confirming their double opt-in. |
| 50 |
* @param boolean $double_optin_template_id Enter the template ID of the template to use for the double opt-in confirmation email. |
| 51 |
* |
| 52 |
* @return boolean |
| 53 |
*/ |
| 54 |
public function subscribe($email, $params, $listid = false, $update_existing = true, $double_optin = false, $double_optin_redirect_url = '', $double_optin_template_id = '') |
| 55 |
{ |
| 56 |
$data = [ |
| 57 |
'email' => $email, |
| 58 |
'attributes' => (object) $params, |
| 59 |
'updateEnabled' => $update_existing |
| 60 |
]; |
| 61 |
|
| 62 |
if ($listid) |
| 63 |
{ |
| 64 |
if (!$double_optin) |
| 65 |
{ |
| 66 |
$data['listIds'] = [(int) $listid]; |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
$data['includeListIds'] = [(int) $listid]; |
| 71 |
$data['redirectionUrl'] = (string) $double_optin_redirect_url; |
| 72 |
$data['templateId'] = (int) $double_optin_template_id; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// Speciffy whether we are subscribing via double optin |
| 77 |
$suffix = $double_optin ? '/doubleOptinConfirmation' : ''; |
| 78 |
|
| 79 |
$this->post('contacts' . $suffix, $data); |
| 80 |
|
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Returns all contact lists, following pagination. |
| 86 |
* |
| 87 |
* API Reference: |
| 88 |
* https://developers.brevo.com/reference/getlists-1 |
| 89 |
* |
| 90 |
* @param int $limit The number of results per page |
| 91 |
* @param string $sort Sort order: `asc` or `desc` |
| 92 |
* |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
public function getLists($limit = 50, $sort = 'asc') |
| 96 |
{ |
| 97 |
$lists = []; |
| 98 |
|
| 99 |
do { |
| 100 |
$response = $this->get( |
| 101 |
'contacts/lists', |
| 102 |
[ |
| 103 |
'limit' => $limit, |
| 104 |
'offset' => count($lists), |
| 105 |
'sort' => $sort, |
| 106 |
] |
| 107 |
); |
| 108 |
|
| 109 |
// A failed request must not be reported as an empty account. |
| 110 |
if (!$this->success()) |
| 111 |
{ |
| 112 |
return []; |
| 113 |
} |
| 114 |
|
| 115 |
// sanity check |
| 116 |
if (!is_array($response) || !isset($response['lists']) || !is_array($response['lists'])) |
| 117 |
{ |
| 118 |
return $lists; |
| 119 |
} |
| 120 |
|
| 121 |
$newLists = []; |
| 122 |
|
| 123 |
foreach ($response['lists'] as $list) |
| 124 |
{ |
| 125 |
if (!is_array($list) || !isset($list['id']) || !isset($list['name'])) |
| 126 |
{ |
| 127 |
continue; |
| 128 |
} |
| 129 |
|
| 130 |
$newLists[] = [ |
| 131 |
'id' => $list['id'], |
| 132 |
'name' => $list['name'] |
| 133 |
]; |
| 134 |
} |
| 135 |
|
| 136 |
if (!$newLists) |
| 137 |
{ |
| 138 |
break; |
| 139 |
} |
| 140 |
|
| 141 |
$lists = array_merge($lists, $newLists); |
| 142 |
|
| 143 |
usleep(250000); |
| 144 |
} while (isset($response['count']) && (int) $response['count'] > count($lists)); |
| 145 |
|
| 146 |
return $lists; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Verifies API credentials against account endpoint. |
| 151 |
* |
| 152 |
* @return bool |
| 153 |
*/ |
| 154 |
public function verifyConnection() |
| 155 |
{ |
| 156 |
$this->get('account'); |
| 157 |
|
| 158 |
return $this->success(); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Get the last error returned by either the network transport, or by the API. |
| 163 |
* |
| 164 |
* API Reference: |
| 165 |
* https://developers.brevo.com/docs/how-it-works#error-codes |
| 166 |
* |
| 167 |
* @return string |
| 168 |
*/ |
| 169 |
public function getLastError() |
| 170 |
{ |
| 171 |
if ($error = parent::getLastError()) |
| 172 |
{ |
| 173 |
return $error; |
| 174 |
} |
| 175 |
|
| 176 |
$body = isset($this->last_response['body']) ? $this->last_response['body'] : []; |
| 177 |
|
| 178 |
if (is_array($body) && !empty($body['message'])) |
| 179 |
{ |
| 180 |
return (string) $body['message']; |
| 181 |
} |
| 182 |
|
| 183 |
return $this->getHttpStatusError(); |
| 184 |
} |
| 185 |
} |
| 186 |
|