public static function remove_user_groups($user_id, $omit_group_ids = array()) { $group_types = pp_get_group_types(array('editable' => true)); foreach (pp_get_group_types(array('editable' => true)) as $agent_type) { if ('pp_group' == $agent_type && in_array('pp_net_group', $group_types)) { continue; } $posted_groups = isset($_POST[$agent_type]) ? array_map('intval', $_POST[$agent_type]) : array(); $stored_groups = array_keys(pp_get_groups_for_user($user_id, $agent_type, array('cols' => 'id'))); if ($omit_group_ids) { $stored_groups = array_diff($stored_groups, $omit_group_ids); } $delete_groups = array_diff($stored_groups, $posted_groups); foreach ($delete_groups as $group_id) { if (pp_has_group_cap('pp_manage_members', $group_id, $agent_type)) { pp_remove_group_user($group_id, $user_id, compact('agent_type')); } } } }
$omit_users = $wpdb->get_col("SELECT u.ID FROM {$wpdb->users} AS u INNER JOIN {$wpdb->pp_group_members} AS gm ON u.ID = gm.user_id INNER JOIN {$wpdb->pp_groups} AS g ON gm.group_id = g.ID WHERE g.metagroup_type = 'wp_role' AND g.metagroup_id IN ('{$role_csv}')"); } } foreach ($results as $row) { if (!in_array($row->ID, $omit_users)) { if (defined('PP_USER_RESULTS_DISPLAY_NAME')) { $title = $row->user_login != $row->display_name ? " title='" . esc_attr($row->user_login) . "'" : ''; echo "<option value='{$row->ID}' class='pp-new-selection'{$title}>{$row->display_name}</option>"; } else { $title = $row->user_login != $row->display_name ? " title='" . esc_attr($row->display_name) . "'" : ''; echo "<option value='{$row->ID}' class='pp-new-selection'{$title}>{$row->user_login}</option>"; } } } } } else { $reqd_caps = apply_filters('pp_edit_groups_reqd_caps', array('pp_edit_groups')); // determine all currently stored groups (of any status) for user in question (not necessarily logged user) if (!empty($agent_id)) { $omit_groups = pp_get_groups_for_user($agent_id, $agent_type, array('status' => 'any')); } else { $omit_groups = array(); } if ($groups = pp_get_groups($agent_type, array('filtering' => true, 'include_norole_groups' => false, 'reqd_caps' => $reqd_caps, 'search' => $search_str))) { foreach ($groups as $row) { if ((empty($row->metagroup_id) || is_null($row->metagroup_id)) && !isset($omit_groups[$row->ID])) { echo "<option value='{$row->ID}'>{$row->name}</option>"; } } } }
public static function flt_users_custom_column($content = '', $column_name, $id) { switch ($column_name) { case 'pp_groups': global $wp_list_table; //if ( ! $agent_type = apply_filters( 'pp_query_group_type', '' ) ) // $agent_type = 'pp_group'; static $all_groups; static $all_group_types; if (!isset($all_groups)) { $all_groups = array(); $all_group_types = pp_get_group_types(array('editable' => true)); } $all_group_names = array(); foreach ($all_group_types as $agent_type) { if (!isset($all_groups[$agent_type])) { $all_groups[$agent_type] = pp_get_groups($agent_type); } if (empty($all_groups[$agent_type])) { continue; } $group_names = array(); if ($group_ids = pp_get_groups_for_user($id, $agent_type, array('cols' => 'id', 'query_user_ids' => array_keys($wp_list_table->items)))) { foreach (array_keys($group_ids) as $group_id) { if (isset($all_groups[$agent_type][$group_id])) { if (empty($all_groups[$agent_type][$group_id]->metagroup_type) || 'wp_role' != $all_groups[$agent_type][$group_id]->metagroup_type) { $group_names[$all_groups[$agent_type][$group_id]->name] = $group_id; } } } if ($group_names) { uksort($group_names, "strnatcasecmp"); foreach ($group_names as $name => $_id) { if (defined('PP_USERS_UI_GROUP_FILTER_LINK')) { $url = add_query_arg('pp_group', $_id, $_SERVER['REQUEST_URI']); $all_group_names[] = "<a href='{$url}'>{$name}</a>"; } else { $all_group_names[] = "<a href='" . "admin.php?page=pp-edit-permissions&action=edit&agent_type={$agent_type}&agent_id={$_id}'>{$name}</a>"; } } //$group_names = array_merge( $group_names, $this_group_names ); } } } return implode(", ", $all_group_names); break; case 'pp_roles': global $wp_list_table, $wp_roles; static $role_info; $role_str = ''; if (!isset($role_info)) { $role_info = ppc_count_assigned_roles('user', array('query_agent_ids' => array_keys($wp_list_table->items))); } $user_object = new WP_User((int) $id); static $hide_roles; if (!isset($hide_roles)) { $hide_roles = !defined('bbp_get_version') ? array('bbp_participant', 'bbp_moderator', 'bbp_keymaster', 'bbp_blocked', 'bbp_spectator') : array(); $hide_roles = apply_filters('pp_hide_roles', $hide_roles); } $user_object->roles = array_diff($user_object->roles, $hide_roles); $role_titles = array(); foreach ($user_object->roles as $role_name) { if (isset($wp_roles->role_names[$role_name])) { $role_titles[] = $wp_roles->role_names[$role_name]; } } if (isset($role_info[$id]) && isset($role_info[$id]['roles'])) { $role_titles = array_merge($role_titles, array_keys($role_info[$id]['roles'])); } $display_limit = 3; if (count($role_titles) > $display_limit) { $excess = count($role_titles) - $display_limit; $role_titles = array_slice($role_titles, 0, $display_limit); $role_titles[] = sprintf(__('%s more', 'pp'), $excess); } $role_str = '<span class="pp-group-site-roles">' . implode(', ', $role_titles) . '</span>'; if (current_user_can('edit_user', $id) && current_user_can('pp_assign_roles')) { $edit_link = "admin.php?page=pp-edit-permissions&action=edit&agent_id={$id}&agent_type=user"; $role_str = "<a href=\"{$edit_link}\">{$role_str}</a><br />"; } return $role_str; break; case 'pp_exceptions': global $wp_list_table; return ppc_list_agent_exceptions('user', $id, array('query_agent_ids' => array_keys($wp_list_table->items))); break; default: return $content; } }
function _get_pp_groups($args = array()) { $args = (array) $args; if (!$this->ID) { $user_groups = array(); if (pp_get_option('anonymous_unfiltered')) { $this->allcaps['pp_unfiltered'] = true; } else { if ($anon_group = pp_get_metagroup('wp_role', 'wp_anon')) { $user_groups[$anon_group->ID] = $anon_group; } if ($all_group = pp_get_metagroup('wp_role', 'wp_all')) { $user_groups[$all_group->ID] = $all_group; } } } else { $user_groups = pp_get_groups_for_user($this->ID, $args['agent_type'], $args); if (isset($this->roles)) { if (pp_get_option('dynamic_wp_roles') || defined('PP_FORCE_DYNAMIC_ROLES')) { $have_role_group_names = array(); foreach ($user_groups as $group) { if ('wp_role' == $group->metagroup_type) { $have_role_group_names[] = $group->metagroup_id; } } if ($missing_role_group_names = array_diff($this->roles, $have_role_group_names)) { global $wpdb; $groups_table = apply_filters('pp_use_groups_table', $wpdb->pp_groups); $add_metagroups = $wpdb->get_results("SELECT * FROM {$groups_table} WHERE metagroup_type = 'wp_role' AND metagroup_id IN ('" . implode("','", $missing_role_group_names) . "')"); foreach ($add_metagroups as $row) { $row->group_id = $row->ID; $row->status = 'active'; $user_groups[$row->ID] = $row; } } } } } return $user_groups; }
public static function display_ui_user_groups($include_role_metagroups = false, $args = array()) { $defaults = array('initial_hide' => false, 'selected_only' => false, 'hide_checkboxes' => false, 'force_display' => false, 'edit_membership_link' => false, 'user_id' => false); extract(array_merge($defaults, $args), EXTR_SKIP); require_once dirname(__FILE__) . '/permissions-ui_pp.php'; if (!is_numeric($user_id)) { global $profileuser; $user_id = !empty($profileuser) ? $profileuser->ID : 0; } $group_types = pp_get_group_types(array('editable' => true)); foreach ($group_types as $agent_type) { //if ( ! $agent_type = apply_filters( 'pp_query_group_type', '' ) ) // $agent_type = 'pp_group'; if ('pp_group' == $agent_type && in_array('pp_net_group', $group_types)) { continue; } if (!pp_has_group_cap('pp_manage_members', 0, $agent_type)) { continue; } if (!($all_groups = pp_get_groups($agent_type))) { continue; } $reqd_caps = (array) apply_filters('pp_edit_groups_reqd_caps', array('pp_edit_groups')); // @todo: reinstate? //$editable_ids = pp_get_groups( 'pp_group', FILTERED_PP, 'id', compact('reqd_caps') ); if (current_user_can('pp_manage_members')) { $editable_ids = array_keys($all_groups); } else { $editable_ids = array(); } $stored_groups = pp_get_groups_for_user($user_id, $agent_type, array('cols' => 'id')); //$addable_ids = array_diff( $editable_ids, array_keys($stored_groups) ); $locked_ids = array_diff(array_keys($stored_groups), $editable_ids); // can't manually edit membership of WP Roles groups or other metagroups lacking _ed_ suffix $all_ids = array(); foreach ($all_groups as $key => $group) { if ($selected_only && !isset($stored_groups[$group->ID])) { unset($all_groups[$key]); continue; } $all_ids[] = $group->ID; if (!$include_role_metagroups && !empty($group->metagroup_id) && 'wp_role' == $group->metagroup_type) { $editable_ids = array_diff($editable_ids, array($group->ID)); unset($stored_groups[$group->ID]); unset($all_groups[$key]); } elseif (!in_array($group->ID, $editable_ids) && !in_array($group->ID, $locked_ids)) { unset($all_groups[$key]); } } $locked_ids = array_diff(array_keys($stored_groups), $editable_ids); // avoid incorrect eligible count if orphaned group roles are included in editable_ids $editable_ids = array_intersect($editable_ids, $all_ids); if (!$all_groups && !$force_display) { continue; } $style = $initial_hide ? "style='display:none'" : ''; echo "<div id='userprofile_groupsdiv_pp' class='pp-group-box pp-group_members' {$style}>"; echo "<h3>"; if ('pp_group' == $agent_type) { if (defined('GROUPS_CAPTION_RS')) { echo GROUPS_CAPTION_RS; } else { _e('Permission Groups', 'pp'); } } else { $group_type_obj = pp_get_group_type_object($agent_type); echo $group_type_obj->labels->name; } echo "</h3>"; $css_id = $agent_type; $args = array('eligible_ids' => $editable_ids, 'locked_ids' => $locked_ids, 'show_subset_caption' => false, 'hide_checkboxes' => $hide_checkboxes); require_once dirname(__FILE__) . '/agents_ui_pp.php'; $pp_agents_ui = pp_init_agents_ui(); $pp_agents_ui->agents_ui($agent_type, $all_groups, $css_id, $stored_groups, $args); if ($edit_membership_link || !$all_groups && $force_display) { ?> <p> <?php if (!$all_groups && $force_display) { _e('This user is not a member of any Permission Groups.', 'pp'); ?> • <?php } ?> <a href='user-edit.php?user_id=<?php echo $user_id; ?> #userprofile_groupsdiv_pp' title='<?php echo esc_attr(__('Edit this user's group membership', 'pp')); ?> '><?php _e('add / edit membership'); ?> </a> <span class="pp-subtext"> <?php $note = apply_filters('pp_user_profile_groups_note', defined('BP_VERSION') ? __('note: BuddyPress Groups and other externally defined groups are not listed here, even if they modify permissions', 'pp') : '', $user_id, $args); echo $note; ?> </span> </p> <?php } echo '</div>'; } // end foreach agent_type echo "<input type='hidden' name='pp_editing_user_groups' value='1' />"; }