/cf7-infusionsoft-addon.php

  1. <?php 
  2. /** 
  3. * Plugin Name: Contact Form 7 - Infusionsoft Add-on 
  4. * Description: An add-on for Contact Form 7 that provides a way to capture leads, tag customers, and send contact form data to InfusionSoft. 
  5. * Version: 1.2.1 
  6. * Author: Ryan Nevius 
  7. * Author URI: http://www.ryannevius.com 
  8. * License: GPLv3 
  9. */ 
  10.  
  11. require_once('src/isdk.php'); 
  12. require_once('cf7-infusionsoft-options.php'); 
  13. require_once('cf7-infusionsoft-modules.php'); 
  14.  
  15. /** 
  16. * Verify CF7 dependencies. 
  17. */ 
  18. function cf7_infusionsoft_admin_notice() { 
  19. // Verify that CF7 is active and updated to the required version (currently 3.9.0) 
  20. if ( is_plugin_active('contact-form-7/wp-contact-form-7.php') ) { 
  21. $wpcf7_path = plugin_dir_path( dirname(__FILE__) ) . 'contact-form-7/wp-contact-form-7.php'; 
  22. $wpcf7_plugin_data = get_plugin_data( $wpcf7_path, false, false); 
  23. $wpcf7_version = (int)preg_replace('/[.]/', '', $wpcf7_plugin_data['Version']); 
  24. // CF7 drops the ending ".0" for new major releases (e.g. Version 4.0 instead of 4.0.0...which would make the above version "40") 
  25. // We need to make sure this value has a digit in the 100s place. 
  26. if ( $wpcf7_version < 100 ) { 
  27. $wpcf7_version = $wpcf7_version * 10; 
  28. // If CF7 version is < 3.9.0 
  29. if ( $wpcf7_version < 390 ) { 
  30. echo '<div class="update-nag"><p><strong>Warning: </strong>Contact Form 7 - InfusionSoft Add-on requires that you have the latest version of Contact Form 7 installed. Please upgrade now.</p></div>'; 
  31. // If it's not installed and activated, throw an error 
  32. else { 
  33. echo '<div class="error"><p>Contact Form 7 is not activated. Contact Form 7 must be installed and activated before you can use the InfusionSoft Add-on.</p></div>'; 
  34. add_action( 'admin_notices', 'cf7_infusionsoft_admin_notice' ); 
  35.  
  36.  
  37. /** 
  38. * Enqueue Scripts with CF7 Dependencies 
  39. */ 
  40. function cf7_infusionsoft_enqueue_scripts() { 
  41. if( !function_exists('wpcf7_add_meta_boxes') ) { 
  42. wp_enqueue_script( 'cf7-infusionsoft-scripts', plugin_dir_url(__FILE__) . 'cf7-infusionsoft-scripts.js', array('jquery', 'wpcf7-admin-taggenerator', 'wpcf7-admin'), null, true ); 
  43. add_action( 'admin_enqueue_scripts', 'cf7_infusionsoft_enqueue_scripts' ); 
  44.  
  45.  
  46. /** 
  47. * Enable the InfusionSoft tags in the tag generator 
  48. */ 
  49. function cf7_infusionsoft_add_tag_generator() { 
  50. if( function_exists('wpcf7_add_tag_generator') ) { 
  51. // Modify callback based on CF7 version 
  52. $callback = function_exists('wpcf7_add_meta_boxes') ? 'wpcf7_tag_generator_infusionsoft_old' : 'wpcf7_tag_generator_infusionsoft'; 
  53. wpcf7_add_tag_generator( 'infusionsoft', 'Infusionsoft Fields', 'wpcf7-tg-pane-infusionsoft', $callback ); 
  54. add_action( 'admin_init', 'cf7_infusionsoft_add_tag_generator', 99 ); 
  55.  
  56.  
  57. /** 
  58. * Adds a box to the main column on the form edit page.  
  59. * 
  60. * CF7 < 4.2 
  61. */ 
  62. function cf7_infusionsoft_tag_add_meta_boxes() { 
  63. add_meta_box( 'cf7-infusionsoft-settings', 'InfusionSoft Settings', 'cf7_infusionsoft_addon_metaboxes', null, 'form', 'low'); 
  64. add_action( 'wpcf7_add_meta_boxes', 'cf7_infusionsoft_tag_add_meta_boxes' ); 
  65.  
  66.  
  67. /** 
  68. * Adds a tab to the editor on the form edit page.  
  69. * 
  70. * CF7 >= 4.2 
  71. */ 
  72. function cf7_infusionsoft_tag_page_panels($panels) { 
  73. $panels['infusionsoft-panel'] = array( 'title' => 'InfusionSoft Options', 'callback' => 'cf7_infusionsoft_addon_panel_meta' ); 
  74. return $panels; 
  75. add_action( 'wpcf7_editor_panels', 'cf7_infusionsoft_tag_page_panels' ); 
  76.  
  77.  
  78. // Create the meta boxes (CF7 < 4.2) 
  79. function cf7_infusionsoft_addon_metaboxes( $post ) { 
  80. // Add an nonce field so we can check for it later. 
  81. wp_nonce_field( 'cf7_infusionsoft_addon_metaboxes', 'cf7_infusionsoft_addon_metaboxes_nonce' ); 
  82.  
  83. /** 
  84. * Use get_post_meta() to retrieve an existing value 
  85. * from the database and use the value for the form. 
  86. */ 
  87. $infusionsoft_addon_tag_value = get_post_meta( $post->id(), '_cf7_infusionsoft_addon_tag_key', true ); 
  88.  
  89. echo '<label for="cf7_infusionsoft_addon_tags"><strong>Contact Tags: </strong></label> '; 
  90. echo '<input type="text" placeholder="infusionsoft_tag_name" id="cf7_infusionsoft_addon_tags" name="cf7_infusionsoft_addon_tags" value="' . esc_attr( $infusionsoft_addon_tag_value ) . '" size="25" />'; 
  91. echo '<p class="howto">Separate multiple tags with commas. These must already be defined in InfusionSoft.</p>'; 
  92. // Create the panel inputs (CF7 >= 4.2) 
  93. function cf7_infusionsoft_addon_panel_meta( $post ) { 
  94. wp_nonce_field( 'cf7_infusionsoft_addon_metaboxes', 'cf7_infusionsoft_addon_metaboxes_nonce' ); 
  95. $infusionsoft_addon_tag_value = get_post_meta( $post->id(), '_cf7_infusionsoft_addon_tag_key', true ); 
  96.  
  97. // The meta box content 
  98. echo '<h3>InfusionSoft Tags</h3> 
  99. <fieldset> 
  100. <legend>Enter tags exactly how they appear in InfusionSoft. Separate multiple tags with commas. <br>These must already be defined in InfusionSoft.</legend> 
  101. <input type="text" placeholder="Tag 1, tag_2, etc." id="cf7_infusionsoft_addon_tags" name="cf7_infusionsoft_addon_tags" value="' . esc_attr( $infusionsoft_addon_tag_value ) . '" size="25" />' . 
  102. '</fieldset>'; 
  103.  
  104. // Store InfusionSoft tag 
  105. function cf7_infusionsoft_addon_save_contact_form( $contact_form ) { 
  106. $contact_form_id = $contact_form->id(); 
  107. if ( !isset( $_POST ) || empty( $_POST ) || !isset( $_POST['cf7_infusionsoft_addon_tags'] ) || !isset( $_POST['cf7_infusionsoft_addon_metaboxes_nonce'] ) ) { 
  108. return; 
  109.  
  110. // Verify that the nonce is valid. 
  111. if ( ! wp_verify_nonce( $_POST['cf7_infusionsoft_addon_metaboxes_nonce'], 'cf7_infusionsoft_addon_metaboxes' ) ) { 
  112. return; 
  113.  
  114. if ( isset( $_POST['cf7_infusionsoft_addon_tags'] ) ) { 
  115. update_post_meta( $contact_form_id,  
  116. '_cf7_infusionsoft_addon_tag_key',  
  117. $_POST['cf7_infusionsoft_addon_tags'] 
  118. ); 
  119. add_action( 'wpcf7_after_save', 'cf7_infusionsoft_addon_save_contact_form' ); 
  120.  
  121.  
  122. function cf7_infusionsoft_addon_signup_form_submitted( $contact_form ) { 
  123. $contact_form_id = $contact_form->id(); 
  124.  
  125. $submission = WPCF7_Submission::get_instance(); 
  126. $posted_data = $submission->get_posted_data();  
  127.  
  128. // If the email address is not set 
  129. if ( empty( $posted_data['infusionsoft-email'] ) ) { 
  130. return; 
  131. // If all looks good, let's try to add the user 
  132. cf7_infusionsoft_addon_add_contact($contact_form_id, $posted_data); 
  133. add_action( 'wpcf7_mail_sent', 'cf7_infusionsoft_addon_signup_form_submitted' ); 
  134.  
  135. function cf7_infusionsoft_addon_add_contact($contact_form_id, $posted_data) { 
  136.  
  137. // Exit right away if the API credentials aren't entered 
  138. $infusionsoft_app_name = get_option( 'infusionsoft_app_name'); 
  139. $infusionsoft_api_key = get_option( 'infusionsoft_api_key'); 
  140.  
  141. if ( empty( $infusionsoft_app_name ) || empty( $infusionsoft_api_key ) ) { 
  142. echo '<script>alert("You must configure the Contact Form 7 - InfusionSoft Add-on in the Admin.")</script>'; 
  143. return; 
  144.  
  145. // Configure a new InfusionSoft connection 
  146. $app = new iSDK(); 
  147. // If no connection is made, get out of here. 
  148. if ( !( $app->cfgCon($infusionsoft_app_name) ) ) { 
  149. return; 
  150.  
  151. // Assemble the contact data 
  152. $contact_data = array( 
  153. 'FirstName' => ( !empty($posted_data['infusionsoft-first-name']) ) ? $posted_data['infusionsoft-first-name'] : '',  
  154. 'LastName' => ( !empty($posted_data['infusionsoft-last-name']) ) ? $posted_data['infusionsoft-last-name'] : '',  
  155. 'Company' => ( !empty($posted_data['infusionsoft-company']) ) ? $posted_data['infusionsoft-company'] : '',  
  156. 'Email' => $posted_data['infusionsoft-email'],  
  157. 'Phone1' => ( !empty($posted_data['infusionsoft-phone']) ) ? $posted_data['infusionsoft-phone'] : '',  
  158. 'ContactNotes' => ( !empty($posted_data['infusionsoft-notes']) ) ? $posted_data['infusionsoft-notes'] : '',  
  159. ); 
  160. // Add the contact to InfusionSoft, with a duplicate check 
  161. $contact_id = $app->addWithDupCheck($contact_data, 'EmailAndName'); 
  162.  
  163. // Set opt-in marketing status 
  164. // InfusionSoft requires a "reason" for setting the opt-in marketing status 
  165. $reason = get_bloginfo('name') . ' Website Signup Form'; 
  166. // And allow them to receive email marketing 
  167. $set_optin_status = $app->optIn($posted_data['infusionsoft-email'], $reason); 
  168.  
  169. // Optionally tag the contact 
  170. $user_tags = get_post_meta( $contact_form_id, '_cf7_infusionsoft_addon_tag_key', true ); 
  171.  
  172. if ( !empty( $user_tags ) ) { 
  173. // Assemble the names into a list of strings (with leading/ending whitespace trimmed) 
  174. $user_tags = array_map( 'trim', explode(', ', $user_tags) ); 
  175.  
  176. foreach ($user_tags as $tag_name) { 
  177. // Search the ContactGroup table for each tag name 
  178. $tag_data = $app->dsFind( 'ContactGroup', 1, 0, 'GroupName', $tag_name, array('Id') ); 
  179. // If the query returns a valid ID 
  180. if ( !empty( $tag_data[0]['Id'] ) ) { 
  181. $tag_the_user = $app->grpAssign( $contact_id, $tag_data[0]['Id'] ); 
  182. }  
.