| 1 |
<?php // phpcs:ignore |
| 2 |
|
| 3 |
namespace OPTN\Includes\Integrations\Implementations; |
| 4 |
|
| 5 |
use http\Url; |
| 6 |
use OPTN\Includes\Integrations\Base\BaseMailIntegration; |
| 7 |
use OPTN\Includes\Utils\Cache; |
| 8 |
use OPTN\Includes\Utils\Utils; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Drip Integration class. |
| 14 |
* |
| 15 |
* @link https://developer.drip.com/ |
| 16 |
*/ |
| 17 |
class Drip extends BaseMailIntegration { |
| 18 |
|
| 19 |
/** |
| 20 |
* Get name |
| 21 |
* |
| 22 |
* @return string |
| 23 |
*/ |
| 24 |
public function get_name() { |
| 25 |
return 'drip'; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Adds a subscribe to Drip by account id |
| 30 |
* |
| 31 |
* @param array $data data. |
| 32 |
* @return bool |
| 33 |
* |
| 34 |
* @link https://developer.drip.com/?shell#create-or-update-a-subscriber |
| 35 |
*/ |
| 36 |
public function add_subscriber( $data ): bool { |
| 37 |
|
| 38 |
if ( ! isset( $data['fields'] ) || ! isset( $data['fields']['email'] ) ) { |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
$subscribers = array(); |
| 43 |
|
| 44 |
if ( ! empty( $data['fields'] ) ) { |
| 45 |
foreach ( $data['fields'] as $key => $value ) { |
| 46 |
$subscribers[ $key ] = $value; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
$body = array( |
| 51 |
'subscribers' => array( $subscribers ), |
| 52 |
); |
| 53 |
|
| 54 |
$success = false; |
| 55 |
|
| 56 |
foreach ( $data['list'] as $list_id ) { |
| 57 |
|
| 58 |
$res = wp_remote_post( |
| 59 |
'https://api.getdrip.com/v2/' . $list_id . '/subscribers', |
| 60 |
array( |
| 61 |
'body' => wp_json_encode( $body ), |
| 62 |
'timeout' => 45, |
| 63 |
'headers' => $this->get_headers(), |
| 64 |
) |
| 65 |
); |
| 66 |
|
| 67 |
// Response code should be either 200 or 201. |
| 68 |
if ( ! is_wp_error( $res ) && in_array( wp_remote_retrieve_response_code( $res ), array( 200, 201 ) ) ) { // phpcs:ignore |
| 69 |
$success = true; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
if ( $success ) { |
| 74 |
$this->add_lead( $data ); |
| 75 |
} |
| 76 |
|
| 77 |
return $success; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
/** |
| 82 |
* Adds a lead to the leads table |
| 83 |
* |
| 84 |
* @param array $data data. |
| 85 |
* @return void |
| 86 |
*/ |
| 87 |
public function add_lead( $data ) { |
| 88 |
|
| 89 |
$fields = Utils::sanitize_json_array( $data['fields'] ); |
| 90 |
$fields['ip'] = Utils::get_user_ip(); |
| 91 |
|
| 92 |
$leads_data = array( |
| 93 |
'email' => sanitize_email( $fields['email'] ), |
| 94 |
'name' => trim( ( $fields['first_name'] ?? '' ) . ' ' . ( $fields['last_name'] ?? '' ) ), |
| 95 |
'conv_id' => intval( $data['conv_id'] ), |
| 96 |
'data' => $fields, |
| 97 |
'integration' => 'drip', |
| 98 |
); |
| 99 |
|
| 100 |
$this->db->add_lead( $leads_data ); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* Gets the fields for Drip by account id |
| 106 |
* |
| 107 |
* @param array $args receive data as array. |
| 108 |
* @return array |
| 109 |
* @link https://developer.drip.com/?shell#custom-fields |
| 110 |
*/ |
| 111 |
public function get_fields( $args = array() ): array { |
| 112 |
|
| 113 |
if ( empty( $args['list_id'] ) ) { |
| 114 |
return array(); |
| 115 |
} |
| 116 |
$use_cache = $args['use_cache']; |
| 117 |
|
| 118 |
$list_ids = is_array( $args['list_id'] ) ? $args['list_id'] : array( $args['list_id'] ); |
| 119 |
$unique_fields = array( |
| 120 |
array( |
| 121 |
'value' => 'email', |
| 122 |
'label' => 'Email', |
| 123 |
), |
| 124 |
); |
| 125 |
|
| 126 |
foreach ( $list_ids as $list_id ) { |
| 127 |
if ( $use_cache ) { |
| 128 |
$cached = Cache::get( 'optn_int_drip_fields_' . $this->account_id . '_' . $list_id ); |
| 129 |
if ( ! empty( $cached ) ) { |
| 130 |
$fields = json_decode( $cached, true ); |
| 131 |
$unique_fields = $this->merge_unique_fields( $unique_fields, $fields ); |
| 132 |
continue; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
$res = wp_remote_get( |
| 137 |
'https://api.getdrip.com/v2/' . $list_id . '/custom_field_identifiers', |
| 138 |
array( |
| 139 |
'timeout' => 45, |
| 140 |
'headers' => $this->get_headers(), |
| 141 |
) |
| 142 |
); |
| 143 |
|
| 144 |
if ( ! is_wp_error( $res ) && 200 == wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore |
| 145 |
$body = wp_remote_retrieve_body( $res ); |
| 146 |
$body = json_decode( $body, true ); |
| 147 |
|
| 148 |
if ( isset( $body['custom_field_identifiers'] ) && is_array( $body['custom_field_identifiers'] ) ) { |
| 149 |
$fields = array(); |
| 150 |
|
| 151 |
foreach ( $body['custom_field_identifiers'] as $d ) { |
| 152 |
$fields[] = array( |
| 153 |
'value' => $d, |
| 154 |
'label' => ucwords( str_replace( '_', ' ', $d ) ), |
| 155 |
); |
| 156 |
} |
| 157 |
|
| 158 |
// Cache the fields. |
| 159 |
Cache::set( 'optn_int_drip_fields_' . $this->account_id . '_' . $list_id, wp_json_encode( $fields ) ); |
| 160 |
|
| 161 |
$unique_fields = $this->merge_unique_fields( $unique_fields, $fields ); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return $unique_fields; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Merge fields for multiple list |
| 171 |
* |
| 172 |
* @param array $existing_fields existing fields. |
| 173 |
* @param array $new_fields new fields. |
| 174 |
* @return array |
| 175 |
*/ |
| 176 |
private function merge_unique_fields( array $existing_fields, array $new_fields ): array { |
| 177 |
foreach ( $new_fields as $field ) { |
| 178 |
if ( ! array_key_exists( $field['value'], array_column( $existing_fields, 'value' ) ) ) { |
| 179 |
$existing_fields[] = $field; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
return $existing_fields; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Gets account list for Drip |
| 188 |
* |
| 189 |
* @param array $args receive optional data as array. |
| 190 |
* @return array |
| 191 |
* |
| 192 |
* @link https://developer.drip.com/?shell#list-all-accounts |
| 193 |
*/ |
| 194 |
public function get_lists( $args = array() ): array { |
| 195 |
if ( ! empty( $args['list_ids'] ) ) { |
| 196 |
return array(); |
| 197 |
} |
| 198 |
$use_cache = $args['use_cache']; |
| 199 |
if ( $use_cache ) { |
| 200 |
$res = Cache::get( 'optn_int_drip_lists_' . $this->account_id ); |
| 201 |
|
| 202 |
if ( ! empty( $res ) ) { |
| 203 |
return json_decode( $res ); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
$res = wp_remote_get( |
| 208 |
'https://api.getdrip.com/v2/accounts', |
| 209 |
array( |
| 210 |
'timeout' => 45, |
| 211 |
'headers' => $this->get_headers(), |
| 212 |
) |
| 213 |
); |
| 214 |
|
| 215 |
if ( ! is_wp_error( $res ) && 200 == wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore |
| 216 |
$body = wp_remote_retrieve_body( $res ); |
| 217 |
$body = json_decode( $body, true ); |
| 218 |
|
| 219 |
if ( isset( $body['accounts'] ) && is_array( $body['accounts'] ) ) { |
| 220 |
|
| 221 |
$ret = array(); |
| 222 |
|
| 223 |
foreach ( $body['accounts'] as $d ) { |
| 224 |
$ret[] = array( |
| 225 |
'value' => $d['id'], |
| 226 |
'label' => $d['name'], |
| 227 |
); |
| 228 |
} |
| 229 |
|
| 230 |
Cache::set( 'optn_int_drip_lists_' . $this->account_id, wp_json_encode( $ret ) ); |
| 231 |
|
| 232 |
return $ret; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
return array(); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Gets headers with authorization token |
| 241 |
* |
| 242 |
* @return array |
| 243 |
*/ |
| 244 |
protected function get_headers() { |
| 245 |
|
| 246 |
$settings = $this->get_settings(); |
| 247 |
$basic_auth = base64_encode( $settings['apiKey'] . ':' ); // phpcs:ignore |
| 248 |
|
| 249 |
return array( |
| 250 |
'User-Agent' => 'WowOptin/' . OPTN_VERSION, |
| 251 |
'Content-Type' => 'application/json', |
| 252 |
'Accept' => 'application/json', |
| 253 |
'Authorization' => 'Basic ' . $basic_auth, |
| 254 |
); |
| 255 |
} |
| 256 |
} |
| 257 |
|