<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! sfwc_is_plugin_active( 'user-switching.php' ) )
	return;

/**
 * When a user switches back to their originating account, destroy Manager and Supervisor cookies if present.
 * 
 * Cookies to be destroyed when a user switches back:
 * 
 *	is_manager
 *	is_supervisor
 *	subaccount_ids
 */
function integration_for_user_switching_plugin( $user_id, $old_user_id, $new_token, $old_token ) {
	
	if ( isset( $_COOKIE[ apply_filters( 'sfwc_cookie_prefix', '' ) . 'is_manager'] ) ) {
		// Destroy is_manager cookie.
		setcookie( apply_filters( 'sfwc_cookie_prefix', '' ) . 'is_manager', '', -1, COOKIEPATH);
	}
	
	if ( isset( $_COOKIE[ apply_filters( 'sfwc_cookie_prefix', '' ) . 'is_supervisor'] ) ) {
		// Destroy is_supervisor cookie
		setcookie( apply_filters( 'sfwc_cookie_prefix', '' ) . 'is_supervisor', '', -1, COOKIEPATH);
	}
	
	if ( isset( $_COOKIE[ apply_filters( 'sfwc_cookie_prefix', '' ) . 'subaccount_ids'] ) ) {
		// Destroy subaccount_ids cookie.
		setcookie( apply_filters( 'sfwc_cookie_prefix', '' ) . 'subaccount_ids', '', -1, COOKIEPATH);
	}

}
add_action( 'switch_back_user', 'integration_for_user_switching_plugin', 20, 4 );