Skip to content

Instantly share code, notes, and snippets.

@verygoodplugins
Last active June 5, 2020 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verygoodplugins/39b82f3cc8559c92305e36263620729c to your computer and use it in GitHub Desktop.
Save verygoodplugins/39b82f3cc8559c92305e36263620729c to your computer and use it in GitHub Desktop.
Connect WP Fusion to two ActiveCampaign accounts and sync users depending on their role
<?php
/*
Plugin Name: AC Account Switch
Description: Switches AC API credentials for WP Fusion depending on user role
Plugin URI: https://verygoodplugins.com/
Version: 1.0
Author: Very Good Plugins
Author URI: https://verygoodplugins.com/
*/
define( 'ACCOUNT_TWO_URL', 'https://account2.api-us1.com' );
define( 'ACCOUNT_TWO_KEY', 'APIKEY' );
define( 'WPF_DISABLE_QUEUE', true );
global $wpf_ac_app_swap;
global $wpf_ac_switched;
$wpf_ac_app_swap = false;
$wpf_ac_switched = false;
function wpf_is_account_two( $user_id ) {
$roles = array( 'deals_contributor' );
$user = get_userdata( $user_id );
if ( affwp_is_affiliate( $user_id ) ) {
return true;
}
foreach ( $roles as $role ) {
if ( 'deals_contributor' === $user->role ) {
return true;
}
}
return false;
}
function wpf_maybe_switch_to_account_two( $user_id ) {
global $wpf_ac_app_swap;
global $wpf_ac_switched;
if( wpf_is_account_two( $user_id ) && $wpf_ac_switched == false ) {
// If user should be sent to second app, and the first app is currently active
if( $wpf_ac_app_swap == false ) {
// If apps haven't been swapped yet, move first app into swap variable
$wpf_ac_app_swap = wp_fusion()->crm->app;
// And initialize second app connection
wp_fusion()->crm->connect( ACCOUNT_TWO_URL, ACCOUNT_TWO_KEY, true );
} else {
// If second app is already saved in the swap, move it to a temp var
$temp_second_app = $wpf_ac_app_swap;
// Store first app in swap
$wpf_ac_app_swap = wp_fusion()->crm->app;
// Put second app back into use
wp_fusion()->crm->app = $temp_second_app;
}
// Set $wpf_ac_switched to true to indicate we're using the second app
$wpf_ac_switched = true;
}
}
add_action( 'wpf_user_register_start', 'wpf_maybe_switch_to_account_two' );
add_action( 'wpf_get_contact_id_start', 'wpf_maybe_switch_to_account_two' );
add_action( 'wpf_pre_pull_user_meta', 'wpf_maybe_switch_to_account_two' );
add_action( 'wpf_get_tags_start', 'wpf_maybe_switch_to_account_two' );
add_action( 'wpf_apply_tags_start', 'wpf_maybe_switch_to_account_two' );
add_action( 'wpf_remove_tags_start', 'wpf_maybe_switch_to_account_two' );
add_action( 'wpf_push_user_meta_start', 'wpf_maybe_switch_to_account_two' );
function wpf_maybe_switch_back( $user_id ) {
global $wpf_ac_app_swap;
global $wpf_ac_switched;
if( $wpf_ac_switched == true ) {
// If the second app is active, move the first app from the swap to a temp variable
$temp_first_app = $wpf_ac_app_swap;
// Store second app in swap
$wpf_ac_app_swap = wp_fusion()->crm->app;
// Put first app back into use
wp_fusion()->crm->app = $temp_first_app;
// Set $wpf_ac_switched to false
$wpf_ac_switched = false;
}
}
add_action( 'wpf_user_created', 'wpf_maybe_switch_back' );
add_action( 'wpf_got_contact_id', 'wpf_maybe_switch_back' );
add_action( 'wpf_user_updated', 'wpf_maybe_switch_back' );
add_action( 'wpf_tags_modified', 'wpf_maybe_switch_back' );
@worldtravlr
Copy link

Thanks Jack!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment