Skip to content

Instantly share code, notes, and snippets.

@razorfrog
Last active April 4, 2024 16:22
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 razorfrog/9a785a848248f041f1f4ab13379cacad to your computer and use it in GitHub Desktop.
Save razorfrog/9a785a848248f041f1f4ab13379cacad to your computer and use it in GitHub Desktop.
Gravity Forms to Groups.IO subgroup signup
<?php
/////////////////////////////////////////////////////////////
// Gravity Forms to groups.io subgroup signup
// API details: https://groups.io/api/20190816#login
// Code details: https://razorfrog.com
/////////////////////////////////////////////////////////////
add_action( 'gform_after_submission_2', 'post_to_third_party', 10, 2 ); // Edit the "_2" to the correct form ID
function post_to_third_party( $entry, $form ) {
// https://docs.gravityforms.com/entry-object/
// Customize these numbers to the correct field IDs
$email = rgar( $entry, '1' ); // Email field
$notes = rgar( $entry, '5' ); // Admin Member Notes field
$firstname = rgar( $entry, '4.3' ); // First Name field
$lastname = rgar( $entry, '4.6' ); // Last Name field
$fullname = $firstname.' '.$lastname;
$acctEmail = 'name@domain.com'; // Admin account email address
$acctPassword = 'password'; // Admin account password
$groupName = 'group-name'; // Your group name
$url = 'https://groups.io/api/v1/login?email='.$acctEmail.'&password='.$acctPassword.'&token=true';
$response = wp_remote_post($url);
$token = 'false';
if (is_wp_error($response)) {
$error = $term->get_error_message();
// GFCommon::log_debug('Error in obtaining token from groups.io'. $error);
return false;
} else {
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body );
$token = $data->token;
// GFCommon::log_debug('Token received is ' . $token);
}
if( $token != 'false' ) {
// get subgroup checkboxes
// Details: https://docs.gravityforms.com/entry-object/#h-checkboxes-field
$form_or_id = 2; // Update this to the form id
$field_id = 3; // Update this number to the checkbox field id
$field = GFAPI::get_field( $form_or_id, $field_id );
$checkboxes = is_object( $field ) ? $field->get_value_export( $entry, $field_id, false ) : ''; // returns values
$checkboxes = str_replace(' ', '', $checkboxes);
// GFCommon::log_debug('Checkbox values:'.$checkboxes);
$url = 'https://groups.io/api/v1/directadd';
$args = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $token . ':' . '' )
),
'body' => array(
'group_name' => $groupName,
'emails' => $email,
'subgroupnames' => $checkboxes
),
);
$request = wp_remote_post( $url, $args );
// GFCommon::log_debug('foorequest: '.print_r($request,true));
$memberID = json_decode($request['body'],true)['added_members'][0]['id'];
// second API request to set user first/last name
$url2 = 'https://groups.io/api/v1/updatemember';
$args2 = array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $token . ':' . '' )
),
'body' => array(
'group_name' => $groupName,
'member_info_id' => $memberID,
'full_name' => $fullname,
'moderator_notes' => $notes
),
);
$request2 = wp_remote_post( $url2, $args2 );
// GFCommon::log_debug('Request2: '.print_r($request2,true));
if( is_wp_error($request) ) {
// GFCommon::log_debug('wp error:'.print_r($request,true));
return false;
} else {
// GFCommon::log_debug('Add sent!'.print_r($request,true));
return true;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment