Skip to content

Instantly share code, notes, and snippets.

@RadGH
Last active May 29, 2019 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RadGH/38473c6ebcda730cedadd49de1ad0348 to your computer and use it in GitHub Desktop.
Save RadGH/38473c6ebcda730cedadd49de1ad0348 to your computer and use it in GitHub Desktop.
Infusionsoft POST to wordpress to change role
<?php
/*
UNTESTED CODE BELOW
Infusionsoft triggers a POST request that sets user roles.
This function retrieves that request and processes the role change.
Be sure to enter your $internal_access_key below.
You may need to change the meta key "infusionsoft_contact_id". Consult your infusionsoft plugin documentation to find what that key is named (or search the wp_usermeta table in the database)
@see https://bbpress.org/forums/topic/infusionsoft-and-forum-role/
@see https://wpfusion.com/documentation/webhooks/infusionsoft-http-posts/
*/
function bbp_infsoft_hook_set_role() {
// Config
$internal_access_key = "ENTER_YOUR_ACCESS_KEY_HERE"; // Or use get_option() to load from the database.
// Get data sent to us by Infusionsoft
$infsoft_contact_id = stripslashes( $_POST['contactId'] );
$remote_access_key = stripslashes( $_POST['access_key'] );
$role = stripslashes( $_POST['bbp_role'] );
// Verify that the infusion soft access key is what the server uses.
// This is a security measure to ensure people can't just change their own role, unless they know the access key.
if ( $internal_access_key !== $remote_access_key ) {
wp_die('Invalid infusionsoft access key.');
exit;
}
// We're confident this is a legit request, so now we just need to get the user.
// If the wordpress user ID is stored in infusionsoft, you could send that as another parameter.
// Otherwise we need to locate which user the "Infusionsoft Contact ID" is assigned to.
$user_query = new WP_User_Query(array(
'meta_query' => array(
array(
'key' => 'infusionsoft_contact_id', // IMPORTANT: Change this to the user meta key that has the infusionsoft ID.
'value' => $infsoft_contact_id,
)
)
));
$results = $user_query->get_results();
if ( empty($results) ) {
// If you get this error, ensure that the meta key used in "user_query" is correct!
wp_die('Invalid infusionsoft contact ID ('. esc_attr($infsoft_contact_id) .'). That user no longer exists on this website.');
exit;
}
// Get the first user from our query. There should only be one.
$user = $results[0];
// Assign the user role with BBPress (from @robin-w)
bbp_set_user_role( $user->ID, $role );
// Send a 1 to the browser to indicate success, then exit.
echo '1';
exit;
}
// Only hook in if the request uses our custom action "bbp_action" = "set_role"
if ( isset($_POST['bbp_action']) && $_POST['bbp_action'] === 'set_role' ) {
add_action( 'init', 'bbp_infsoft_hook_set_role' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment