| 1 |
<?php // phpcs:ignore |
| 2 |
|
| 3 |
namespace OPTN\Includes\Integrations\Implementations; |
| 4 |
|
| 5 |
use OPTN\Includes\Integrations\Base\BaseMailIntegration; |
| 6 |
use OPTN\Includes\Utils\Cache; |
| 7 |
use OPTN\Includes\Utils\Utils; |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) || exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Brevo Integration class. |
| 13 |
* |
| 14 |
* @link https://developers.brevo.com/reference/getting-started-1 |
| 15 |
*/ |
| 16 |
class Brevo extends BaseMailIntegration { |
| 17 |
|
| 18 |
/** |
| 19 |
* Get name |
| 20 |
* |
| 21 |
* @return string |
| 22 |
*/ |
| 23 |
public function get_name() { |
| 24 |
return 'brevo'; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Adds a subscribe to Brevo |
| 29 |
* |
| 30 |
* @param array $data data. |
| 31 |
* @return bool |
| 32 |
* |
| 33 |
* @link https://developers.brevo.com/reference/createcontact |
| 34 |
*/ |
| 35 |
public function add_subscriber( $data ): bool { |
| 36 |
|
| 37 |
if ( ! isset( $data['fields'] ) || ! isset( $data['fields']['email'] ) ) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
$email = $data['fields']['email']; |
| 42 |
unset( $data['fields']['email'] ); |
| 43 |
|
| 44 |
$body = array( |
| 45 |
'email' => $email, |
| 46 |
'listIds' => $data['list'], |
| 47 |
'updateEnabled' => true, |
| 48 |
); |
| 49 |
|
| 50 |
// Sending empty attributes will fail the request. |
| 51 |
if ( ! empty( $data['fields'] ) ) { |
| 52 |
$body['attributes'] = $data['fields']; |
| 53 |
} |
| 54 |
|
| 55 |
$res = wp_remote_post( |
| 56 |
'https://api.brevo.com/v3/contacts', |
| 57 |
array( |
| 58 |
'body' => wp_json_encode( $body ), |
| 59 |
'timeout' => 45, |
| 60 |
'headers' => $this->get_headers(), |
| 61 |
) |
| 62 |
); |
| 63 |
|
| 64 |
$code = wp_remote_retrieve_response_code( $res ); |
| 65 |
|
| 66 |
// Brevo returns 400 if email already exists. |
| 67 |
if ( 400 === $code ) { |
| 68 |
$body = wp_remote_retrieve_body( $res ); |
| 69 |
$body = json_decode( $body, true ); |
| 70 |
$error_code = $body['code'] ?? ''; |
| 71 |
|
| 72 |
if ( 'duplicate_parameter' === $error_code ) { |
| 73 |
return true; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if ( ! is_wp_error( $res ) && in_array( $code, array( 200, 201, 204 ) ) ) { // phpcs:ignore |
| 78 |
$data['fields']['email'] = $email; |
| 79 |
$this->add_lead( $data ); |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Adds a lead to the leads table |
| 88 |
* |
| 89 |
* @param array $data data. |
| 90 |
* @return void |
| 91 |
*/ |
| 92 |
public function add_lead( $data ) { |
| 93 |
|
| 94 |
$fields = Utils::sanitize_json_array( $data['fields'] ); |
| 95 |
$fields['ip'] = Utils::get_user_ip(); |
| 96 |
|
| 97 |
$leads_data = array( |
| 98 |
'email' => sanitize_email( $fields['email'] ), |
| 99 |
'name' => isset( $fields['name'] ) ? $fields['name'] : null, |
| 100 |
'conv_id' => intval( $data['conv_id'] ), |
| 101 |
'data' => $fields, |
| 102 |
'integration' => 'brevo', |
| 103 |
); |
| 104 |
|
| 105 |
$this->db->add_lead( $leads_data ); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
/** |
| 110 |
* Gets the fields for brevo |
| 111 |
* |
| 112 |
* @param array $args receive data as array. |
| 113 |
* @return array |
| 114 |
* |
| 115 |
* @link https://developers.brevo.com/reference/getattributes-1 |
| 116 |
*/ |
| 117 |
public function get_fields( $args = array() ): array { |
| 118 |
$use_cache = $args['use_cache']; |
| 119 |
if ( $use_cache ) { |
| 120 |
$res = $this->get_cached_fields(); |
| 121 |
|
| 122 |
if ( ! empty( $res ) ) { |
| 123 |
return $res; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
$res = wp_remote_get( |
| 128 |
'https://api.brevo.com/v3/contacts/attributes', |
| 129 |
array( |
| 130 |
'timeout' => 45, |
| 131 |
'headers' => $this->get_headers(), |
| 132 |
) |
| 133 |
); |
| 134 |
|
| 135 |
if ( ! is_wp_error( $res ) && 200 == wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore |
| 136 |
$body = wp_remote_retrieve_body( $res ); |
| 137 |
$body = json_decode( $body, true ); |
| 138 |
|
| 139 |
if ( isset( $body['attributes'] ) && is_array( $body['attributes'] ) ) { |
| 140 |
|
| 141 |
$ret = array(); |
| 142 |
|
| 143 |
foreach ( $body['attributes'] as $d ) { |
| 144 |
$ret[] = array( |
| 145 |
'value' => $d['name'], |
| 146 |
'label' => $d['name'], |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
$ret[] = array( |
| 151 |
'value' => 'email', |
| 152 |
'label' => 'Email', |
| 153 |
); |
| 154 |
|
| 155 |
$this->set_fields_cache( $ret ); |
| 156 |
|
| 157 |
return $ret; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
return array(); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Gets groups for brevo |
| 166 |
* |
| 167 |
* @param array $args receive data as array. |
| 168 |
* @return array |
| 169 |
* |
| 170 |
* @link https://developers.brevo.com/reference/getlists-1 |
| 171 |
*/ |
| 172 |
public function get_lists( $args = array() ): array { |
| 173 |
$use_cache = $args['use_cache']; |
| 174 |
if ( $use_cache ) { |
| 175 |
$res = $this->get_cached_lists(); |
| 176 |
|
| 177 |
if ( ! empty( $res ) ) { |
| 178 |
return $res; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
$res = wp_remote_get( |
| 183 |
'https://api.brevo.com/v3/contacts/lists', |
| 184 |
array( |
| 185 |
'timeout' => 45, |
| 186 |
'headers' => $this->get_headers(), |
| 187 |
) |
| 188 |
); |
| 189 |
|
| 190 |
if ( ! is_wp_error( $res ) && 200 == wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore |
| 191 |
$body = wp_remote_retrieve_body( $res ); |
| 192 |
$body = json_decode( $body, true ); |
| 193 |
|
| 194 |
if ( isset( $body['lists'] ) && is_array( $body['lists'] ) ) { |
| 195 |
|
| 196 |
$ret = array(); |
| 197 |
|
| 198 |
foreach ( $body['lists'] as $d ) { |
| 199 |
$ret[] = array( |
| 200 |
'value' => $d['id'], |
| 201 |
'label' => $d['name'], |
| 202 |
); |
| 203 |
} |
| 204 |
|
| 205 |
$this->set_lists_cache( $ret ); |
| 206 |
|
| 207 |
return $ret; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
return array(); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Gets headers with authorization token |
| 216 |
* |
| 217 |
* @return array |
| 218 |
*/ |
| 219 |
protected function get_headers() { |
| 220 |
|
| 221 |
$settings = $this->get_settings(); |
| 222 |
|
| 223 |
return array( |
| 224 |
'Content-Type' => 'application/json', |
| 225 |
'Accept' => 'application/json', |
| 226 |
'api-key' => $settings['apiKey'], |
| 227 |
); |
| 228 |
} |
| 229 |
} |
| 230 |
|