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; }
/** * 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; } } } } } }
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; }
/** * 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; }
/** * 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; } } } } }
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; }
/** * 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); } } } } } }
/** * 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; }
/** * Create "Registered" group for new blog and add its admin user. * * @see Groups_Controller::wpmu_new_blog() * * @param int $blog_id * @param int $user_id blog's admin user's id * @param string $domain (optional) * @param string $path (optional) * @param int $site_id (optional) * @param array $meta (optional) */ public static function wpmu_new_blog($blog_id, $user_id, $domain = null, $path = null, $site_id = null, $meta = null) { if (is_multisite()) { Groups_Controller::switch_to_blog($blog_id); } if (!($group = Groups_Group::read_by_name(self::REGISTERED_GROUP_NAME))) { $group_id = Groups_Group::create(array("name" => self::REGISTERED_GROUP_NAME)); } else { $group_id = $group->group_id; } // add the blog's admin user to the group if ($group_id) { if (!Groups_User_Group::read($user_id, $group_id)) { Groups_User_Group::create(array("user_id" => $user_id, "group_id" => $group_id)); } } if (is_multisite()) { Groups_Controller::restore_current_blog(); } }
/** * 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' ? '∞' : '-'); 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;">∞</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>'; } } }
/** * 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; }