<?php
add_action('mepr-event-member-added','my_mepr_event_member_added');
function my_mepr_event_member_added($userObj) {
	// GET USER DATA
	$user = $userObj->get_data();
	
	// CREATE META POST TO REGISTER NEW USER TAGS
	$categoryObj = get_category_by_slug( 'metaposts' );
	$post_title = 'META-POST-' . $user->user_nicename;
	$filter_tag = get_user_filter_tags($user);
	$new_post_id = wp_insert_post(array (
		'post_type' => 'post',
		'post_title' => $post_title,
		'post_status' => 'private',
		'comment_status' => 'closed',
		'ping_status' => 'closed',
		'tags_input' => array( $user->user_nicename, $filter_tag[requests], $filter_tag[expired], $filter_tag[appointment] ),
		'post_category' => array( $categoryObj->term_id ),
	));			
	if ( $new_post_id ) {
		php_console_log('Post created with ID: ' . $new_post_id);			
	}	
	
	// CUSTOM USER METADATA
	add_user_meta( $user->ID, 'appointments_old', 0, false );
	add_user_meta( $user->ID, 'user_login_count', 0, false );
	
	// GET NEWLY CREATED TAG ID
	$tag = get_term_by('slug', $user->user_nicename, 'post_tag');
	$tag_id =  $tag->term_id;	
	
	// ADD RULE VIA RESTAPI
	$post_title = 'Posts Tagged: ' . $user->user_nicename . ' (auto)';
	$data_add = array(
	  	'post_title'   			=> $post_title,
		'access_type' 			=> 'member',
	 	'access_operator'   	=> 'is',
		'access_condition' 		=> $user->user_email,
		'rule_type'   			=> 'tag',
		'unauth_excerpt_type' 	=> 'more',
	 	'rule_content'    		=> $tag_id,
	);
	$response = mbpr_restapi_add_rule($data_add);
	
	// LOGOUT
	//wp_logout();
	// get all sessions for user with ID $user_id
	$sessions = WP_Session_Tokens::get_instance($user->ID);

	// we have got the sessions, destroy them all!
	$sessions->destroy_all();
}