Last active
March 13, 2024 22:47
-
-
Save verygoodplugins/8ac75415dc9528d0ea9df368a67f0f36 to your computer and use it in GitHub Desktop.
Add new contacts to an alternate CRM (Intercom) when a user registers, and apply tags in Intercom when tags are applied in the primary CRM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Put your intercom access token here | |
define( 'INTERCOM_ACCESS_TOKEN', 'aG9tOjyIyZmMxZDc2X2YxMzBfNDBhZV9hOTVjXzRhZDRlNzBiZWMyMzoxOjA=' ); | |
// We'll save the alternate CRM in a global so it can be re-used if multiple API calls need to be made in the same request | |
global $intercom; | |
/** | |
* Load Intercom and initialize the connection | |
* | |
* @return object Intercom CRM | |
*/ | |
function wpf_connect_to_intercom() { | |
global $intercom; | |
if( is_object( $intercom ) ) { | |
return $intercom; | |
} | |
require_once WPF_DIR_PATH . 'includes/crms/intercom/class-intercom.php'; | |
$intercom = new WPF_Intercom; | |
$intercom->get_params( INTERCOM_ACCESS_TOKEN ); | |
return $intercom; | |
} | |
/** | |
* Get an Intercom contact ID for a user, or look it up | |
* | |
* @return mixed Contact ID if found, "false" if not | |
*/ | |
function wpf_get_intercom_contact_id( $user_id ) { | |
$contact_id = get_user_meta( $user_id, 'wpf_intercom_contact_id', true ); | |
if( ! empty( $contact_id ) ) { | |
return $contact_id; | |
} else { | |
$intercom = wpf_connect_to_intercom(); | |
$user = get_user_by( 'id', $user_id ); | |
$contact_id = $intercom->get_contact_id( $user->user_email ); | |
if( ! is_wp_error( $contact_id ) && $contact_id != false ) { | |
update_user_meta( $user_id, 'wpf_intercom_contact_id', $contact_id ); | |
return $contact_id; | |
} else { | |
return false; | |
} | |
} | |
} | |
/** | |
* Add a new contact to Intercom at registration and optionally apply tags | |
* | |
* @return void | |
*/ | |
function wpf_add_to_intercom( $user_id, $contact_id, $post_data ) { | |
$intercom = wpf_connect_to_intercom(); | |
// Check if there's already a contact record in Intercom | |
$contact_id = $intercom->get_contact_id( $post_data['user_email'] ); | |
if( ! is_wp_error( $contact_id ) && $contact_id == false ) { | |
// You have to manually specify Intercom internal fields here, since the WPF Settings >> Contact fields tab is configured for ActiveCampaign | |
$contact_data = array( | |
'email' => $post_data['user_email'], | |
'name' => $post_data['first_name'] . ' ' . $post_data['last_name'] | |
); | |
// "false" in the second parameter tells it not to use the field mapping set up in the WP Fusion settings | |
$contact_id = $intercom->add_contact( $contact_data, false ); | |
} | |
// Save the contact ID for later reference | |
update_user_meta( $user_id, 'wpf_intercom_contact_id', $contact_id ); | |
// Now you can apply tags | |
$intercom->apply_tags( array( 'Tag One', 'Tag 2' ), $contact_id ); | |
} | |
add_action( 'wpf_user_created', 'wpf_add_to_intercom', 10, 3 ); | |
/** | |
* Apply tags in Intercom when tags are applied in WP Fusion | |
* | |
* @return void | |
*/ | |
function wpf_apply_tags_in_intercom( $user_id, $tags ) { | |
$intercom = wpf_connect_to_intercom(); | |
$contact_id = wpf_get_intercom_contact_id( $user_id ); | |
if( $contact_id !== false ) { | |
$intercom->apply_tags( $tags, $contact_id ); | |
} | |
} | |
add_action( 'wpf_tags_applied', 'wpf_apply_tags_in_intercom', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment