Created
August 11, 2015 10:41
-
-
Save bappi-d-great/cf1c337e5b7cdba77eb3 to your computer and use it in GitHub Desktop.
Remove role and assign subscriber role to the user or membership cancel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'ms_model_event', 'my_event_handler', 10, 2 ); | |
/** | |
* Handles an event and process the correct communication if required. | |
* | |
* @param MS_Model_Event $event The event that is processed. | |
* @param mixed $data The data passed to the event handler. | |
*/ | |
function my_event_handler( $event, $data ) { | |
$member = false; | |
$subscription = false; | |
$membership = false; | |
switch ( $event->type ) { | |
case MS_Model_Event::TYPE_MS_CANCELED: | |
// A membership was cancelled - either by Admin or by the member. | |
// No more payments will be made but member has access until current period ends. | |
$subscription = $data; | |
$membership = $data->get_membership(); | |
$member = $subscription->get_member(); | |
assign_default_role( $member->id ); | |
break; | |
case MS_Model_Event::TYPE_MS_DEACTIVATED: | |
// A membership was permanently deactivated. Member has no access anymore. | |
$subscription = $data; | |
$membership = $data->get_membership(); | |
$member = $subscription->get_member(); | |
assign_default_role( $member->id ); | |
break; | |
} | |
} | |
function assign_default_role( $user_id = 0 ){ | |
$user = new WP_User( $user_id ); | |
$user->set_role( 'subscriber' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment