Skip to content

Instantly share code, notes, and snippets.

@hmouhtar
Created June 3, 2022 16:51
Show Gist options
  • Save hmouhtar/597311811fdf34ba6fdf78f3a79b7460 to your computer and use it in GitHub Desktop.
Save hmouhtar/597311811fdf34ba6fdf78f3a79b7460 to your computer and use it in GitHub Desktop.
Gravity Flow - Redirect based on current user step.
/*
The Collective Rising relies on Gravity Flow to automate the membership workflow. After a member applies, an admin from TCR
accepts/rejects the application. Accepted members must complete a form with information that will be used to create their profile
in the website, and they should be redirected to that form if they haven't finished it yet and try to navigate to their profile.
This code snippet, although short, required a lot of research on the methods Gravity Flow used to gather information of the user progress
in the workflow.
*/
function redirect_to_profile_builder() {
$user = wp_get_current_user();
if ( ! is_wp_error( $user ) && in_array( 'tcr_member', $user->roles ) ) {
$requestInvitationFormID = 49; // the ID of the Gravity Form used to apply to a membership.
$sendToCreateProfileStepIDs = array( 200, 155 ); // Each step in Gravity Flow has an ID, these are the IDs of the steps to redirect users to the profile builder.
$entrySearchCriteria = array( // Gravity Form entry search criteria
'field_filters' => array(
array(
'key' => 'created_by',
'value' => $user->ID,
),
),
);
$gFlowAPI = new Gravity_Flow_API( $requestInvitationFormID ); // Each Gravity Form has a Gravity Flow workflow linked to it.
$entry = GFAPI::get_entries( $requestInvitationFormID, $entrySearchCriteria )[0]; // Get Gravity Form first entry from the current user.
$currentEntryStep = $gFlowAPI->get_current_step( $entry ); // Get the current step of this user in the Gravity workflow.
if ( $currentEntryStep && in_array( $currentEntryStep->get_id(), $sendToCreateProfileStepIDs ) ) { // Check if the user still hasn't completed the create profile step.
$meprUser = MeprUtils::get_currentuserinfo();
$createProfileFormURL = $currentEntryStep->get_target_form_url( $currentEntryStep->submit_page );
if ( intval( $meprUser->get_num_logins() ) <= 1 && empty( $_REQUEST['login'] ) ) { // If this is the first time the user is logging in after being invited, show a welcome message.
MeprEvent::record( 'login', $meprUser );
wp_redirect( add_query_arg( 'login', 'first', $createProfileFormURL ) );
} else {
wp_redirect( add_query_arg( 'profile', 'required', $createProfileFormURL ) );
}
exit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment