}
        }
        $U_in_G = false;
        $groups_user = new Groups_User($user_id);
        foreach ($groups_user->groups as $group) {
            if ($group->group_id === $group_id) {
                $U_in_G = true;
                break;
            }
        }
        if ($U_in_G) {
            if (count($P) > 0) {
                if (count($B) == count($P)) {
                    Groups_User_Group::delete($user_id, $group_id);
                    if (GROUPS_WS_LOG) {
                        error_log(sprintf(__METHOD__ . ' terminated membership for user ID %d with group ID %d', $user_id, $group_id));
                    }
                    $B = array();
                } else {
                    $B = array_diff($B, $P);
                }
            }
        } else {
            $B = array_diff($B, $P);
        }
        $b->content = $B;
        $b->release();
    }
}
Groups_WS_Terminator::init();
 /**
  * Update the user profile.
  * 
  * @param int $user_id
  */
 public static function edit_user_profile_update($user_id)
 {
     global $wpdb;
     if (current_user_can(GROUPS_ADMINISTER_GROUPS)) {
         if (!empty($_POST['gw-ts']) && is_array($_POST['gw-ts'])) {
             foreach ($_POST['gw-ts'] as $group_id => $timestamps) {
                 foreach ($timestamps as $ts => $date) {
                     $year = !empty($date['year']) ? intval($date['year']) : null;
                     $month = !empty($date['month']) ? intval($date['month']) : 1;
                     $day = !empty($date['day']) ? intval($date['day']) : 1;
                     $hour = !empty($date['hour']) ? intval($date['hour']) : 0;
                     $minute = !empty($date['minute']) ? intval($date['minute']) : 0;
                     $second = !empty($date['second']) ? intval($date['second']) : 0;
                     if ($year !== null) {
                         $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
                         if ($ts != $timestamp) {
                             // Only allow extensions.
                             // If the time is set to a point in the past and the
                             // core groups field still has the group entry (which would
                             // need to be expected, then the eternity timestamp would
                             // be removed form the bucket but the user-group
                             // assignment would still be intact.
                             // Instead of setting the time to a point in the past,
                             // the group can simply be removed from the core
                             // groups field (by the user who is editing the profile).
                             if ($timestamp > time()) {
                                 Groups_WS_Terminator::lift_scheduled_terminations($user_id, $group_id, false);
                                 Groups_WS_Terminator::schedule_termination($timestamp, $user_id, $group_id);
                             }
                         }
                     } else {
                         if ($ts !== Groups_WS_Terminator::ETERNITY) {
                             Groups_WS_Terminator::lift_scheduled_terminations($user_id, $group_id);
                             Groups_WS_Terminator::mark_as_eternal($user_id, $group_id);
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * Handle group assignment : assign the user to the groups related to the subscription's product.
  * @param int $user_id
  * @param string $subscription_key
  */
 public static function activated_subscription($user_id, $subscription_key)
 {
     $subscription = WC_Subscriptions_Manager::get_users_subscription($user_id, $subscription_key);
     if (isset($subscription['product_id']) && isset($subscription['order_id'])) {
         $product_id = $subscription['product_id'];
         $order_id = $subscription['order_id'];
         // Leasving this here for reference, it can be assumed that normally,
         // if the product's groups are modified, a reactivation should take its
         // data from the current product, not from its previous state.
         // See if the subscription was activated before and try to get subscription's groups.
         // If there are any, use these instead of those from the product.
         // This is necessary when a subscription has been cancelled and re-activated and the
         // original product groups were modified since and we do NOT want to make group
         // assignments based on the current state of the product.
         $done = false;
         //$groups_product_groups = get_user_meta( $user_id, '_groups_product_groups', true );
         //if ( isset( $groups_product_groups[$order_id] ) && isset( $groups_product_groups[$order_id][$product_id] ) &&
         //	 isset( $groups_product_groups[$order_id][$product_id]['groups'] ) &&
         //	 isset( $groups_product_groups[$order_id][$product_id]['subscription_key'] ) &&
         //	( $groups_product_groups[$order_id][$product_id]['subscription_key'] === $subscription_key )
         //) {
         //	foreach( $groups_product_groups[$order_id][$product_id]['groups'] as $group_id ) {
         //		Groups_User_Group::create( $user_id, $group_id );
         //	}
         //	$done = true;
         //}
         // maybe unschedule pending expiration
         wp_clear_scheduled_hook('groups_ws_subscription_expired', array('user_id' => $user_id, 'subscription_key' => $subscription_key));
         if (!$done) {
             // get the product from the subscription
             $product = new WC_Product($product_id);
             if ($product->exists()) {
                 // get the groups related to the product
                 if ($product_groups = get_post_meta($product_id, '_groups_groups', false)) {
                     if (count($product_groups) > 0) {
                         // add the groups to the subscription (in case the product is changed later on, the subscription is still valid)
                         $groups_product_groups = get_user_meta($user_id, '_groups_product_groups', true);
                         if (empty($groups_product_groups)) {
                             $groups_product_groups = array();
                         }
                         $groups_product_groups[$order_id][$product_id]['groups'] = $product_groups;
                         $groups_product_groups[$order_id][$product_id]['subscription_key'] = $subscription_key;
                         update_user_meta($user_id, '_groups_product_groups', $groups_product_groups);
                         // add the user to the groups
                         foreach ($product_groups as $group_id) {
                             $result = Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $group_id));
                         }
                         Groups_WS_Terminator::mark_as_eternal($user_id, $group_id);
                     }
                 }
                 // remove from groups
                 if ($product_groups_remove = get_post_meta($product_id, '_groups_groups_remove', false)) {
                     if (count($product_groups_remove) > 0) {
                         $groups_product_groups_remove = get_user_meta($user_id, '_groups_product_groups_remove', true);
                         if (empty($groups_product_groups_remove)) {
                             $groups_product_groups_remove = array();
                         }
                         $groups_product_groups_remove[$order_id][$product_id]['groups'] = $product_groups_remove;
                         update_user_meta($user_id, '_groups_product_groups_remove', $groups_product_groups_remove);
                         // remove the user from the groups
                         foreach ($product_groups_remove as $group_id) {
                             $result = Groups_User_Group::delete($user_id, $group_id);
                         }
                     }
                 }
             }
         }
     }
 }