cf7_infusionsoft_addon_add_contact

The Contact Form 7 - Infusionsoft Add-on cf7 infusionsoft addon add contact function.

Description

cf7_infusionsoft_addon_add_contact( $contact_form_id, $posted_data ); 

Parameters (2)

0. $contact_form_id
The contact form id.
1. $posted_data
The posted data.

Usage

  1. if ( !function_exists( 'cf7_infusionsoft_addon_add_contact' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'contact-form-7-infusionsoft-add-on/cf7-infusionsoft-addon.php'; 
  3.  
  4. // The contact form id. 
  5. $contact_form_id = null; 
  6.  
  7. // The posted data. 
  8. $posted_data = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = cf7_infusionsoft_addon_add_contact($contact_form_id, $posted_data); 
  12.  

Defined (1)

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

/cf7-infusionsoft-addon.php  
  1. function cf7_infusionsoft_addon_add_contact($contact_form_id, $posted_data) { 
  2.  
  3. // Exit right away if the API credentials aren't entered 
  4. $infusionsoft_app_name = get_option( 'infusionsoft_app_name'); 
  5. $infusionsoft_api_key = get_option( 'infusionsoft_api_key'); 
  6.  
  7. if ( empty( $infusionsoft_app_name ) || empty( $infusionsoft_api_key ) ) { 
  8. echo '<script>alert("You must configure the Contact Form 7 - InfusionSoft Add-on in the Admin.")</script>'; 
  9. return; 
  10.  
  11. // Configure a new InfusionSoft connection 
  12. $app = new iSDK(); 
  13. // If no connection is made, get out of here. 
  14. if ( !( $app->cfgCon($infusionsoft_app_name) ) ) { 
  15. return; 
  16.  
  17. // Assemble the contact data 
  18. $contact_data = array( 
  19. 'FirstName' => ( !empty($posted_data['infusionsoft-first-name']) ) ? $posted_data['infusionsoft-first-name'] : '',  
  20. 'LastName' => ( !empty($posted_data['infusionsoft-last-name']) ) ? $posted_data['infusionsoft-last-name'] : '',  
  21. 'Company' => ( !empty($posted_data['infusionsoft-company']) ) ? $posted_data['infusionsoft-company'] : '',  
  22. 'Email' => $posted_data['infusionsoft-email'],  
  23. 'Phone1' => ( !empty($posted_data['infusionsoft-phone']) ) ? $posted_data['infusionsoft-phone'] : '',  
  24. 'ContactNotes' => ( !empty($posted_data['infusionsoft-notes']) ) ? $posted_data['infusionsoft-notes'] : '',  
  25. ); 
  26. // Add the contact to InfusionSoft, with a duplicate check 
  27. $contact_id = $app->addWithDupCheck($contact_data, 'EmailAndName'); 
  28.  
  29. // Set opt-in marketing status 
  30. // InfusionSoft requires a "reason" for setting the opt-in marketing status 
  31. $reason = get_bloginfo('name') . ' Website Signup Form'; 
  32. // And allow them to receive email marketing 
  33. $set_optin_status = $app->optIn($posted_data['infusionsoft-email'], $reason); 
  34.  
  35. // Optionally tag the contact 
  36. $user_tags = get_post_meta( $contact_form_id, '_cf7_infusionsoft_addon_tag_key', true ); 
  37.  
  38. if ( !empty( $user_tags ) ) { 
  39. // Assemble the names into a list of strings (with leading/ending whitespace trimmed) 
  40. $user_tags = array_map( 'trim', explode(', ', $user_tags) ); 
  41.  
  42. foreach ($user_tags as $tag_name) { 
  43. // Search the ContactGroup table for each tag name 
  44. $tag_data = $app->dsFind( 'ContactGroup', 1, 0, 'GroupName', $tag_name, array('Id') ); 
  45. // If the query returns a valid ID 
  46. if ( !empty( $tag_data[0]['Id'] ) ) { 
  47. $tag_the_user = $app->grpAssign( $contact_id, $tag_data[0]['Id'] ); 
  48. }