| 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 |
* Active Campaign Integration class. |
| 13 |
* |
| 14 |
* @link https://developers.activecampaign.com/reference/overview |
| 15 |
*/ |
| 16 |
class ActiveCampaign extends BaseMailIntegration { |
| 17 |
|
| 18 |
/** |
| 19 |
* Get name |
| 20 |
* |
| 21 |
* @return string |
| 22 |
*/ |
| 23 |
public function get_name() { |
| 24 |
return 'activecampaign'; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Adds a contact |
| 29 |
* |
| 30 |
* @param array $data data. |
| 31 |
* @return bool |
| 32 |
* |
| 33 |
* @link https://developers.activecampaign.com/reference/create-a-new-contact, https://developers.activecampaign.com/reference/create-contact-tag, https://developers.activecampaign.com/reference/update-list-status-for-contact |
| 34 |
*/ |
| 35 |
public function add_subscriber( $data ): bool { |
| 36 |
|
| 37 |
if ( ! isset( $data['fields'] ) ) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
$tags = $data['tags'] ?? array(); |
| 42 |
$lists = $data['lists'] ?? array(); |
| 43 |
|
| 44 |
$body = array( |
| 45 |
'allowNullEmail' => true, |
| 46 |
); |
| 47 |
|
| 48 |
$fixed_fields = array( |
| 49 |
'email', |
| 50 |
'firstName', |
| 51 |
'lastName', |
| 52 |
'phone', |
| 53 |
); |
| 54 |
|
| 55 |
foreach ( $data['fields'] as $field => $value ) { |
| 56 |
if ( in_array( $field, $fixed_fields, true ) ) { |
| 57 |
$body[ $field ] = $value; |
| 58 |
} else { |
| 59 |
if ( ! isset( $body['fieldValues'] ) ) { |
| 60 |
$body['fieldValues'] = array(); |
| 61 |
} |
| 62 |
|
| 63 |
$body['fieldValues'][] = array( |
| 64 |
'field' => strval( $field ), |
| 65 |
'value' => $value, |
| 66 |
); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
$settings = $this->get_settings(); |
| 71 |
$url = rtrim( $settings['url'], '/' ) . '/api/3/contacts'; |
| 72 |
|
| 73 |
$body = array( |
| 74 |
'contact' => $body, |
| 75 |
); |
| 76 |
|
| 77 |
$res = wp_remote_post( |
| 78 |
$url, |
| 79 |
array( |
| 80 |
'body' => wp_json_encode( $body ), |
| 81 |
'timeout' => 45, |
| 82 |
'headers' => $this->get_headers(), |
| 83 |
) |
| 84 |
); |
| 85 |
|
| 86 |
$resp_body = json_decode( wp_remote_retrieve_body( $res ), true ); |
| 87 |
$contact_id = $resp_body['contact']['id'] ?? null; |
| 88 |
|
| 89 |
if ( is_wp_error( $res ) || 201 !== wp_remote_retrieve_response_code( $res ) || empty( $contact_id ) ) { |
| 90 |
|
| 91 |
// Check for duplicate contact. |
| 92 |
$err_body = json_decode( wp_remote_retrieve_body( $res ), true ); |
| 93 |
$err_code = $err_body['errors'][0]['code'] ?? null; |
| 94 |
|
| 95 |
if ( 'duplicate' === $err_code ) { |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
if ( ! empty( $tags ) ) { |
| 103 |
// TODO @samin: Implement parallel requests. |
| 104 |
$url = rtrim( $settings['url'], '/' ) . '/api/3/contactTags'; |
| 105 |
foreach ( $tags as $tag ) { |
| 106 |
$res = wp_remote_post( |
| 107 |
$url, |
| 108 |
array( |
| 109 |
'body' => wp_json_encode( |
| 110 |
array( |
| 111 |
'contactTag' => array( |
| 112 |
'contact' => $contact_id, |
| 113 |
'tag' => $tag, |
| 114 |
), |
| 115 |
) |
| 116 |
), |
| 117 |
'timeout' => 45, |
| 118 |
'headers' => $this->get_headers(), |
| 119 |
'blocking' => true, |
| 120 |
) |
| 121 |
); |
| 122 |
|
| 123 |
if ( is_wp_error( $res ) || 201 !== wp_remote_retrieve_response_code( $res ) ) { |
| 124 |
return false; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if ( ! empty( $lists ) ) { |
| 130 |
// TODO @samin: Implement parallel requests. |
| 131 |
$url = rtrim( $settings['url'], '/' ) . '/api/3/contactLists'; |
| 132 |
foreach ( $lists as $list ) { |
| 133 |
$res = wp_remote_post( |
| 134 |
$url, |
| 135 |
array( |
| 136 |
'body' => wp_json_encode( |
| 137 |
array( |
| 138 |
'contactList' => array( |
| 139 |
'list' => $list, |
| 140 |
'contact' => $contact_id, |
| 141 |
'status' => '1', |
| 142 |
), |
| 143 |
) |
| 144 |
), |
| 145 |
'timeout' => 45, |
| 146 |
'headers' => $this->get_headers(), |
| 147 |
'blocking' => true, |
| 148 |
) |
| 149 |
); |
| 150 |
|
| 151 |
if ( is_wp_error( $res ) || 201 !== wp_remote_retrieve_response_code( $res ) ) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
$this->add_lead( $data ); |
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Adds a lead to the leads table |
| 163 |
* |
| 164 |
* @param array $data data. |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
public function add_lead( $data ) { |
| 168 |
|
| 169 |
$fields = Utils::sanitize_json_array( $data['fields'] ); |
| 170 |
$ip = Utils::get_user_ip(); |
| 171 |
|
| 172 |
$leads_data = array( |
| 173 |
'email' => isset( $fields['email'] ) ? sanitize_email( $fields['email'] ) : ( sanitize_text_field( $fields['phone'] ?? '' ) ), |
| 174 |
'name' => trim( ( $fields['firstName'] ?? '' ) . ' ' . ( $fields['lastName'] ?? '' ) ), |
| 175 |
'conv_id' => intval( $data['conv_id'] ), |
| 176 |
'data' => array_merge( |
| 177 |
$fields, |
| 178 |
array( |
| 179 |
'tags' => $data['tags'], |
| 180 |
'lists' => $data['lists'], |
| 181 |
'ip' => $ip, |
| 182 |
) |
| 183 |
), |
| 184 |
'integration' => 'activecampaign', |
| 185 |
); |
| 186 |
|
| 187 |
$this->db->add_lead( $leads_data ); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
/** |
| 192 |
* Gets the fields. |
| 193 |
* |
| 194 |
* @param array $args receive data as array. |
| 195 |
* @return array |
| 196 |
* |
| 197 |
* @link https://developers.activecampaign.com/reference/retrieve-fields |
| 198 |
*/ |
| 199 |
public function get_fields( $args = array() ): array { |
| 200 |
$use_cache = $args['use_cache']; |
| 201 |
|
| 202 |
if ( $use_cache ) { |
| 203 |
$res = $this->get_cached_fields(); |
| 204 |
|
| 205 |
if ( ! empty( $res ) ) { |
| 206 |
return $res; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
$settings = $this->get_settings(); |
| 211 |
$url = rtrim( $settings['url'], '/' ) . '/api/3/fields'; |
| 212 |
$url = add_query_arg( |
| 213 |
array( |
| 214 |
'limit' => 9999, |
| 215 |
), |
| 216 |
$url |
| 217 |
); |
| 218 |
|
| 219 |
$res = wp_remote_get( |
| 220 |
$url, |
| 221 |
array( |
| 222 |
'timeout' => 45, |
| 223 |
'headers' => $this->get_headers(), |
| 224 |
) |
| 225 |
); |
| 226 |
|
| 227 |
$default_fields = array( |
| 228 |
array( |
| 229 |
'value' => 'email', |
| 230 |
'label' => 'Email', |
| 231 |
), |
| 232 |
array( |
| 233 |
'value' => 'firstName', |
| 234 |
'label' => 'First Name', |
| 235 |
), |
| 236 |
array( |
| 237 |
'value' => 'lastName', |
| 238 |
'label' => 'Last Name', |
| 239 |
), |
| 240 |
array( |
| 241 |
'value' => 'phone', |
| 242 |
'label' => 'Phone', |
| 243 |
), |
| 244 |
); |
| 245 |
|
| 246 |
if ( ! is_wp_error( $res ) && 200 == wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore |
| 247 |
$body = wp_remote_retrieve_body( $res ); |
| 248 |
$body = json_decode( $body, true ); |
| 249 |
|
| 250 |
if ( isset( $body['fields'] ) && is_array( $body['fields'] ) ) { |
| 251 |
|
| 252 |
$ret = array(); |
| 253 |
|
| 254 |
foreach ( $body['fields'] as $d ) { |
| 255 |
$ret[] = array( |
| 256 |
'value' => $d['id'], |
| 257 |
'label' => $d['title'], |
| 258 |
); |
| 259 |
} |
| 260 |
|
| 261 |
$ret = array_merge( $default_fields, $ret ); |
| 262 |
|
| 263 |
$this->set_fields_cache( $ret ); |
| 264 |
|
| 265 |
return $ret; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
return array(); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Gets tags |
| 274 |
* |
| 275 |
* @param array $args receive data as array. |
| 276 |
* @return array |
| 277 |
* |
| 278 |
* @link https://developers.activecampaign.com/reference/retrieve-all-tags, https://developers.activecampaign.com/reference/retrieve-all-lists |
| 279 |
*/ |
| 280 |
public function get_lists( $args = array() ): array { |
| 281 |
$use_cache = $args['use_cache']; |
| 282 |
|
| 283 |
if ( $use_cache ) { |
| 284 |
$res = $this->get_cached_lists(); |
| 285 |
|
| 286 |
if ( ! empty( $res ) ) { |
| 287 |
return $res; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
$data = array( |
| 292 |
'lists' => array(), |
| 293 |
'tags' => array(), |
| 294 |
); |
| 295 |
|
| 296 |
// Tags. |
| 297 |
$settings = $this->get_settings(); |
| 298 |
$url = rtrim( $settings['url'], '/' ) . '/api/3/tags'; |
| 299 |
|
| 300 |
$res = wp_remote_get( |
| 301 |
$url, |
| 302 |
array( |
| 303 |
'timeout' => 45, |
| 304 |
'headers' => $this->get_headers(), |
| 305 |
) |
| 306 |
); |
| 307 |
|
| 308 |
if ( ! is_wp_error( $res ) && 200 == wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore |
| 309 |
$body = wp_remote_retrieve_body( $res ); |
| 310 |
$body = json_decode( $body, true ); |
| 311 |
|
| 312 |
if ( isset( $body['tags'] ) && is_array( $body['tags'] ) ) { |
| 313 |
foreach ( $body['tags'] as $d ) { |
| 314 |
$data['tags'][] = array( |
| 315 |
'value' => $d['id'], |
| 316 |
'label' => $d['tag'], |
| 317 |
); |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
// Lists. |
| 323 |
$url = rtrim( $settings['url'], '/' ) . '/api/3/lists'; |
| 324 |
$url = add_query_arg( |
| 325 |
array( |
| 326 |
'limit' => 9999, |
| 327 |
), |
| 328 |
$url |
| 329 |
); |
| 330 |
|
| 331 |
$res = wp_remote_get( |
| 332 |
$url, |
| 333 |
array( |
| 334 |
'timeout' => 45, |
| 335 |
'headers' => $this->get_headers(), |
| 336 |
) |
| 337 |
); |
| 338 |
|
| 339 |
if ( ! is_wp_error( $res ) && 200 == wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore |
| 340 |
$body = wp_remote_retrieve_body( $res ); |
| 341 |
$body = json_decode( $body, true ); |
| 342 |
|
| 343 |
if ( isset( $body['lists'] ) && is_array( $body['lists'] ) ) { |
| 344 |
foreach ( $body['lists'] as $d ) { |
| 345 |
$data['lists'][] = array( |
| 346 |
'value' => $d['id'], |
| 347 |
'label' => $d['name'], |
| 348 |
); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
$this->set_lists_cache( $data ); |
| 354 |
|
| 355 |
return $data; |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Gets headers with authorization token |
| 360 |
* |
| 361 |
* @return array |
| 362 |
*/ |
| 363 |
protected function get_headers() { |
| 364 |
|
| 365 |
$settings = $this->get_settings(); |
| 366 |
|
| 367 |
return array( |
| 368 |
'Content-Type' => 'application/json', |
| 369 |
'Accept' => 'application/json', |
| 370 |
'Api-Token' => $settings['apiKey'], |
| 371 |
); |
| 372 |
} |
| 373 |
} |
| 374 |
|