Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MaximilianoRicoTabo/8b53e89ff7e9dcfd3677e6955b8ee6a3 to your computer and use it in GitHub Desktop.
Save MaximilianoRicoTabo/8b53e89ff7e9dcfd3677e6955b8ee6a3 to your computer and use it in GitHub Desktop.
Retroactively assign a user to a Parent account when using the Sponsored Members Add On.
<?php
/**
* Retroactively assign a user to a Parent account when using the Sponsored Members Add On.
* Enter the user ID of parent account in the "Assign to Parent Account" on the "Edit Profile" screen.
* This gist assume user being edited has an active membership and it's a sponsored level.
*
* Note: The user's active Membership Level must be the correct "Child" level for that Parent/Child relationship.
*
*/
function pmprosm_assign_child_members( $profileuser ) {
if ( function_exists( 'pmprosm_getSponsor' ) && current_user_can( 'edit_users' ) ) {
// Get the parent for this user.
$parent = pmprosm_getSponsor( $profileuser->ID );
if ( ! empty( $parent ) ) {
$parent_id = $parent->ID;
} else {
$parent_id = '';
}
// Show the field to update the parent for this user.
if ( empty( $parent_id ) ) { ?>
<table class="form-table">
<tr>
<th>
<label for="assign_child_member"><?php _e('Assign to Parent Account'); ?></label>
</th>
<td>
<input type="text" value="<?php echo $parent_id; ?>" name="assign_child_member" id="assign_child_member" />
<p class="description">Enter the Parent Account's user ID for this user.</p>
</td>
</tr>
</table>
<?php
}
}
}
add_action( 'show_user_profile', 'pmprosm_assign_child_members', 20, 1 );
add_action( 'edit_user_profile', 'pmprosm_assign_child_members', 20, 1 );
function pmprosm_profile_assign_child_members( $user_id, $old_user_data ) {
if ( isset( $_POST[ 'assign_child_member' ] ) && (int)( $_POST[ 'assign_child_member' ] ) ){
// Get the vars.
$parent_id = $_POST[ 'assign_child_member' ];
$sponsored_code = pmprosm_getCodeByUserID( $parent_id );
//get the parent user
$parent_user = get_userdata( $parent_id );
//get the parent's membership level
$main_level = pmpro_getMembershipLevelForUser( $parent_id );
$sponsored_level = pmpro_getMembershipLevelForUser( $user_id );
if ( pmprosm_isSponsoredLevel( $sponsored_level->id ) ) {
// Set their new membership level and assign their sponsor.
pmprosm_changeMembershipLevelWithCode( $sponsored_level->id, $user_id, $sponsored_code );
pmprosm_addDiscountCodeUse( $user_id, $sponsored_level->id, $sponsored_code );
}
}
}
add_action( 'profile_update', 'pmprosm_profile_assign_child_members', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment