GFExactTarget

The Gravity Forms ExactTarget Add-On GFExactTarget class.

Defined (2)

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

/exacttarget.php  
  1. class GFExactTarget { 
  2.  
  3. private static $path = "gravity-forms-exacttarget/exacttarget.php"; 
  4. private static $url = "http://www.gravityforms.com"; 
  5. private static $slug = "gravity-forms-exacttarget"; 
  6. private static $version = "1.0"; 
  7. private static $min_gravityforms_version = "1.3.9"; 
  8.  
  9. //Plugin starting point. Will load appropriate files 
  10. public static function init() { 
  11. global $pagenow; 
  12. if($pagenow == 'plugins.php' || defined('RG_CURRENT_PAGE') && RG_CURRENT_PAGE == "plugins.php") { 
  13. //loading translations 
  14. load_plugin_textdomain('gravity-forms-exacttarget', FALSE, '/gravity-forms-exacttarget/languages' ); 
  15.  
  16. add_action('after_plugin_row_' . self::$path, array('GFExactTarget', 'plugin_row') ); 
  17.  
  18. add_filter('plugin_action_links', array('GFExactTarget', 'settings_link'), 10, 2 ); 
  19.  
  20.  
  21. if(!self::is_gravityforms_supported()) { 
  22. return; 
  23.  
  24. if(is_admin()) { 
  25. //loading translations 
  26. load_plugin_textdomain('gravity-forms-exacttarget', FALSE, '/gravity-forms-exacttarget/languages' ); 
  27.  
  28. add_filter("transient_update_plugins", array('GFExactTarget', 'check_update')); 
  29. #add_filter("site_transient_update_plugins", array('GFExactTarget', 'check_update')); 
  30.  
  31. //creates a new Settings page on Gravity Forms' settings screen 
  32. if(self::has_access("gravityforms_exacttarget")) { 
  33. RGForms::add_settings_page("ExactTarget", array("GFExactTarget", "settings_page"), self::get_base_url() . "/images/exacttarget_wordpress_icon_32.png"); 
  34.  
  35. //integrating with Members plugin 
  36. if(function_exists('members_get_capabilities')) 
  37. add_filter('members_get_capabilities', array("GFExactTarget", "members_get_capabilities")); 
  38.  
  39. //creates the subnav left menu 
  40. add_filter("gform_addon_navigation", array('GFExactTarget', 'create_menu')); 
  41.  
  42. if(self::is_exacttarget_page()) { 
  43.  
  44. //enqueueing sack for AJAX requests 
  45. wp_enqueue_script(array("sack")); 
  46.  
  47. //loading data lib 
  48. require_once(self::get_base_path() . "/data.php"); 
  49.  
  50.  
  51. //loading Gravity Forms tooltips 
  52. require_once(GFCommon::get_base_path() . "/tooltips.php"); 
  53. add_filter('gform_tooltips', array('GFExactTarget', 'tooltips')); 
  54.  
  55. //runs the setup when version changes 
  56. self::setup(); 
  57.  
  58. else if(in_array(RG_CURRENT_PAGE, array("admin-ajax.php"))) { 
  59.  
  60. //loading data class 
  61. require_once(self::get_base_path() . "/data.php"); 
  62.  
  63. add_action('wp_ajax_rg_update_feed_active', array('GFExactTarget', 'update_feed_active')); 
  64. add_action('wp_ajax_gf_select_exacttarget_form', array('GFExactTarget', 'select_exacttarget_form')); 
  65.  
  66. else{ 
  67. //handling post submission. 
  68. add_action("gform_post_submission", array('GFExactTarget', 'export'), 10, 2); 
  69.  
  70. public static function update_feed_active() { 
  71. check_ajax_referer('rg_update_feed_active', 'rg_update_feed_active'); 
  72. $id = $_POST["feed_id"]; 
  73. $feed = GFExactTargetData::get_feed($id); 
  74. GFExactTargetData::update_feed($id, $feed["form_id"], $_POST["is_active"], $feed["meta"]); 
  75.  
  76. //-------------- Automatic upgrade --------------------------------------------------- 
  77.  
  78. public static function plugin_row() { 
  79. if(!self::is_gravityforms_supported()) { 
  80. $message = sprintf(__("%sGravity Forms%s is required. Activate it now or %spurchase it today!%s"), "<a href='http://wordpressformplugin.com/?r=et'>", "</a>", "<a href='http://wordpressformplugin.com/?r=et'>", "</a>"); 
  81. self::display_plugin_message($message, true); 
  82.  
  83. function settings_link( $links, $file ) { 
  84. static $this_plugin; 
  85. if( ! $this_plugin ) $this_plugin = plugin_basename(__FILE__); 
  86. if ( $file == $this_plugin ) { 
  87. $settings_link = '<a href="' . admin_url( 'admin.php?page=gf_exacttarget' ) . '" title="' . __('Select the Gravity Form you would like to integrate with ExactTarget. Contacts generated by this form will be automatically added to your ExactTarget account.', 'gravity-forms-exacttarget') . '">' . __('Feeds', 'gravity-forms-exacttarget') . '</a>'; 
  88. array_unshift( $links, $settings_link ); // before other links 
  89. $settings_link = '<a href="' . admin_url( 'admin.php?page=gf_settings&addon=ExactTarget' ) . '" title="' . __('Configure your ExactTarget settings.', 'gravity-forms-exacttarget') . '">' . __('Settings', 'gravity-forms-exacttarget') . '</a>'; 
  90. array_unshift( $links, $settings_link ); // before other links 
  91. return $links; 
  92.  
  93. public static function display_plugin_message($message, $is_error = false) { 
  94. $style = ''; 
  95. if($is_error) 
  96. $style = 'style="background-color: #ffebe8;"'; 
  97.  
  98. echo '</tr><tr class="plugin-update-tr"><td colspan="5" class="plugin-update"><div class="update-message" ' . $style . '>' . $message . '</div></td>'; 
  99.  
  100.  
  101. //Returns true if the current page is an Feed pages. Returns false if not 
  102. private static function is_exacttarget_page() { 
  103. global $plugin_page; $current_page = ''; 
  104. $exacttarget_pages = array("gf_exacttarget"); 
  105.  
  106. if(isset($_GET['page'])) { 
  107. $current_page = trim(strtolower($_GET["page"])); 
  108.  
  109. return (in_array($plugin_page, $exacttarget_pages) || in_array($current_page, $exacttarget_pages)); 
  110.  
  111.  
  112. //Creates or updates database tables. Will only run when version changes 
  113. private static function setup() { 
  114.  
  115. if(get_option("gf_exacttarget_version") != self::$version) 
  116. GFExactTargetData::update_table(); 
  117.  
  118. update_option("gf_exacttarget_version", self::$version); 
  119.  
  120. //Adds feed tooltips to the list of tooltips 
  121. public static function tooltips($tooltips) { 
  122. $exacttarget_tooltips = array( 
  123. "exacttarget_contact_list" => "<h6>" . __("ExactTarget List", "gravity-forms-exacttarget") . "</h6>" . __("Select the ExactTarget list you would like to add your contacts to.", "gravity-forms-exacttarget"),  
  124. "exacttarget_gravity_form" => "<h6>" . __("Gravity Form", "gravity-forms-exacttarget") . "</h6>" . __("Select the Gravity Form you would like to integrate with ExactTarget. Contacts generated by this form will be automatically added to your ExactTarget account.", "gravity-forms-exacttarget"),  
  125. "exacttarget_map_fields" => "<h6>" . __("Map Fields", "gravity-forms-exacttarget") . "</h6>" . __("Associate your ExactTarget attributes to the appropriate Gravity Form fields by selecting.", "gravity-forms-exacttarget"),  
  126. "exacttarget_optin_condition" => "<h6>" . __("Opt-In Condition", "gravity-forms-exacttarget") . "</h6>" . __("When the opt-in condition is enabled, form submissions will only be exported to ExactTarget when the condition is met. When disabled all form submissions will be exported.", "gravity-forms-exacttarget"),  
  127.  
  128. ); 
  129. return array_merge($tooltips, $exacttarget_tooltips); 
  130.  
  131. //Creates ExactTarget left nav menu under Forms 
  132. public static function create_menu($menus) { 
  133.  
  134. // Adding submenu if user has access 
  135. $permission = self::has_access("gravityforms_exacttarget"); 
  136. if(!empty($permission)) 
  137. $menus[] = array("name" => "gf_exacttarget", "label" => __("ExactTarget", "gravity-forms-exacttarget"), "callback" => array("GFExactTarget", "exacttarget_page"), "permission" => $permission); 
  138.  
  139. return $menus; 
  140.  
  141. public static function settings_page() { 
  142.  
  143. if(isset($_POST["uninstall"])) { 
  144. check_admin_referer("uninstall", "gf_exacttarget_uninstall"); 
  145. self::uninstall(); 
  146.  
  147. ?> 
  148. <div class="updated fade" style="padding:20px;"><?php _e(sprintf("Gravity Forms ExactTarget Add-On has been successfully uninstalled. It can be re-activated from the %splugins page%s.", "<a href='plugins.php'>", "</a>"), "gravity-forms-exacttarget")?></div> 
  149. <?php 
  150. return; 
  151. else if(isset($_POST["gf_exacttarget_submit"])) { 
  152. check_admin_referer("update", "gf_exacttarget_update"); 
  153. $settings = array( 
  154. "username" => stripslashes($_POST["gf_exacttarget_username"]),  
  155. "password" => stripslashes($_POST["gf_exacttarget_password"]),  
  156. "mid" => stripslashes((int)$_POST["gf_exacttarget_mid"]),  
  157. "debug" => isset($_POST["gf_exacttarget_debug"]),  
  158. "subscriberkey" => isset($_POST["gf_exacttarget_subscriberkey"]),  
  159. "s4" => isset($_POST["gf_exacttarget_s4"]),  
  160. "addtype" => stripslashes($_POST["gf_exacttarget_addtype"]) 
  161. ); 
  162. update_option("gf_exacttarget_settings", $settings); 
  163. else{ 
  164. $settings = get_option("gf_exacttarget_settings"); 
  165.  
  166. $settings = wp_parse_args($settings, array( 
  167. "username" => '',  
  168. "password" => '',  
  169. "mid" => '',  
  170. "subscriberkey" => false,  
  171. "debug" => false,  
  172. "s4" => false,  
  173. "addtype" => 'api' 
  174. )); 
  175.  
  176. $api = self::get_api(); 
  177. $message = ''; 
  178.  
  179. if(!empty($settings["username"]) || !empty($settings["password"])) { 
  180. $api->TestAPI(); 
  181.  
  182. $new = false; 
  183. if(!empty($settings["username"]) && !empty($settings["password"]) && empty($api->lastError)) { 
  184. $message = sprintf(__("Valid username and API key. Now go %sconfigure form integration with ExactTarget%s!", "gravity-forms-exacttarget"), '<a href="'.admin_url('admin.php?page=gf_exacttarget').'">', '</a>'); 
  185. $class = "updated valid_credentials"; 
  186. $valid = true; 
  187. $new = false; 
  188. } else if(!empty($settings["username"]) || !empty($settings["password"])) { 
  189. $message = __("Invalid username and/or password. Please try another combination. (Message from ExactTarget: “".$api->lastError.'”)', "gravity-forms-exacttarget"); 
  190. $valid = false; 
  191. $class = "error invalid_credentials"; 
  192. $new = false; 
  193. } else if (empty($settings["username"]) && empty($settings["password"])) { 
  194. $new = true; 
  195. $valid = false; 
  196. $class = 'updated notice'; 
  197.  
  198.  
  199. if($message) { 
  200. ?> 
  201. <div id="message" class="<?php echo $class ?>"><?php echo wpautop($message); ?></div> 
  202. <?php 
  203. /** <?php _e("", "gravity-forms-exacttarget"); ?> */ 
  204.  
  205. ?> 
  206.  
  207. <?php if($new) { @include_once('register.php'); } ?> 
  208.  
  209. <form method="post" action="<?php echo remove_query_arg(array('refresh', 'retrieveListNames', '_wpnonce')); ?>" <?php if($new) { echo ' class="alignleft" style="width:60%; clear:left;"'; } ?>> 
  210. <?php wp_nonce_field("update", "gf_exacttarget_update") ?> 
  211.  
  212. <h2><?php _e("ExactTarget Account Information", "gravity-forms-exacttarget") ?></h2> 
  213.  
  214. <table class="form-table"> 
  215. <tr> 
  216. <th scope="row"><label for="gf_exacttarget_username"><?php _e("ExactTarget Account Username", "gravity-forms-exacttarget"); ?></label> </th> 
  217. <td><input type="text" id="gf_exacttarget_username" name="gf_exacttarget_username" size="30" value="<?php echo empty($settings["username"]) ? '' : esc_attr($settings["username"]); ?>"/></td> 
  218. </tr> 
  219. <tr> 
  220. <th scope="row"><label for="gf_exacttarget_password"><?php _e("Password", "gravity-forms-exacttarget"); ?></label> </th> 
  221. <td><input type="password" id="gf_exacttarget_password" name="gf_exacttarget_password" size="40" value="<?php echo !empty($settings["password"]) ? esc_attr($settings["password"]) : ''; ?>"/></td> 
  222. </tr> 
  223. <tr> 
  224. <th scope="row"><label for="gf_exacttarget_s4"><?php _e("Are you on S4?", "gravity-forms-exacttarget"); ?></label></th> 
  225. <td><input type="checkbox" id="gf_exacttarget_s4" name="gf_exacttarget_s4" value="1" <?php checked($settings["s4"], true); ?>/> 
  226. <span class="howto"><?php _e("When you log in to ExactTarget, does the URL start with <code>https://members.<strong>s4</strong>.exacttarget.com</code>? if so, you are on S4. Otherwise, you are not. Need more help?", "gravity-forms-exacttarget"); ?> <a rel="external" target="_blank" href="http://wiki.memberlandingpages.com/010_ExactTarget/010_Getting_Started/The_Getting_Started_Guide/Set_Up_Your_Account#How_To_Determine_What_Instance_You're_On"><?php _e("How to Determine What Instance You're On", "gravity-forms-exacttarget"); ?></a>.</span> 
  227. </td> 
  228. </tr> 
  229. <tr> 
  230. <th scope="row"><label for="gf_exacttarget_addtype"><?php _e("Submission Type", "gravity-forms-exacttarget"); ?></label></th> 
  231. <td> 
  232. <label for="gf_exacttarget_addtype_api" style="margin-right:1em;"><input type="radio" id="gf_exacttarget_addtype_api" name="gf_exacttarget_addtype" value="api" <?php checked($settings["addtype"], 'api'); ?>/> <?php _e("API (default)", "gravity-forms-exacttarget"); ?></label> 
  233. <label for="gf_exacttarget_addtype_webcollect"><input type="radio" id="gf_exacttarget_addtype_webcollect" name="gf_exacttarget_addtype" value="webcollect" <?php checked($settings["addtype"], 'webcollect'); ?>/> <?php _e("Web Collect", "gravity-forms-exacttarget"); ?></label> 
  234. <span class="howto"><?php _e(sprintf('Using the %sXML API%s is the preferred method, but if for some reason you would like to use %sWeb Collect%s instead, you can.', '<a href="http://wiki.memberlandingpages.com/030_Developer_Documentation/040_XML_API">', '</a>', '<a href="http://wiki.memberlandingpages.com/010_ExactTarget/030_Subscribers/Web_Collect">', '</a>'), "gravity-forms-exacttarget"); ?></span> 
  235. </td> 
  236. </tr> 
  237. <tr> 
  238. <th scope="row"><label for="gf_exacttarget_mid"><?php _e("Member ID", "gravity-forms-exacttarget"); ?></label></th> 
  239. <td valign="top"><input type="text" id="gf_exacttarget_mid" name="gf_exacttarget_mid" size="10" value="<?php echo !empty($settings["mid"]) ? esc_attr($settings["mid"]) : ''; ?>"/><span class="howto"><?php _e("You can find your member ID in the upper-right corner of your ExactTarget application, next to your account name.", "gravity-forms-exacttarget"); ?></span></td> 
  240. </tr> 
  241. <tr> 
  242. <th scope="row"><label for="gf_exacttarget_subscriberkey"><?php _e("Subscriber Key is Required For This Account", "gravity-forms-exacttarget"); ?></label> </th> 
  243. <td><input type="checkbox" id="gf_exacttarget_subscriberkey" name="gf_exacttarget_subscriberkey" value="1" <?php checked($settings["subscriberkey"], true); ?>/></td> 
  244. </tr> 
  245. <tr> 
  246. <th scope="row"><label for="gf_exacttarget_debug"><?php _e("Debug Form Submissions for Administrators", "gravity-forms-exacttarget"); ?></label> </th> 
  247. <td><input type="checkbox" id="gf_exacttarget_debug" name="gf_exacttarget_debug" value="1" <?php checked($settings["debug"], true); ?>/></td> 
  248. </tr> 
  249. <tr> 
  250. <td colspan="2" ><input type="submit" name="gf_exacttarget_submit" class="button-primary" value="<?php _e("Save Settings", "gravity-forms-exacttarget") ?>" /></td> 
  251. </tr> 
  252.  
  253. </table> 
  254. <?php if($valid) { ?> 
  255.  
  256. <div id="listnames"> 
  257. <div class="hr-divider"></div> 
  258. <h3><?php _e("Retrieve List Names", "gravity-forms-exacttarget") ?></h3> 
  259. <?php 
  260.  
  261. if(isset($_REQUEST['retrieveListNames']) && isset($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'retrieveListNames')) { 
  262.  
  263. $lists_raw = $api->Lists(true); 
  264.  
  265. $i = 0; $count = sizeof($lists_raw); 
  266. echo '<h4>'.__(sprintf('Retrieving list names for %d lists', $count), "gravity-forms-exacttarget").'</h4>'; 
  267. echo '<ol class="lists_loading ol-decimal" style="list-style:decimal outside!important; margin-left:1.8em!important;">'; 
  268. foreach($lists_raw as $listid => $list) { 
  269. $i++; 
  270. echo '<li style="margin-bottom:.5em!important; list-style: decimal outside;">List #'.$listid; 
  271. $list_xml = $api->ListRetrieve($listid); 
  272. if($list_xml->system->list->list_type == 'Public') { 
  273. $lists["{$listid}"] = (array)$list_xml->system->list; 
  274. echo ': “<strong>'.$list_xml->system->list->list_name.'</strong>”'; 
  275. echo ' ('.$i.' of '.$count.' / '. round(($i/$count * 100), 1).'% Completed)</li>'; 
  276. flush(); 
  277.  
  278. echo '</ol>'; 
  279. @set_transient('extr_lists_all', $lists, 60*60*24*365); 
  280. } else { 
  281. ?> 
  282. <p> 
  283. <a class="submit button button-secondary" href="<?php echo add_query_arg(array('retrieveListNames' =>true, '_wpnonce' => wp_create_nonce('retrieveListNames'))); ?>"><?php _e("Retrieve List Names", "gravity-forms-exacttarget") ?></a></p> 
  284. <p><span class="howto"><?php _e("If you have many lists and only the list IDs are shown when setting up your forms, click the button above to retrieve full list name information.", "gravity-forms-exacttarget") ?></span></p> 
  285.  
  286. <?php } ?> 
  287.  
  288. </div> 
  289. </form> 
  290. <?php } ?> 
  291. <form action="" method="post"> 
  292. <?php wp_nonce_field("uninstall", "gf_exacttarget_uninstall") ?> 
  293. <?php if(GFCommon::current_user_can_any("gravityforms_exacttarget_uninstall")) { ?> 
  294. <div class="hr-divider"></div> 
  295.  
  296. <h3><?php _e("Uninstall ExactTarget Add-On", "gravity-forms-exacttarget") ?></h3> 
  297. <div class="delete-alert"><?php _e("Warning! This operation deletes ALL ExactTarget Feeds.", "gravity-forms-exacttarget") ?> 
  298. <?php 
  299. $uninstall_button = '<input type="submit" name="uninstall" value="' . __("Uninstall ExactTarget Add-On", "gravity-forms-exacttarget") . '" class="button" onclick="return confirm(\'' . __("Warning! ALL ExactTarget Feeds will be deleted. This cannot be undone. \'OK\' to delete, \'Cancel\' to stop", "gravity-forms-exacttarget") . '\');"/>'; 
  300. echo apply_filters("gform_exacttarget_uninstall_button", $uninstall_button); 
  301. ?> 
  302. </div> 
  303. <?php } ?> 
  304. </form> 
  305. <?php 
  306.  
  307. public static function exacttarget_page() { 
  308. $view = isset($_GET["view"]) ? $_GET["view"] : ''; 
  309. if($view == "edit") 
  310. self::edit_page($_GET["id"]); 
  311. else 
  312. self::list_page(); 
  313.  
  314. //Displays the ExactTarget feeds list page 
  315. private static function list_page() { 
  316. if(!self::is_gravityforms_supported()) { 
  317. die(__(sprintf("The ExactTarget Add-On requires Gravity Forms %s. Upgrade automatically on the %sPlugin page%s.", self::$min_gravityforms_version, "<a href='plugins.php'>", "</a>"), "gravity-forms-exacttarget")); 
  318.  
  319. if(isset($_POST["action"]) && $_POST["action"] == "delete") { 
  320. check_admin_referer("list_action", "gf_exacttarget_list"); 
  321.  
  322. $id = absint($_POST["action_argument"]); 
  323. GFExactTargetData::delete_feed($id); 
  324. ?> 
  325. <div class="updated fade" style="padding:6px"><?php _e("Feed deleted.", "gravity-forms-exacttarget") ?></div> 
  326. <?php 
  327. else if (!empty($_POST["bulk_action"])) { 
  328. check_admin_referer("list_action", "gf_exacttarget_list"); 
  329. $selected_feeds = $_POST["feed"]; 
  330. if(is_array($selected_feeds)) { 
  331. foreach($selected_feeds as $feed_id) 
  332. GFExactTargetData::delete_feed($feed_id); 
  333. ?> 
  334. <div class="updated fade" style="padding:6px"><?php _e("Feeds deleted.", "gravity-forms-exacttarget") ?></div> 
  335. <?php 
  336.  
  337. ?> 
  338. <div class="wrap"> 
  339. <img alt="<?php _e("ExactTarget Feeds", "gravity-forms-exacttarget") ?>" src="<?php echo self::get_base_url()?>/images/exacttarget_wordpress_icon_32.png" style="float:left; margin:15px 7px 0 0;"/> 
  340. <h2><?php _e("ExactTarget Feeds", "gravity-forms-exacttarget"); ?> 
  341. <a class="button add-new-h2" href="admin.php?page=gf_exacttarget&view=edit&id=0"><?php _e("Add New", "gravity-forms-exacttarget") ?></a> 
  342. </h2> 
  343.  
  344. <ul class="subsubsub"> 
  345. <li><a href="<?php echo admin_url('admin.php?page=gf_settings&addon=ExactTarget'); ?>">ExactTarget Settings</a> |</li> 
  346. <li><a href="<?php echo admin_url('admin.php?page=gf_exacttarget'); ?>" class="current">ExactTarget Feeds</a></li> 
  347. </ul> 
  348.  
  349. <form id="feed_form" method="post"> 
  350. <?php wp_nonce_field('list_action', 'gf_exacttarget_list') ?> 
  351. <input type="hidden" id="action" name="action"/> 
  352. <input type="hidden" id="action_argument" name="action_argument"/> 
  353.  
  354. <div class="tablenav"> 
  355. <div class="alignleft actions" style="padding:8px 0 7px; 0"> 
  356. <label class="hidden" for="bulk_action"><?php _e("Bulk action", "gravity-forms-exacttarget") ?></label> 
  357. <select name="bulk_action" id="bulk_action"> 
  358. <option value=''> <?php _e("Bulk action", "gravity-forms-exacttarget") ?> </option> 
  359. <option value='delete'><?php _e("Delete", "gravity-forms-exacttarget") ?></option> 
  360. </select> 
  361. <?php 
  362. echo '<input type="submit" class="button" value="' . __("Apply", "gravity-forms-exacttarget") . '" onclick="if( jQuery(\'#bulk_action\').val() == \'delete\' && !confirm(\'' . __("Delete selected feeds? ", "gravity-forms-exacttarget") . __("\'Cancel\' to stop, \'OK\' to delete.", "gravity-forms-exacttarget") .'\')) { return false; } return true;"/>'; 
  363. ?> 
  364. </div> 
  365. </div> 
  366. <table class="widefat fixed" cellspacing="0"> 
  367. <thead> 
  368. <tr> 
  369. <th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox" /></th> 
  370. <th scope="col" id="active" class="manage-column check-column"></th> 
  371. <th scope="col" class="manage-column"><?php _e("Form", "gravity-forms-exacttarget") ?></th> 
  372. <th scope="col" class="manage-column"><?php _e("ExactTarget Lists", "gravity-forms-exacttarget") ?></th> 
  373. </tr> 
  374. </thead> 
  375.  
  376. <tfoot> 
  377. <tr> 
  378. <th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox" /></th> 
  379. <th scope="col" id="active" class="manage-column check-column"></th> 
  380. <th scope="col" class="manage-column"><?php _e("Form", "gravity-forms-exacttarget") ?></th> 
  381. <th scope="col" class="manage-column"><?php _e("ExactTarget Lists", "gravity-forms-exacttarget") ?></th> 
  382. </tr> 
  383. </tfoot> 
  384.  
  385. <tbody class="list:user user-list"> 
  386. <?php 
  387.  
  388. $settings = GFExactTargetData::get_feeds(); 
  389. if(is_array($settings) && !empty($settings)) { 
  390. foreach($settings as $setting) { 
  391. ?> 
  392. <tr class='author-self status-inherit' valign="top"> 
  393. <th scope="row" class="check-column"><input type="checkbox" name="feed[]" value="<?php echo $setting["id"] ?>"/></th> 
  394. <td><img src="<?php echo self::get_base_url() ?>/images/active<?php echo intval($setting["is_active"]) ?>.png" alt="<?php echo $setting["is_active"] ? __("Active", "gravity-forms-exacttarget") : __("Inactive", "gravity-forms-exacttarget");?>" title="<?php echo $setting["is_active"] ? __("Active", "gravity-forms-exacttarget") : __("Inactive", "gravity-forms-exacttarget");?>" onclick="ToggleActive(this, <?php echo $setting['id'] ?>); " /></td> 
  395. <td class="column-title"> 
  396. <a href="admin.php?page=gf_exacttarget&view=edit&id=<?php echo $setting["id"] ?>" title="<?php _e("Edit", "gravity-forms-exacttarget") ?>"><?php echo $setting["form_title"] ?></a> 
  397. <div class="row-actions"> 
  398. <span class="edit"> 
  399. <a title="Edit this setting" href="admin.php?page=gf_exacttarget&view=edit&id=<?php echo $setting["id"] ?>" title="<?php _e("Edit", "gravity-forms-exacttarget") ?>"><?php _e("Edit", "gravity-forms-exacttarget") ?></a> 
  400. </span> 
  401.  
  402. <span class="edit"> 
  403. <a title="<?php _e("Delete", "gravity-forms-exacttarget") ?>" href="javascript: if(confirm('<?php _e("Delete this feed? ", "gravity-forms-exacttarget") ?> <?php _e("\'Cancel\' to stop, \'OK\' to delete.", "gravity-forms-exacttarget") ?>')) { DeleteSetting(<?php echo $setting["id"] ?>);}"><?php _e("Delete", "gravity-forms-exacttarget")?></a> 
  404.  
  405. </span> 
  406. </div> 
  407. </td> 
  408. <td class="column-date"><ul class="ul-disc"><li><?php echo implode('</li><li>', explode(', ', $setting["meta"]["contact_list_name"])) ?></li></ul></td> 
  409. </tr> 
  410. <?php 
  411. else { 
  412. $api = self::get_api(); 
  413. if(!empty($api) && empty($api->lastError)) { 
  414. ?> 
  415. <tr> 
  416. <td colspan="4" style="padding:20px;"> 
  417. <?php _e(sprintf("You don't have any ExactTarget feeds configured. Let's go %screate one%s!", '<a href="'.admin_url('admin.php?page=gf_exacttarget&view=edit&id=0').'">', "</a>"), "gravity-forms-exacttarget"); ?> 
  418. </td> 
  419. </tr> 
  420. <?php 
  421. else{ 
  422. ?> 
  423. <tr> 
  424. <td colspan="4" style="padding:20px;"> 
  425. <?php _e(sprintf("To get started, please configure your %sExactTarget Settings%s.", '<a href="admin.php?page=gf_settings&addon=ExactTarget">', "</a>"), "gravity-forms-exacttarget"); ?> 
  426. </td> 
  427. </tr> 
  428. <?php 
  429. ?> 
  430. </tbody> 
  431. </table> 
  432. </form> 
  433. </div> 
  434. <script type="text/javascript"> 
  435. function DeleteSetting(id) { 
  436. jQuery("#action_argument").val(id); 
  437. jQuery("#action").val("delete"); 
  438. jQuery("#feed_form")[0].submit(); 
  439. function ToggleActive(img, feed_id) { 
  440. var is_active = img.src.indexOf("active1.png") >=0 
  441. if(is_active) { 
  442. img.src = img.src.replace("active1.png", "active0.png"); 
  443. jQuery(img).attr('title', '<?php _e("Inactive", "gravity-forms-exacttarget") ?>').attr('alt', '<?php _e("Inactive", "gravity-forms-exacttarget") ?>'); 
  444. else{ 
  445. img.src = img.src.replace("active0.png", "active1.png"); 
  446. jQuery(img).attr('title', '<?php _e("Active", "gravity-forms-exacttarget") ?>').attr('alt', '<?php _e("Active", "gravity-forms-exacttarget") ?>'); 
  447.  
  448. var mysack = new sack("<?php echo admin_url("admin-ajax.php")?>" ); 
  449. mysack.execute = 1; 
  450. mysack.method = 'POST'; 
  451. mysack.setVar( "action", "rg_update_feed_active" ); 
  452. mysack.setVar( "rg_update_feed_active", "<?php echo wp_create_nonce("rg_update_feed_active") ?>" ); 
  453. mysack.setVar( "feed_id", feed_id ); 
  454. mysack.setVar( "is_active", is_active ? 0 : 1 ); 
  455. mysack.encVar( "cookie", document.cookie, false ); 
  456. mysack.onError = function() { alert('<?php _e("Ajax error while updating feed", "gravity-forms-exacttarget" ) ?>' )}; 
  457. mysack.runAJAX(); 
  458.  
  459. return true; 
  460. </script> 
  461. <?php 
  462.  
  463. public static function get_api() { 
  464. if(!class_exists("ExactTarget")) 
  465. require_once("api/ExactTarget.class.php"); 
  466.  
  467. return new ExactTarget(); 
  468.  
  469. private static function edit_page() { 
  470. ?> 
  471. <style type="text/css"> 
  472. label span.howto { cursor: default; } 
  473. .exacttarget_col_heading{padding-bottom:2px; border-bottom: 1px solid #ccc; font-weight:bold;} 
  474. .exacttarget_field_cell {padding: 6px 17px 0 0; margin-right:15px;} 
  475. .gfield_required{color:red;} 
  476.  
  477. .feeds_validation_error{ background-color:#FFDFDF;} 
  478. .feeds_validation_error td{ margin-top:4px; margin-bottom:6px; padding-top:6px; padding-bottom:6px; border-top:1px dotted #C89797; border-bottom:1px dotted #C89797} 
  479.  
  480. .left_header{float:left; width:200px; padding-right: 20px;} 
  481. .margin_vertical_10{margin: 20px 0;} 
  482. #gf_exacttarget_list_list { margin-left:220px; padding-top: 1px } 
  483. #exacttarget_doubleoptin_warning{padding-left: 5px; padding-bottom:4px; font-size: 10px;} 
  484. </style> 
  485. <script type="text/javascript"> 
  486. var form = Array(); 
  487. </script> 
  488. <div class="wrap"> 
  489. <img alt="<?php _e("ExactTarget", "gravity-forms-exacttarget") ?>" style="margin: .75em 7px 0pt 0pt; float: left;" src="<?php echo self::get_base_url() ?>/images/exacttarget_wordpress_icon_32.png"/> 
  490. <h2><?php _e("ExactTarget Feed", "gravity-forms-exacttarget") ?></h2> 
  491.  
  492. <?php 
  493. //getting ExactTarget API 
  494. $api = self::get_api(); 
  495.  
  496. //ensures valid credentials were entered in the settings page 
  497. if(!empty($api->lastError)) { 
  498. ?> 
  499. <div class="error" id="message" style="margin-top:20px;"><?php echo wpautop(sprintf(__("We are unable to login to ExactTarget with the provided username and API key. Please make sure they are valid in the %sSettings Page%s", "gravity-forms-exacttarget"), "<a href='?page=gf_settings&addon=ExactTarget'>", "</a>")); ?></div> 
  500. <?php 
  501. return; 
  502.  
  503. //getting setting id (0 when creating a new one) 
  504. $id = !empty($_POST["exacttarget_setting_id"]) ? $_POST["exacttarget_setting_id"] : absint($_GET["id"]); 
  505. $config = empty($id) ? array("meta" => array(), "is_active" => true) : GFExactTargetData::get_feed($id); 
  506.  
  507.  
  508. //getting merge vars 
  509. $merge_vars = array(); 
  510.  
  511. //updating meta information 
  512. if(isset($_POST["gf_exacttarget_submit"])) { 
  513. $list_ids = $list_names = array(); 
  514. foreach($_POST["gf_exacttarget_list"] as $list) { 
  515. list($list_id, $list_name) = explode("|:|", stripslashes($list)); 
  516. $list_ids[] = $list_id; 
  517. $list_names[] = $list_name; 
  518. # print_r(array('id' => $list_ids, 'name' => $list_names)); 
  519. $config["meta"]["contact_list_id"] = empty($list_ids) ? 0 : implode(', ', $list_ids); 
  520. $config["meta"]["contact_list_name"] = implode(', ', $list_names); 
  521. $config["form_id"] = absint($_POST["gf_exacttarget_form"]); 
  522.  
  523. $is_valid = true; 
  524. $merge_vars = $api->Attributes($config["meta"]["contact_list_id"]); 
  525.  
  526. $field_map = array(); 
  527. foreach($merge_vars as $key => $var) { 
  528. $field_name = "exacttarget_map_field_" . $key; 
  529. $mapped_field = isset($_POST[$field_name]) ? stripslashes($_POST[$field_name]) : ''; 
  530. if(!empty($mapped_field)) { 
  531. $field_map[$key] = $mapped_field; 
  532. else{ 
  533. unset($field_map[$key]); 
  534. if($var["required"] == "True") 
  535. $is_valid = false; 
  536. unset($_POST["{$field_name}"]); 
  537.  
  538. // Go through the items that were not in the field map; 
  539. // the Custom Fields 
  540. foreach($_POST as $k => $v) { 
  541. if(preg_match('/exacttarget\_map\_field\_/', $k)) { 
  542. $tag = str_replace('exacttarget_map_field_', '', $k); 
  543. $field_map[$tag] = stripslashes($_POST[$k]); 
  544.  
  545. $config["meta"]["field_map"] = $field_map; 
  546. #$config["meta"]["double_optin"] = !empty($_POST["exacttarget_double_optin"]) ? true : false; 
  547. #$config["meta"]["welcome_email"] = !empty($_POST["exacttarget_welcome_email"]) ? true : false; 
  548.  
  549. $config["meta"]["optin_enabled"] = !empty($_POST["exacttarget_optin_enable"]) ? true : false; 
  550. $config["meta"]["optin_field_id"] = $config["meta"]["optin_enabled"] ? isset($_POST["exacttarget_optin_field_id"]) ? $_POST["exacttarget_optin_field_id"] : '' : ""; 
  551. $config["meta"]["optin_operator"] = $config["meta"]["optin_enabled"] ? isset($_POST["exacttarget_optin_operator"]) ? $_POST["exacttarget_optin_operator"] : '' : ""; 
  552. $config["meta"]["optin_value"] = $config["meta"]["optin_enabled"] ? $_POST["exacttarget_optin_value"] : ""; 
  553.  
  554.  
  555.  
  556. if($is_valid) { 
  557. $id = GFExactTargetData::update_feed($id, $config["form_id"], $config["is_active"], $config["meta"]); 
  558. ?> 
  559. <div id="message" class="updated fade" style="margin-top:10px;"><p><?php echo sprintf(__("Feed Updated. %sback to list%s", "gravity-forms-exacttarget"), "<a href='?page=gf_exacttarget'>", "</a>") ?></p> 
  560. <input type="hidden" name="exacttarget_setting_id" value="<?php echo $id ?>"/> 
  561. </div> 
  562. <?php 
  563. else{ 
  564. ?> 
  565. <div class="error" style="padding:6px"><?php echo __("Feed could not be updated. Please enter all required information below.", "gravity-forms-exacttarget") ?></div> 
  566. <?php 
  567. if(!function_exists('gform_tooltip')) { 
  568. require_once(GFCommon::get_base_path() . "/tooltips.php"); 
  569.  
  570. ?> 
  571. <form method="post" action="<?php echo remove_query_arg('refresh'); ?>"> 
  572. <input type="hidden" name="exacttarget_setting_id" value="<?php echo $id ?>"/> 
  573. <div class="margin_vertical_10"> 
  574. <h2><?php _e('1. Select the lists to merge with.', "gravity-forms-exacttarget"); ?></h2> 
  575. <label for="gf_exacttarget_list" class="left_header"><?php _e("ExactTarget List", "gravity-forms-exacttarget"); ?> <?php gform_tooltip("exacttarget_contact_list") ?> <span class="howto"><?php _e(sprintf("%sRefresh lists%s", '<a href="'.add_query_arg('refresh', 'lists').'">', '</a>'), "gravity-forms-exacttarget"); ?></span></label> 
  576.  
  577. <?php 
  578. $trans = get_transient('extr_lists'); 
  579. if(!isset($_POST["gf_exacttarget_submit"]) && (!$trans || ($trans && isset($_REQUEST['refresh']) && $_REQUEST['refresh'] === 'lists'))) { ?> 
  580. <p class="lists_loading hide-if-no-js" style='padding:5px;'><img src="<?php echo GFExactTarget::get_base_url() ?>/images/loading.gif" id="exacttarget_wait" style="padding-right:5px;" width="16" height="16" /> <?php _e('Lists are being loaded', 'gravity-forms-exacttarget'); ?></p> 
  581. <?php 
  582.  
  583. //getting all contact lists 
  584. $lists = $api->Lists(); 
  585.  
  586. if (!$lists) { 
  587. echo __("Could not load ExactTarget contact lists. <br/>Error: ", "gravity-forms-exacttarget"); 
  588. echo isset($api->errorMessage) ? $api->errorMessage : ''; 
  589. else{ 
  590. if(isset($config["meta"]["contact_list_id"])) { 
  591. $contact_lists = explode(', ' , $config["meta"]["contact_list_id"]); 
  592. } else { 
  593. $contact_lists = array(); 
  594. ?> 
  595. <?php 
  596. if(!get_transient('extr_lists_all')) { 
  597. echo sprintf('%sYour list size is large; only list ID\'s are shown. %sRetrieve List Names%s%s', '<p>', '<a href="'.add_query_arg(array('retrieveListNames' =>true, '_wpnonce' => wp_create_nonce('retrieveListNames')), admin_url('admin.php?page=gf_settings&addon=ExactTarget')).'" class="button-secondary button">', '</a>', '</p>'); 
  598. ?> 
  599. <ul id="gf_exacttarget_list_list" class="hide-if-js"> 
  600. <?php 
  601. foreach ($lists as $key => $list) { 
  602. $selected = in_array($key, $contact_lists) ? "checked='checked'" : ""; 
  603. ?> 
  604. <li><label style="display:block;" for="gf_exacttarget_list_<?php echo esc_html($key); ?>"><input type="checkbox" name="gf_exacttarget_list[]" id="gf_exacttarget_list_<?php echo esc_html($key); ?>" value="<?php echo esc_html($key) . "|:|" . esc_html($list['list_name']) ?>" <?php echo $selected ?> /> <?php echo esc_html($list['list_name']) ?></label></li> 
  605. <?php 
  606. ?> 
  607. </ul> 
  608. <script type="text/javascript"> 
  609. if(jQuery('.lists_loading').length && jQuery('#gf_exacttarget_list_list').length) { 
  610. jQuery('.lists_loading').fadeOut(function() { jQuery('#gf_exacttarget_list_list').fadeIn(); }); 
  611. } else if(jQuery('#gf_exacttarget_list_list').length) { 
  612. jQuery('#gf_exacttarget_list_list').show(); 
  613. </script> 
  614. <?php 
  615. ?> 
  616. <div class="clear"></div> 
  617. </div> 
  618. <?php flush(); ?> 
  619. <div id="exacttarget_form_container" valign="top" class="margin_vertical_10" <?php echo empty($config["meta"]["contact_list_id"]) ? "style='display:none;'" : "" ?>> 
  620. <h2><?php _e('2. Select the form to tap into.', "gravity-forms-exacttarget"); ?></h2> 
  621. <label for="gf_exacttarget_form" class="left_header"><?php _e("Gravity Form", "gravity-forms-exacttarget"); ?> <?php gform_tooltip("exacttarget_gravity_form") ?></label> 
  622.  
  623. <select id="gf_exacttarget_form" name="gf_exacttarget_form" onchange="SelectForm(jQuery('#gf_exacttarget_list_list input').serialize(), jQuery(this).val());"> 
  624. <option value=""><?php _e("Select a form", "gravity-forms-exacttarget"); ?> </option> 
  625. <?php 
  626. $forms = RGFormsModel::get_forms(); 
  627. foreach($forms as $form) { 
  628. $selected = absint($form->id) == $config["form_id"] ? "selected='selected'" : ""; 
  629. ?> 
  630. <option value="<?php echo absint($form->id) ?>" <?php echo $selected ?>><?php echo esc_html($form->title) ?></option> 
  631. <?php 
  632. ?> 
  633. </select> 
  634.    
  635. <img src="<?php echo GFExactTarget::get_base_url() ?>/images/loading.gif" id="exacttarget_wait" style="display: none;"/> 
  636. </div> 
  637. <div class="clear"></div> 
  638. <div id="exacttarget_field_group" valign="top" <?php echo empty($config["meta"]["contact_list_id"]) || empty($config["form_id"]) ? "style='display:none;'" : "" ?>> 
  639. <div id="exacttarget_field_container" valign="top" class="margin_vertical_10" > 
  640. <h2><?php _e('3. Map form fields to ExactTarget attributes.', "gravity-forms-exacttarget"); ?></h2> 
  641. <p class="description" style="margin-bottom:1em;"><?php _e(sprintf('If you don’t see an attribute listed, you need to create it in ExactTarget first under %sSubscribers > Profile Management%s.%sOnly mapped fields will be added to ExactTarget.', '<em style="font-style:normal;">', '</em>', '<br />'), "gravity-forms-exacttarget"); ?></p> 
  642. <label for="exacttarget_fields" class="left_header"><?php _e("Map Fields", "gravity-forms-exacttarget"); ?> <?php gform_tooltip("exacttarget_map_fields") ?> <span class="howto"><?php _e(sprintf("%sRefresh fields%s", '<a href="'.add_query_arg('refresh', 'attributes').'">', '</a>'), "gravity-forms-exacttarget"); ?></span></label> 
  643.  
  644. <div id="exacttarget_field_list"> 
  645. <?php 
  646. if(!empty($config["form_id"])) { 
  647.  
  648. //getting list of all ExactTarget merge variables for the selected contact list 
  649. if(empty($merge_vars)) 
  650. $merge_vars = $api->Attributes(); 
  651.  
  652. //getting field map UI 
  653. echo self::get_field_mapping($config, $config["form_id"], $merge_vars); 
  654.  
  655. //getting list of selection fields to be used by the optin 
  656. $form_meta = RGFormsModel::get_form_meta($config["form_id"]); 
  657. $selection_fields = GFCommon::get_selection_fields($form_meta, $config["meta"]["optin_field_id"]); 
  658. ?> 
  659. </div> 
  660. <div class="clear"></div> 
  661. </div> 
  662.  
  663. <div id="exacttarget_optin_container" valign="top" class="margin_vertical_10"> 
  664. <label for="exacttarget_optin" class="left_header"><?php _e("Opt-In Condition", "gravity-forms-exacttarget"); ?> <?php gform_tooltip("exacttarget_optin_condition") ?></label> 
  665. <div id="exacttarget_optin"> 
  666. <table> 
  667. <tr> 
  668. <td> 
  669. <input type="checkbox" id="exacttarget_optin_enable" name="exacttarget_optin_enable" value="1" onclick="if(this.checked) {jQuery('#exacttarget_optin_condition_field_container').show('slow');} else{jQuery('#exacttarget_optin_condition_field_container').hide('slow');}" <?php echo !empty($config["meta"]["optin_enabled"]) ? "checked='checked'" : ""?>/> 
  670. <label for="exacttarget_optin_enable"><?php _e("Enable", "gravity-forms-exacttarget"); ?></label> 
  671. </td> 
  672. </tr> 
  673. <tr> 
  674. <td> 
  675. <div id="exacttarget_optin_condition_field_container" <?php echo empty($config["meta"]["optin_enabled"]) ? "style='display:none'" : ""?>> 
  676. <div id="exacttarget_optin_condition_fields" <?php echo empty($selection_fields) ? "style='display:none'" : ""?>> 
  677. <?php _e("Export to ExactTarget if ", "gravity-forms-exacttarget") ?> 
  678.  
  679. <select id="exacttarget_optin_field_id" name="exacttarget_optin_field_id" class='optin_select' onchange='jQuery("#exacttarget_optin_value").html(GetFieldValues(jQuery(this).val(), "", 20));'><?php echo $selection_fields ?></select> 
  680. <select id="exacttarget_optin_operator" name="exacttarget_optin_operator" /> 
  681. <option value="is" <?php echo (isset($config["meta"]["optin_operator"]) && $config["meta"]["optin_operator"] == "is") ? "selected='selected'" : "" ?>><?php _e("is", "gravity-forms-exacttarget") ?></option> 
  682. <option value="isnot" <?php echo (isset($config["meta"]["optin_operator"]) && $config["meta"]["optin_operator"] == "isnot") ? "selected='selected'" : "" ?>><?php _e("is not", "gravity-forms-exacttarget") ?></option> 
  683. </select> 
  684. <select id="exacttarget_optin_value" name="exacttarget_optin_value" class='optin_select'> 
  685. </select> 
  686.  
  687. </div> 
  688. <div id="exacttarget_optin_condition_message" <?php echo !empty($selection_fields) ? "style='display:none'" : ""?>> 
  689. <?php _e("To create an Opt-In condition, your form must have a drop down, checkbox or multiple choice field.", "gravityform") ?> 
  690. </div> 
  691. </div> 
  692. </td> 
  693. </tr> 
  694. </table> 
  695. </div> 
  696.  
  697. <script type="text/javascript"> 
  698. <?php 
  699. if(!empty($config["form_id"])) { 
  700. ?> 
  701. //creating Javascript form object 
  702. form = <?php echo GFCommon::json_encode($form_meta)?> ; 
  703.  
  704. //initializing drop downs 
  705. jQuery(document).ready(function() { 
  706. var selectedField = "<?php echo str_replace('"', '\"', $config["meta"]["optin_field_id"])?>"; 
  707. var selectedValue = "<?php echo str_replace('"', '\"', $config["meta"]["optin_value"])?>"; 
  708. SetOptin(selectedField, selectedValue); 
  709. }); 
  710. <?php 
  711. ?> 
  712. </script> 
  713. </div> 
  714.  
  715. <div id="exacttarget_submit_container" class="margin_vertical_10"> 
  716. <input type="submit" name="gf_exacttarget_submit" value="<?php echo empty($id) ? __("Save Feed", "gravity-forms-exacttarget") : __("Update Feed", "gravity-forms-exacttarget"); ?>" class="button-primary"/> 
  717. </div> 
  718. </div> 
  719. </form> 
  720. </div> 
  721.  
  722. <script type="text/javascript"> 
  723. jQuery(document).ready(function($) { 
  724. $('#gf_exacttarget_list_list').live('load change', function() { 
  725. $('.lists_loading').hide(); 
  726. }); 
  727. $("#gf_exacttarget_list_list input").bind('click change', function() { 
  728. if($("#gf_exacttarget_list_list input:checked").length > 0) { 
  729. SelectList(1); 
  730. } else { 
  731. SelectList(false); 
  732. jQuery("#gf_exacttarget_form").val(""); 
  733. }); 
  734.  
  735. <?php if(isset($_REQUEST['id'])) { ?> 
  736. $('#exacttarget_field_list').live('load', function() { 
  737. $('.exacttarget_field_cell select').each(function() { 
  738. var $select = $(this); 
  739. if($().prop) { 
  740. var label = $.trim($('label[for='+$(this).prop('name')+']').text()); 
  741. } else { 
  742. var label = $.trim($('label[for='+$(this).attr('name')+']').text()); 
  743. label = label.replace(' *', ''); 
  744.  
  745. if($select.val() === '') { 
  746. $('option', $select).each(function() { 
  747. if($(this).text() === label) { 
  748. if($().prop) { 
  749. $('option:contains('+label+')', $select).prop('selected', true); 
  750. } else { 
  751. $('option:contains('+label+')', $select).prop('selected', true); 
  752. }); 
  753. }); 
  754. }); 
  755. <?php } ?> 
  756. }); 
  757. </script> 
  758. <script type="text/javascript"> 
  759.  
  760. function SelectList(listId) { 
  761. if(listId) { 
  762. jQuery("#exacttarget_form_container").slideDown(); 
  763. // jQuery("#gf_exacttarget_form").val(""); 
  764. else{ 
  765. jQuery("#exacttarget_form_container").slideUp(); 
  766. EndSelectForm(""); 
  767.  
  768. function SelectForm(listId, formId) { 
  769. if(!formId) { 
  770. jQuery("#exacttarget_field_group").slideUp(); 
  771. return; 
  772.  
  773. jQuery("#exacttarget_wait").show(); 
  774. jQuery("#exacttarget_field_group").slideUp(); 
  775.  
  776. var mysack = new sack("<?php bloginfo( 'wpurl' ); ?>/wp-admin/admin-ajax.php" ); 
  777. mysack.execute = 1; 
  778. mysack.method = 'POST'; 
  779. mysack.setVar( "action", "gf_select_exacttarget_form" ); 
  780. mysack.setVar( "gf_select_exacttarget_form", "<?php echo wp_create_nonce("gf_select_exacttarget_form") ?>" ); 
  781. mysack.setVar( "list_ids", listId); 
  782. mysack.setVar( "form_id", formId); 
  783. mysack.encVar( "cookie", document.cookie, false ); 
  784. mysack.onError = function() {jQuery("#exacttarget_wait").hide(); alert('<?php _e("Ajax error while selecting a form", "gravity-forms-exacttarget") ?>' )}; 
  785. mysack.runAJAX(); 
  786. return true; 
  787.  
  788. function SetOptin(selectedField, selectedValue) { 
  789.  
  790. //load form fields 
  791. jQuery("#exacttarget_optin_field_id").html(GetSelectableFields(selectedField, 20)); 
  792. var optinConditionField = jQuery("#exacttarget_optin_field_id").val(); 
  793.  
  794. if(optinConditionField) { 
  795. jQuery("#exacttarget_optin_condition_message").hide(); 
  796. jQuery("#exacttarget_optin_condition_fields").show(); 
  797. jQuery("#exacttarget_optin_value").html(GetFieldValues(optinConditionField, selectedValue, 20)); 
  798. else{ 
  799. jQuery("#exacttarget_optin_condition_message").show(); 
  800. jQuery("#exacttarget_optin_condition_fields").hide(); 
  801.  
  802. function EndSelectForm(fieldList, form_meta) { 
  803. //setting global form object 
  804. form = form_meta; 
  805.  
  806. if(fieldList) { 
  807.  
  808. SetOptin("", ""); 
  809.  
  810. jQuery("#exacttarget_field_list").html(fieldList); 
  811. jQuery("#exacttarget_field_group").slideDown(); 
  812. jQuery('#exacttarget_field_list').trigger('load'); 
  813. else{ 
  814. jQuery("#exacttarget_field_group").slideUp(); 
  815. jQuery("#exacttarget_field_list").html(""); 
  816. jQuery("#exacttarget_wait").hide(); 
  817.  
  818. function GetFieldValues(fieldId, selectedValue, labelMaxCharacters) { 
  819. if(!fieldId) 
  820. return ""; 
  821.  
  822. var str = ""; 
  823. var field = GetFieldById(fieldId); 
  824. if(!field || !field.choices) 
  825. return ""; 
  826.  
  827. var isAnySelected = false; 
  828.  
  829. for(var i=0; i<field.choices.length; i++) { 
  830. var fieldValue = field.choices[i].value ? field.choices[i].value : field.choices[i].text; 
  831. var isSelected = fieldValue == selectedValue; 
  832. var selected = isSelected ? "selected='selected'" : ""; 
  833. if(isSelected) 
  834. isAnySelected = true; 
  835.  
  836. str += "<option value='" + fieldValue.replace("'", "'") + "' " + selected + ">" + TruncateMiddle(field.choices[i].text, labelMaxCharacters) + "</option>"; 
  837.  
  838. if(!isAnySelected && selectedValue) { 
  839. str += "<option value='" + selectedValue.replace("'", "'") + "' selected='selected'>" + TruncateMiddle(selectedValue, labelMaxCharacters) + "</option>"; 
  840.  
  841. return str; 
  842.  
  843. function GetFieldById(fieldId) { 
  844. for(var i=0; i<form.fields.length; i++) { 
  845. if(form.fields[i].id == fieldId) 
  846. return form.fields[i]; 
  847. return null; 
  848.  
  849. function TruncateMiddle(text, maxCharacters) { 
  850. if(text.length <= maxCharacters) 
  851. return text; 
  852. var middle = parseInt(maxCharacters / 2); 
  853. return text.substr(0, middle) + "..." + text.substr(text.length - middle, middle); 
  854.  
  855. function GetSelectableFields(selectedFieldId, labelMaxCharacters) { 
  856. var str = ""; 
  857. var inputType; 
  858. for(var i=0; i<form.fields.length; i++) { 
  859. fieldLabel = form.fields[i].adminLabel ? form.fields[i].adminLabel : form.fields[i].label; 
  860. inputType = form.fields[i].inputType ? form.fields[i].inputType : form.fields[i].type; 
  861. if(inputType == "checkbox" || inputType == "radio" || inputType == "select") { 
  862. var selected = form.fields[i].id == selectedFieldId ? "selected='selected'" : ""; 
  863. str += "<option value='" + form.fields[i].id + "' " + selected + ">" + TruncateMiddle(fieldLabel, labelMaxCharacters) + "</option>"; 
  864. return str; 
  865.  
  866. </script> 
  867.  
  868. <?php 
  869.  
  870.  
  871. public static function add_permissions() { 
  872. global $wp_roles; 
  873. $wp_roles->add_cap("administrator", "gravityforms_exacttarget"); 
  874. $wp_roles->add_cap("administrator", "gravityforms_exacttarget_uninstall"); 
  875.  
  876. //Target of Member plugin filter. Provides the plugin with Gravity Forms lists of capabilities 
  877. public static function members_get_capabilities( $caps ) { 
  878. return array_merge($caps, array("gravityforms_exacttarget", "gravityforms_exacttarget_uninstall")); 
  879.  
  880. public static function disable_exacttarget() { 
  881. delete_option("gf_exacttarget_settings"); 
  882.  
  883. public static function select_exacttarget_form() { 
  884. check_ajax_referer("gf_select_exacttarget_form", "gf_select_exacttarget_form"); 
  885.  
  886. $api = self::get_api(); 
  887.  
  888. if(!empty($api->lastError) || !isset($_POST["list_ids"])) { 
  889. die("EndSelectForm();"); 
  890.  
  891. parse_str($_POST["list_ids"], $lists); 
  892.  
  893. $form_id = intval($_POST["form_id"]); 
  894.  
  895. $setting_id = 0; 
  896.  
  897. //getting list of all ExactTarget merge variables for the selected contact list 
  898. $merge_vars = $api->Attributes(); 
  899.  
  900. //getting configuration 
  901. $config = GFExactTargetData::get_feed($setting_id); 
  902.  
  903. //getting field map UI 
  904. $str = self::get_field_mapping($config, $form_id, $merge_vars); 
  905.  
  906. //fields meta 
  907. $form = RGFormsModel::get_form_meta($form_id); 
  908. //$fields = $form["fields"]; 
  909. die("EndSelectForm('" . str_replace("'", "\'", $str) . "', " . GFCommon::json_encode($form) . ");"); 
  910.  
  911. private static function get_field_mapping($config, $form_id, $merge_vars) { 
  912.  
  913. //getting list of all fields for the selected form 
  914. $form_fields = self::get_form_fields($form_id); 
  915. $form = RGFormsModel::get_form_meta($form_id); 
  916.  
  917. $usedFields = $customFields = array(); 
  918.  
  919. $str = ''; 
  920.  
  921. $str .= "<table cellpadding='0' cellspacing='0'><tr><td class='exacttarget_col_heading'>" . __("Attribute", "gravity-forms-exacttarget") . "</td><td class='exacttarget_col_heading'>" . __("Form Fields", "gravity-forms-exacttarget") . "</td></tr>"; 
  922.  
  923. foreach($merge_vars as $key => $var) { 
  924. $selected_field = (isset($config["meta"]) && isset($config["meta"]["field_map"]) && isset($config["meta"]["field_map"][$key])) ? $config["meta"]["field_map"][$key] : ''; 
  925. $required = $var["required"] == "True" ? "<span class='gfield_required'>*</span>" : ""; 
  926. $error_class = $var["required"] == "True" && empty($selected_field) && !empty($_POST["gf_exacttarget_submit"]) ? " feeds_validation_error" : ""; 
  927. $str .= "<tr class='$error_class'><td class='exacttarget_field_cell'><label for='exacttarget_map_field_".$key."'>" . $var["name"] . " $required</label></td><td class='exacttarget_field_cell'>" . self::get_mapped_field_list($key, $selected_field, $form_fields) . "</td></tr>"; 
  928. $str .= "</table>"; 
  929.  
  930. return $str; 
  931.  
  932. private function getNewTag($tag, $used = array()) { 
  933. if(isset($used[$tag])) { 
  934. $i = 1; 
  935. while($i < 1000) { 
  936. if(!isset($used[$tag.'_'.$i])) { 
  937. return $tag.'_'.$i; 
  938. $i++; 
  939. return $tag; 
  940.  
  941. public static function get_form_fields($form_id) { 
  942. $form = RGFormsModel::get_form_meta($form_id); 
  943. $fields = array(); 
  944.  
  945. //Adding default fields 
  946. array_push($form["fields"], array("id" => "date_created" , "label" => __("Entry Date", "gravity-forms-exacttarget"))); 
  947. array_push($form["fields"], array("id" => "ip" , "label" => __("User IP", "gravity-forms-exacttarget"))); 
  948. array_push($form["fields"], array("id" => "source_url" , "label" => __("Source Url", "gravity-forms-exacttarget"))); 
  949.  
  950. if(is_array($form["fields"])) { 
  951. foreach($form["fields"] as $field) { 
  952. if(isset($field["inputs"]) && is_array($field["inputs"]) && $field['type'] !== 'checkbox' && $field['type'] !== 'select') { 
  953.  
  954. //If this is an address field, add full name to the list 
  955. if(RGFormsModel::get_input_type($field) == "address") 
  956. $fields[] = array($field["id"], GFCommon::get_label($field) . " (" . __("Full" , "gravity-forms-exacttarget") . ")"); 
  957.  
  958. foreach($field["inputs"] as $input) 
  959. $fields[] = array($input["id"], GFCommon::get_label($field, $input["id"])); 
  960. else if(empty($field["displayOnly"])) { 
  961. $fields[] = array($field["id"], GFCommon::get_label($field)); 
  962. return $fields; 
  963.  
  964. private static function get_address($entry, $field_id) { 
  965. $street_value = str_replace(" ", " ", trim($entry[$field_id . ".1"])); 
  966. $street2_value = str_replace(" ", " ", trim($entry[$field_id . ".2"])); 
  967. $city_value = str_replace(" ", " ", trim($entry[$field_id . ".3"])); 
  968. $state_value = str_replace(" ", " ", trim($entry[$field_id . ".4"])); 
  969. $zip_value = trim($entry[$field_id . ".5"]); 
  970. $country_value = GFCommon::get_country_code(trim($entry[$field_id . ".6"])); 
  971.  
  972. $address = $street_value; 
  973. $address .= !empty($address) && !empty($street2_value) ? " $street2_value" : $street2_value; 
  974. $address .= !empty($address) && (!empty($city_value) || !empty($state_value)) ? " $city_value" : $city_value; 
  975. $address .= !empty($address) && !empty($city_value) && !empty($state_value) ? " $state_value" : $state_value; 
  976. $address .= !empty($address) && !empty($zip_value) ? " $zip_value" : $zip_value; 
  977. $address .= !empty($address) && !empty($country_value) ? " $country_value" : $country_value; 
  978.  
  979. return $address; 
  980.  
  981. public static function get_mapped_field_list($variable_name, $selected_field, $fields) { 
  982. $field_name = "exacttarget_map_field_" . $variable_name; 
  983. $str = "<select name='$field_name' id='$field_name'><option value=''>" . __("", "gravity-forms-exacttarget") . "</option>"; 
  984. foreach($fields as $field) { 
  985. $field_id = $field[0]; 
  986. $field_label = $field[1]; 
  987.  
  988. $selected = $field_id == $selected_field ? "selected='selected'" : ""; 
  989. $str .= "<option value='" . $field_id . "' ". $selected . ">" . $field_label . "</option>"; 
  990. $str .= "</select>"; 
  991. return $str; 
  992.  
  993. public static function get_mapped_field_checkbox($variable_name, $selected_field, $field) { 
  994. $field_name = "exacttarget_map_field_" . $variable_name; 
  995. $field_id = $field[0]; 
  996. $str = "<input name='$field_name' id='$field_name' type='checkbox' value='$field_id'"; 
  997. $selected = $field_id == $selected_field ? " checked='checked'" : false; 
  998. if($selected) { 
  999. $str .= $selected; 
  1000.  
  1001. $str .= " />"; 
  1002. return $str; 
  1003.  
  1004. public static function export($entry, $form) { 
  1005. //Login to ExactTarget 
  1006. $api = self::get_api(); 
  1007. if(!empty($api->lastError)) 
  1008. return; 
  1009.  
  1010. //loading data class 
  1011. require_once(self::get_base_path() . "/data.php"); 
  1012.  
  1013. //getting all active feeds 
  1014. $feeds = GFExactTargetData::get_feed_by_form($form["id"], true); 
  1015. foreach($feeds as $feed) { 
  1016. //only export if user has opted in 
  1017. if(self::is_optin($form, $feed)) 
  1018. self::export_feed($entry, $form, $feed, $api); 
  1019.  
  1020. public static function export_feed($entry, $form, $feed, $api) { 
  1021. #print_r($feed); die(); 
  1022. $double_optin = false; // $feed["meta"]["double_optin"] ? true : false; 
  1023. $send_welcome = false; // $feed["meta"]["welcome_email"] ? true : false; 
  1024. $email_field_id = $feed["meta"]["field_map"]["email_address"]; 
  1025. $email = $entry[$email_field_id]; 
  1026.  
  1027. $merge_vars = array(''); 
  1028. foreach($feed["meta"]["field_map"] as $var_tag => $field_id) { 
  1029.  
  1030. $field = RGFormsModel::get_field($form, $field_id); 
  1031.  
  1032. if($var_tag == 'address_full') { 
  1033. $merge_vars[$var_tag] = self::get_address($entry, $field_id); 
  1034. } else if($var_tag == 'country') { 
  1035. # echo $entry[$field_id]; die(); 
  1036. $merge_vars[$var_tag] = empty($entry[$field_id]) ? '' : GFCommon::get_country_code(trim($entry[$field_id])); 
  1037. } else if($var_tag != "email") { 
  1038. if(!empty($entry[$field_id])) { 
  1039. if($field['type'] == 'textarea') { 
  1040. $merge_vars[$var_tag] = '<![CDATA['.$entry[$field_id].']]>'; 
  1041. } else{ 
  1042. $merge_vars[$var_tag] = $entry[$field_id]; 
  1043. } else { 
  1044. foreach($entry as $key => $value) { 
  1045. if(floor($key) == floor($field_id) && !empty($value)) { 
  1046. $merge_vars[$var_tag][] = $value; 
  1047.  
  1048. if(apply_filters('gf_exacttarget_add_source', true) && isset($form['title'])) { 
  1049. $merge_vars['source_form'] = $form['title']; 
  1050.  
  1051. if((empty($api->addtype) || $api->addtype == 'api') && empty($api->subscriberkey)) { 
  1052. $lists = explode(', ', $feed["meta"]["contact_list_id"]); 
  1053. foreach($lists as $list) { 
  1054. $api->AddMembership($list, $email, $merge_vars); 
  1055. } else { 
  1056. $api->listSubscribe($feed["meta"]["contact_list_id"], $email, $merge_vars); 
  1057.  
  1058.  
  1059. public static function uninstall() { 
  1060.  
  1061. //loading data lib 
  1062. require_once(self::get_base_path() . "/data.php"); 
  1063.  
  1064. if(!GFExactTarget::has_access("gravityforms_exacttarget_uninstall")) 
  1065. die(__("You don't have adequate permission to uninstall ExactTarget Add-On.", "gravity-forms-exacttarget")); 
  1066.  
  1067. //droping all tables 
  1068. GFExactTargetData::drop_tables(); 
  1069.  
  1070. //removing options 
  1071. delete_option("gf_exacttarget_settings"); 
  1072. delete_option("gf_exacttarget_version"); 
  1073.  
  1074. //Deactivating plugin 
  1075. $plugin = "gravity-forms-exacttarget/exacttarget.php"; 
  1076. deactivate_plugins($plugin); 
  1077. update_option('recently_activated', array($plugin => time()) + (array)get_option('recently_activated')); 
  1078.  
  1079. public static function is_optin($form, $settings) { 
  1080. $config = $settings["meta"]; 
  1081. $operator = $config["optin_operator"]; 
  1082.  
  1083. $field = RGFormsModel::get_field($form, $config["optin_field_id"]); 
  1084. $field_value = RGFormsModel::get_field_value($field, array()); 
  1085. $is_value_match = is_array($field_value) ? in_array($config["optin_value"], $field_value) : $field_value == $config["optin_value"]; 
  1086.  
  1087. return !$config["optin_enabled"] || empty($field) || ($operator == "is" && $is_value_match) || ($operator == "isnot" && !$is_value_match); 
  1088.  
  1089.  
  1090. private static function is_gravityforms_installed() { 
  1091. return class_exists("RGForms"); 
  1092.  
  1093. private static function is_gravityforms_supported() { 
  1094. if(class_exists("GFCommon")) { 
  1095. $is_correct_version = version_compare(GFCommon::$version, self::$min_gravityforms_version, ">="); 
  1096. return $is_correct_version; 
  1097. else{ 
  1098. return false; 
  1099.  
  1100. private function simpleXMLToArray($xml,  
  1101. $flattenValues=true,  
  1102. $flattenAttributes = true,  
  1103. $flattenChildren=true,  
  1104. $valueKey='@value',  
  1105. $attributesKey='@attributes',  
  1106. $childrenKey='@children') { 
  1107.  
  1108. $return = array(); 
  1109. if(!($xml instanceof SimpleXMLElement)) {return $return;} 
  1110. $name = $xml->getName(); 
  1111. $_value = trim((string)$xml); 
  1112. if(strlen($_value)==0) {$_value = null;}; 
  1113.  
  1114. if($_value!==null) { 
  1115. if(!$flattenValues) {$return[$valueKey] = $_value;} 
  1116. else{$return = $_value;} 
  1117.  
  1118. $children = array(); 
  1119. $first = true; 
  1120. foreach($xml->children() as $elementName => $child) { 
  1121. $value = self::simpleXMLToArray($child, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey); 
  1122. if(isset($children[$elementName])) { 
  1123. if($first) { 
  1124. $temp = $children[$elementName]; 
  1125. unset($children[$elementName]); 
  1126. $children[$elementName][] = $temp; 
  1127. $first=false; 
  1128. $children[$elementName][] = $value; 
  1129. else{ 
  1130. $children[$elementName] = $value; 
  1131. if(count($children)>0) { 
  1132. if(!$flattenChildren) {$return[$childrenKey] = $children;} 
  1133. else{$return = array_merge($return, $children);} 
  1134.  
  1135. $attributes = array(); 
  1136. foreach($xml->attributes() as $name=>$value) { 
  1137. $attributes[$name] = trim($value); 
  1138. if(count($attributes)>0) { 
  1139. if(!$flattenAttributes) {$return[$attributesKey] = $attributes;} 
  1140. else{$return = array_merge($return, $attributes);} 
  1141.  
  1142. return $return; 
  1143.  
  1144. private function convert_xml_to_object($response) { 
  1145. $response = @simplexml_load_string($response); // Added @ 1.2.2 
  1146. if(is_object($response)) { 
  1147. return $response; 
  1148. } else { 
  1149. return false; 
  1150.  
  1151. private function convert_xml_to_array($response) { 
  1152. $response = self::convert_xml_to_object($response); 
  1153. $response = self::simpleXMLToArray($response); 
  1154. if(is_array($response)) { 
  1155. return $response; 
  1156. } else { 
  1157. return false; 
  1158.  
  1159. protected static function has_access($required_permission) { 
  1160. $has_members_plugin = function_exists('members_get_capabilities'); 
  1161. $has_access = $has_members_plugin ? current_user_can($required_permission) : current_user_can("level_7"); 
  1162. if($has_access) 
  1163. return $has_members_plugin ? $required_permission : "level_7"; 
  1164. else 
  1165. return false; 
  1166.  
  1167. //Returns the url of the plugin's root folder 
  1168. protected function get_base_url() { 
  1169. return plugins_url(null, __FILE__); 
  1170.  
  1171. //Returns the physical path of the plugin's root folder 
  1172. protected function get_base_path() { 
  1173. $folder = basename(dirname(__FILE__)); 
  1174. return WP_PLUGIN_DIR . "/" . $folder; 
  1175.  
  1176.  
  1. class GFExactTarget { 
  2.  
  3. private static $path = "gravity-forms-exacttarget/exacttarget.php"; 
  4. private static $url = "http://www.gravityforms.com"; 
  5. private static $slug = "gravity-forms-exacttarget"; 
  6. private static $version = "1.0"; 
  7. private static $min_gravityforms_version = "1.3.9"; 
  8.  
  9. //Plugin starting point. Will load appropriate files 
  10. public static function init() { 
  11. global $pagenow; 
  12. if($pagenow == 'plugins.php' || defined('RG_CURRENT_PAGE') && RG_CURRENT_PAGE == "plugins.php") { 
  13. //loading translations 
  14. load_plugin_textdomain('gravity-forms-exacttarget', FALSE, '/gravity-forms-exacttarget/languages' ); 
  15.  
  16. add_action('after_plugin_row_' . self::$path, array('GFExactTarget', 'plugin_row') ); 
  17.  
  18. add_filter('plugin_action_links', array('GFExactTarget', 'settings_link'), 10, 2 ); 
  19.  
  20.  
  21. if(!self::is_gravityforms_supported()) { 
  22. return; 
  23.  
  24. if(is_admin()) { 
  25. //loading translations 
  26. load_plugin_textdomain('gravity-forms-exacttarget', FALSE, '/gravity-forms-exacttarget/languages' ); 
  27.  
  28. add_filter("transient_update_plugins", array('GFExactTarget', 'check_update')); 
  29. #add_filter("site_transient_update_plugins", array('GFExactTarget', 'check_update')); 
  30.  
  31. //creates a new Settings page on Gravity Forms' settings screen 
  32. if(self::has_access("gravityforms_exacttarget")) { 
  33. RGForms::add_settings_page("ExactTarget", array("GFExactTarget", "settings_page"), self::get_base_url() . "/images/exacttarget_wordpress_icon_32.png"); 
  34.  
  35. //integrating with Members plugin 
  36. if(function_exists('members_get_capabilities')) 
  37. add_filter('members_get_capabilities', array("GFExactTarget", "members_get_capabilities")); 
  38.  
  39. //creates the subnav left menu 
  40. add_filter("gform_addon_navigation", array('GFExactTarget', 'create_menu')); 
  41.  
  42. if(self::is_exacttarget_page()) { 
  43.  
  44. //enqueueing sack for AJAX requests 
  45. wp_enqueue_script(array("sack")); 
  46.  
  47. //loading data lib 
  48. require_once(self::get_base_path() . "/data.php"); 
  49.  
  50.  
  51. //loading Gravity Forms tooltips 
  52. require_once(GFCommon::get_base_path() . "/tooltips.php"); 
  53. add_filter('gform_tooltips', array('GFExactTarget', 'tooltips')); 
  54.  
  55. //runs the setup when version changes 
  56. self::setup(); 
  57.  
  58. else if(in_array(RG_CURRENT_PAGE, array("admin-ajax.php"))) { 
  59.  
  60. //loading data class 
  61. require_once(self::get_base_path() . "/data.php"); 
  62.  
  63. add_action('wp_ajax_rg_update_feed_active', array('GFExactTarget', 'update_feed_active')); 
  64. add_action('wp_ajax_gf_select_exacttarget_form', array('GFExactTarget', 'select_exacttarget_form')); 
  65.  
  66. else{ 
  67. //handling post submission. 
  68. add_action("gform_post_submission", array('GFExactTarget', 'export'), 10, 2); 
  69.  
  70. public static function update_feed_active() { 
  71. check_ajax_referer('rg_update_feed_active', 'rg_update_feed_active'); 
  72. $id = $_POST["feed_id"]; 
  73. $feed = GFExactTargetData::get_feed($id); 
  74. GFExactTargetData::update_feed($id, $feed["form_id"], $_POST["is_active"], $feed["meta"]); 
  75.  
  76. //-------------- Automatic upgrade --------------------------------------------------- 
  77.  
  78. public static function plugin_row() { 
  79. if(!self::is_gravityforms_supported()) { 
  80. $message = sprintf(__("%sGravity Forms%s is required. Activate it now or %spurchase it today!%s"), "<a href='http://wordpressformplugin.com/?r=et'>", "</a>", "<a href='http://wordpressformplugin.com/?r=et'>", "</a>"); 
  81. self::display_plugin_message($message, true); 
  82.  
  83. function settings_link( $links, $file ) { 
  84. static $this_plugin; 
  85. if( ! $this_plugin ) $this_plugin = plugin_basename(__FILE__); 
  86. if ( $file == $this_plugin ) { 
  87. $settings_link = '<a href="' . admin_url( 'admin.php?page=gf_exacttarget' ) . '" title="' . __('Select the Gravity Form you would like to integrate with ExactTarget. Contacts generated by this form will be automatically added to your ExactTarget account.', 'gravity-forms-exacttarget') . '">' . __('Feeds', 'gravity-forms-exacttarget') . '</a>'; 
  88. array_unshift( $links, $settings_link ); // before other links 
  89. $settings_link = '<a href="' . admin_url( 'admin.php?page=gf_settings&addon=ExactTarget' ) . '" title="' . __('Configure your ExactTarget settings.', 'gravity-forms-exacttarget') . '">' . __('Settings', 'gravity-forms-exacttarget') . '</a>'; 
  90. array_unshift( $links, $settings_link ); // before other links 
  91. return $links; 
  92.  
  93. public static function display_plugin_message($message, $is_error = false) { 
  94. $style = ''; 
  95. if($is_error) 
  96. $style = 'style="background-color: #ffebe8;"'; 
  97.  
  98. echo '</tr><tr class="plugin-update-tr"><td colspan="5" class="plugin-update"><div class="update-message" ' . $style . '>' . $message . '</div></td>'; 
  99.  
  100.  
  101. //Returns true if the current page is an Feed pages. Returns false if not 
  102. private static function is_exacttarget_page() { 
  103. global $plugin_page; $current_page = ''; 
  104. $exacttarget_pages = array("gf_exacttarget"); 
  105.  
  106. if(isset($_GET['page'])) { 
  107. $current_page = trim(strtolower($_GET["page"])); 
  108.  
  109. return (in_array($plugin_page, $exacttarget_pages) || in_array($current_page, $exacttarget_pages)); 
  110.  
  111.  
  112. //Creates or updates database tables. Will only run when version changes 
  113. private static function setup() { 
  114.  
  115. if(get_option("gf_exacttarget_version") != self::$version) 
  116. GFExactTargetData::update_table(); 
  117.  
  118. update_option("gf_exacttarget_version", self::$version); 
  119.  
  120. //Adds feed tooltips to the list of tooltips 
  121. public static function tooltips($tooltips) { 
  122. $exacttarget_tooltips = array( 
  123. "exacttarget_contact_list" => "<h6>" . __("ExactTarget List", "gravity-forms-exacttarget") . "</h6>" . __("Select the ExactTarget list you would like to add your contacts to.", "gravity-forms-exacttarget"),  
  124. "exacttarget_gravity_form" => "<h6>" . __("Gravity Form", "gravity-forms-exacttarget") . "</h6>" . __("Select the Gravity Form you would like to integrate with ExactTarget. Contacts generated by this form will be automatically added to your ExactTarget account.", "gravity-forms-exacttarget"),  
  125. "exacttarget_map_fields" => "<h6>" . __("Map Fields", "gravity-forms-exacttarget") . "</h6>" . __("Associate your ExactTarget attributes to the appropriate Gravity Form fields by selecting.", "gravity-forms-exacttarget"),  
  126. "exacttarget_optin_condition" => "<h6>" . __("Opt-In Condition", "gravity-forms-exacttarget") . "</h6>" . __("When the opt-in condition is enabled, form submissions will only be exported to ExactTarget when the condition is met. When disabled all form submissions will be exported.", "gravity-forms-exacttarget"),  
  127.  
  128. ); 
  129. return array_merge($tooltips, $exacttarget_tooltips); 
  130.  
  131. //Creates ExactTarget left nav menu under Forms 
  132. public static function create_menu($menus) { 
  133.  
  134. // Adding submenu if user has access 
  135. $permission = self::has_access("gravityforms_exacttarget"); 
  136. if(!empty($permission)) 
  137. $menus[] = array("name" => "gf_exacttarget", "label" => __("ExactTarget", "gravity-forms-exacttarget"), "callback" => array("GFExactTarget", "exacttarget_page"), "permission" => $permission); 
  138.  
  139. return $menus; 
  140.  
  141. public static function settings_page() { 
  142.  
  143. if(isset($_POST["uninstall"])) { 
  144. check_admin_referer("uninstall", "gf_exacttarget_uninstall"); 
  145. self::uninstall(); 
  146.  
  147. ?> 
  148. <div class="updated fade" style="padding:20px;"><?php _e(sprintf("Gravity Forms ExactTarget Add-On has been successfully uninstalled. It can be re-activated from the %splugins page%s.", "<a href='plugins.php'>", "</a>"), "gravity-forms-exacttarget")?></div> 
  149. <?php 
  150. return; 
  151. else if(isset($_POST["gf_exacttarget_submit"])) { 
  152. check_admin_referer("update", "gf_exacttarget_update"); 
  153. $settings = array( 
  154. "username" => stripslashes($_POST["gf_exacttarget_username"]),  
  155. "password" => stripslashes($_POST["gf_exacttarget_password"]),  
  156. "mid" => stripslashes((int)$_POST["gf_exacttarget_mid"]),  
  157. "debug" => isset($_POST["gf_exacttarget_debug"]),  
  158. "subscriberkey" => isset($_POST["gf_exacttarget_subscriberkey"]),  
  159. "s4" => isset($_POST["gf_exacttarget_s4"]),  
  160. "addtype" => stripslashes($_POST["gf_exacttarget_addtype"]) 
  161. ); 
  162. update_option("gf_exacttarget_settings", $settings); 
  163. else{ 
  164. $settings = get_option("gf_exacttarget_settings"); 
  165.  
  166. $settings = wp_parse_args($settings, array( 
  167. "username" => '',  
  168. "password" => '',  
  169. "mid" => '',  
  170. "subscriberkey" => false,  
  171. "debug" => false,  
  172. "s4" => false,  
  173. "addtype" => 'api' 
  174. )); 
  175.  
  176. $api = self::get_api(); 
  177. $message = ''; 
  178.  
  179. if(!empty($settings["username"]) || !empty($settings["password"])) { 
  180. $api->TestAPI(); 
  181.  
  182. $new = false; 
  183. if(!empty($settings["username"]) && !empty($settings["password"]) && empty($api->lastError)) { 
  184. $message = sprintf(__("Valid username and API key. Now go %sconfigure form integration with ExactTarget%s!", "gravity-forms-exacttarget"), '<a href="'.admin_url('admin.php?page=gf_exacttarget').'">', '</a>'); 
  185. $class = "updated valid_credentials"; 
  186. $valid = true; 
  187. $new = false; 
  188. } else if(!empty($settings["username"]) || !empty($settings["password"])) { 
  189. $message = __("Invalid username and/or password. Please try another combination. (Message from ExactTarget: “".$api->lastError.'”)', "gravity-forms-exacttarget"); 
  190. $valid = false; 
  191. $class = "error invalid_credentials"; 
  192. $new = false; 
  193. } else if (empty($settings["username"]) && empty($settings["password"])) { 
  194. $new = true; 
  195. $valid = false; 
  196. $class = 'updated notice'; 
  197.  
  198.  
  199. if($message) { 
  200. ?> 
  201. <div id="message" class="<?php echo $class ?>"><?php echo wpautop($message); ?></div> 
  202. <?php 
  203. /** <?php _e("", "gravity-forms-exacttarget"); ?> */ 
  204.  
  205. ?> 
  206.  
  207. <?php if($new) { @include_once('register.php'); } ?> 
  208.  
  209. <form method="post" action="<?php echo remove_query_arg(array('refresh', 'retrieveListNames', '_wpnonce')); ?>" <?php if($new) { echo ' class="alignleft" style="width:60%; clear:left;"'; } ?>> 
  210. <?php wp_nonce_field("update", "gf_exacttarget_update") ?> 
  211.  
  212. <h2><?php _e("ExactTarget Account Information", "gravity-forms-exacttarget") ?></h2> 
  213.  
  214. <table class="form-table"> 
  215. <tr> 
  216. <th scope="row"><label for="gf_exacttarget_username"><?php _e("ExactTarget Account Username", "gravity-forms-exacttarget"); ?></label> </th> 
  217. <td><input type="text" id="gf_exacttarget_username" name="gf_exacttarget_username" size="30" value="<?php echo empty($settings["username"]) ? '' : esc_attr($settings["username"]); ?>"/></td> 
  218. </tr> 
  219. <tr> 
  220. <th scope="row"><label for="gf_exacttarget_password"><?php _e("Password", "gravity-forms-exacttarget"); ?></label> </th> 
  221. <td><input type="password" id="gf_exacttarget_password" name="gf_exacttarget_password" size="40" value="<?php echo !empty($settings["password"]) ? esc_attr($settings["password"]) : ''; ?>"/></td> 
  222. </tr> 
  223. <tr> 
  224. <th scope="row"><label for="gf_exacttarget_s4"><?php _e("Are you on S4?", "gravity-forms-exacttarget"); ?></label></th> 
  225. <td><input type="checkbox" id="gf_exacttarget_s4" name="gf_exacttarget_s4" value="1" <?php checked($settings["s4"], true); ?>/> 
  226. <span class="howto"><?php _e("When you log in to ExactTarget, does the URL start with <code>https://members.<strong>s4</strong>.exacttarget.com</code>? if so, you are on S4. Otherwise, you are not. Need more help?", "gravity-forms-exacttarget"); ?> <a rel="external" target="_blank" href="http://wiki.memberlandingpages.com/010_ExactTarget/010_Getting_Started/The_Getting_Started_Guide/Set_Up_Your_Account#How_To_Determine_What_Instance_You're_On"><?php _e("How to Determine What Instance You're On", "gravity-forms-exacttarget"); ?></a>.</span> 
  227. </td> 
  228. </tr> 
  229. <tr> 
  230. <th scope="row"><label for="gf_exacttarget_addtype"><?php _e("Submission Type", "gravity-forms-exacttarget"); ?></label></th> 
  231. <td> 
  232. <label for="gf_exacttarget_addtype_api" style="margin-right:1em;"><input type="radio" id="gf_exacttarget_addtype_api" name="gf_exacttarget_addtype" value="api" <?php checked($settings["addtype"], 'api'); ?>/> <?php _e("API (default)", "gravity-forms-exacttarget"); ?></label> 
  233. <label for="gf_exacttarget_addtype_webcollect"><input type="radio" id="gf_exacttarget_addtype_webcollect" name="gf_exacttarget_addtype" value="webcollect" <?php checked($settings["addtype"], 'webcollect'); ?>/> <?php _e("Web Collect", "gravity-forms-exacttarget"); ?></label> 
  234. <span class="howto"><?php _e(sprintf('Using the %sXML API%s is the preferred method, but if for some reason you would like to use %sWeb Collect%s instead, you can.', '<a href="http://wiki.memberlandingpages.com/030_Developer_Documentation/040_XML_API">', '</a>', '<a href="http://wiki.memberlandingpages.com/010_ExactTarget/030_Subscribers/Web_Collect">', '</a>'), "gravity-forms-exacttarget"); ?></span> 
  235. </td> 
  236. </tr> 
  237. <tr> 
  238. <th scope="row"><label for="gf_exacttarget_mid"><?php _e("Member ID", "gravity-forms-exacttarget"); ?></label></th> 
  239. <td valign="top"><input type="text" id="gf_exacttarget_mid" name="gf_exacttarget_mid" size="10" value="<?php echo !empty($settings["mid"]) ? esc_attr($settings["mid"]) : ''; ?>"/><span class="howto"><?php _e("You can find your member ID in the upper-right corner of your ExactTarget application, next to your account name.", "gravity-forms-exacttarget"); ?></span></td> 
  240. </tr> 
  241. <tr> 
  242. <th scope="row"><label for="gf_exacttarget_subscriberkey"><?php _e("Subscriber Key is Required For This Account", "gravity-forms-exacttarget"); ?></label> </th> 
  243. <td><input type="checkbox" id="gf_exacttarget_subscriberkey" name="gf_exacttarget_subscriberkey" value="1" <?php checked($settings["subscriberkey"], true); ?>/></td> 
  244. </tr> 
  245. <tr> 
  246. <th scope="row"><label for="gf_exacttarget_debug"><?php _e("Debug Form Submissions for Administrators", "gravity-forms-exacttarget"); ?></label> </th> 
  247. <td><input type="checkbox" id="gf_exacttarget_debug" name="gf_exacttarget_debug" value="1" <?php checked($settings["debug"], true); ?>/></td> 
  248. </tr> 
  249. <tr> 
  250. <td colspan="2" ><input type="submit" name="gf_exacttarget_submit" class="button-primary" value="<?php _e("Save Settings", "gravity-forms-exacttarget") ?>" /></td> 
  251. </tr> 
  252.  
  253. </table> 
  254. <?php if($valid) { ?> 
  255.  
  256. <div id="listnames"> 
  257. <div class="hr-divider"></div> 
  258. <h3><?php _e("Retrieve List Names", "gravity-forms-exacttarget") ?></h3> 
  259. <?php 
  260.  
  261. if(isset($_REQUEST['retrieveListNames']) && isset($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'retrieveListNames')) { 
  262.  
  263. $lists_raw = $api->Lists(true); 
  264.  
  265. $i = 0; $count = sizeof($lists_raw); 
  266. echo '<h4>'.__(sprintf('Retrieving list names for %d lists', $count), "gravity-forms-exacttarget").'</h4>'; 
  267. echo '<ol class="lists_loading ol-decimal" style="list-style:decimal outside!important; margin-left:1.8em!important;">'; 
  268. foreach($lists_raw as $listid => $list) { 
  269. $i++; 
  270. echo '<li style="margin-bottom:.5em!important; list-style: decimal outside;">List #'.$listid; 
  271. $list_xml = $api->ListRetrieve($listid); 
  272. if($list_xml->system->list->list_type == 'Public') { 
  273. $lists["{$listid}"] = (array)$list_xml->system->list; 
  274. echo ': “<strong>'.$list_xml->system->list->list_name.'</strong>”'; 
  275. echo ' ('.$i.' of '.$count.' / '. round(($i/$count * 100), 1).'% Completed)</li>'; 
  276. flush(); 
  277.  
  278. echo '</ol>'; 
  279. @set_transient('extr_lists_all', $lists, 60*60*24*365); 
  280. } else { 
  281. ?> 
  282. <p> 
  283. <a class="submit button button-secondary" href="<?php echo add_query_arg(array('retrieveListNames' =>true, '_wpnonce' => wp_create_nonce('retrieveListNames'))); ?>"><?php _e("Retrieve List Names", "gravity-forms-exacttarget") ?></a></p> 
  284. <p><span class="howto"><?php _e("If you have many lists and only the list IDs are shown when setting up your forms, click the button above to retrieve full list name information.", "gravity-forms-exacttarget") ?></span></p> 
  285.  
  286. <?php } ?> 
  287.  
  288. </div> 
  289. </form> 
  290. <?php } ?> 
  291. <form action="" method="post"> 
  292. <?php wp_nonce_field("uninstall", "gf_exacttarget_uninstall") ?> 
  293. <?php if(GFCommon::current_user_can_any("gravityforms_exacttarget_uninstall")) { ?> 
  294. <div class="hr-divider"></div> 
  295.  
  296. <h3><?php _e("Uninstall ExactTarget Add-On", "gravity-forms-exacttarget") ?></h3> 
  297. <div class="delete-alert"><?php _e("Warning! This operation deletes ALL ExactTarget Feeds.", "gravity-forms-exacttarget") ?> 
  298. <?php 
  299. $uninstall_button = '<input type="submit" name="uninstall" value="' . __("Uninstall ExactTarget Add-On", "gravity-forms-exacttarget") . '" class="button" onclick="return confirm(\'' . __("Warning! ALL ExactTarget Feeds will be deleted. This cannot be undone. \'OK\' to delete, \'Cancel\' to stop", "gravity-forms-exacttarget") . '\');"/>'; 
  300. echo apply_filters("gform_exacttarget_uninstall_button", $uninstall_button); 
  301. ?> 
  302. </div> 
  303. <?php } ?> 
  304. </form> 
  305. <?php 
  306.  
  307. public static function exacttarget_page() { 
  308. $view = isset($_GET["view"]) ? $_GET["view"] : ''; 
  309. if($view == "edit") 
  310. self::edit_page($_GET["id"]); 
  311. else 
  312. self::list_page(); 
  313.  
  314. //Displays the ExactTarget feeds list page 
  315. private static function list_page() { 
  316. if(!self::is_gravityforms_supported()) { 
  317. die(__(sprintf("The ExactTarget Add-On requires Gravity Forms %s. Upgrade automatically on the %sPlugin page%s.", self::$min_gravityforms_version, "<a href='plugins.php'>", "</a>"), "gravity-forms-exacttarget")); 
  318.  
  319. if(isset($_POST["action"]) && $_POST["action"] == "delete") { 
  320. check_admin_referer("list_action", "gf_exacttarget_list"); 
  321.  
  322. $id = absint($_POST["action_argument"]); 
  323. GFExactTargetData::delete_feed($id); 
  324. ?> 
  325. <div class="updated fade" style="padding:6px"><?php _e("Feed deleted.", "gravity-forms-exacttarget") ?></div> 
  326. <?php 
  327. else if (!empty($_POST["bulk_action"])) { 
  328. check_admin_referer("list_action", "gf_exacttarget_list"); 
  329. $selected_feeds = $_POST["feed"]; 
  330. if(is_array($selected_feeds)) { 
  331. foreach($selected_feeds as $feed_id) 
  332. GFExactTargetData::delete_feed($feed_id); 
  333. ?> 
  334. <div class="updated fade" style="padding:6px"><?php _e("Feeds deleted.", "gravity-forms-exacttarget") ?></div> 
  335. <?php 
  336.  
  337. ?> 
  338. <div class="wrap"> 
  339. <img alt="<?php _e("ExactTarget Feeds", "gravity-forms-exacttarget") ?>" src="<?php echo self::get_base_url()?>/images/exacttarget_wordpress_icon_32.png" style="float:left; margin:15px 7px 0 0;"/> 
  340. <h2><?php _e("ExactTarget Feeds", "gravity-forms-exacttarget"); ?> 
  341. <a class="button add-new-h2" href="admin.php?page=gf_exacttarget&view=edit&id=0"><?php _e("Add New", "gravity-forms-exacttarget") ?></a> 
  342. </h2> 
  343.  
  344. <ul class="subsubsub"> 
  345. <li><a href="<?php echo admin_url('admin.php?page=gf_settings&addon=ExactTarget'); ?>">ExactTarget Settings</a> |</li> 
  346. <li><a href="<?php echo admin_url('admin.php?page=gf_exacttarget'); ?>" class="current">ExactTarget Feeds</a></li> 
  347. </ul> 
  348.  
  349. <form id="feed_form" method="post"> 
  350. <?php wp_nonce_field('list_action', 'gf_exacttarget_list') ?> 
  351. <input type="hidden" id="action" name="action"/> 
  352. <input type="hidden" id="action_argument" name="action_argument"/> 
  353.  
  354. <div class="tablenav"> 
  355. <div class="alignleft actions" style="padding:8px 0 7px; 0"> 
  356. <label class="hidden" for="bulk_action"><?php _e("Bulk action", "gravity-forms-exacttarget") ?></label> 
  357. <select name="bulk_action" id="bulk_action"> 
  358. <option value=''> <?php _e("Bulk action", "gravity-forms-exacttarget") ?> </option> 
  359. <option value='delete'><?php _e("Delete", "gravity-forms-exacttarget") ?></option> 
  360. </select> 
  361. <?php 
  362. echo '<input type="submit" class="button" value="' . __("Apply", "gravity-forms-exacttarget") . '" onclick="if( jQuery(\'#bulk_action\').val() == \'delete\' && !confirm(\'' . __("Delete selected feeds? ", "gravity-forms-exacttarget") . __("\'Cancel\' to stop, \'OK\' to delete.", "gravity-forms-exacttarget") .'\')) { return false; } return true;"/>'; 
  363. ?> 
  364. </div> 
  365. </div> 
  366. <table class="widefat fixed" cellspacing="0"> 
  367. <thead> 
  368. <tr> 
  369. <th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox" /></th> 
  370. <th scope="col" id="active" class="manage-column check-column"></th> 
  371. <th scope="col" class="manage-column"><?php _e("Form", "gravity-forms-exacttarget") ?></th> 
  372. <th scope="col" class="manage-column"><?php _e("ExactTarget Lists", "gravity-forms-exacttarget") ?></th> 
  373. </tr> 
  374. </thead> 
  375.  
  376. <tfoot> 
  377. <tr> 
  378. <th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox" /></th> 
  379. <th scope="col" id="active" class="manage-column check-column"></th> 
  380. <th scope="col" class="manage-column"><?php _e("Form", "gravity-forms-exacttarget") ?></th> 
  381. <th scope="col" class="manage-column"><?php _e("ExactTarget Lists", "gravity-forms-exacttarget") ?></th> 
  382. </tr> 
  383. </tfoot> 
  384.  
  385. <tbody class="list:user user-list"> 
  386. <?php 
  387.  
  388. $settings = GFExactTargetData::get_feeds(); 
  389. if(is_array($settings) && !empty($settings)) { 
  390. foreach($settings as $setting) { 
  391. ?> 
  392. <tr class='author-self status-inherit' valign="top"> 
  393. <th scope="row" class="check-column"><input type="checkbox" name="feed[]" value="<?php echo $setting["id"] ?>"/></th> 
  394. <td><img src="<?php echo self::get_base_url() ?>/images/active<?php echo intval($setting["is_active"]) ?>.png" alt="<?php echo $setting["is_active"] ? __("Active", "gravity-forms-exacttarget") : __("Inactive", "gravity-forms-exacttarget");?>" title="<?php echo $setting["is_active"] ? __("Active", "gravity-forms-exacttarget") : __("Inactive", "gravity-forms-exacttarget");?>" onclick="ToggleActive(this, <?php echo $setting['id'] ?>); " /></td> 
  395. <td class="column-title"> 
  396. <a href="admin.php?page=gf_exacttarget&view=edit&id=<?php echo $setting["id"] ?>" title="<?php _e("Edit", "gravity-forms-exacttarget") ?>"><?php echo $setting["form_title"] ?></a> 
  397. <div class="row-actions"> 
  398. <span class="edit"> 
  399. <a title="Edit this setting" href="admin.php?page=gf_exacttarget&view=edit&id=<?php echo $setting["id"] ?>" title="<?php _e("Edit", "gravity-forms-exacttarget") ?>"><?php _e("Edit", "gravity-forms-exacttarget") ?></a> 
  400. </span> 
  401.  
  402. <span class="edit"> 
  403. <a title="<?php _e("Delete", "gravity-forms-exacttarget") ?>" href="javascript: if(confirm('<?php _e("Delete this feed? ", "gravity-forms-exacttarget") ?> <?php _e("\'Cancel\' to stop, \'OK\' to delete.", "gravity-forms-exacttarget") ?>')) { DeleteSetting(<?php echo $setting["id"] ?>);}"><?php _e("Delete", "gravity-forms-exacttarget")?></a> 
  404.  
  405. </span> 
  406. </div> 
  407. </td> 
  408. <td class="column-date"><ul class="ul-disc"><li><?php echo implode('</li><li>', explode(', ', $setting["meta"]["contact_list_name"])) ?></li></ul></td> 
  409. </tr> 
  410. <?php 
  411. else { 
  412. $api = self::get_api(); 
  413. if(!empty($api) && empty($api->lastError)) { 
  414. ?> 
  415. <tr> 
  416. <td colspan="4" style="padding:20px;"> 
  417. <?php _e(sprintf("You don't have any ExactTarget feeds configured. Let's go %screate one%s!", '<a href="'.admin_url('admin.php?page=gf_exacttarget&view=edit&id=0').'">', "</a>"), "gravity-forms-exacttarget"); ?> 
  418. </td> 
  419. </tr> 
  420. <?php 
  421. else{ 
  422. ?> 
  423. <tr> 
  424. <td colspan="4" style="padding:20px;"> 
  425. <?php _e(sprintf("To get started, please configure your %sExactTarget Settings%s.", '<a href="admin.php?page=gf_settings&addon=ExactTarget">', "</a>"), "gravity-forms-exacttarget"); ?> 
  426. </td> 
  427. </tr> 
  428. <?php 
  429. ?> 
  430. </tbody> 
  431. </table> 
  432. </form> 
  433. </div> 
  434. <script type="text/javascript"> 
  435. function DeleteSetting(id) { 
  436. jQuery("#action_argument").val(id); 
  437. jQuery("#action").val("delete"); 
  438. jQuery("#feed_form")[0].submit(); 
  439. function ToggleActive(img, feed_id) { 
  440. var is_active = img.src.indexOf("active1.png") >=0 
  441. if(is_active) { 
  442. img.src = img.src.replace("active1.png", "active0.png"); 
  443. jQuery(img).attr('title', '<?php _e("Inactive", "gravity-forms-exacttarget") ?>').attr('alt', '<?php _e("Inactive", "gravity-forms-exacttarget") ?>'); 
  444. else{ 
  445. img.src = img.src.replace("active0.png", "active1.png"); 
  446. jQuery(img).attr('title', '<?php _e("Active", "gravity-forms-exacttarget") ?>').attr('alt', '<?php _e("Active", "gravity-forms-exacttarget") ?>'); 
  447.  
  448. var mysack = new sack("<?php echo admin_url("admin-ajax.php")?>" ); 
  449. mysack.execute = 1; 
  450. mysack.method = 'POST'; 
  451. mysack.setVar( "action", "rg_update_feed_active" ); 
  452. mysack.setVar( "rg_update_feed_active", "<?php echo wp_create_nonce("rg_update_feed_active") ?>" ); 
  453. mysack.setVar( "feed_id", feed_id ); 
  454. mysack.setVar( "is_active", is_active ? 0 : 1 ); 
  455. mysack.encVar( "cookie", document.cookie, false ); 
  456. mysack.onError = function() { alert('<?php _e("Ajax error while updating feed", "gravity-forms-exacttarget" ) ?>' )}; 
  457. mysack.runAJAX(); 
  458.  
  459. return true; 
  460. </script> 
  461. <?php 
  462.  
  463. public static function get_api() { 
  464. if(!class_exists("ExactTarget")) 
  465. require_once("api/ExactTarget.class.php"); 
  466.  
  467. return new ExactTarget(); 
  468.  
  469. private static function edit_page() { 
  470. ?> 
  471. <style type="text/css"> 
  472. label span.howto { cursor: default; } 
  473. .exacttarget_col_heading{padding-bottom:2px; border-bottom: 1px solid #ccc; font-weight:bold;} 
  474. .exacttarget_field_cell {padding: 6px 17px 0 0; margin-right:15px;} 
  475. .gfield_required{color:red;} 
  476.  
  477. .feeds_validation_error{ background-color:#FFDFDF;} 
  478. .feeds_validation_error td{ margin-top:4px; margin-bottom:6px; padding-top:6px; padding-bottom:6px; border-top:1px dotted #C89797; border-bottom:1px dotted #C89797} 
  479.  
  480. .left_header{float:left; width:200px; padding-right: 20px;} 
  481. .margin_vertical_10{margin: 20px 0;} 
  482. #gf_exacttarget_list_list { margin-left:220px; padding-top: 1px } 
  483. #exacttarget_doubleoptin_warning{padding-left: 5px; padding-bottom:4px; font-size: 10px;} 
  484. </style> 
  485. <script type="text/javascript"> 
  486. var form = Array(); 
  487. </script> 
  488. <div class="wrap"> 
  489. <img alt="<?php _e("ExactTarget", "gravity-forms-exacttarget") ?>" style="margin: .75em 7px 0pt 0pt; float: left;" src="<?php echo self::get_base_url() ?>/images/exacttarget_wordpress_icon_32.png"/> 
  490. <h2><?php _e("ExactTarget Feed", "gravity-forms-exacttarget") ?></h2> 
  491.  
  492. <?php 
  493. //getting ExactTarget API 
  494. $api = self::get_api(); 
  495.  
  496. //ensures valid credentials were entered in the settings page 
  497. if(!empty($api->lastError)) { 
  498. ?> 
  499. <div class="error" id="message" style="margin-top:20px;"><?php echo wpautop(sprintf(__("We are unable to login to ExactTarget with the provided username and API key. Please make sure they are valid in the %sSettings Page%s", "gravity-forms-exacttarget"), "<a href='?page=gf_settings&addon=ExactTarget'>", "</a>")); ?></div> 
  500. <?php 
  501. return; 
  502.  
  503. //getting setting id (0 when creating a new one) 
  504. $id = !empty($_POST["exacttarget_setting_id"]) ? $_POST["exacttarget_setting_id"] : absint($_GET["id"]); 
  505. $config = empty($id) ? array("meta" => array(), "is_active" => true) : GFExactTargetData::get_feed($id); 
  506.  
  507.  
  508. //getting merge vars 
  509. $merge_vars = array(); 
  510.  
  511. //updating meta information 
  512. if(isset($_POST["gf_exacttarget_submit"])) { 
  513. $list_ids = $list_names = array(); 
  514. foreach($_POST["gf_exacttarget_list"] as $list) { 
  515. list($list_id, $list_name) = explode("|:|", stripslashes($list)); 
  516. $list_ids[] = $list_id; 
  517. $list_names[] = $list_name; 
  518. # print_r(array('id' => $list_ids, 'name' => $list_names)); 
  519. $config["meta"]["contact_list_id"] = empty($list_ids) ? 0 : implode(', ', $list_ids); 
  520. $config["meta"]["contact_list_name"] = implode(', ', $list_names); 
  521. $config["form_id"] = absint($_POST["gf_exacttarget_form"]); 
  522.  
  523. $is_valid = true; 
  524. $merge_vars = $api->Attributes($config["meta"]["contact_list_id"]); 
  525.  
  526. $field_map = array(); 
  527. foreach($merge_vars as $key => $var) { 
  528. $field_name = "exacttarget_map_field_" . $key; 
  529. $mapped_field = isset($_POST[$field_name]) ? stripslashes($_POST[$field_name]) : ''; 
  530. if(!empty($mapped_field)) { 
  531. $field_map[$key] = $mapped_field; 
  532. else{ 
  533. unset($field_map[$key]); 
  534. if($var["required"] == "True") 
  535. $is_valid = false; 
  536. unset($_POST["{$field_name}"]); 
  537.  
  538. // Go through the items that were not in the field map; 
  539. // the Custom Fields 
  540. foreach($_POST as $k => $v) { 
  541. if(preg_match('/exacttarget\_map\_field\_/', $k)) { 
  542. $tag = str_replace('exacttarget_map_field_', '', $k); 
  543. $field_map[$tag] = stripslashes($_POST[$k]); 
  544.  
  545. $config["meta"]["field_map"] = $field_map; 
  546. #$config["meta"]["double_optin"] = !empty($_POST["exacttarget_double_optin"]) ? true : false; 
  547. #$config["meta"]["welcome_email"] = !empty($_POST["exacttarget_welcome_email"]) ? true : false; 
  548.  
  549. $config["meta"]["optin_enabled"] = !empty($_POST["exacttarget_optin_enable"]) ? true : false; 
  550. $config["meta"]["optin_field_id"] = $config["meta"]["optin_enabled"] ? isset($_POST["exacttarget_optin_field_id"]) ? $_POST["exacttarget_optin_field_id"] : '' : ""; 
  551. $config["meta"]["optin_operator"] = $config["meta"]["optin_enabled"] ? isset($_POST["exacttarget_optin_operator"]) ? $_POST["exacttarget_optin_operator"] : '' : ""; 
  552. $config["meta"]["optin_value"] = $config["meta"]["optin_enabled"] ? $_POST["exacttarget_optin_value"] : ""; 
  553.  
  554.  
  555.  
  556. if($is_valid) { 
  557. $id = GFExactTargetData::update_feed($id, $config["form_id"], $config["is_active"], $config["meta"]); 
  558. ?> 
  559. <div id="message" class="updated fade" style="margin-top:10px;"><p><?php echo sprintf(__("Feed Updated. %sback to list%s", "gravity-forms-exacttarget"), "<a href='?page=gf_exacttarget'>", "</a>") ?></p> 
  560. <input type="hidden" name="exacttarget_setting_id" value="<?php echo $id ?>"/> 
  561. </div> 
  562. <?php 
  563. else{ 
  564. ?> 
  565. <div class="error" style="padding:6px"><?php echo __("Feed could not be updated. Please enter all required information below.", "gravity-forms-exacttarget") ?></div> 
  566. <?php 
  567. if(!function_exists('gform_tooltip')) { 
  568. require_once(GFCommon::get_base_path() . "/tooltips.php"); 
  569.  
  570. ?> 
  571. <form method="post" action="<?php echo remove_query_arg('refresh'); ?>"> 
  572. <input type="hidden" name="exacttarget_setting_id" value="<?php echo $id ?>"/> 
  573. <div class="margin_vertical_10"> 
  574. <h2><?php _e('1. Select the lists to merge with.', "gravity-forms-exacttarget"); ?></h2> 
  575. <label for="gf_exacttarget_list" class="left_header"><?php _e("ExactTarget List", "gravity-forms-exacttarget"); ?> <?php gform_tooltip("exacttarget_contact_list") ?> <span class="howto"><?php _e(sprintf("%sRefresh lists%s", '<a href="'.add_query_arg('refresh', 'lists').'">', '</a>'), "gravity-forms-exacttarget"); ?></span></label> 
  576.  
  577. <?php 
  578. $trans = get_transient('extr_lists'); 
  579. if(!isset($_POST["gf_exacttarget_submit"]) && (!$trans || ($trans && isset($_REQUEST['refresh']) && $_REQUEST['refresh'] === 'lists'))) { ?> 
  580. <p class="lists_loading hide-if-no-js" style='padding:5px;'><img src="<?php echo GFExactTarget::get_base_url() ?>/images/loading.gif" id="exacttarget_wait" style="padding-right:5px;" width="16" height="16" /> <?php _e('Lists are being loaded', 'gravity-forms-exacttarget'); ?></p> 
  581. <?php 
  582.  
  583. //getting all contact lists 
  584. $lists = $api->Lists(); 
  585.  
  586. if (!$lists) { 
  587. echo __("Could not load ExactTarget contact lists. <br/>Error: ", "gravity-forms-exacttarget"); 
  588. echo isset($api->errorMessage) ? $api->errorMessage : ''; 
  589. else{ 
  590. if(isset($config["meta"]["contact_list_id"])) { 
  591. $contact_lists = explode(', ' , $config["meta"]["contact_list_id"]); 
  592. } else { 
  593. $contact_lists = array(); 
  594. ?> 
  595. <?php 
  596. if(!get_transient('extr_lists_all')) { 
  597. echo sprintf('%sYour list size is large; only list ID\'s are shown. %sRetrieve List Names%s%s', '<p>', '<a href="'.add_query_arg(array('retrieveListNames' =>true, '_wpnonce' => wp_create_nonce('retrieveListNames')), admin_url('admin.php?page=gf_settings&addon=ExactTarget')).'" class="button-secondary button">', '</a>', '</p>'); 
  598. ?> 
  599. <ul id="gf_exacttarget_list_list" class="hide-if-js"> 
  600. <?php 
  601. foreach ($lists as $key => $list) { 
  602. $selected = in_array($key, $contact_lists) ? "checked='checked'" : ""; 
  603. ?> 
  604. <li><label style="display:block;" for="gf_exacttarget_list_<?php echo esc_html($key); ?>"><input type="checkbox" name="gf_exacttarget_list[]" id="gf_exacttarget_list_<?php echo esc_html($key); ?>" value="<?php echo esc_html($key) . "|:|" . esc_html($list['list_name']) ?>" <?php echo $selected ?> /> <?php echo esc_html($list['list_name']) ?></label></li> 
  605. <?php 
  606. ?> 
  607. </ul> 
  608. <script type="text/javascript"> 
  609. if(jQuery('.lists_loading').length && jQuery('#gf_exacttarget_list_list').length) { 
  610. jQuery('.lists_loading').fadeOut(function() { jQuery('#gf_exacttarget_list_list').fadeIn(); }); 
  611. } else if(jQuery('#gf_exacttarget_list_list').length) { 
  612. jQuery('#gf_exacttarget_list_list').show(); 
  613. </script> 
  614. <?php 
  615. ?> 
  616. <div class="clear"></div> 
  617. </div> 
  618. <?php flush(); ?> 
  619. <div id="exacttarget_form_container" valign="top" class="margin_vertical_10" <?php echo empty($config["meta"]["contact_list_id"]) ? "style='display:none;'" : "" ?>> 
  620. <h2><?php _e('2. Select the form to tap into.', "gravity-forms-exacttarget"); ?></h2> 
  621. <label for="gf_exacttarget_form" class="left_header"><?php _e("Gravity Form", "gravity-forms-exacttarget"); ?> <?php gform_tooltip("exacttarget_gravity_form") ?></label> 
  622.  
  623. <select id="gf_exacttarget_form" name="gf_exacttarget_form" onchange="SelectForm(jQuery('#gf_exacttarget_list_list input').serialize(), jQuery(this).val());"> 
  624. <option value=""><?php _e("Select a form", "gravity-forms-exacttarget"); ?> </option> 
  625. <?php 
  626. $forms = RGFormsModel::get_forms(); 
  627. foreach($forms as $form) { 
  628. $selected = absint($form->id) == $config["form_id"] ? "selected='selected'" : ""; 
  629. ?> 
  630. <option value="<?php echo absint($form->id) ?>" <?php echo $selected ?>><?php echo esc_html($form->title) ?></option> 
  631. <?php 
  632. ?> 
  633. </select> 
  634.    
  635. <img src="<?php echo GFExactTarget::get_base_url() ?>/images/loading.gif" id="exacttarget_wait" style="display: none;"/> 
  636. </div> 
  637. <div class="clear"></div> 
  638. <div id="exacttarget_field_group" valign="top" <?php echo empty($config["meta"]["contact_list_id"]) || empty($config["form_id"]) ? "style='display:none;'" : "" ?>> 
  639. <div id="exacttarget_field_container" valign="top" class="margin_vertical_10" > 
  640. <h2><?php _e('3. Map form fields to ExactTarget attributes.', "gravity-forms-exacttarget"); ?></h2> 
  641. <p class="description" style="margin-bottom:1em;"><?php _e(sprintf('If you don’t see an attribute listed, you need to create it in ExactTarget first under %sSubscribers > Profile Management%s.%sOnly mapped fields will be added to ExactTarget.', '<em style="font-style:normal;">', '</em>', '<br />'), "gravity-forms-exacttarget"); ?></p> 
  642. <label for="exacttarget_fields" class="left_header"><?php _e("Map Fields", "gravity-forms-exacttarget"); ?> <?php gform_tooltip("exacttarget_map_fields") ?> <span class="howto"><?php _e(sprintf("%sRefresh fields%s", '<a href="'.add_query_arg('refresh', 'attributes').'">', '</a>'), "gravity-forms-exacttarget"); ?></span></label> 
  643.  
  644. <div id="exacttarget_field_list"> 
  645. <?php 
  646. if(!empty($config["form_id"])) { 
  647.  
  648. //getting list of all ExactTarget merge variables for the selected contact list 
  649. if(empty($merge_vars)) 
  650. $merge_vars = $api->Attributes(); 
  651.  
  652. //getting field map UI 
  653. echo self::get_field_mapping($config, $config["form_id"], $merge_vars); 
  654.  
  655. //getting list of selection fields to be used by the optin 
  656. $form_meta = RGFormsModel::get_form_meta($config["form_id"]); 
  657. $selection_fields = GFCommon::get_selection_fields($form_meta, $config["meta"]["optin_field_id"]); 
  658. ?> 
  659. </div> 
  660. <div class="clear"></div> 
  661. </div> 
  662.  
  663. <div id="exacttarget_optin_container" valign="top" class="margin_vertical_10"> 
  664. <label for="exacttarget_optin" class="left_header"><?php _e("Opt-In Condition", "gravity-forms-exacttarget"); ?> <?php gform_tooltip("exacttarget_optin_condition") ?></label> 
  665. <div id="exacttarget_optin"> 
  666. <table> 
  667. <tr> 
  668. <td> 
  669. <input type="checkbox" id="exacttarget_optin_enable" name="exacttarget_optin_enable" value="1" onclick="if(this.checked) {jQuery('#exacttarget_optin_condition_field_container').show('slow');} else{jQuery('#exacttarget_optin_condition_field_container').hide('slow');}" <?php echo !empty($config["meta"]["optin_enabled"]) ? "checked='checked'" : ""?>/> 
  670. <label for="exacttarget_optin_enable"><?php _e("Enable", "gravity-forms-exacttarget"); ?></label> 
  671. </td> 
  672. </tr> 
  673. <tr> 
  674. <td> 
  675. <div id="exacttarget_optin_condition_field_container" <?php echo empty($config["meta"]["optin_enabled"]) ? "style='display:none'" : ""?>> 
  676. <div id="exacttarget_optin_condition_fields" <?php echo empty($selection_fields) ? "style='display:none'" : ""?>> 
  677. <?php _e("Export to ExactTarget if ", "gravity-forms-exacttarget") ?> 
  678.  
  679. <select id="exacttarget_optin_field_id" name="exacttarget_optin_field_id" class='optin_select' onchange='jQuery("#exacttarget_optin_value").html(GetFieldValues(jQuery(this).val(), "", 20));'><?php echo $selection_fields ?></select> 
  680. <select id="exacttarget_optin_operator" name="exacttarget_optin_operator" /> 
  681. <option value="is" <?php echo (isset($config["meta"]["optin_operator"]) && $config["meta"]["optin_operator"] == "is") ? "selected='selected'" : "" ?>><?php _e("is", "gravity-forms-exacttarget") ?></option> 
  682. <option value="isnot" <?php echo (isset($config["meta"]["optin_operator"]) && $config["meta"]["optin_operator"] == "isnot") ? "selected='selected'" : "" ?>><?php _e("is not", "gravity-forms-exacttarget") ?></option> 
  683. </select> 
  684. <select id="exacttarget_optin_value" name="exacttarget_optin_value" class='optin_select'> 
  685. </select> 
  686.  
  687. </div> 
  688. <div id="exacttarget_optin_condition_message" <?php echo !empty($selection_fields) ? "style='display:none'" : ""?>> 
  689. <?php _e("To create an Opt-In condition, your form must have a drop down, checkbox or multiple choice field.", "gravityform") ?> 
  690. </div> 
  691. </div> 
  692. </td> 
  693. </tr> 
  694. </table> 
  695. </div> 
  696.  
  697. <script type="text/javascript"> 
  698. <?php 
  699. if(!empty($config["form_id"])) { 
  700. ?> 
  701. //creating Javascript form object 
  702. form = <?php echo GFCommon::json_encode($form_meta)?> ; 
  703.  
  704. //initializing drop downs 
  705. jQuery(document).ready(function() { 
  706. var selectedField = "<?php echo str_replace('"', '\"', $config["meta"]["optin_field_id"])?>"; 
  707. var selectedValue = "<?php echo str_replace('"', '\"', $config["meta"]["optin_value"])?>"; 
  708. SetOptin(selectedField, selectedValue); 
  709. }); 
  710. <?php 
  711. ?> 
  712. </script> 
  713. </div> 
  714.  
  715. <div id="exacttarget_submit_container" class="margin_vertical_10"> 
  716. <input type="submit" name="gf_exacttarget_submit" value="<?php echo empty($id) ? __("Save Feed", "gravity-forms-exacttarget") : __("Update Feed", "gravity-forms-exacttarget"); ?>" class="button-primary"/> 
  717. </div> 
  718. </div> 
  719. </form> 
  720. </div> 
  721.  
  722. <script type="text/javascript"> 
  723. jQuery(document).ready(function($) { 
  724. $('#gf_exacttarget_list_list').live('load change', function() { 
  725. $('.lists_loading').hide(); 
  726. }); 
  727. $("#gf_exacttarget_list_list input").bind('click change', function() { 
  728. if($("#gf_exacttarget_list_list input:checked").length > 0) { 
  729. SelectList(1); 
  730. } else { 
  731. SelectList(false); 
  732. jQuery("#gf_exacttarget_form").val(""); 
  733. }); 
  734.  
  735. <?php if(isset($_REQUEST['id'])) { ?> 
  736. $('#exacttarget_field_list').live('load', function() { 
  737. $('.exacttarget_field_cell select').each(function() { 
  738. var $select = $(this); 
  739. if($().prop) { 
  740. var label = $.trim($('label[for='+$(this).prop('name')+']').text()); 
  741. } else { 
  742. var label = $.trim($('label[for='+$(this).attr('name')+']').text()); 
  743. label = label.replace(' *', ''); 
  744.  
  745. if($select.val() === '') { 
  746. $('option', $select).each(function() { 
  747. if($(this).text() === label) { 
  748. if($().prop) { 
  749. $('option:contains('+label+')', $select).prop('selected', true); 
  750. } else { 
  751. $('option:contains('+label+')', $select).prop('selected', true); 
  752. }); 
  753. }); 
  754. }); 
  755. <?php } ?> 
  756. }); 
  757. </script> 
  758. <script type="text/javascript"> 
  759.  
  760. function SelectList(listId) { 
  761. if(listId) { 
  762. jQuery("#exacttarget_form_container").slideDown(); 
  763. // jQuery("#gf_exacttarget_form").val(""); 
  764. else{ 
  765. jQuery("#exacttarget_form_container").slideUp(); 
  766. EndSelectForm(""); 
  767.  
  768. function SelectForm(listId, formId) { 
  769. if(!formId) { 
  770. jQuery("#exacttarget_field_group").slideUp(); 
  771. return; 
  772.  
  773. jQuery("#exacttarget_wait").show(); 
  774. jQuery("#exacttarget_field_group").slideUp(); 
  775.  
  776. var mysack = new sack("<?php bloginfo( 'wpurl' ); ?>/wp-admin/admin-ajax.php" ); 
  777. mysack.execute = 1; 
  778. mysack.method = 'POST'; 
  779. mysack.setVar( "action", "gf_select_exacttarget_form" ); 
  780. mysack.setVar( "gf_select_exacttarget_form", "<?php echo wp_create_nonce("gf_select_exacttarget_form") ?>" ); 
  781. mysack.setVar( "list_ids", listId); 
  782. mysack.setVar( "form_id", formId); 
  783. mysack.encVar( "cookie", document.cookie, false ); 
  784. mysack.onError = function() {jQuery("#exacttarget_wait").hide(); alert('<?php _e("Ajax error while selecting a form", "gravity-forms-exacttarget") ?>' )}; 
  785. mysack.runAJAX(); 
  786. return true; 
  787.  
  788. function SetOptin(selectedField, selectedValue) { 
  789.  
  790. //load form fields 
  791. jQuery("#exacttarget_optin_field_id").html(GetSelectableFields(selectedField, 20)); 
  792. var optinConditionField = jQuery("#exacttarget_optin_field_id").val(); 
  793.  
  794. if(optinConditionField) { 
  795. jQuery("#exacttarget_optin_condition_message").hide(); 
  796. jQuery("#exacttarget_optin_condition_fields").show(); 
  797. jQuery("#exacttarget_optin_value").html(GetFieldValues(optinConditionField, selectedValue, 20)); 
  798. else{ 
  799. jQuery("#exacttarget_optin_condition_message").show(); 
  800. jQuery("#exacttarget_optin_condition_fields").hide(); 
  801.  
  802. function EndSelectForm(fieldList, form_meta) { 
  803. //setting global form object 
  804. form = form_meta; 
  805.  
  806. if(fieldList) { 
  807.  
  808. SetOptin("", ""); 
  809.  
  810. jQuery("#exacttarget_field_list").html(fieldList); 
  811. jQuery("#exacttarget_field_group").slideDown(); 
  812. jQuery('#exacttarget_field_list').trigger('load'); 
  813. else{ 
  814. jQuery("#exacttarget_field_group").slideUp(); 
  815. jQuery("#exacttarget_field_list").html(""); 
  816. jQuery("#exacttarget_wait").hide(); 
  817.  
  818. function GetFieldValues(fieldId, selectedValue, labelMaxCharacters) { 
  819. if(!fieldId) 
  820. return ""; 
  821.  
  822. var str = ""; 
  823. var field = GetFieldById(fieldId); 
  824. if(!field || !field.choices) 
  825. return ""; 
  826.  
  827. var isAnySelected = false; 
  828.  
  829. for(var i=0; i<field.choices.length; i++) { 
  830. var fieldValue = field.choices[i].value ? field.choices[i].value : field.choices[i].text; 
  831. var isSelected = fieldValue == selectedValue; 
  832. var selected = isSelected ? "selected='selected'" : ""; 
  833. if(isSelected) 
  834. isAnySelected = true; 
  835.  
  836. str += "<option value='" + fieldValue.replace("'", "'") + "' " + selected + ">" + TruncateMiddle(field.choices[i].text, labelMaxCharacters) + "</option>"; 
  837.  
  838. if(!isAnySelected && selectedValue) { 
  839. str += "<option value='" + selectedValue.replace("'", "'") + "' selected='selected'>" + TruncateMiddle(selectedValue, labelMaxCharacters) + "</option>"; 
  840.  
  841. return str; 
  842.  
  843. function GetFieldById(fieldId) { 
  844. for(var i=0; i<form.fields.length; i++) { 
  845. if(form.fields[i].id == fieldId) 
  846. return form.fields[i]; 
  847. return null; 
  848.  
  849. function TruncateMiddle(text, maxCharacters) { 
  850. if(text.length <= maxCharacters) 
  851. return text; 
  852. var middle = parseInt(maxCharacters / 2); 
  853. return text.substr(0, middle) + "..." + text.substr(text.length - middle, middle); 
  854.  
  855. function GetSelectableFields(selectedFieldId, labelMaxCharacters) { 
  856. var str = ""; 
  857. var inputType; 
  858. for(var i=0; i<form.fields.length; i++) { 
  859. fieldLabel = form.fields[i].adminLabel ? form.fields[i].adminLabel : form.fields[i].label; 
  860. inputType = form.fields[i].inputType ? form.fields[i].inputType : form.fields[i].type; 
  861. if(inputType == "checkbox" || inputType == "radio" || inputType == "select") { 
  862. var selected = form.fields[i].id == selectedFieldId ? "selected='selected'" : ""; 
  863. str += "<option value='" + form.fields[i].id + "' " + selected + ">" + TruncateMiddle(fieldLabel, labelMaxCharacters) + "</option>"; 
  864. return str; 
  865.  
  866. </script> 
  867.  
  868. <?php 
  869.  
  870.  
  871. public static function add_permissions() { 
  872. global $wp_roles; 
  873. $wp_roles->add_cap("administrator", "gravityforms_exacttarget"); 
  874. $wp_roles->add_cap("administrator", "gravityforms_exacttarget_uninstall"); 
  875.  
  876. //Target of Member plugin filter. Provides the plugin with Gravity Forms lists of capabilities 
  877. public static function members_get_capabilities( $caps ) { 
  878. return array_merge($caps, array("gravityforms_exacttarget", "gravityforms_exacttarget_uninstall")); 
  879.  
  880. public static function disable_exacttarget() { 
  881. delete_option("gf_exacttarget_settings"); 
  882.  
  883. public static function select_exacttarget_form() { 
  884. check_ajax_referer("gf_select_exacttarget_form", "gf_select_exacttarget_form"); 
  885.  
  886. $api = self::get_api(); 
  887.  
  888. if(!empty($api->lastError) || !isset($_POST["list_ids"])) { 
  889. die("EndSelectForm();"); 
  890.  
  891. parse_str($_POST["list_ids"], $lists); 
  892.  
  893. $form_id = intval($_POST["form_id"]); 
  894.  
  895. $setting_id = 0; 
  896.  
  897. //getting list of all ExactTarget merge variables for the selected contact list 
  898. $merge_vars = $api->Attributes(); 
  899.  
  900. //getting configuration 
  901. $config = GFExactTargetData::get_feed($setting_id); 
  902.  
  903. //getting field map UI 
  904. $str = self::get_field_mapping($config, $form_id, $merge_vars); 
  905.  
  906. //fields meta 
  907. $form = RGFormsModel::get_form_meta($form_id); 
  908. //$fields = $form["fields"]; 
  909. die("EndSelectForm('" . str_replace("'", "\'", $str) . "', " . GFCommon::json_encode($form) . ");"); 
  910.  
  911. private static function get_field_mapping($config, $form_id, $merge_vars) { 
  912.  
  913. //getting list of all fields for the selected form 
  914. $form_fields = self::get_form_fields($form_id); 
  915. $form = RGFormsModel::get_form_meta($form_id); 
  916.  
  917. $usedFields = $customFields = array(); 
  918.  
  919. $str = ''; 
  920.  
  921. $str .= "<table cellpadding='0' cellspacing='0'><tr><td class='exacttarget_col_heading'>" . __("Attribute", "gravity-forms-exacttarget") . "</td><td class='exacttarget_col_heading'>" . __("Form Fields", "gravity-forms-exacttarget") . "</td></tr>"; 
  922.  
  923. foreach($merge_vars as $key => $var) { 
  924. $selected_field = (isset($config["meta"]) && isset($config["meta"]["field_map"]) && isset($config["meta"]["field_map"][$key])) ? $config["meta"]["field_map"][$key] : ''; 
  925. $required = $var["required"] == "True" ? "<span class='gfield_required'>*</span>" : ""; 
  926. $error_class = $var["required"] == "True" && empty($selected_field) && !empty($_POST["gf_exacttarget_submit"]) ? " feeds_validation_error" : ""; 
  927. $str .= "<tr class='$error_class'><td class='exacttarget_field_cell'><label for='exacttarget_map_field_".$key."'>" . $var["name"] . " $required</label></td><td class='exacttarget_field_cell'>" . self::get_mapped_field_list($key, $selected_field, $form_fields) . "</td></tr>"; 
  928. $str .= "</table>"; 
  929.  
  930. return $str; 
  931.  
  932. private function getNewTag($tag, $used = array()) { 
  933. if(isset($used[$tag])) { 
  934. $i = 1; 
  935. while($i < 1000) { 
  936. if(!isset($used[$tag.'_'.$i])) { 
  937. return $tag.'_'.$i; 
  938. $i++; 
  939. return $tag; 
  940.  
  941. public static function get_form_fields($form_id) { 
  942. $form = RGFormsModel::get_form_meta($form_id); 
  943. $fields = array(); 
  944.  
  945. //Adding default fields 
  946. array_push($form["fields"], array("id" => "date_created" , "label" => __("Entry Date", "gravity-forms-exacttarget"))); 
  947. array_push($form["fields"], array("id" => "ip" , "label" => __("User IP", "gravity-forms-exacttarget"))); 
  948. array_push($form["fields"], array("id" => "source_url" , "label" => __("Source Url", "gravity-forms-exacttarget"))); 
  949.  
  950. if(is_array($form["fields"])) { 
  951. foreach($form["fields"] as $field) { 
  952. if(isset($field["inputs"]) && is_array($field["inputs"]) && $field['type'] !== 'checkbox' && $field['type'] !== 'select') { 
  953.  
  954. //If this is an address field, add full name to the list 
  955. if(RGFormsModel::get_input_type($field) == "address") 
  956. $fields[] = array($field["id"], GFCommon::get_label($field) . " (" . __("Full" , "gravity-forms-exacttarget") . ")"); 
  957.  
  958. foreach($field["inputs"] as $input) 
  959. $fields[] = array($input["id"], GFCommon::get_label($field, $input["id"])); 
  960. else if(empty($field["displayOnly"])) { 
  961. $fields[] = array($field["id"], GFCommon::get_label($field)); 
  962. return $fields; 
  963.  
  964. private static function get_address($entry, $field_id) { 
  965. $street_value = str_replace(" ", " ", trim($entry[$field_id . ".1"])); 
  966. $street2_value = str_replace(" ", " ", trim($entry[$field_id . ".2"])); 
  967. $city_value = str_replace(" ", " ", trim($entry[$field_id . ".3"])); 
  968. $state_value = str_replace(" ", " ", trim($entry[$field_id . ".4"])); 
  969. $zip_value = trim($entry[$field_id . ".5"]); 
  970. $country_value = GFCommon::get_country_code(trim($entry[$field_id . ".6"])); 
  971.  
  972. $address = $street_value; 
  973. $address .= !empty($address) && !empty($street2_value) ? " $street2_value" : $street2_value; 
  974. $address .= !empty($address) && (!empty($city_value) || !empty($state_value)) ? " $city_value" : $city_value; 
  975. $address .= !empty($address) && !empty($city_value) && !empty($state_value) ? " $state_value" : $state_value; 
  976. $address .= !empty($address) && !empty($zip_value) ? " $zip_value" : $zip_value; 
  977. $address .= !empty($address) && !empty($country_value) ? " $country_value" : $country_value; 
  978.  
  979. return $address; 
  980.  
  981. public static function get_mapped_field_list($variable_name, $selected_field, $fields) { 
  982. $field_name = "exacttarget_map_field_" . $variable_name; 
  983. $str = "<select name='$field_name' id='$field_name'><option value=''>" . __("", "gravity-forms-exacttarget") . "</option>"; 
  984. foreach($fields as $field) { 
  985. $field_id = $field[0]; 
  986. $field_label = $field[1]; 
  987.  
  988. $selected = $field_id == $selected_field ? "selected='selected'" : ""; 
  989. $str .= "<option value='" . $field_id . "' ". $selected . ">" . $field_label . "</option>"; 
  990. $str .= "</select>"; 
  991. return $str; 
  992.  
  993. public static function get_mapped_field_checkbox($variable_name, $selected_field, $field) { 
  994. $field_name = "exacttarget_map_field_" . $variable_name; 
  995. $field_id = $field[0]; 
  996. $str = "<input name='$field_name' id='$field_name' type='checkbox' value='$field_id'"; 
  997. $selected = $field_id == $selected_field ? " checked='checked'" : false; 
  998. if($selected) { 
  999. $str .= $selected; 
  1000.  
  1001. $str .= " />"; 
  1002. return $str; 
  1003.  
  1004. public static function export($entry, $form) { 
  1005. //Login to ExactTarget 
  1006. $api = self::get_api(); 
  1007. if(!empty($api->lastError)) 
  1008. return; 
  1009.  
  1010. //loading data class 
  1011. require_once(self::get_base_path() . "/data.php"); 
  1012.  
  1013. //getting all active feeds 
  1014. $feeds = GFExactTargetData::get_feed_by_form($form["id"], true); 
  1015. foreach($feeds as $feed) { 
  1016. //only export if user has opted in 
  1017. if(self::is_optin($form, $feed)) 
  1018. self::export_feed($entry, $form, $feed, $api); 
  1019.  
  1020. public static function export_feed($entry, $form, $feed, $api) { 
  1021. #print_r($feed); die(); 
  1022. $double_optin = false; // $feed["meta"]["double_optin"] ? true : false; 
  1023. $send_welcome = false; // $feed["meta"]["welcome_email"] ? true : false; 
  1024. $email_field_id = $feed["meta"]["field_map"]["email_address"]; 
  1025. $email = $entry[$email_field_id]; 
  1026.  
  1027. $merge_vars = array(''); 
  1028. foreach($feed["meta"]["field_map"] as $var_tag => $field_id) { 
  1029.  
  1030. $field = RGFormsModel::get_field($form, $field_id); 
  1031.  
  1032. if($var_tag == 'address_full') { 
  1033. $merge_vars[$var_tag] = self::get_address($entry, $field_id); 
  1034. } else if($var_tag == 'country') { 
  1035. # echo $entry[$field_id]; die(); 
  1036. $merge_vars[$var_tag] = empty($entry[$field_id]) ? '' : GFCommon::get_country_code(trim($entry[$field_id])); 
  1037. } else if($var_tag != "email") { 
  1038. if(!empty($entry[$field_id])) { 
  1039. if($field['type'] == 'textarea') { 
  1040. $merge_vars[$var_tag] = '<![CDATA['.$entry[$field_id].']]>'; 
  1041. } else{ 
  1042. $merge_vars[$var_tag] = $entry[$field_id]; 
  1043. } else { 
  1044. foreach($entry as $key => $value) { 
  1045. if(floor($key) == floor($field_id) && !empty($value)) { 
  1046. $merge_vars[$var_tag][] = $value; 
  1047.  
  1048. if(apply_filters('gf_exacttarget_add_source', true) && isset($form['title'])) { 
  1049. $merge_vars['source_form'] = $form['title']; 
  1050.  
  1051. if((empty($api->addtype) || $api->addtype == 'api') && empty($api->subscriberkey)) { 
  1052. $lists = explode(', ', $feed["meta"]["contact_list_id"]); 
  1053. foreach($lists as $list) { 
  1054. $api->AddMembership($list, $email, $merge_vars); 
  1055. } else { 
  1056. $api->listSubscribe($feed["meta"]["contact_list_id"], $email, $merge_vars); 
  1057.  
  1058.  
  1059. public static function uninstall() { 
  1060.  
  1061. //loading data lib 
  1062. require_once(self::get_base_path() . "/data.php"); 
  1063.  
  1064. if(!GFExactTarget::has_access("gravityforms_exacttarget_uninstall")) 
  1065. die(__("You don't have adequate permission to uninstall ExactTarget Add-On.", "gravity-forms-exacttarget")); 
  1066.  
  1067. //droping all tables 
  1068. GFExactTargetData::drop_tables(); 
  1069.  
  1070. //removing options 
  1071. delete_option("gf_exacttarget_settings"); 
  1072. delete_option("gf_exacttarget_version"); 
  1073.  
  1074. //Deactivating plugin 
  1075. $plugin = "gravity-forms-exacttarget/exacttarget.php"; 
  1076. deactivate_plugins($plugin); 
  1077. update_option('recently_activated', array($plugin => time()) + (array)get_option('recently_activated')); 
  1078.  
  1079. public static function is_optin($form, $settings) { 
  1080. $config = $settings["meta"]; 
  1081. $operator = $config["optin_operator"]; 
  1082.  
  1083. $field = RGFormsModel::get_field($form, $config["optin_field_id"]); 
  1084. $field_value = RGFormsModel::get_field_value($field, array()); 
  1085. $is_value_match = is_array($field_value) ? in_array($config["optin_value"], $field_value) : $field_value == $config["optin_value"]; 
  1086.  
  1087. return !$config["optin_enabled"] || empty($field) || ($operator == "is" && $is_value_match) || ($operator == "isnot" && !$is_value_match); 
  1088.  
  1089.  
  1090. private static function is_gravityforms_installed() { 
  1091. return class_exists("RGForms"); 
  1092.  
  1093. private static function is_gravityforms_supported() { 
  1094. if(class_exists("GFCommon")) { 
  1095. $is_correct_version = version_compare(GFCommon::$version, self::$min_gravityforms_version, ">="); 
  1096. return $is_correct_version; 
  1097. else{ 
  1098. return false; 
  1099.  
  1100. private function simpleXMLToArray($xml,  
  1101. $flattenValues=true,  
  1102. $flattenAttributes = true,  
  1103. $flattenChildren=true,  
  1104. $valueKey='@value',  
  1105. $attributesKey='@attributes',  
  1106. $childrenKey='@children') { 
  1107.  
  1108. $return = array(); 
  1109. if(!($xml instanceof SimpleXMLElement)) {return $return;} 
  1110. $name = $xml->getName(); 
  1111. $_value = trim((string)$xml); 
  1112. if(strlen($_value)==0) {$_value = null;}; 
  1113.  
  1114. if($_value!==null) { 
  1115. if(!$flattenValues) {$return[$valueKey] = $_value;} 
  1116. else{$return = $_value;} 
  1117.  
  1118. $children = array(); 
  1119. $first = true; 
  1120. foreach($xml->children() as $elementName => $child) { 
  1121. $value = self::simpleXMLToArray($child, $flattenValues, $flattenAttributes, $flattenChildren, $valueKey, $attributesKey, $childrenKey); 
  1122. if(isset($children[$elementName])) { 
  1123. if($first) { 
  1124. $temp = $children[$elementName]; 
  1125. unset($children[$elementName]); 
  1126. $children[$elementName][] = $temp; 
  1127. $first=false; 
  1128. $children[$elementName][] = $value; 
  1129. else{ 
  1130. $children[$elementName] = $value; 
  1131. if(count($children)>0) { 
  1132. if(!$flattenChildren) {$return[$childrenKey] = $children;} 
  1133. else{$return = array_merge($return, $children);} 
  1134.  
  1135. $attributes = array(); 
  1136. foreach($xml->attributes() as $name=>$value) { 
  1137. $attributes[$name] = trim($value); 
  1138. if(count($attributes)>0) { 
  1139. if(!$flattenAttributes) {$return[$attributesKey] = $attributes;} 
  1140. else{$return = array_merge($return, $attributes);} 
  1141.  
  1142. return $return; 
  1143.  
  1144. private function convert_xml_to_object($response) { 
  1145. $response = @simplexml_load_string($response); // Added @ 1.2.2 
  1146. if(is_object($response)) { 
  1147. return $response; 
  1148. } else { 
  1149. return false; 
  1150.  
  1151. private function convert_xml_to_array($response) { 
  1152. $response = self::convert_xml_to_object($response); 
  1153. $response = self::simpleXMLToArray($response); 
  1154. if(is_array($response)) { 
  1155. return $response; 
  1156. } else { 
  1157. return false; 
  1158.  
  1159. protected static function has_access($required_permission) { 
  1160. $has_members_plugin = function_exists('members_get_capabilities'); 
  1161. $has_access = $has_members_plugin ? current_user_can($required_permission) : current_user_can("level_7"); 
  1162. if($has_access) 
  1163. return $has_members_plugin ? $required_permission : "level_7"; 
  1164. else 
  1165. return false; 
  1166.  
  1167. //Returns the url of the plugin's root folder 
  1168. protected function get_base_url() { 
  1169. return plugins_url(null, __FILE__); 
  1170.  
  1171. //Returns the physical path of the plugin's root folder 
  1172. protected function get_base_path() { 
  1173. $folder = basename(dirname(__FILE__)); 
  1174. return WP_PLUGIN_DIR . "/" . $folder; 
  1175.  
  1176.