| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* GetResponse |
| 5 |
* |
| 6 |
* Copyright: © 2009-2011 |
| 7 |
* {@link http://websharks-inc.com/ WebSharks, Inc.} |
| 8 |
* (coded in the USA) |
| 9 |
* |
| 10 |
* Released under the terms of the GNU General Public License. |
| 11 |
* You should have received a copy of the GNU General Public License, |
| 12 |
* along with this software. In the main directory, see: /licensing/ |
| 13 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 14 |
* |
| 15 |
* @since 141004 |
| 16 |
* @package s2Member\List_Servers |
| 17 |
*/ |
| 18 |
if(!defined('WPINC')) // MUST have WordPress. |
| 19 |
exit('Do not access this file directly.'); |
| 20 |
|
| 21 |
if(!class_exists('c_ws_plugin__s2member_getresponse')) |
| 22 |
{ |
| 23 |
/** |
| 24 |
* GetResponse |
| 25 |
* |
| 26 |
* @since 141004 |
| 27 |
* @package s2Member\List_Servers |
| 28 |
*/ |
| 29 |
class c_ws_plugin__s2member_getresponse extends c_ws_plugin__s2member_list_server_base |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Subscribe. |
| 33 |
* |
| 34 |
* @since 141004 |
| 35 |
* @package s2Member\List_Servers |
| 36 |
* |
| 37 |
* @param array $args Input arguments. |
| 38 |
* |
| 39 |
* @return bool True if successful. |
| 40 |
*/ |
| 41 |
public static function subscribe($args) |
| 42 |
{ |
| 43 |
if(!($args = self::validate_args($args))) |
| 44 |
return FALSE; // Invalid args. |
| 45 |
|
| 46 |
if(!$args->opt_in) // Double check. |
| 47 |
return FALSE; // Must say explicitly. |
| 48 |
|
| 49 |
if(!$GLOBALS['WS_PLUGIN__']['s2member']['o']['getresponse_api_key']) |
| 50 |
return FALSE; // Not possible. |
| 51 |
|
| 52 |
if(empty($GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$args->level.'_getresponse_list_ids'])) |
| 53 |
return FALSE; // No list configured at this level. |
| 54 |
|
| 55 |
$gr_level_list_ids = $GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$args->level.'_getresponse_list_ids']; |
| 56 |
|
| 57 |
extract((array)$args); // Extract the arguments for back compat. w/ filters that relied upon them. |
| 58 |
|
| 59 |
foreach(preg_split('/['."\r\n\t".';,]+/', $gr_level_list_ids, -1, PREG_SPLIT_NO_EMPTY) as $_gr_list) |
| 60 |
{ |
| 61 |
$_gr = array( |
| 62 |
'args' => $args, |
| 63 |
'function' => __FUNCTION__, |
| 64 |
'list' => trim($_gr_list), |
| 65 |
'list_id' => trim($_gr_list), |
| 66 |
'api_method' => 'add_contact' |
| 67 |
); |
| 68 |
if(!$_gr['list'] || !$_gr['list_id']) |
| 69 |
continue; // List missing. |
| 70 |
|
| 71 |
$_gr['api_method'] = 'get_contacts'; // Check if exists. |
| 72 |
$_gr['api_headers'] = array('Content-Type' => 'application/json'); |
| 73 |
$_gr['api_params'] = array($GLOBALS['WS_PLUGIN__']['s2member']['o']['getresponse_api_key'], |
| 74 |
array('campaigns' => array($_gr['list_id']), 'email' => array('EQUALS' => $args->email))); |
| 75 |
$_gr['api_request'] = json_encode(array('method' => $_gr['api_method'], 'params' => $_gr['api_params'], 'id' => uniqid('', TRUE))); |
| 76 |
if(is_object($_gr['api_response'] = json_decode(c_ws_plugin__s2member_utils_urls::remote('https://api2.getresponse.com', $_gr['api_request'], array('headers' => $_gr['api_headers'])))) && empty($_gr['api_response']->error) |
| 77 |
&& ($_gr['api_response_contact_ids'] = array_keys((array)$_gr['api_response']->result)) && ($_gr['api_response_contact_id'] = $_gr['api_response_contact_ids'][0]) |
| 78 |
) // They already exist on this list, we need to update the existing subscription here instead of adding a new one. |
| 79 |
{ |
| 80 |
$_gr['api_method'] = 'set_contact_name'; // Update. |
| 81 |
$_gr['api_headers'] = array('Content-Type' => 'application/json'); |
| 82 |
$_gr['api_params'] = array($GLOBALS['WS_PLUGIN__']['s2member']['o']['getresponse_api_key'], |
| 83 |
array('contact' => $_gr['api_response_contact_id'], 'name' => $args->name)); |
| 84 |
$_gr['api_request'] = json_encode(array('method' => $_gr['api_method'], 'params' => $_gr['api_params'], 'id' => uniqid('', TRUE))); |
| 85 |
if(is_object($_gr['api_response'] = json_decode(c_ws_plugin__s2member_utils_urls::remote('https://api2.getresponse.com', $_gr['api_request'], array('headers' => $_gr['api_headers'])))) && empty($_gr['api_response']->error)) |
| 86 |
{ |
| 87 |
$_gr['api_method'] = 'set_contact_customs'; // Update. |
| 88 |
$_gr['api_headers'] = array('Content-Type' => 'application/json'); |
| 89 |
$_gr['api_params'] = array($GLOBALS['WS_PLUGIN__']['s2member']['o']['getresponse_api_key'], |
| 90 |
array('contact' => $_gr['api_response_contact_id'], 'customs' => apply_filters('ws_plugin__s2member_getresponse_customs_array', array(), get_defined_vars()))); |
| 91 |
$_gr['api_request'] = json_encode(array('method' => $_gr['api_method'], 'params' => $_gr['api_params'], 'id' => uniqid('', TRUE))); |
| 92 |
if(is_object($_gr['api_response'] = json_decode(c_ws_plugin__s2member_utils_urls::remote('https://api2.getresponse.com', $_gr['api_request'], array('headers' => $_gr['api_headers'])))) && empty($_gr['api_response']->error)) |
| 93 |
$_gr['api_success'] = $success = TRUE; // Flag this as `TRUE`; assists with return value below. |
| 94 |
} |
| 95 |
} |
| 96 |
else // Create a new contact; i.e., they do not exist yet. |
| 97 |
{ |
| 98 |
$_gr['api_method'] = 'add_contact'; // Add new contact. |
| 99 |
$_gr['api_headers'] = array('Content-Type' => 'application/json'); |
| 100 |
$_gr['api_params'] = array($GLOBALS['WS_PLUGIN__']['s2member']['o']['getresponse_api_key'], |
| 101 |
array('name' => $args->name, 'email' => $args->email, 'ip' => $args->ip, |
| 102 |
'campaign' => $_gr['list_id'], 'action' => 'standard', 'cycle_day' => 0, |
| 103 |
'customs' => apply_filters('ws_plugin__s2member_getresponse_customs_array', array(), get_defined_vars()))); |
| 104 |
if(!$_gr['api_params'][1]['ip'] || $_gr['api_params'][1]['ip'] === 'unknown') unset($_gr['api_params'][1]['ip']); |
| 105 |
|
| 106 |
$_gr['api_request'] = json_encode(array('method' => $_gr['api_method'], 'params' => $_gr['api_params'], 'id' => uniqid('', TRUE))); |
| 107 |
if(is_object($_gr['api_response'] = json_decode(c_ws_plugin__s2member_utils_urls::remote('https://api2.getresponse.com', $_gr['api_request'], array('headers' => $_gr['api_headers'])))) && empty($_gr['api_response']->error) && $_gr['api_response']->result->queued) |
| 108 |
$_gr['api_success'] = $success = TRUE; // Flag this as `TRUE`; assists with return value below. |
| 109 |
} |
| 110 |
c_ws_plugin__s2member_utils_logs::log_entry('getresponse-api', $_gr); |
| 111 |
} |
| 112 |
unset($_gr_list, $_gr); // Just a little housekeeping. |
| 113 |
|
| 114 |
return !empty($success); // If one suceeds. |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Unsubscribe. |
| 119 |
* |
| 120 |
* @since 141004 |
| 121 |
* @package s2Member\List_Servers |
| 122 |
* |
| 123 |
* @param array $args Input arguments. |
| 124 |
* |
| 125 |
* @return bool True if successful. |
| 126 |
*/ |
| 127 |
public static function unsubscribe($args) |
| 128 |
{ |
| 129 |
if(!($args = self::validate_args($args))) |
| 130 |
return FALSE; // Invalid args. |
| 131 |
|
| 132 |
if(!$args->opt_out) // Double check. |
| 133 |
return FALSE; // Must say explicitly. |
| 134 |
|
| 135 |
if(!$GLOBALS['WS_PLUGIN__']['s2member']['o']['getresponse_api_key']) |
| 136 |
return FALSE; // Not possible. |
| 137 |
|
| 138 |
if(empty($GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$args->level.'_getresponse_list_ids'])) |
| 139 |
return FALSE; // No list configured at this level. |
| 140 |
|
| 141 |
$gr_level_list_ids = $GLOBALS['WS_PLUGIN__']['s2member']['o']['level'.$args->level.'_getresponse_list_ids']; |
| 142 |
|
| 143 |
extract((array)$args); // Extract the arguments for back compat. w/ filters that relied upon them. |
| 144 |
|
| 145 |
foreach(preg_split('/['."\r\n\t".';,]+/', $gr_level_list_ids, -1, PREG_SPLIT_NO_EMPTY) as $_gr_list) |
| 146 |
{ |
| 147 |
$_gr = array( |
| 148 |
'args' => $args, |
| 149 |
'function' => __FUNCTION__, |
| 150 |
'list' => trim($_gr_list), |
| 151 |
'list_id' => trim($_gr_list), |
| 152 |
'api_method' => 'delete_contact' |
| 153 |
); |
| 154 |
if(!$_gr['list'] || !$_gr['list_id']) |
| 155 |
continue; // List missing. |
| 156 |
|
| 157 |
$_gr['api_method'] = 'get_contacts'; // Check if exists. |
| 158 |
$_gr['api_headers'] = array('Content-Type' => 'application/json'); |
| 159 |
$_gr['api_params'] = array($GLOBALS['WS_PLUGIN__']['s2member']['o']['getresponse_api_key'], |
| 160 |
array('campaigns' => array($_gr['list_id']), 'email' => array('EQUALS' => $args->email))); |
| 161 |
$_gr['api_request'] = json_encode(array('method' => $_gr['api_method'], 'params' => $_gr['api_params'], 'id' => uniqid('', TRUE))); |
| 162 |
if(is_object($_gr['api_response'] = json_decode(c_ws_plugin__s2member_utils_urls::remote('https://api2.getresponse.com', $_gr['api_request'], array('headers' => $_gr['api_headers'])))) && empty($_gr['api_response']->error) |
| 163 |
&& ($_gr['api_response_contact_ids'] = array_keys((array)$_gr['api_response']->result)) && ($_gr['api_response_contact_id'] = $_gr['api_response_contact_ids'][0]) |
| 164 |
)// They exist on this list, so we can remove theme here via `delete_contact`. |
| 165 |
{ |
| 166 |
$_gr['api_method'] = 'delete_contact'; // Delete. |
| 167 |
$_gr['api_headers'] = array('Content-Type' => 'application/json'); |
| 168 |
$_gr['api_params'] = array($GLOBALS['WS_PLUGIN__']['s2member']['o']['getresponse_api_key'], |
| 169 |
array('contact' => $_gr['api_response_contact_id'])); |
| 170 |
$_gr['api_request'] = json_encode(array('method' => $_gr['api_method'], 'params' => $_gr['api_params'], 'id' => uniqid('', TRUE))); |
| 171 |
if(is_object($_gr['api_response'] = json_decode(c_ws_plugin__s2member_utils_urls::remote('https://api2.getresponse.com', $_gr['api_request'], array('headers' => $_gr['api_headers'])))) && empty($_gr['api_response']->error) && $_gr['api_response']->result->deleted) |
| 172 |
$_gr['api_success'] = $success = TRUE; // Flag this as `TRUE`; assists with return value below. |
| 173 |
} |
| 174 |
c_ws_plugin__s2member_utils_logs::log_entry('getresponse-api', $_gr); |
| 175 |
} |
| 176 |
unset($_gr_list, $_gr); // Just a little housekeeping. |
| 177 |
|
| 178 |
return !empty($success); // If one suceeds. |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Transition. |
| 183 |
* |
| 184 |
* @since 141004 |
| 185 |
* @package s2Member\List_Servers |
| 186 |
* |
| 187 |
* @param array $old_args Input arguments. |
| 188 |
* @param array $new_args Input arguments. |
| 189 |
* |
| 190 |
* @return bool True if successful. |
| 191 |
*/ |
| 192 |
public static function transition($old_args, $new_args) |
| 193 |
{ |
| 194 |
return self::unsubscribe($old_args) && self::subscribe($new_args); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|