ExactTarget

ExactTarget for PHP.

Defined (2)

The class is defined in the following location(s).

/api/ExactTarget.class.php  
  1. class ExactTarget { 
  2.  
  3. var $username = ''; 
  4. var $password = ''; 
  5. var $s4 = ''; 
  6. var $debug = ''; 
  7. var $subscriberkey = ''; 
  8. var $mid = ''; 
  9.  
  10.  
  11. function ExactTarget($apikey = null, $user = null, $password=null) { 
  12. self::updateSettings($this); 
  13.  
  14. public function updateSettings($object = false) { 
  15. if($object !== false) $object->lastError = ''; 
  16. $settings = get_option("gf_exacttarget_settings"); 
  17. if(!$settings || !is_array($settings)) { return; } 
  18. foreach($settings as $key => $value) { 
  19. if($key === 'debug' || $key === 'subscriberkey') { 
  20. $object->{$key} = !empty($value); 
  21. } else { 
  22. $object->{$key} = trim($value); 
  23.  
  24. public function TestAPI() { 
  25. $xml = $this->MakeRequest('<system></system>'); 
  26. $result = $this->DoRequest($xml); 
  27. $response = $this->getResponse($result, true); 
  28. if(is_array($response) || $response === false) { 
  29. return false; 
  30.  
  31. return true; 
  32.  
  33. public function AddList($name = '', $type = 'public') { 
  34.  
  35. $xml = self::MakeRequest(' 
  36. <system> 
  37. <system_name>list</system_name>\ 
  38. <action>add</action> 
  39. <search_type></search_type> 
  40. <search_value></search_value> 
  41. <list_type>'.$type.'</list_type> 
  42. <list_name>'.$name.'</list_name> 
  43. </system>' 
  44. ); 
  45.  
  46. return self::DoRequest($xml); 
  47.  
  48. public function MakeRequest($xml = '') { 
  49. return ' 
  50. <?xml version="1.0" ?> 
  51. <exacttarget> 
  52. <authorization> 
  53. <username>'.$this->username.'</username> 
  54. <password>'.$this->password.'</password> 
  55. </authorization> 
  56. '.$xml.' 
  57. </exacttarget>'; 
  58.  
  59. public function DoRequest($xml = '') { 
  60.  
  61. $args = array( 
  62. 'headers'=> array('Content-Type' => 'application/x-www-form-urlencoded'),  
  63. 'method' => 'POST',  
  64. 'sslverify' => false,  
  65. 'timeout' => apply_filters('exacttarget_dorequest_timeout', 30) 
  66. ); 
  67.  
  68. if($this->s4) { 
  69. $url = 'https://api.s4.exacttarget.com/integrate.aspx?qf=xml&xml='.urlencode($xml); 
  70. } else { 
  71. $url = 'https://api.dc1.exacttarget.com/integrate.aspx?qf=xml&xml='.urlencode($xml); 
  72.  
  73. $result = wp_remote_request($url, $args); 
  74.  
  75. $this->lastRequest = $result; 
  76.  
  77. if(is_wp_error($result)) { 
  78. $this->lastError = $result->get_error_message(); 
  79. $this->r($this->lastError); 
  80. return false; 
  81.  
  82. $body = wp_remote_retrieve_body($result); 
  83.  
  84. $this->r(array('URL Submitted To' => $url, 'WordPress `wp_remote_request` Settings' => $args, 'Result' => $result)); 
  85.  
  86. return $body; 
  87.  
  88. public function Lists($showRaw = false) { 
  89.  
  90. flush(); 
  91.  
  92. if((!isset($_REQUEST['refresh']) || isset($_REQUEST['refresh']) && $_REQUEST['refresh'] !== 'lists') && !isset($_REQUEST['retrieveListNames'])) { 
  93.  
  94. // Is it saved already in a transient? 
  95. $lists = get_transient('extr_lists_all'); 
  96. if(!empty($lists) && is_array($lists)) { 
  97. return $lists; 
  98.  
  99. // Check if raw data already exists 
  100. $lists = get_transient('extr_lists_raw'); 
  101. if(!empty($lists) && is_array($lists)) { 
  102. return $lists; 
  103.  
  104. } else { 
  105. $lists = array(); 
  106.  
  107. $xml = self::MakeRequest(' 
  108. <system> 
  109. <system_name>list</system_name>\ 
  110. <action>retrieve</action> 
  111. <search_type>listname</search_type> 
  112. <search_value></search_value> 
  113. </system>' 
  114. ); 
  115.  
  116. $result = self::DoRequest($xml); 
  117.  
  118. if(!$result) { 
  119. $this->r('List retrieval failed.'); 
  120. return false; 
  121.  
  122. $response = @simplexml_load_string($result); 
  123.  
  124. if(!empty($response->system) && !empty($response->system->list->listid) && !empty($response->system->list->listid)) { 
  125. $i = 0; $count = sizeof($response->system->list->listid); 
  126.  
  127. if($count > 20 || $showRaw) { 
  128. if(!empty($response->system) && !empty($response->system->list->listid) && !empty($response->system->list->listid)) { 
  129. foreach($response->system->list->listid as $listid) { 
  130. $lists["{$listid}"] = array('list_name' => (string)$listid); 
  131. @set_transient('extr_lists_raw', $lists, 60*60*24*365); 
  132. } elseif(!$showRaw) { 
  133. foreach($response->system->list->listid as $listid) { 
  134. $list_xml = self::ListRetrieve($listid); 
  135. if($list_xml->system->list->list_type == 'Public') { 
  136. $lists["{$listid}"] = (array)$list_xml->system->list; 
  137. @set_transient('extr_lists_all', $lists, 60*60*24*365); 
  138.  
  139.  
  140. return $lists; 
  141.  
  142. public function ListRetrieve($listid = 0) { 
  143. $list = self::DoRequest(self::MakeRequest('<system> 
  144. <system_name>list</system_name> 
  145. <action>retrieve</action> 
  146. <search_type>listid</search_type> 
  147. <search_value>'.$listid.'</search_value> 
  148. </system>')); 
  149. $list_xml = @simplexml_load_string( $list ); 
  150. return $list_xml; 
  151.  
  152. public function Attributes() { 
  153.  
  154. $attrs = array(); 
  155.  
  156. $xml = self::MakeRequest(' 
  157. <system> 
  158. <system_name>accountinfo</system_name> 
  159. <action>retrieve_attrbs</action> 
  160. <search_type/> 
  161. <search_value/> 
  162. </system>' 
  163. ); 
  164.  
  165. $result = self::DoRequest( $xml ); 
  166.  
  167. if(!$result) { 
  168. $this->r('Attribute retrieval failed.'); 
  169. return false; 
  170.  
  171. $response = @simplexml_load_string( $result ); 
  172.  
  173. if(!empty($response->system) && !empty($response->system->profile) && !empty($response->system->profile->attribute)) { 
  174. foreach($response->system->profile->attribute as $key => $attr) { 
  175.  
  176. foreach($attr as $k => $v) { 
  177. if(!is_array($v)) { 
  178. $attr->{$k} = (string)$v; 
  179.  
  180. $attrs[sanitize_user(str_replace(' ', '_', strtolower((string)$attr->name)), true)] = (array)$attr; 
  181.  
  182. return $attrs; 
  183. return false; 
  184.  
  185. public function AddMembership($list, $email, $additional = array(), $update = '') { 
  186.  
  187. if(empty($update)) { 
  188. $this->r(array('List ID' => $list, 'Email Address' => $email, 'Mapped, Submitted Fields' => $additional)); 
  189.  
  190. $xml = ' 
  191. <system> 
  192. <system_name>subscriber</system_name> 
  193. <action>'; if(empty($update)) { $xml .= 'add'; } else { $xml .= 'edit'; } $xml .= '</action> 
  194. <search_type>listid</search_type> 
  195. <search_value>'.$list.'</search_value> 
  196. <search_value2>'.$update.'</search_value2> 
  197. <values> 
  198. <Email__Address>'.$email.'</Email__Address> 
  199. <status>active</status>'; 
  200. foreach($additional as $key => $value) { 
  201. if(empty($key)) { continue; } 
  202. $newkey = str_replace(' ', '__', ucwords(str_replace('_', ' ', $key))); 
  203.  
  204. if(is_array($value)) { 
  205. $value = esc_html(implode(', ', $value)); 
  206. } elseif(!preg_match('/\<\!\[CDATA\[/', $value)) { 
  207. $value = esc_html($value); 
  208. $xml .= ' 
  209. <'.$newkey.'>'.$value.'</'.$newkey.'>'; 
  210. if(!empty($this->mid) && is_numeric($this->mid)) { 
  211. $xml .= '<ChannelMemberID>'.$this->mid.'</ChannelMemberID>'; 
  212.  
  213. $xml .= ' 
  214. </values>'; 
  215. if(empty($update)) { $xml .= ' 
  216. <update>true</update>'; } 
  217. $xml .= ' 
  218. </system>'; 
  219.  
  220. $this->r(htmlentities($xml), 'Posted XML'); 
  221.  
  222. $xml = self::MakeRequest( $xml ); 
  223.  
  224. $response = self::DoRequest($xml); 
  225.  
  226. if(!$response) { 
  227. $this->r('Add/update subscriber failed.'); 
  228. return false; 
  229.  
  230. $response = $this->getResponse($response); 
  231. if($response === false || is_array($response)) { 
  232. if($response[0] == 14) { 
  233. $this->r($response[1], 'Error 14'); 
  234. self::AddMembership($list, $email, $additional, $email); 
  235. } else { 
  236. $this->r($this->lastError); 
  237. return false; 
  238. } else { 
  239. // Return subscriber ID if successful 
  240. $this->r($response, 'ID of Subscriber'); 
  241. return $response; 
  242.  
  243. return true; 
  244.  
  245. public function getResponse($response = '', $testapi = false) { 
  246. if(preg_match('/\<error\>([0-9]+)\<\/error\>(?:\s+)?<error\_description\>(.*?)\<\/error_description\>/ism', $response, $matches)) { 
  247. if($testapi && $matches[1] === '71') { 
  248. $this->lastError = false; 
  249. return $matches[1]; 
  250. $this->lastError = 'Error '.$matches[1].': '.$matches[2]; 
  251. return array($matches[1], $matches[2]); 
  252. } elseif(preg_match('/\<subscriber_description\>([0-9]+)\<\/subscriber_description\>/ism', $response, $matches)) { 
  253. $this->lastError = false; 
  254. return $matches[1]; 
  255. return false; 
  256.  
  257. public function errorCodeMessage($errorcode = '', $errorcontrol = '') { 
  258.  
  259. switch($errorcode) { 
  260. case "1" : $strError = __("An error has occurred while attempting to save your subscriber information.", "gravity-forms-exacttarget"); break; 
  261. case "2" : $strError = __("The list provided does not exist.", "gravity-forms-exacttarget"); break; 
  262. case "3" : $strError = __("Information was not provided for a mandatory field. (".$errorcontrol.")", "gravity-forms-exacttarget"); break; 
  263. case "4" : $strError = __("Invalid information was provided. (".$errorcontrol.")", "gravity-forms-exacttarget"); break; 
  264. case "5" : $strError = __("Information provided is not unique. (".$errorcontrol.")", "gravity-forms-exacttarget"); break; 
  265. case "6" : $strError = __("An error has occurred while attempting to save your subscriber information.", "gravity-forms-exacttarget"); break; 
  266. case "7" : $strError = __("An error has occurred while attempting to save your subscriber information.", "gravity-forms-exacttarget"); break; 
  267. case "8" : $strError = __("Subscriber already exists.", "gravity-forms-exacttarget"); break; 
  268. case "9" : $strError = __("An error has occurred while attempting to save your subscriber information.", "gravity-forms-exacttarget"); break; 
  269. case "10" : $strError = __("An error has occurred while attempting to save your subscriber information.", "gravity-forms-exacttarget"); break; 
  270. case "12" : $strError = __("The subscriber you are attempting to insert is on the master unsubscribe list or the global unsubscribe list.", "gravity-forms-exacttarget"); break; 
  271. case "13" : $strError = __("Check that the list ID and/or MID specified in your code is correct.", "gravity-forms-exacttarget"); break; 
  272. default : $strError = __("Error", "gravity-forms-exacttarget"); break; 
  273. return $strError; 
  274.  
  275. public function listSubscribe($lists = array(), $email = '', $merge_vars = array()) { 
  276.  
  277. $this->lastError = ''; 
  278.  
  279. if(!is_array($lists)) { $lists = explode(', ', $lists); } 
  280.  
  281. if(empty($this->mid)) { 
  282. $this->lastError = 'The MID was not defined in the ExactTarget settings. The attempt to add the subscriber was not made.'; 
  283. $this->r($this->lastError); 
  284. return; 
  285. } elseif(empty($lists)) { 
  286. $this->lastError = 'No lists were selected. The attempt to add the subscriber was not made.'; 
  287. $this->r($this->lastError); 
  288. return; 
  289.  
  290. $params = $merge_vars; 
  291.  
  292. foreach($params as $key => $p) { 
  293. if(is_array($p)) { 
  294. $p = implode(', ', $p); 
  295. } else { 
  296. $p = rtrim(trim($p)); 
  297. if(empty($p) && $p !== '0') { 
  298. unset($params[$key]); 
  299. } else { 
  300. $params[$key] = $p; 
  301.  
  302. #echo '<pre>'; 
  303.  
  304. foreach($params as $key => $value) { 
  305. $newkey = ucwords(str_replace('_', ' ', $key)); 
  306. $params["{$newkey}"] = esc_html($value); 
  307. unset($params[$key]); 
  308.  
  309. if($this->subscriberkey) { 
  310. $params["Subscriber Key"] = $email; 
  311.  
  312. $params['MID'] = $this->mid; 
  313. $params['SubAction'] = 'sub_add_update'; 
  314.  
  315. $this->r(array('List IDs' => $lists, 'Email Address' => $email, 'Mapped, Submitted Fields' => $params)); 
  316.  
  317.  
  318. $args = array( 
  319. 'method' => 'POST',  
  320. 'sslverify' => false,  
  321. 'timeout' => 30 
  322. ); 
  323.  
  324. if($this->s4) { 
  325. $url = 'http://cl.s4.exct.net/subscribe.aspx?lid='.implode('&lid=', $lists).'&'.http_build_query($params); 
  326. } else { 
  327. $url = 'http://cl.exct.net/subscribe.aspx?lid='.implode('&lid=', $lists).'&'.http_build_query($params); 
  328.  
  329. $result = wp_remote_request($url, $args); 
  330.  
  331. $this->r(array('URL Submitted To' => $url, 'WordPress `wp_remote_request` Settings' => $args, 'Result' => $result)); 
  332.  
  333. $body = $result['body']; 
  334.  
  335. if($result['response']['code'] !== 200) { 
  336. if($result['response']['code'] == 500) { 
  337. $this->lastError = $body; 
  338. } else { 
  339. $this->lastError = 'Server error: '.$result['response']['code'].' ('.$result['response']['message'].')'; 
  340. } else if(is_wp_error($result) || !$result) { 
  341. $this->lastError = 'The form submission failed.'; 
  342. } elseif(preg_match('/action\=\"app_error\.aspx\?(.*?)"/', $body, $matches)) { 
  343. parse_str(str_replace('&', '&', $matches[1]), $error); 
  344. $this->lastError = $this->errorCodeMessage($error['errorcode'], $error['errorcontrol']); 
  345. } else { 
  346. $this->lastError = ''; 
  347. $this->r('It seems the form was successfully submitted.', 'Contact Creation Success'); 
  348. return true; 
  349.  
  350. $this->r($this->lastError, 'Contact Creation Failure'); 
  351. return false; 
  352.  
  353.  
  354. function r($debugging = '', $title = '') { 
  355. if($this->debug && current_user_can('manage_options') && !is_admin()) { 
  356. echo '<div style="background-color:white;border:1px solid #ccc; padding:10px; margin:6px; position:relative; font-size:14px;"> 
  357. <p style="text-align:center; color:#ccc; margin:0; padding:0; position:absolute; right:.5em; top:.5em;">Admin-only Debugging Results</p> 
  358. '; 
  359. if($title) { 
  360. echo '<h3>'.$title.'</h3>'; 
  361. echo '<pre>'; 
  362. print_r($debugging); 
  363. echo '</pre> 
  364. </div>'; 
  1. class ExactTarget { 
  2.  
  3. var $username = ''; 
  4. var $password = ''; 
  5. var $s4 = ''; 
  6. var $debug = ''; 
  7. var $subscriberkey = ''; 
  8. var $mid = ''; 
  9.  
  10.  
  11. function ExactTarget($apikey = null, $user = null, $password=null) { 
  12. self::updateSettings($this); 
  13.  
  14. public function updateSettings($object = false) { 
  15. $api->lastError = ''; 
  16. $settings = get_option("gf_exacttarget_settings"); 
  17. if(!$settings || !is_array($settings)) { return; } 
  18. foreach($settings as $key => $value) { 
  19. if($key === 'debug' || $key === 'subscriberkey') { 
  20. $object->{$key} = !empty($value); 
  21. } else { 
  22. $object->{$key} = trim($value); 
  23.  
  24. public function TestAPI() { 
  25. $xml = $this->MakeRequest('<system></system>'); 
  26. $result = $this->DoRequest($xml); 
  27. $response = $this->getResponse($result, true); 
  28. if(is_array($response) || $response === false) { 
  29. return false; 
  30.  
  31. return true; 
  32.  
  33. public function AddList($name = '', $type = 'public') { 
  34.  
  35. $xml = self::MakeRequest(' 
  36. <system> 
  37. <system_name>list</system_name>\ 
  38. <action>add</action> 
  39. <search_type></search_type> 
  40. <search_value></search_value> 
  41. <list_type>'.$type.'</list_type> 
  42. <list_name>'.$name.'</list_name> 
  43. </system>' 
  44. ); 
  45.  
  46. return self::DoRequest($xml); 
  47.  
  48. public function MakeRequest($xml = '') { 
  49. return ' 
  50. <?xml version="1.0" ?> 
  51. <exacttarget> 
  52. <authorization> 
  53. <username>'.$this->username.'</username> 
  54. <password>'.$this->password.'</password> 
  55. </authorization> 
  56. '.$xml.' 
  57. </exacttarget>'; 
  58.  
  59. public function DoRequest($xml = '') { 
  60.  
  61. $args = array( 
  62. 'headers'=> array('Content-Type' => 'application/x-www-form-urlencoded'),  
  63. 'method' => 'POST',  
  64. 'sslverify' => false,  
  65. 'timeout' => apply_filters('exacttarget_dorequest_timeout', 30) 
  66. ); 
  67.  
  68. if($this->s4) { 
  69. $url = 'https://api.s4.exacttarget.com/integrate.aspx?qf=xml&xml='.urlencode($xml); 
  70. } else { 
  71. $url = 'https://api.dc1.exacttarget.com/integrate.aspx?qf=xml&xml='.urlencode($xml); 
  72.  
  73. $result = wp_remote_request($url, $args); 
  74.  
  75. $this->lastRequest = $result; 
  76.  
  77. if(is_wp_error($result)) { 
  78. $this->lastError = $result->get_error_message(); 
  79. $this->r($this->lastError); 
  80. return false; 
  81.  
  82. $body = wp_remote_retrieve_body($result); 
  83.  
  84. $this->r(array('URL Submitted To' => $url, 'WordPress `wp_remote_request` Settings' => $args, 'Result' => $result)); 
  85.  
  86. return $body; 
  87.  
  88. public function Lists($showRaw = false) { 
  89.  
  90. flush(); 
  91.  
  92. if((!isset($_REQUEST['refresh']) || isset($_REQUEST['refresh']) && $_REQUEST['refresh'] !== 'lists') && !isset($_REQUEST['retrieveListNames'])) { 
  93.  
  94. // Is it saved already in a transient? 
  95. $lists = get_transient('extr_lists_all'); 
  96. if(!empty($lists) && is_array($lists)) { 
  97. return $lists; 
  98.  
  99. // Check if raw data already exists 
  100. $lists = get_transient('extr_lists_raw'); 
  101. if(!empty($lists) && is_array($lists)) { 
  102. return $lists; 
  103.  
  104. } else { 
  105. $lists = array(); 
  106.  
  107. $xml = self::MakeRequest(' 
  108. <system> 
  109. <system_name>list</system_name>\ 
  110. <action>retrieve</action> 
  111. <search_type>listname</search_type> 
  112. <search_value></search_value> 
  113. </system>' 
  114. ); 
  115.  
  116. $result = self::DoRequest($xml); 
  117.  
  118. if(!$result) { 
  119. $this->r('List retrieval failed.'); 
  120. return false; 
  121.  
  122. $response = @simplexml_load_string($result); 
  123.  
  124. if(!empty($response->system) && !empty($response->system->list->listid) && !empty($response->system->list->listid)) { 
  125. $i = 0; $count = sizeof($response->system->list->listid); 
  126.  
  127. if($count > 20 || $showRaw) { 
  128. if(!empty($response->system) && !empty($response->system->list->listid) && !empty($response->system->list->listid)) { 
  129. foreach($response->system->list->listid as $listid) { 
  130. $lists["{$listid}"] = array('list_name' => (string)$listid); 
  131. @set_transient('extr_lists_raw', $lists, 60*60*24*365); 
  132. } elseif(!$showRaw) { 
  133. foreach($response->system->list->listid as $listid) { 
  134. $list_xml = self::ListRetrieve($listid); 
  135. if($list_xml->system->list->list_type == 'Public') { 
  136. $lists["{$listid}"] = (array)$list_xml->system->list; 
  137. @set_transient('extr_lists_all', $lists, 60*60*24*365); 
  138.  
  139.  
  140. return $lists; 
  141.  
  142. public function ListRetrieve($listid = 0) { 
  143. $list = self::DoRequest(self::MakeRequest('<system> 
  144. <system_name>list</system_name> 
  145. <action>retrieve</action> 
  146. <search_type>listid</search_type> 
  147. <search_value>'.$listid.'</search_value> 
  148. </system>')); 
  149. $list_xml = @simplexml_load_string( $list ); 
  150. return $list_xml; 
  151.  
  152. public function Attributes() { 
  153.  
  154. $attrs = array(); 
  155.  
  156. $xml = self::MakeRequest(' 
  157. <system> 
  158. <system_name>accountinfo</system_name> 
  159. <action>retrieve_attrbs</action> 
  160. <search_type/> 
  161. <search_value/> 
  162. </system>' 
  163. ); 
  164.  
  165. $result = self::DoRequest( $xml ); 
  166.  
  167. if(!$result) { 
  168. $this->r('Attribute retrieval failed.'); 
  169. return false; 
  170.  
  171. $response = @simplexml_load_string( $result ); 
  172.  
  173. if(!empty($response->system) && !empty($response->system->profile) && !empty($response->system->profile->attribute)) { 
  174. foreach($response->system->profile->attribute as $key => $attr) { 
  175.  
  176. foreach($attr as $k => $v) { 
  177. if(!is_array($v)) { 
  178. $attr->{$k} = (string)$v; 
  179.  
  180. $attrs[sanitize_user(str_replace(' ', '_', strtolower((string)$attr->name)), true)] = (array)$attr; 
  181.  
  182. return $attrs; 
  183. return false; 
  184.  
  185. public function AddMembership($list, $email, $additional = array(), $update = '') { 
  186.  
  187. if(empty($update)) { 
  188. $this->r(array('List ID' => $list, 'Email Address' => $email, 'Mapped, Submitted Fields' => $additional)); 
  189.  
  190. $xml = ' 
  191. <system> 
  192. <system_name>subscriber</system_name> 
  193. <action>'; if(empty($update)) { $xml .= 'add'; } else { $xml .= 'edit'; } $xml .= '</action> 
  194. <search_type>listid</search_type> 
  195. <search_value>'.$list.'</search_value> 
  196. <search_value2>'.$update.'</search_value2> 
  197. <values> 
  198. <Email__Address>'.$email.'</Email__Address> 
  199. <status>active</status>'; 
  200. foreach($additional as $key => $value) { 
  201. if(empty($key)) { continue; } 
  202. $newkey = str_replace(' ', '__', ucwords(str_replace('_', ' ', $key))); 
  203.  
  204. if(is_array($value)) { 
  205. $value = esc_html(implode(', ', $value)); 
  206. } elseif(!preg_match('/\<\!\[CDATA\[/', $value)) { 
  207. $value = esc_html($value); 
  208. $xml .= ' 
  209. <'.$newkey.'>'.$value.'</'.$newkey.'>'; 
  210. if(!empty($this->mid) && is_numeric($this->mid)) { 
  211. $xml .= '<ChannelMemberID>'.$this->mid.'</ChannelMemberID>'; 
  212.  
  213. $xml .= ' 
  214. </values>'; 
  215. if(empty($update)) { $xml .= ' 
  216. <update>true</update>'; } 
  217. $xml .= ' 
  218. </system>'; 
  219.  
  220. $this->r(htmlentities($xml), 'Posted XML'); 
  221.  
  222. $xml = self::MakeRequest( $xml ); 
  223.  
  224. $response = self::DoRequest($xml); 
  225.  
  226. if(!$response) { 
  227. $this->r('Add/update subscriber failed.'); 
  228. return false; 
  229.  
  230. $response = $this->getResponse($response); 
  231. if($response === false || is_array($response)) { 
  232. if($response[0] == 14) { 
  233. $this->r($response[1], 'Error 14'); 
  234. self::AddMembership($list, $email, $additional, $email); 
  235. } else { 
  236. $this->r($this->lastError); 
  237. return false; 
  238. } else { 
  239. // Return subscriber ID if successful 
  240. $this->r($response, 'ID of Subscriber'); 
  241. return $response; 
  242.  
  243. return true; 
  244.  
  245. public function getResponse($response = '', $testapi = false) { 
  246. if(preg_match('/\<error\>([0-9]+)\<\/error\>(?:\s+)?<error\_description\>(.*?)\<\/error_description\>/ism', $response, $matches)) { 
  247. if($testapi && $matches[1] === '71') { 
  248. $this->lastError = false; 
  249. return $matches[1]; 
  250. $this->lastError = 'Error '.$matches[1].': '.$matches[2]; 
  251. return array($matches[1], $matches[2]); 
  252. } elseif(preg_match('/\<subscriber_description\>([0-9]+)\<\/subscriber_description\>/ism', $response, $matches)) { 
  253. $this->lastError = false; 
  254. return $matches[1]; 
  255. return false; 
  256.  
  257. public function errorCodeMessage($errorcode = '', $errorcontrol = '') { 
  258.  
  259. switch($errorcode) { 
  260. case "1" : $strError = __("An error has occurred while attempting to save your subscriber information.", "gravity-forms-exacttarget"); break; 
  261. case "2" : $strError = __("The list provided does not exist.", "gravity-forms-exacttarget"); break; 
  262. case "3" : $strError = __("Information was not provided for a mandatory field. (".$errorcontrol.")", "gravity-forms-exacttarget"); break; 
  263. case "4" : $strError = __("Invalid information was provided. (".$errorcontrol.")", "gravity-forms-exacttarget"); break; 
  264. case "5" : $strError = __("Information provided is not unique. (".$errorcontrol.")", "gravity-forms-exacttarget"); break; 
  265. case "6" : $strError = __("An error has occurred while attempting to save your subscriber information.", "gravity-forms-exacttarget"); break; 
  266. case "7" : $strError = __("An error has occurred while attempting to save your subscriber information.", "gravity-forms-exacttarget"); break; 
  267. case "8" : $strError = __("Subscriber already exists.", "gravity-forms-exacttarget"); break; 
  268. case "9" : $strError = __("An error has occurred while attempting to save your subscriber information.", "gravity-forms-exacttarget"); break; 
  269. case "10" : $strError = __("An error has occurred while attempting to save your subscriber information.", "gravity-forms-exacttarget"); break; 
  270. case "12" : $strError = __("The subscriber you are attempting to insert is on the master unsubscribe list or the global unsubscribe list.", "gravity-forms-exacttarget"); break; 
  271. case "13" : $strError = __("Check that the list ID and/or MID specified in your code is correct.", "gravity-forms-exacttarget"); break; 
  272. default : $strError = __("Error", "gravity-forms-exacttarget"); break; 
  273. return $strError; 
  274.  
  275. public function listSubscribe($lists = array(), $email = '', $merge_vars = array()) { 
  276.  
  277. $this->lastError = ''; 
  278.  
  279. if(!is_array($lists)) { $lists = explode(', ', $lists); } 
  280.  
  281. if(empty($this->mid)) { 
  282. $this->lastError = 'The MID was not defined in the ExactTarget settings. The attempt to add the subscriber was not made.'; 
  283. $this->r($this->lastError); 
  284. return; 
  285. } elseif(empty($lists)) { 
  286. $this->lastError = 'No lists were selected. The attempt to add the subscriber was not made.'; 
  287. $this->r($this->lastError); 
  288. return; 
  289.  
  290. $params = $merge_vars; 
  291.  
  292. foreach($params as $key => $p) { 
  293. if(is_array($p)) { 
  294. $p = implode(', ', $p); 
  295. } else { 
  296. $p = rtrim(trim($p)); 
  297. if(empty($p) && $p !== '0') { 
  298. unset($params[$key]); 
  299. } else { 
  300. $params[$key] = $p; 
  301.  
  302. #echo '<pre>'; 
  303.  
  304. foreach($params as $key => $value) { 
  305. $newkey = ucwords(str_replace('_', ' ', $key)); 
  306. $params["{$newkey}"] = esc_html($value); 
  307. unset($params[$key]); 
  308.  
  309. if($this->subscriberkey) { 
  310. $params["Subscriber Key"] = $email; 
  311.  
  312. $params['MID'] = $this->mid; 
  313. $params['SubAction'] = 'sub_add_update'; 
  314.  
  315. $this->r(array('List IDs' => $lists, 'Email Address' => $email, 'Mapped, Submitted Fields' => $params)); 
  316.  
  317.  
  318. $args = array( 
  319. 'method' => 'POST',  
  320. 'sslverify' => false,  
  321. 'timeout' => 30 
  322. ); 
  323.  
  324. if($this->s4) { 
  325. $url = 'http://cl.s4.exct.net/subscribe.aspx?lid='.implode('&lid=', $lists).'&'.http_build_query($params); 
  326. } else { 
  327. $url = 'http://cl.exct.net/subscribe.aspx?lid='.implode('&lid=', $lists).'&'.http_build_query($params); 
  328.  
  329. $result = wp_remote_request($url, $args); 
  330.  
  331. $this->r(array('URL Submitted To' => $url, 'WordPress `wp_remote_request` Settings' => $args, 'Result' => $result)); 
  332.  
  333. $body = $result['body']; 
  334.  
  335. if($result['response']['code'] !== 200) { 
  336. if($result['response']['code'] == 500) { 
  337. $this->lastError = $body; 
  338. } else { 
  339. $this->lastError = 'Server error: '.$result['response']['code'].' ('.$result['response']['message'].')'; 
  340. } else if(is_wp_error($result) || !$result) { 
  341. $this->lastError = 'The form submission failed.'; 
  342. } elseif(preg_match('/action\=\"app_error\.aspx\?(.*?)"/', $body, $matches)) { 
  343. parse_str(str_replace('&', '&', $matches[1]), $error); 
  344. $this->lastError = $this->errorCodeMessage($error['errorcode'], $error['errorcontrol']); 
  345. } else { 
  346. $this->lastError = ''; 
  347. $this->r('It seems the form was successfully submitted.', 'Contact Creation Success'); 
  348. return true; 
  349.  
  350. $this->r($this->lastError, 'Contact Creation Failure'); 
  351. return false; 
  352.  
  353.  
  354. function r($debugging = '', $title = '') { 
  355. if($this->debug && current_user_can('manage_options') && !is_admin()) { 
  356. echo '<div style="background-color:white;border:1px solid #ccc; padding:10px; margin:6px; position:relative; font-size:14px;"> 
  357. <p style="text-align:center; color:#ccc; margin:0; padding:0; position:absolute; right:.5em; top:.5em;">Admin-only Debugging Results</p> 
  358. '; 
  359. if($title) { 
  360. echo '<h3>'.$title.'</h3>'; 
  361. echo '<pre>'; 
  362. print_r($debugging); 
  363. echo '</pre> 
  364. </div>';