Skip to content

Instantly share code, notes, and snippets.

@itthinx
Last active August 29, 2015 14:25
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 itthinx/7f08f37bf3e93f9d4c99 to your computer and use it in GitHub Desktop.
Save itthinx/7f08f37bf3e93f9d4c99 to your computer and use it in GitHub Desktop.
/**
* Yves' implementation to add a capability to a group when a purchase has been completed.
*
* http://www.itthinx.com/plugins/groups-paypal/comment-page-1/#comment-647332
*/
add_action('frm_payment_paypal_ipn', 'ls4s_sprint_purchase_completed');
function ls4s_sprint_purchase_completed($args)
{
global $frmdb, $wpdb;
$entry_id = $args['payment']->item_id;
$form_id = $args['entry']->form_id;
if ($form_id == LS4S_FRM_FORM_SPURCH) { // fire on Sprint Purchased Form only
if ( !$args['pay_vars']['completed'] ) return; // do nothing if payment not completed
$wpdb->update( $frmdb->entry_metas, array('meta_value' => 'Yes'), array(
'item_id' => $entry_id, 'field_id' => LS4S_FRM_FIELD_SPURCH_PCOMP
) ); // payment was completed so flag set to 'Yes' (my stuff)
// Retrieving the group and capability names from the form fields:
$ptype = FrmProEntriesController::get_field_value_shortcode(array('field_id' => LS4S_FRM_FIELD_SPURCH_PTYPE, 'entry_id' => $entry_id));
$pname = FrmProEntriesController::get_field_value_shortcode(array('field_id' => LS4S_FRM_FIELD_SPURCH_PNAME, 'entry_id' => $entry_id));
$psprn = FrmProEntriesController::get_field_value_shortcode(array('field_id' => LS4S_FRM_FIELD_SPURCH_SNUMB, 'entry_id' => $entry_id));
$pslug = strtolower(sanitize_title($pname, 'j-u-n-k')); // program name slug
$pgrpname = strtolower($ptype) . '_' . $pslug; // program group name
$pcapname = $pgrpname . '__' . $psprn; // name of the capability to be added to the program group
$group = Groups_Group::read_by_name( $pgrpname );
$capability = Groups_Capability::read_by_capability( $pcapname );
if ( $group && $capability ) { // add the capability to the group
if ( !Groups_Group_Capability::read( $group->group_id, $capability->capability_id ) ) {
Groups_Group_Capability::create(array('group_id' => $group->group_id, 'capability_id' => $capability->capability_id));
}
}
// Maybe other things to do when payment is completed, such as set return url, etc.
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment