function find_all_groups_for_user($user_id)
{
    $result = array();
    // Find all possible capabilites
    $all_groups = Groups_Group::get_groups();
    // Iterate, find what capabilites the user has
    foreach ($all_groups as $group) {
        $OK = Groups_User_Group::read($user_id, $group->group_id);
        if ($OK) {
            $result[] = $group;
        }
    }
    return $result;
}
Ejemplo n.º 2
0
 /**
  * Returns true if the user belongs to the group.
  * 
  * @param int $group_id
  * @return boolean
  */
 public function is_member($group_id)
 {
     $result = false;
     if ($this->user !== null) {
         if (isset($this->user->ID)) {
             $user_group = Groups_User_Group::read(Groups_Utility::id($this->user->ID), Groups_Utility::id($group_id));
             $result = $user_group !== false;
             unset($user_group);
         }
     }
     return $result;
 }
Ejemplo n.º 3
0
 /**
  * Adds or removes users to/from groups.
  */
 public static function load_users()
 {
     if (current_user_can(GROUPS_ADMINISTER_GROUPS)) {
         $users = isset($_REQUEST['users']) ? $_REQUEST['users'] : null;
         $action = null;
         if (!empty($_REQUEST['groups'])) {
             if ($_GET['groups-action'] == "add-group") {
                 $action = 'add';
             } else {
                 if ($_GET['groups-action'] == "remove-group") {
                     $action = 'remove';
                 }
             }
         }
         if ($users !== null && $action !== null) {
             if (wp_verify_nonce($_REQUEST['bulk-user-group-nonce'], 'user-group')) {
                 foreach ($users as $user_id) {
                     switch ($action) {
                         case 'add':
                             $group_ids = isset($_GET['group_ids']) ? $_GET['group_ids'] : null;
                             if ($group_ids !== null) {
                                 foreach ($group_ids as $group_id) {
                                     if (!Groups_User_Group::read($user_id, $group_id)) {
                                         Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $group_id));
                                     }
                                 }
                             }
                             break;
                         case 'remove':
                             $group_ids = isset($_GET['group_ids']) ? $_GET['group_ids'] : null;
                             if ($group_ids !== null) {
                                 foreach ($group_ids as $group_id) {
                                     if (Groups_User_Group::read($user_id, $group_id)) {
                                         Groups_User_Group::delete($user_id, $group_id);
                                     }
                                 }
                             }
                             break;
                     }
                 }
                 $referer = wp_get_referer();
                 if ($referer) {
                     $redirect_to = remove_query_arg(array('action', 'action2', 'add-to-group', 'bulk-user-group-nonce', 'group_id', 'new_role', 'remove-from-group', 'users'), $referer);
                     wp_redirect($redirect_to);
                     exit;
                 }
             }
         }
     }
 }
Ejemplo n.º 4
0
function woocommerce_dynamic_pricing_groups_is_rule_set_valid_for_user($result, $condition, $rule_set)
{
    $groups_user = new Groups_User(get_current_user_id());
    switch ($condition['type']) {
        case 'apply_to':
            if (is_array($condition['args']) && isset($condition['args']['applies_to'])) {
                if ($condition['args']['applies_to'] == 'groups' && isset($condition['args']['groups']) && is_array($condition['args']['groups'])) {
                    if (is_user_logged_in()) {
                        foreach ($condition['args']['groups'] as $group) {
                            $current_group = Groups_Group::read($group);
                            if ($current_group) {
                                if (Groups_User_Group::read($groups_user->user->ID, $current_group->group_id)) {
                                    $result = 1;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            break;
        default:
            break;
    }
    return $result;
}
 /**
  * Renders a form that lets a user leave a group.
  * * Attributes:
  * - "group" : (required) group name or id
  *
  * @param array $atts attributes
  * @param string $content not used
  */
 public static function groups_leave($atts, $content = null)
 {
     $nonce_action = 'groups_action';
     $nonce = 'nonce_leave';
     $output = "";
     $options = shortcode_atts(array('group' => '', 'display_message' => true, 'submit_text' => __('Leave the %s group', GROUPS_PLUGIN_DOMAIN)), $atts);
     extract($options);
     if ($display_message === 'false') {
         $display_message = false;
     }
     $group = trim($options['group']);
     $current_group = Groups_Group::read($group);
     if (!$current_group) {
         $current_group = Groups_Group::read_by_name($group);
     }
     if ($current_group) {
         if ($user_id = get_current_user_id()) {
             $submitted = false;
             $invalid_nonce = false;
             if (!empty($_POST['groups_action']) && $_POST['groups_action'] == 'leave') {
                 $submitted = true;
                 if (!wp_verify_nonce($_POST[$nonce], $nonce_action)) {
                     $invalid_nonce = true;
                 }
             }
             if ($submitted && !$invalid_nonce) {
                 // remove user from group
                 if (isset($_POST['group_id'])) {
                     $leave_group = Groups_Group::read($_POST['group_id']);
                     Groups_User_Group::delete($user_id, $leave_group->group_id);
                 }
             }
             if (Groups_User_Group::read($user_id, $current_group->group_id)) {
                 $submit_text = sprintf($options['submit_text'], wp_filter_nohtml_kses($current_group->name));
                 $output .= '<div class="groups-join">';
                 $output .= '<form action="#" method="post">';
                 $output .= '<input type="hidden" name="groups_action" value="leave" />';
                 $output .= '<input type="hidden" name="group_id" value="' . esc_attr($current_group->group_id) . '" />';
                 $output .= '<input type="submit" value="' . $submit_text . '" />';
                 $output .= wp_nonce_field($nonce_action, $nonce, true, false);
                 $output .= '</form>';
                 $output .= '</div>';
             } else {
                 if ($display_message) {
                     if ($submitted && !$invalid_nonce && isset($leave_group) && $leave_group->group_id === $current_group->group_id) {
                         $output .= '<div class="groups-join left">';
                         $output .= sprintf(__('You have left the %s group.', GROUPS_PLUGIN_DOMAIN), wp_filter_nohtml_kses($leave_group->name));
                         $output .= '</div>';
                     }
                 }
             }
         }
     }
     return $output;
 }
 /**
  * Deletes the user from the group if no other orders than $order_id
  * currently grant membership to that group.
  * 
  * @param int $user_id
  * @param int $group_id
  * @param int $order_id
  */
 public static function maybe_delete($user_id, $group_id, $order_id)
 {
     $order_ids = self::get_valid_order_ids_granting_group_membership_from_order_items($user_id, $group_id);
     $ids = array_diff($order_ids, array($order_id));
     if (count($ids) == 0) {
         Groups_User_Group::delete($user_id, $group_id);
         if (GROUPS_WS_LOG) {
             error_log(sprintf(__METHOD__ . ' deleted membership for user ID %d with group ID %d', $user_id, $group_id));
         }
     } else {
         if (GROUPS_WS_LOG) {
             error_log(sprintf(__METHOD__ . ' membership for user ID %d with group ID %d has not been deleted due to other orders granting membership, order IDs: %s', $user_id, $group_id, implode(',', $order_ids)));
         }
     }
 }
 /**
  * Updates the group membership.
  * @param int $user_id
  */
 public static function edit_user_profile_update($user_id)
 {
     global $wpdb;
     if (current_user_can(GROUPS_ADMINISTER_GROUPS)) {
         $groups_table = _groups_get_tablename('group');
         if ($groups = $wpdb->get_results("SELECT * FROM {$groups_table}")) {
             $user_group_ids = isset($_POST['group_ids']) && is_array($_POST['group_ids']) ? $_POST['group_ids'] : array();
             foreach ($groups as $group) {
                 if (in_array($group->group_id, $user_group_ids)) {
                     if (!Groups_User_Group::read($user_id, $group->group_id)) {
                         Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $group->group_id));
                     }
                 } else {
                     if (Groups_User_Group::read($user_id, $group->group_id)) {
                         Groups_User_Group::delete($user_id, $group->group_id);
                     }
                 }
             }
         }
     }
 }
 /**
  * Save capability options.
  * 
  * @param int $post_id
  * @param mixed $post post data (not used here)
  */
 public static function save_post($post_id = null, $post = null)
 {
     if (defined("DOING_AUTOSAVE") && DOING_AUTOSAVE) {
     } else {
         $post_type = get_post_type($post_id);
         $post_type_object = get_post_type_object($post_type);
         if ($post_type_object && $post_type != 'attachment') {
             $post_types_option = Groups_Options::get_option(Groups_Post_Access::POST_TYPES, array());
             if (!isset($post_types_option[$post_type]['add_meta_box']) || $post_types_option[$post_type]['add_meta_box']) {
                 if (isset($_POST[self::NONCE]) && wp_verify_nonce($_POST[self::NONCE], self::SET_CAPABILITY)) {
                     $post_type = isset($_POST["post_type"]) ? $_POST["post_type"] : null;
                     if ($post_type !== null) {
                         // See http://codex.wordpress.org/Function_Reference/current_user_can 20130119 WP 3.5
                         // "... Some capability checks (like 'edit_post' or 'delete_page') require this [the post ID] be provided."
                         // If the post ID is not provided, it will throw:
                         // PHP Notice:  Undefined offset: 0 in /var/www/groups-forums/wp-includes/capabilities.php on line 1067
                         $edit_post_type = 'edit_' . $post_type;
                         if ($post_type_object = get_post_type_object($post_type)) {
                             if (!isset($post_type_object->capabilities)) {
                                 // get_post_type_capabilities() (WP 3.8) will throw a warning
                                 // when trying to merge the missing property otherwise. It's either a
                                 // bug or the function's documentation should make it clear that you
                                 // have to provide that.
                                 $post_type_object->capabilities = array();
                             }
                             $caps_object = get_post_type_capabilities($post_type_object);
                             if (isset($caps_object->edit_post)) {
                                 $edit_post_type = $caps_object->edit_post;
                             }
                         }
                         if (current_user_can($edit_post_type, $post_id)) {
                             // quick-create ?
                             if (current_user_can(GROUPS_ADMINISTER_GROUPS)) {
                                 if (!empty($_POST['quick-group-capability'])) {
                                     $creator_id = get_current_user_id();
                                     $datetime = date('Y-m-d H:i:s', time());
                                     $name = ucfirst(strtolower(trim($_POST['quick-group-capability'])));
                                     if (strlen($name) > 0) {
                                         // create or obtain the group
                                         if ($group = Groups_Group::read_by_name($name)) {
                                         } else {
                                             if ($group_id = Groups_Group::create(compact('creator_id', 'datetime', 'name'))) {
                                                 $group = Groups_Group::read($group_id);
                                             }
                                         }
                                         // create or obtain the capability
                                         $name = strtolower($name);
                                         if ($capability = Groups_Capability::read_by_capability($name)) {
                                         } else {
                                             if ($capability_id = Groups_Capability::create(array('capability' => $name))) {
                                                 $capability = Groups_Capability::read($capability_id);
                                             }
                                         }
                                         if ($group && $capability) {
                                             // add the capability to the group
                                             if (!Groups_Group_Capability::read($group->group_id, $capability->capability_id)) {
                                                 Groups_Group_Capability::create(array('group_id' => $group->group_id, 'capability_id' => $capability->capability_id));
                                             }
                                             // enable the capability for access restriction
                                             $valid_read_caps = Groups_Options::get_option(Groups_Post_Access::READ_POST_CAPABILITIES, array(Groups_Post_Access::READ_POST_CAPABILITY));
                                             if (!in_array($capability->capability, $valid_read_caps)) {
                                                 $valid_read_caps[] = $capability->capability;
                                             }
                                             Groups_Options::update_option(Groups_Post_Access::READ_POST_CAPABILITIES, $valid_read_caps);
                                             // add the current user to the group
                                             Groups_User_Group::create(array('user_id' => get_current_user_id(), 'group_id' => $group->group_id));
                                             // put the capability ID in $_POST[self::CAPABILITY] so it is treated below
                                             if (empty($_POST[self::CAPABILITY])) {
                                                 $_POST[self::CAPABILITY] = array();
                                             }
                                             if (!in_array($capability->capability_id, $_POST[self::CAPABILITY])) {
                                                 $_POST[self::CAPABILITY][] = $capability->capability_id;
                                             }
                                         }
                                     }
                                 }
                             }
                             // set
                             if (self::user_can_restrict()) {
                                 $valid_read_caps = self::get_valid_read_caps_for_user();
                                 foreach ($valid_read_caps as $valid_read_cap) {
                                     if ($capability = Groups_Capability::read_by_capability($valid_read_cap)) {
                                         if (!empty($_POST[self::CAPABILITY]) && is_array($_POST[self::CAPABILITY]) && in_array($capability->capability_id, $_POST[self::CAPABILITY])) {
                                             Groups_Post_Access::create(array('post_id' => $post_id, 'capability' => $capability->capability));
                                         } else {
                                             Groups_Post_Access::delete($post_id, $capability->capability);
                                         }
                                     }
                                 }
                             }
                             // show groups
                             Groups_Options::update_user_option(self::SHOW_GROUPS, !empty($_POST[self::SHOW_GROUPS]));
                         }
                     }
                 }
             }
         }
     }
 }
     *  removed_user_from_blog action.
     * 
     * @param int $user_id
     * @param int $blog_id
     */
    public static function remove_user_from_blog($user_id, $blog_id)
    {
        if (is_multisite()) {
            Groups_Controller::switch_to_blog($blog_id);
        }
        global $wpdb;
        $group_table = _groups_get_tablename("group");
        $user_group_table = _groups_get_tablename("user_group");
        // We can end up here while a blog is being deleted, in that case,
        // the tables have already been deleted.
        if ($wpdb->get_var("SHOW TABLES LIKE '" . $group_table . "'") == $group_table && $wpdb->get_var("SHOW TABLES LIKE '" . $user_group_table . "'") == $user_group_table) {
            $rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$user_group_table}\n\t\t\t\tLEFT JOIN {$group_table} ON {$user_group_table}.group_id = {$group_table}.group_id\n\t\t\t\tWHERE {$user_group_table}.user_id = %d\n\t\t\t\t", Groups_Utility::id($user_id)));
            if ($rows) {
                foreach ($rows as $row) {
                    // don't optimize that, favour standard deletion
                    self::delete($row->user_id, $row->group_id);
                }
            }
        }
        if (is_multisite()) {
            Groups_Controller::restore_current_blog();
        }
    }
}
Groups_User_Group::init();
Ejemplo n.º 10
0
 /**
  * Renders time-limited group membership info for the user.
  * 
  * The <code>groups_woocommerce_show_buckets_membership</code> filter can be used to modify how membership info is rendered.
  * 
  * @param object $user
  */
 private static function show_buckets($user)
 {
     $user_buckets = get_user_meta($user->ID, '_groups_buckets', true);
     if ($user_buckets) {
         if (current_user_can(GROUPS_ADMINISTER_GROUPS)) {
             wp_enqueue_style('groups-ws-user-edit');
             wp_enqueue_script('groups-ws-edit-timestamp');
         }
         $timestamp_entries = array();
         echo '<h3>';
         echo __('Group Memberships', GROUPS_WS_PLUGIN_DOMAIN);
         echo '</h3>';
         echo '<ul>';
         uksort($user_buckets, array(__CLASS__, 'bucket_cmp'));
         foreach ($user_buckets as $group_id => $timestamps) {
             if ($group = Groups_Group::read($group_id)) {
                 if (Groups_User_Group::read($user->ID, $group_id)) {
                     echo '<li>';
                     $ts = null;
                     foreach ($timestamps as $timestamp) {
                         if (intval($timestamp) === Groups_WS_Terminator::ETERNITY) {
                             $ts = Groups_WS_Terminator::ETERNITY;
                             break;
                         } else {
                             if ($timestamp > $ts) {
                                 $ts = $timestamp;
                             }
                         }
                     }
                     if ($ts !== null) {
                         $timestamp_entries[$group_id] = $ts;
                         if ($ts === Groups_WS_Terminator::ETERNITY) {
                             $membership_info = sprintf(__('<em>%s</em> membership.', GROUPS_WS_PLUGIN_DOMAIN), wp_filter_nohtml_kses($group->name));
                         } else {
                             $date = date_i18n(get_option('date_format'), $ts);
                             $time = date_i18n(get_option('time_format'), $ts);
                             $membership_info = sprintf(__('<em>%1$s</em> membership until <span class="timestamp">%2$s at %3$s</span>.', GROUPS_WS_PLUGIN_DOMAIN), wp_filter_nohtml_kses($group->name), $date, $time);
                         }
                         echo apply_filters('groups_woocommerce_show_buckets_membership', $membership_info, $group_id, $ts);
                     }
                     if (GROUPS_WS_LOG) {
                         echo '<ul>';
                         foreach ($timestamps as $timestamp) {
                             echo '<li>';
                             if (intval($timestamp) === Groups_WS_Terminator::ETERNITY) {
                                 echo __('Unlimited', GROUPS_WS_PLUGIN_DOMAIN);
                             } else {
                                 echo date('Y-m-d H:i:s', $timestamp);
                             }
                             echo '</li>';
                         }
                         echo '<ul>';
                     }
                     echo '</li>';
                 }
             }
         }
         echo '</ul>';
         if (current_user_can(GROUPS_ADMINISTER_GROUPS) && count($timestamp_entries) > 0) {
             echo '<h4>';
             echo __('Edit Memberships', GROUPS_WS_PLUGIN_DOMAIN);
             echo '</h4>';
             echo '<table>';
             echo '<tr>';
             echo '<th>' . __('Group', GROUPS_WS_PLUGIN_DOMAIN) . '</th>';
             echo '<th>' . __('Year', GROUPS_WS_PLUGIN_DOMAIN) . '</th>';
             echo '<th>' . __('Month', GROUPS_WS_PLUGIN_DOMAIN) . '</th>';
             echo '<th>' . __('Day', GROUPS_WS_PLUGIN_DOMAIN) . '</th>';
             echo '<th>' . __('Hour', GROUPS_WS_PLUGIN_DOMAIN) . '</th>';
             echo '<th>' . __('Minute', GROUPS_WS_PLUGIN_DOMAIN) . '</th>';
             echo '<th>' . __('Second', GROUPS_WS_PLUGIN_DOMAIN) . '</th>';
             echo '</tr>';
             foreach ($timestamp_entries as $group_id => $ts) {
                 if ($group = Groups_Group::read($group_id)) {
                     if ($group->name !== Groups_Registered::REGISTERED_GROUP_NAME) {
                         echo '<tr>';
                         echo '<td>';
                         echo wp_filter_nohtml_kses($group->name);
                         echo '</td>';
                         $id_prefix = sprintf('%d-%d', $group_id, $ts);
                         if ($ts === Groups_WS_Terminator::ETERNITY) {
                             $fields = array('Y' => 'year', 'm' => 'month', 'd' => 'day', 'H' => 'hour', 'i' => 'minute', 's' => 'second');
                             $size = array('Y' => 4, 'm' => 2, 'd' => 2, 'H' => 2, 'i' => 2, 's' => 2);
                             $min = array('Y' => 0, 'm' => 1, 'd' => 1, 'H' => 0, 'i' => 0, 's' => 0);
                             $max = array('Y' => 9999, 'm' => 12, 'd' => 31, 'H' => 23, 'i' => 59, 's' => 59);
                             foreach ($fields as $format => $suffix) {
                                 echo '<td>';
                                 printf('<input name="%s" id="%s" class="timestamp-field %s" type="number" value="" size="%d" maxlength="%d" min="%d" max="%d" autocomplete="off" placeholder="%s"/>', sprintf('gw-ts[%d][%d][%s]', $group_id, $ts, $suffix), $id_prefix . '_' . $suffix, $suffix, $size[$format], $size[$format], $min[$format], $max[$format], $suffix == 'year' ? '&infin;' : '-');
                                 echo '</td>';
                             }
                             echo '<td>';
                             echo '</td>';
                         } else {
                             $fields = array('Y' => 'year', 'm' => 'month', 'd' => 'day', 'H' => 'hour', 'i' => 'minute', 's' => 'second');
                             $size = array('Y' => 4, 'm' => 2, 'd' => 2, 'H' => 2, 'i' => 2, 's' => 2);
                             $min = array('Y' => 0, 'm' => 1, 'd' => 1, 'H' => 0, 'i' => 0, 's' => 0);
                             $max = array('Y' => 9999, 'm' => 12, 'd' => 31, 'H' => 23, 'i' => 59, 's' => 59);
                             foreach ($fields as $format => $suffix) {
                                 echo '<td>';
                                 printf('<input name="%s" id="%s" class="timestamp-field %s" type="number" value="%d" size="%d" maxlength="%d" min="%d" max="%d" autocomplete="off" />', sprintf('gw-ts[%d][%d][%s]', $group_id, $ts, $suffix), $id_prefix . '_' . $suffix, $suffix, date($format, $ts), $size[$format], $size[$format], $min[$format], $max[$format]);
                                 echo '</td>';
                             }
                             echo '<td>';
                             printf('<button class="eternal button" value="%s" title="%s" style="display:none;">&infin;</button>', $id_prefix, __('Convert to unlimited membership.', GROUPS_WS_PLUGIN_DOMAIN));
                             echo '<noscript>';
                             echo __('Empty year sets unlimited membership.', GROUPS_WS_PLUGIN_DOMAIN);
                             echo '</noscript>';
                             echo '</td>';
                         }
                         echo '</tr>';
                     }
                 }
             }
             echo '</table>';
             echo '<p class="description">';
             echo __('Current memberships can be extended by modifying their time of expiration.', GROUPS_WS_PLUGIN_DOMAIN);
             echo ' ';
             echo __('Time-limited memberships can be converted to unlimited memberships by clearing their year.', GROUPS_WS_PLUGIN_DOMAIN);
             echo ' ';
             echo __('Unlimited memberships can be converted to time-limited memberships by indicating at least the year of expiration.', GROUPS_WS_PLUGIN_DOMAIN);
             echo ' ';
             echo __('Modifications to points in the past are not allowed.', GROUPS_WS_PLUGIN_DOMAIN);
             echo ' ';
             echo __('To expire a membership immediately, remove the corresponding group from the Groups field.', GROUPS_WS_PLUGIN_DOMAIN);
             echo ' ';
             echo __('To add a membership, first add the corresponding group to the Groups field and update the user profile; then limit the membership if desired.', GROUPS_WS_PLUGIN_DOMAIN);
             echo ' ';
             printf(__('Dates and times are relative to %s (GMT %s).', GROUPS_WS_PLUGIN_DOMAIN), date('T', time()), date('O', time()));
             echo '</p>';
         }
     }
 }
 /**
  * Assign a user to its "Registered" group for the given blog.
  * 
  * @param int $user_id
  * @param WP_string $role
  */
 function add_user_to_blog($user_id, $role, $blog_id)
 {
     if (is_multisite()) {
         Groups_Controller::switch_to_blog($blog_id);
     }
     global $wpdb;
     // Check if the group table exists, if it does not exist, we are
     // probably here because the action has been triggered in the middle
     // of wpmu_create_blog() before the wpmu_new_blog action has been
     // triggered. In that case, just skip this as the user will be added
     // later when wpmu_new_blog is triggered, the activation sequence has
     // created the tables and all users of the new blog are added to
     // that blog's "Registered" group.
     $group_table = _groups_get_tablename('group');
     if ($wpdb->get_var("SHOW TABLES LIKE '" . $group_table . "'") == $group_table) {
         $registered_group = Groups_Group::read_by_name(self::REGISTERED_GROUP_NAME);
         if (!$registered_group) {
             $registered_group_id = Groups_Group::create(array("name" => self::REGISTERED_GROUP_NAME));
         } else {
             $registered_group_id = $registered_group->group_id;
         }
         if ($registered_group_id) {
             Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $registered_group_id));
         }
     }
     if (is_multisite()) {
         Groups_Controller::restore_current_blog();
     }
 }
 /**
  * Renders time-limited group membership info for the user.
  * 
  * The <code>groups_woocommerce_show_membership</code> filter can be
  * used to modify how membership info is rendered.
  * 
  * Possible shortcode attributes passed in $atts :
  * 
  * - 'exclude' : Groups to exclude can be specified using the exclude
  *               entry for $atts. By default, the Registered group is
  *               excluded.
  * - 'no_memberships' : The label to be used when there are no memberships.
  *                      This defaults to 'No memberships'.
  * 
  * @param array $atts
  * @param string $content
  * @return string
  */
 public static function groups_woocommerce_memberships($atts, $content = null)
 {
     $atts = shortcode_atts(array('exclude' => Groups_Registered::REGISTERED_GROUP_NAME, 'show_count' => true, 'count_0' => 'No memberships.', 'count_1' => 'One membership.', 'count_n' => '%d memberships.'), $atts);
     extract($atts);
     $exclude = array_map('trim', explode(',', $exclude));
     $show_count = !($show_count === false || strtolower($show_count) == 'no' || strtolower($show_count) == 'false');
     // for translation
     __('No memberships.', GROUPS_WS_PLUGIN_DOMAIN);
     _n('One membership.', '%d memberships.', 0, GROUPS_WS_PLUGIN_DOMAIN);
     $output = '';
     if ($user_id = get_current_user_id()) {
         $user = wp_get_current_user();
         $n = 0;
         $list_output = '';
         $user_buckets = get_user_meta($user->ID, '_groups_buckets', true);
         if ($user_buckets) {
             $list_output .= '<ul>';
             uksort($user_buckets, array(__CLASS__, 'bucket_cmp'));
             foreach ($user_buckets as $group_id => $timestamps) {
                 if ($group = Groups_Group::read($group_id)) {
                     if (!in_array($group->name, $exclude) && Groups_User_Group::read($user->ID, $group_id)) {
                         $n++;
                         $list_output .= '<li>';
                         $ts = null;
                         foreach ($timestamps as $timestamp) {
                             if (intval($timestamp) === Groups_WS_Terminator::ETERNITY) {
                                 $ts = Groups_WS_Terminator::ETERNITY;
                                 break;
                             } else {
                                 if ($timestamp > $ts) {
                                     $ts = $timestamp;
                                 }
                             }
                         }
                         if ($ts !== null) {
                             if ($ts === Groups_WS_Terminator::ETERNITY) {
                                 $membership_info = sprintf(__('<em>%s</em> membership.', GROUPS_WS_PLUGIN_DOMAIN), wp_filter_nohtml_kses($group->name));
                             } else {
                                 $date = date_i18n(get_option('date_format'), $ts);
                                 $time = date_i18n(get_option('time_format'), $ts);
                                 $membership_info = sprintf(__('<em>%1$s</em> membership until %2$s at %3$s.', GROUPS_WS_PLUGIN_DOMAIN), wp_filter_nohtml_kses($group->name), $date, $time);
                             }
                         }
                         $list_output .= apply_filters('groups_woocommerce_show_membership', $membership_info, $group_id, $ts);
                         $list_output .= '</li>';
                     }
                 }
             }
             $list_output .= '</ul>';
         }
         if ($show_count) {
             $output .= '<div class="membership-count">';
             if ($n > 0) {
                 $output .= sprintf(_n($count_1, $count_n, $n, GROUPS_WS_PLUGIN_DOMAIN), $n);
             } else {
                 $output .= $count_0;
             }
             $output .= '</div>';
         }
         if ($n > 0) {
             $output .= '<div class="membership-list">';
             $output .= $list_output;
             $output .= '</div>';
         }
     }
     return $output;
 }
 /**
  * Execute membership termination.
  * @param int $user_id
  * @param int $group_id
  */
 public static function terminate_membership($user_id, $group_id)
 {
     if (GROUPS_WS_LOG) {
         error_log(sprintf(__METHOD__ . ' testing for user ID %d with group ID %d', $user_id, $group_id));
     }
     require_once GROUPS_WS_CORE_LIB . '/class-groups-ws-bucket.php';
     $b = new Groups_WS_Bucket($user_id, $group_id);
     $b->acquire();
     $B = $b->content;
     $t1 = time();
     $P = array();
     foreach ($B as $t) {
         if ($t <= $t1 && $t !== self::ETERNITY) {
             $P[] = $t;
         }
     }
     $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();
 }
 /**
  * Same as when a subscription is cancelled.
  * @param int $user_id
  * @param string $subscription_key
  */
 public static function subscription_expired($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'];
         $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'])) {
             foreach ($groups_product_groups[$order_id][$product_id]['groups'] as $group_id) {
                 Groups_User_Group::delete($user_id, $group_id);
             }
         }
     }
 }
 private function handle_condition($condition)
 {
     $groups_user = new Groups_User(get_current_user_id());
     $result = 0;
     if ($groups_user && $groups_user->user) {
         switch ($condition['type']) {
             case 'apply_to':
                 if (is_array($condition['args']) && isset($condition['args']['applies_to'])) {
                     if ($condition['args']['applies_to'] == 'groups' && isset($condition['args']['groups']) && is_array($condition['args']['groups'])) {
                         if (is_user_logged_in()) {
                             foreach ($condition['args']['groups'] as $group) {
                                 $current_group = Groups_Group::read($group);
                                 if ($current_group) {
                                     if (Groups_User_Group::read($groups_user->user->ID, $current_group->group_id)) {
                                         $result = 1;
                                         break;
                                     }
                                 }
                             }
                         }
                     }
                 }
                 break;
             default:
                 break;
         }
     }
     return $result;
 }
 /**
  * Takes one attribute "group" which is a comma-separated list of group
  * names or ids (can be mixed).
  * The content is shown if the current user does NOT belong to the group(s).
  *
  * @param array $atts attributes
  * @param string $content content to render
  */
 public static function groups_non_member($atts, $content = null)
 {
     $output = "";
     $options = shortcode_atts(array("group" => ""), $atts);
     $show_content = true;
     if ($content !== null) {
         $groups_user = new Groups_User(get_current_user_id());
         $groups = explode(",", $options['group']);
         foreach ($groups as $group) {
             $group = trim($group);
             $current_group = Groups_Group::read($group);
             if (!$current_group) {
                 $current_group = Groups_Group::read_by_name($group);
             }
             if ($current_group) {
                 if (Groups_User_Group::read($groups_user->user->ID, $current_group->group_id)) {
                     $show_content = false;
                     break;
                 }
             }
         }
         if ($show_content) {
             remove_shortcode('groups_non_member');
             $content = do_shortcode($content);
             add_shortcode('groups_non_member', array(__CLASS__, 'groups_non_member'));
             $output = $content;
         }
     }
     return $output;
 }
 /**
  * Adds or removes users to/from groups.
  */
 public static function load_users()
 {
     if (current_user_can(GROUPS_ADMINISTER_GROUPS)) {
         $group_id = isset($_REQUEST['group_id']) ? $_REQUEST['group_id'] : null;
         $users = isset($_REQUEST['users']) ? $_REQUEST['users'] : null;
         $action = null;
         if (!empty($_REQUEST['add-to-group'])) {
             $action = 'add';
         } else {
             if (!empty($_REQUEST['remove-from-group'])) {
                 $action = 'remove';
             }
         }
         if ($group_id !== null && $users !== null && $action !== null) {
             if (wp_verify_nonce($_REQUEST['bulk-user-group-nonce'], 'user-group')) {
                 foreach ($users as $user_id) {
                     switch ($action) {
                         case 'add':
                             if (!Groups_User_Group::read($user_id, $group_id)) {
                                 Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $group_id));
                             }
                             break;
                         case 'remove':
                             if (Groups_User_Group::read($user_id, $group_id)) {
                                 Groups_User_Group::delete($user_id, $group_id);
                             }
                             break;
                     }
                 }
             }
         }
     }
 }
 public static function create_registration_group($group_name, $users)
 {
     // Check if Groups plugin is active
     if (is_plugin_active('groups/groups.php')) {
         Groups_Group::create(array('name' => $group_name));
         if ($group = Groups_Group::read_by_name($group_name)) {
             $group_id = $group->group_id;
         }
         if (!empty($group_id)) {
             foreach ($users as $user_id) {
                 Groups_User_Group::create(array('user_id' => $user_id, 'group_id' => $group_id));
             }
         }
     }
 }