| 1 |
<?php // phpcs:ignore |
| 2 |
|
| 3 |
namespace OPTN\Includes\Integrations\Implementations; |
| 4 |
|
| 5 |
use OPTN\Includes\Integrations\Base\BaseMailIntegration; |
| 6 |
use OPTN\Includes\Utils\Utils; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Get Response Integration class. |
| 12 |
* |
| 13 |
* @link https://apidocs.getresponse.com/v3 |
| 14 |
*/ |
| 15 |
class GetResponse extends BaseMailIntegration { |
| 16 |
|
| 17 |
/** |
| 18 |
* Get name |
| 19 |
* |
| 20 |
* @return string |
| 21 |
*/ |
| 22 |
public function get_name() { |
| 23 |
return 'getresponse'; |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* Adds a subscribe to get response |
| 29 |
* |
| 30 |
* @param array $data data. |
| 31 |
* @return bool |
| 32 |
* |
| 33 |
* @link https://apireference.getresponse.com/#operation/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 |
$fields = $data['fields']; |
| 42 |
|
| 43 |
$body = array( |
| 44 |
'email' => $fields['email'], |
| 45 |
'ipAddress' => Utils::get_user_ip(), |
| 46 |
); |
| 47 |
if ( ! empty( $fields ) ) { |
| 48 |
|
| 49 |
$fields_arr = array(); |
| 50 |
unset( $fields['email'] ); |
| 51 |
if ( ! empty( $fields['name'] ) ) { |
| 52 |
$body['name'] = $fields['name']; |
| 53 |
unset( $fields['name'] ); |
| 54 |
} |
| 55 |
foreach ( $fields as $key => $value ) { |
| 56 |
$fields_arr[] = array( |
| 57 |
'customFieldId' => $key, |
| 58 |
'value' => is_array( $value ) ? $value : array( $value ), |
| 59 |
); |
| 60 |
} |
| 61 |
$body['customFieldValues'] = $fields_arr; |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
$success = false; |
| 66 |
$duplicate = false; |
| 67 |
foreach ( $data['list'] as $list_id ) { |
| 68 |
$body['campaign'] = array( |
| 69 |
'campaignId' => $list_id, |
| 70 |
); |
| 71 |
|
| 72 |
$res = wp_remote_post( |
| 73 |
'https://api.getresponse.com/v3/contacts', |
| 74 |
array( |
| 75 |
'body' => wp_json_encode( $body ), |
| 76 |
'timeout' => 45, |
| 77 |
'headers' => $this->get_headers(), |
| 78 |
) |
| 79 |
); |
| 80 |
|
| 81 |
if ( ! is_wp_error( $res ) ) { |
| 82 |
$code = (int) wp_remote_retrieve_response_code( $res ); |
| 83 |
|
| 84 |
if ( in_array( $code, array( 200, 201, 202 ), true ) ) { |
| 85 |
$success = true; |
| 86 |
} |
| 87 |
|
| 88 |
// Duplicate email case. |
| 89 |
if ( in_array( $code, array( 409 ), true ) ) { |
| 90 |
$success = true; |
| 91 |
$duplicate = true; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if ( $success && ! $duplicate ) { |
| 97 |
$this->add_lead( $data ); |
| 98 |
} |
| 99 |
|
| 100 |
return $success; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Adds a lead to the leads table |
| 105 |
* |
| 106 |
* @param array $data data. |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
public function add_lead( $data ) { |
| 110 |
|
| 111 |
$fields = Utils::sanitize_json_array( $data['fields'] ); |
| 112 |
$fields['ip'] = Utils::get_user_ip(); |
| 113 |
|
| 114 |
$leads_data = array( |
| 115 |
'email' => sanitize_email( $fields['email'] ), |
| 116 |
'name' => isset( $fields['name'] ) ? $fields['name'] : null, |
| 117 |
'conv_id' => intval( $data['conv_id'] ), |
| 118 |
'data' => $fields, |
| 119 |
'integration' => $this->get_name(), |
| 120 |
); |
| 121 |
|
| 122 |
$this->db->add_lead( $leads_data ); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
/** |
| 127 |
* Gets the fields for Get Response |
| 128 |
* |
| 129 |
* @param array $args receive data as array. |
| 130 |
* @return array |
| 131 |
* |
| 132 |
* @link https://apireference.getresponse.com/#operation/getCustomFieldList |
| 133 |
*/ |
| 134 |
public function get_fields( $args = array() ): array { |
| 135 |
|
| 136 |
$use_cache = $args['use_cache']; |
| 137 |
if ( $use_cache ) { |
| 138 |
$res = $this->get_cached_fields(); |
| 139 |
|
| 140 |
if ( ! empty( $res ) ) { |
| 141 |
return $res; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
$res = wp_remote_get( |
| 146 |
'https://api.getresponse.com/v3/custom-fields', |
| 147 |
array( |
| 148 |
'timeout' => 45, |
| 149 |
'headers' => $this->get_headers(), |
| 150 |
) |
| 151 |
); |
| 152 |
|
| 153 |
if ( ! is_wp_error( $res ) && 200 == wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore |
| 154 |
$body = wp_remote_retrieve_body( $res ); |
| 155 |
$body = json_decode( $body, true ); |
| 156 |
|
| 157 |
if ( isset( $body ) && is_array( $body ) ) { |
| 158 |
|
| 159 |
$ret = array( |
| 160 |
array( |
| 161 |
'value' => 'email', |
| 162 |
'label' => 'Email', |
| 163 |
), |
| 164 |
array( |
| 165 |
'value' => 'name', |
| 166 |
'label' => 'Name', |
| 167 |
), |
| 168 |
); |
| 169 |
|
| 170 |
foreach ( $body as $d ) { |
| 171 |
$ret[] = array( |
| 172 |
'value' => $d['customFieldId'], |
| 173 |
'label' => $d['name'], |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
$this->set_fields_cache( $ret ); |
| 178 |
|
| 179 |
return $ret; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
return array(); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Gets groups for get response |
| 188 |
* |
| 189 |
* @param array $args receive data as array. |
| 190 |
* @return array |
| 191 |
* |
| 192 |
* @link https://apireference.getresponse.com/#operation/getCampaignList |
| 193 |
*/ |
| 194 |
public function get_lists( $args = array() ): array { |
| 195 |
$use_cache = $args['use_cache']; |
| 196 |
|
| 197 |
if ( $use_cache ) { |
| 198 |
$res = $this->get_cached_lists(); |
| 199 |
|
| 200 |
if ( ! empty( $res ) ) { |
| 201 |
return $res; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
$res = wp_remote_get( |
| 206 |
'https://api.getresponse.com/v3/campaigns', |
| 207 |
array( |
| 208 |
'timeout' => 45, |
| 209 |
'headers' => $this->get_headers(), |
| 210 |
) |
| 211 |
); |
| 212 |
|
| 213 |
if ( ! is_wp_error( $res ) && 200 == wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore |
| 214 |
$body = wp_remote_retrieve_body( $res ); |
| 215 |
$body = json_decode( $body, true ); |
| 216 |
|
| 217 |
if ( isset( $body ) && is_array( $body ) ) { |
| 218 |
|
| 219 |
$ret = array(); |
| 220 |
|
| 221 |
foreach ( $body as $d ) { |
| 222 |
$ret[] = array( |
| 223 |
'value' => $d['campaignId'], |
| 224 |
'label' => $d['name'], |
| 225 |
); |
| 226 |
} |
| 227 |
|
| 228 |
$this->set_lists_cache( $ret ); |
| 229 |
|
| 230 |
return $ret; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return array(); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Gets headers with authorization token |
| 239 |
* |
| 240 |
* @return array |
| 241 |
*/ |
| 242 |
protected function get_headers() { |
| 243 |
|
| 244 |
$settings = $this->get_settings(); |
| 245 |
|
| 246 |
return array( |
| 247 |
'Content-Type' => 'application/json', |
| 248 |
'Accept' => 'application/json', |
| 249 |
'X-Auth-Token' => 'api-key ' . $settings['apiKey'], |
| 250 |
); |
| 251 |
} |
| 252 |
} |
| 253 |
|