/**
  * Creates group membership for the order.
  * @param int $order_id
  */
 public static function order_status_completed($order_id)
 {
     $unhandled = self::register_order($order_id);
     $order = new WC_Order();
     if ($order->get_order($order_id)) {
         if ($items = $order->get_items()) {
             if ($user_id = $order->user_id) {
                 // not much we can do if there isn't
                 foreach ($items as $item) {
                     if ($product = $order->get_product_from_item($item)) {
                         if ($product_groups = get_post_meta($product->id, '_groups_groups', false)) {
                             // don't act on subscriptions here
                             if (!class_exists('WC_Subscriptions_Product') || !WC_Subscriptions_Product::is_subscription($product->id)) {
                                 if (count($product_groups) > 0) {
                                     // add the groups to the user by order and product so that if the product is changed later on,
                                     // the data is still valid for what has been purchased
                                     $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;
                                     update_user_meta($user_id, '_groups_product_groups', $groups_product_groups);
                                     global $groups_ws_product_with_duration;
                                     $groups_ws_product_with_duration = Groups_WS_Product::has_duration($product);
                                     // 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));
                                         if ($groups_ws_product_with_duration) {
                                             if ($unhandled) {
                                                 Groups_WS_Terminator::schedule_termination(time() + Groups_WS_Product::get_duration($product), $user_id, $group_id);
                                             }
                                         } else {
                                             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 (!class_exists('WC_Subscriptions_Product') || !WC_Subscriptions_Product::is_subscription($product->id)) {
                                 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);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * 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);
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * Creates group membership for the order.
  * @param int $order_id
  */
 public static function order_status_completed($order_id)
 {
     $unhandled = self::register_order($order_id);
     if (!$unhandled) {
         if (GROUPS_WS_LOG) {
             error_log(sprintf(__METHOD__ . ' abandoned due to previously handled order ID %d', $order_id));
         }
         return;
     }
     if ($order = Groups_WS_Helper::get_order($order_id)) {
         if ($items = $order->get_items()) {
             if ($user_id = $order->user_id) {
                 // not much we can do if there isn't
                 foreach ($items as $item) {
                     if ($product = $order->get_product_from_item($item)) {
                         // add to groups
                         $product_groups = get_post_meta($product->id, '_groups_groups', false);
                         if ($product->product_type == 'variation') {
                             if (isset($product->variation_id)) {
                                 if ($variation_product_groups = get_post_meta($product->variation_id, '_groups_variation_groups', false)) {
                                     $product_groups = array_merge($product_groups, $variation_product_groups);
                                 }
                             }
                         }
                         if ($product_groups) {
                             // don't act on subscriptions here
                             if (!class_exists('WC_Subscriptions_Product') || !WC_Subscriptions_Product::is_subscription($product->id)) {
                                 if (count($product_groups) > 0) {
                                     // add the groups to the user by order and product so that if the product is changed later on,
                                     // the data is still valid for what has been purchased
                                     $groups_product_groups = get_user_meta($user_id, '_groups_product_groups', true);
                                     if (empty($groups_product_groups)) {
                                         $groups_product_groups = array();
                                     }
                                     $start = time();
                                     $groups_product_groups[$order_id][$product->id]['version'] = GROUPS_WS_VERSION;
                                     $groups_product_groups[$order_id][$product->id]['start'] = $start;
                                     $groups_product_groups[$order_id][$product->id]['groups'] = $product_groups;
                                     update_user_meta($user_id, '_groups_product_groups', $groups_product_groups);
                                     global $groups_ws_product_with_duration;
                                     $groups_ws_product_with_duration = Groups_WS_Product::has_duration($product);
                                     if ($groups_ws_product_with_duration) {
                                         $groups_product_groups[$order_id][$product->id]['duration'] = get_post_meta($product->id, '_groups_duration', true);
                                         $groups_product_groups[$order_id][$product->id]['duration_uom'] = get_post_meta($product->id, '_groups_duration_uom', true);
                                         update_user_meta($user_id, '_groups_product_groups', $groups_product_groups);
                                     }
                                     // add the user to the groups
                                     foreach ($product_groups as $group_id) {
                                         Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $group_id));
                                         if ($groups_ws_product_with_duration) {
                                             Groups_WS_Terminator::schedule_termination($start + Groups_WS_Product::get_duration($product), $user_id, $group_id);
                                         } else {
                                             Groups_WS_Terminator::mark_as_eternal($user_id, $group_id);
                                         }
                                     }
                                 }
                             }
                         }
                         // remove from groups
                         $product_groups_remove = get_post_meta($product->id, '_groups_groups_remove', false);
                         if ($product->product_type == 'variation') {
                             if (isset($product->variation_id)) {
                                 if ($variation_product_groups_remove = get_post_meta($product->variation_id, '_groups_variation_groups_remove', false)) {
                                     $product_groups_remove = array_merge($product_groups_remove, $variation_product_groups_remove);
                                 }
                             }
                         }
                         if ($product_groups_remove) {
                             if (!class_exists('WC_Subscriptions_Product') || !WC_Subscriptions_Product::is_subscription($product->id)) {
                                 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) {
                                         self::maybe_delete($user_id, $group_id, $order_id);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }