GFLightBlueAPI

The Gravity Forms Light Blue API Add-On GFLightBlueAPI class.

Defined (1)

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

/gravity-forms-light-blue.php  
  1. class GFLightBlueAPI { 
  2.  
  3. private static $name = "Gravity Forms Light Blue Add-On"; 
  4. private static $path = "gravity-forms-light-blue/gravity-forms-light-blue.php"; 
  5. private static $version = "0.1"; 
  6. private static $min_gravityforms_version = "1.6"; // I'm specifying 1.6 in case we decide to change from gform_pre_submission to gform_after_submission 
  7.  
  8.  
  9. //Plugin starting point. Will load appropriate files 
  10. public static function init() { 
  11.  
  12. add_action("admin_notices", array('GFLightBlueAPI', 'display_gravity_forms_status')); 
  13.  
  14. if( !self::is_gravityforms_supported()) { 
  15. return; 
  16.  
  17. $plugin_file = self::$path; 
  18. add_filter( "plugin_action_links_{$plugin_file}", array('GFLightBlueAPI', 'plugin_settings_link'), 10, 2 ); 
  19.  
  20. if(is_admin()) { //creates a new Settings page on Gravity Forms' settings screen 
  21. RGForms::add_settings_page("Light Blue API", array("GFLightBlueAPI", "lb_settings_page") ); 
  22.  
  23. add_action("gform_pre_submission", Array( "GFLightBlueAPI", "lb_gform_pre_submission" ) ); 
  24.  
  25. } // end of init() 
  26.  
  27.  
  28. public static function lb_settings_page() { 
  29.  
  30. if(!empty($_POST["uninstall"])) { 
  31. check_admin_referer("deactivate", "gf_light_blue_deactivate"); 
  32. self::deactivate(); 
  33. ?> 
  34. <div class="updated fade" style="padding:20px;">The Light Blue Gravity Forms add-on has been successfully uninstalled. It can be re-activated from the <a href='plugins.php'>plugins page</a>.</div> 
  35. <?php 
  36. return; 
  37. } elseif(!empty($_POST["gf_light_blue_submit"])) { 
  38. check_admin_referer("update", "gf_light_blue_update"); 
  39. $settings = array( 
  40. "key" => stripslashes($_POST["gf_light_blue_api_key"]),  
  41. "date_format" => stripslashes($_POST["gf_light_blue_date_format"]),  
  42. "decimal_separator" => stripslashes($_POST["gf_light_blue_decimal_separator"]),  
  43. "debug" => stripslashes($_POST["gf_light_blue_debug"]) 
  44. ); 
  45. update_option("gf_light_blue_settings", $settings); 
  46. else{ 
  47. $settings = get_option("gf_light_blue_settings"); 
  48.  
  49. if ($settings["key"] == '') { 
  50. $api_test_message = ""; 
  51. } elseif ( self::lb_test_account( $api_test_message ) ) { 
  52. $api_test_message = "<p style='display:block;margin-top:10px;color:green;'>Account confirmed</p>"; 
  53. } else { 
  54. $api_test_message = "<p style='display:block;margin-top:10px;color:red;'>".$api_test_message."</p>"; 
  55.  
  56. ?> 
  57. <div class="wrap"> 
  58. <h2>Light Blue API Setup</h2> 
  59.  
  60. <div class="hr-divider"></div> 
  61.  
  62. <h3>Using the Gravity Forms Light Blue API add on</h3> 
  63. <p>To send data to the Light Blue API, you need to associate the fields in your forms with parameters that the Light Blue API recognises.</p> 
  64. <p>To do this, you need to set the 'Parameter Name' for each field on your forms. You can find the 'Parameter Name' in the form editor, by clicking the 'Edit' button for a field, going to the 'Advanced' tab, checking the 'Allow field to be populated dynamically' box.</p> 
  65. <p>You can find a list of the parameters that the Light Blue API recognises <a href='http://www.lightbluesoftware.com/api/' target='_blank'>on our website</a>. Any fields in your forms that do not have their 'Parameter Name' set, or have a 'Parameter Name' that doesn't match a valid Light Blue API paramater, will be ignored but stored in the Gravity Forms entries database as usual.</p> 
  66.  
  67. <div class="hr-divider"></div> 
  68.  
  69. <form method="post" action="" style="margin: 30px 0 30px; clear:both;"> 
  70. <?php wp_nonce_field("update", "gf_light_blue_update") ?> 
  71. <h3>Your Light Blue Account</h3> 
  72. <p>If you don't have a subscription to Light Blue's online services, you can <a href='http://www.lightbluesoftware.com' target='_blank'>sign up for one here</a>.</p> 
  73.  
  74. <table class="form-table"> 
  75. <tr> 
  76. <th scope="row"><label for="gf_light_blue_api_key">API Key</label></th> 
  77. <td> 
  78. <input type="text" size="75" id="gf_light_blue_api_key" class="code pre" name="gf_light_blue_api_key" value="<?php echo esc_attr($settings["key"]) ?>"/> 
  79. <small style="display:block;">You can find your Light Blue API key by logging into your account <a href='http://www.lightbluesoftware.com' target='_blank'>on our website</a>.</small> 
  80. <?php print $api_test_message;?> 
  81. </td> 
  82. </tr> 
  83.  
  84. <tr> 
  85. <th scope="row"><label for="gf_light_blue_date_format">Date Format</label></th> 
  86. <td> 
  87. <select name="gf_light_blue_date_format" id="gf_light_blue_date_format"> 
  88. <option <?php if( $settings["date_format"] == "dd/mm/yyyy" ) { print "selected='selected'"; } ?> value="dd/mm/yyyy">dd/mm/yyyy</option> 
  89. <option <?php if( $settings["date_format"] == "d/m/yyyy" ) { print "selected='selected'"; } ?> value="d/m/yyyy">d/m/yyyy</option> 
  90. <option <?php if( $settings["date_format"] == "mm/dd/yyyy" ) { print "selected='selected'"; } ?> value="mm/dd/yyyy">mm/dd/yyyy</option> 
  91. <option <?php if( $settings["date_format"] == "m/d/yyyy" ) { print "selected='selected'"; } ?> value="m/d/yyyy">m/d/yyyy</option> 
  92. </select> 
  93. <small style="display:block;">You need to make sure that the format of any date fields in your forms matches the date format you've selected here, or the Light Blue API might not understand dates that you send to it.</small> 
  94. </td> 
  95. </tr> 
  96. <tr> 
  97. <th scope="row">Time Format</th> 
  98. <td> 
  99. <small style="display:block;">You don't need to specify a time format, but the format of any time fields in your forms must be one of the following: hh:mm (i.e. a 24-hour time with leading zeros for the hour), h:mm (i.e. a 24-hour time without leading zeros for the hour), hh:mm AM/PM, h:mm AM/PM</small> 
  100. </td> 
  101. </tr> 
  102. <tr> 
  103. <th scope="row"><label for="gf_light_blue_decimal_separator">Decimal Separator</label></th> 
  104. <td> 
  105. <input type="text" size="5" id="gf_light_blue_decimal_separator" class="code pre" name="gf_light_blue_decimal_separator" value="<?php echo esc_attr($settings["decimal_separator"]) ?>"/> 
  106. <small style="display:block;">If you send currency values to the Light Blue API, it will assume that you are using a full stop character as your decimal separator (e.g. $10.00) unless you specify a different decimal separator here.</small> 
  107. </td> 
  108. </tr> 
  109.  
  110.  
  111. <tr> 
  112. <th scope="row">Debugging</th> 
  113. <td> 
  114. <label for="gf_light_blue_debug"> 
  115. <?php if( isset($settings["debug"]) && ($settings["debug"] == 'on' ) ) {  
  116. print "<input name='gf_light_blue_debug' type='checkbox' id='gf_light_blue_debug' checked>";  
  117. } else {  
  118. print "<input name='gf_light_blue_debug' type='checkbox' id='gf_light_blue_debug'>";  
  119. } ?> 
  120. Email debugging logs to your WordPress admin email address. 
  121. </label> 
  122. <small style="display:block;">(n.b. this option should only be turned on to help Light Blue Software diagnose any problems you have with sending data to the Light Blue API)</small> 
  123. </td> 
  124. </tr> 
  125. <tr> 
  126. <td colspan="2" ><input type="submit" name="gf_light_blue_submit" class="submit button-primary" value="Update Settings" /></td> 
  127. </tr> 
  128. </table> 
  129. </form> 
  130.  
  131. <div class="hr-divider"></div> 
  132.  
  133. <form action="" method="post"> 
  134. <?php wp_nonce_field("deactivate", "gf_light_blue_deactivate") ?> 
  135.  
  136. <h3>Deactivate Light Blue API add-on</h3> 
  137. <div class="delete-alert alert_red"> 
  138. <h3>Warning</h3> 
  139. <p>Deactivating the Light Blue API add-on for Gravity Forms will mean that your forms will no longer submit data to the Light Blue API.</p> 
  140. <input type="submit" name="uninstall" value="Deactivate Light Blue API Add-On" class="button" onclick="return confirm(\'Really deactivate the Light Blue add-on?\');"/> 
  141. </div> 
  142. </form> 
  143.  
  144.  
  145. </div> 
  146. <?php 
  147.  
  148.  
  149.  
  150. public static function lb_test_account( &$message ) { 
  151.  
  152. $settings = get_option("gf_light_blue_settings"); 
  153. $submitted_data = Array(); 
  154. $submitted_data["Key"] = $settings["key"]; 
  155.  
  156. // Post the submitted values to the Light Blue API account check script 
  157. $response = wp_remote_post( "https://online.lightbluesoftware.com/apiCheck.php", array( 
  158. 'method' => 'POST',  
  159. 'timeout' => 45,  
  160. 'redirection' => 5,  
  161. 'body' => $submitted_data,  
  162. ); 
  163.  
  164. if ( is_wp_error( $response ) ) { 
  165. $message = "WordPress error: ".$response->get_error_message(); 
  166. Return False; 
  167. } else { 
  168. if ( $response["response"]["code"] == 200 ) { 
  169. $status_array = Explode( Chr(13), $response["body"] ); 
  170. if( $status_array[0] == 0 ) { 
  171. $message = $status_array[1]; 
  172. Return True; 
  173. } else { 
  174. $message = $status_array[1]; 
  175. Return False; 
  176.  
  177.  
  178. public static function lb_gform_pre_submission( $form ) { 
  179. $submitted_data = array(); 
  180. $settings = get_option("gf_light_blue_settings"); 
  181.  
  182. /** 
  183. if( isset($settings["debug"]) && ($settings["debug"] == 'on' ) ) { // Send debugging email 
  184. ob_start(); 
  185. var_dump($form); 
  186. mail( get_option('admin_email'), 'Light Blue API for Gravity Forms $form', ob_get_contents() ); 
  187. ob_end_clean(); 
  188. ob_start(); 
  189. var_dump($_POST); 
  190. mail( get_option('admin_email'), 'Light Blue API for Gravity Forms $_POST', ob_get_contents() ); 
  191. ob_end_clean(); 
  192. */ 
  193.  
  194. // Build up an array of values submitted via the form 
  195. foreach( $form["fields"] as $field) { 
  196. $label = ""; 
  197. $value = ""; 
  198. if( is_array( $field["inputs"] ) ) { // This is multi-input field, like the name field. 
  199. if( $field["type"] == "checkbox" ) { 
  200. if( isset( $field["inputName"] ) && ($field["inputName"] != '') ) { 
  201. $label = $field["inputName"]; 
  202. foreach( $field["inputs"] as $input ) { 
  203. if( isset( $_POST["input_".str_replace( '.', '_', $input["id"] )] ) ) { 
  204. if( $value != '' ) { $value .= Chr(13); } 
  205. $value .= stripslashes( $_POST["input_".str_replace('.', '_', $input["id"] )] ); 
  206. }  
  207. }  
  208. } else { 
  209. foreach( $field["inputs"] as $input ) { 
  210. if( isset( $input["name"] ) && ($input["name"] != '') ) { 
  211. $label = $input["name"]; 
  212. if( isset( $_POST["input_".str_replace( '.', '_', $input["id"] )] ) ) { 
  213. $value = stripslashes( $_POST["input_".str_replace('.', '_', $input["id"] )] ); 
  214. if ($value != '') { 
  215. $submitted_data[$label] = $value; 
  216. }  
  217. $value = ""; // Clear $value because we're setting $submitted_data within this loop instead of within the fields loop 
  218. } elseif( $field["type"] == "multiselect" ) { 
  219. if( isset( $field["inputName"] ) && ($field["inputName"] != '') ) { 
  220. $label = $field["inputName"]; 
  221. if( isset( $_POST["input_" . $field["id"]] ) ) { 
  222. $value_array = $_POST["input_" . $field["id"]]; 
  223. foreach( $value_array as $multiselect_value ) { 
  224. if( $value != '' ) { $value .= Chr(13); } 
  225. $value .= stripslashes( $multiselect_value ); 
  226. } else { // This is a single-input field 
  227. if( isset( $field["inputName"] ) && ($field["inputName"] != '') ) { 
  228. $label = $field["inputName"]; 
  229. if( isset( $_POST["input_" . $field["id"]] ) ) { 
  230. $value = stripslashes( $_POST["input_" . $field["id"]] ); 
  231. if ($value != '') { 
  232. $submitted_data[$label] = $value; 
  233. }  
  234.  
  235. if( Count( $submitted_data ) == 0 ) { Return; } 
  236.  
  237. $submitted_data["Type"] = "contact form"; 
  238. $submitted_data["Source"] = self::$name; 
  239. $submitted_data["SourceAPIVersion"] = self::$version; 
  240. $submitted_data["Key"] = $settings["key"]; 
  241. if( isset( $settings["date_format"] ) && ($settings["date_format"] != '') ) { 
  242. $submitted_data["DateFormat"] = $settings["date_format"]; 
  243. if( isset( $settings["decimal_separator"] ) && ($settings["decimal_separator"] != '') ) { 
  244. $submitted_data["DecimalSeparator"] = $settings["decimal_separator"]; 
  245.  
  246. if( isset($settings["debug"]) && ($settings["debug"] == 'on' ) ) { // Send debugging email 
  247. ob_start(); 
  248. var_dump($submitted_data); 
  249. mail( get_option('admin_email'), 'Light Blue API for Gravity Forms about to send', ob_get_contents() ); 
  250. ob_end_clean(); 
  251. }  
  252.  
  253. // Post the submitted values to the Light Blue API 
  254. $response = wp_remote_post( "https://online.lightbluesoftware.com/api.php", array( 
  255. 'method' => 'POST',  
  256. 'timeout' => 45,  
  257. 'redirection' => 5,  
  258. 'body' => $submitted_data,  
  259. ); 
  260.  
  261.  
  262. if( isset($settings["debug"]) && ($settings["debug"] == 'on' ) ) { // Send debugging email 
  263. if ( is_wp_error( $response ) ) { 
  264. $error_message = $response->get_error_message(); 
  265. mail( get_option('admin_email'), 'Light Blue API for Gravity Forms sending error', $error_message ); 
  266. } else { 
  267. ob_start(); 
  268. var_dump($response); 
  269. mail( get_option('admin_email'), 'Light Blue API for Gravity Forms sent', ob_get_contents() ); 
  270. ob_end_clean(); 
  271.  
  272.  
  273. } // end of lb_gform_pre_submission 
  274.  
  275.  
  276.  
  277.  
  278. public static function plugin_settings_link( $links, $file ) { // modify the link by unshifting the array 
  279. $settings_link = '<a href="' . admin_url( 'admin.php?page=gf_settings&subview=Light+Blue+API' ) . '">Settings</a>'; 
  280. array_unshift( $links, $settings_link ); 
  281. return $links; 
  282.  
  283.  
  284. public static function deactivate() { 
  285. // Remove stored settings 
  286. delete_option("gf_light_blue_settings"); 
  287.  
  288. // Deactivate plugin 
  289. $plugin = self::$path; 
  290. deactivate_plugins($plugin); 
  291. update_option('recently_activated', array($plugin => time()) + (array)get_option('recently_activated')); 
  292.  
  293. public static function display_gravity_forms_status() { 
  294. if( self::is_gravity_forms_installed( True ) == 1) { // Installed, so check version requirements 
  295. if( self::is_gravityforms_supported() == False ) { 
  296. $message = "<p>The ".self::$name." plugin requires <a href='http://www.gravityforms.com' target='_blank'>Gravity Forms</a> version ".self::$min_gravityforms_version.", and you are using version ".GFCommon::$version.". Please update Gravity Forms.</p>"; 
  297. print "<div id='message' class='updated'>".$message."</div>"; 
  298.  
  299. public static function is_gravity_forms_installed( $printStatus = False ) { 
  300. $message = ''; 
  301. $installed = 0; 
  302. if(!class_exists('RGForms')) { 
  303. if(file_exists(WP_PLUGIN_DIR.'/gravityforms/gravityforms.php')) { 
  304. $installed = 2; 
  305. $message = "<p>Gravity Forms is installed but not active. Please activate the Gravity Forms plugin on the <a href='".wp_nonce_url(admin_url('plugins.php'))."'>plugin settins page</a> to use the ".self::$name." plugin.</p>"; 
  306. } else { 
  307. $installed = 0; 
  308. $message = "<p>The ".self::$name." plugin requires the <a href='http://www.gravityforms.com' target='_blank'>Gravity Forms</a> plugin.</p>"; 
  309. } else { 
  310. $installed = 1; 
  311. if(($message != '') && ($printStatus == True)) { 
  312. print "<div id='message' class='updated'>".$message."</div>"; 
  313. return $installed; 
  314.  
  315. private static function is_gravityforms_supported() { 
  316. if(class_exists("GFCommon")) { 
  317. $is_correct_version = version_compare(GFCommon::$version, self::$min_gravityforms_version, ">="); 
  318. return $is_correct_version; 
  319. else{ 
  320. return false; 
  321.