function groups_who_can($reqd_caps, $cols = COLS_ALL_RS, $object_src_name = '', $object_id = 0, $args = array()) { global $wpdb; $defaults = array('orderby' => '', 'disable_memcache' => false, 'force_refresh' => false); $args = array_merge($defaults, (array) $args); extract($args); $cache_flag = "rs_groups_who_can"; $cache_id = md5(serialize($reqd_caps) . $cols . 'src' . $object_src_name . 'id' . $object_id . serialize($args)); if (!$force_refresh) { $groups = wpp_cache_get($cache_id, $cache_flag); if (is_array($groups)) { return $groups; } } if (!is_array($reqd_caps)) { $reqd_caps = $reqd_caps ? array($reqd_caps) : array(); } if (!$orderby && (COLS_ALL_RS == $cols || COLS_ID_DISPLAYNAME_RS == $cols)) { $orderby = " ORDER BY display_name"; } if (!is_array($args)) { $args = array(); } if (isset($args['ignore_group_roles'])) { unset($args['ignore_group_roles']); } $args['ignore_user_roles'] = 1; $args['querying_groups'] = 1; $where = $this->flt_users_where('', $reqd_caps, $object_src_name, $object_id, $args); if (COL_ID_RS == $cols) { $qry = "SELECT DISTINCT group_id as ID FROM {$wpdb->user2role2object_rs} AS gro WHERE 1=1 {$where} AND gro.group_id > 0 {$orderby}"; $groups = scoper_get_col($qry); } else { $grp = $wpdb->groups_rs; $qry = "SELECT DISTINCT {$grp}.{$wpdb->groups_id_col} AS ID, {$grp}.{$wpdb->groups_name_col} AS display_name, {$grp}.{$wpdb->groups_descript_col} as descript FROM {$grp}" . " INNER JOIN {$wpdb->user2group_rs} as u2g ON u2g.{$wpdb->user2group_gid_col} = {$grp}.{$wpdb->groups_id_col}" . " INNER JOIN {$wpdb->user2role2object_rs} AS gro ON {$grp}.{$wpdb->groups_id_col} = gro.group_id WHERE 1=1 {$where} {$orderby}"; $groups = scoper_get_results($qry); } wpp_cache_set($cache_id, $groups, $cache_flag); return $groups; }
function users_who_can($reqd_caps, $cols = COLS_ALL_RS, $object_src_name = '', $object_id = 0, $args = array()) { // if there are not capability requirements, no need to load Users_Interceptor filtering class if (!$reqd_caps) { if (COL_ID_RS == $cols) { $qcols = 'ID'; } elseif (COLS_ID_NAME_RS == $cols) { $qcols = "ID, user_login AS display_name"; } elseif (COLS_ID_DISPLAYNAME_RS == $cols) { $qcols = "ID, display_name"; } elseif (COLS_ALL_RS == $cols) { $qcols = "*"; } else { $qcols = $cols; } global $wpdb; $orderby = $cols == COL_ID_RS ? '' : 'ORDER BY display_name'; if (IS_MU_RS && !scoper_get_option('mu_sitewide_groups') && !defined('FORCE_ALL_SITE_USERS_RS')) { $qry = "SELECT {$qcols} FROM {$wpdb->users} INNER JOIN {$wpdb->usermeta} AS um ON {$wpdb->users}.ID = um.user_id AND um.meta_key = '{$wpdb->prefix}capabilities' {$orderby}"; } else { $qry = "SELECT {$qcols} FROM {$wpdb->users} {$orderby}"; } if (COL_ID_RS == $cols) { return scoper_get_col($qry); } else { return scoper_get_results($qry); } } else { $defaults = array('where' => '', 'orderby' => '', 'disable_memcache' => false, 'group_ids' => '', 'force_refresh' => false, 'force_all_users' => false); $args = array_merge($defaults, (array) $args); extract($args); $cache_flag = "rs_users_who_can"; $cache_id = md5(serialize($reqd_caps) . $cols . 'src' . $object_src_name . 'id' . $object_id . serialize($args)); if (!$force_refresh) { // if we already have the results cached, no need to load Users_Interceptor filtering class $users = wpp_cache_get($cache_id, $cache_flag); if (is_array($users)) { return $users; } } $this->init_users_interceptor(); $users = $GLOBALS['users_interceptor']->users_who_can($reqd_caps, $cols, $object_src_name, $object_id, $args); wpp_cache_set($cache_id, $users, $cache_flag); return $users; } }
function get_groups_for_user($user_id, $args = array()) { if (empty($args['status'])) { $status = 'active'; } elseif ('any' == $args['status']) { $args['no_cache'] = true; $status = ''; } else { $args['no_cache'] = true; $status = $args['status']; } if (empty($args['no_cache'])) { $cache = wpp_cache_get($user_id, 'group_membership_for_user'); if (is_array($cache)) { return $cache; } } global $wpdb; if (!$wpdb->user2group_rs) { return array(); } if (!$user_id) { // include WP metagroup for anonymous user return array_fill_keys(scoper_get_col("SELECT {$wpdb->groups_id_col} FROM {$wpdb->groups_rs} WHERE {$wpdb->groups_rs}.{$wpdb->groups_meta_id_col} = 'wp_anon'"), 'true'); } $status_clause = $status ? "AND status = '{$status}'" : ''; $query = "SELECT {$wpdb->user2group_gid_col} FROM {$wpdb->user2group_rs} WHERE {$wpdb->user2group_uid_col} = '{$user_id}' {$status_clause} ORDER BY {$wpdb->user2group_gid_col}"; if (!($user_groups = scoper_get_col($query))) { $user_groups = array(); } // include WP metagroup(s) for WP blogrole(s) $metagroup_ids = array(); if (!empty($args['metagroup_roles'])) { foreach (array_keys($args['metagroup_roles']) as $role_handle) { $metagroup_ids[] = 'wp_role_' . str_replace('wp_', '', $role_handle); } } if ($metagroup_ids) { $meta_id_in = "'" . implode("', '", $metagroup_ids) . "'"; $query = "SELECT {$wpdb->groups_id_col} FROM {$wpdb->groups_rs}" . " WHERE {$wpdb->groups_rs}.{$wpdb->groups_meta_id_col} IN ({$meta_id_in})" . " ORDER BY {$wpdb->groups_id_col}"; if ($meta_groups = scoper_get_col($query)) { $user_groups = array_merge($user_groups, $meta_groups); } } if ($user_groups && empty($args['no_cache'])) { // users should always be in at least a metagroup. Problem with caching empty result on user creation beginning with WP 2.8 $user_groups = array_fill_keys($user_groups, 1); wpp_cache_set($user_id, $user_groups, 'group_membership_for_user'); } return $user_groups; }
function get_groups_for_user($user_id, $args = array()) { if (!defined('SCOPER_ANON_METAGROUP')) { return array(); } if (empty($args['no_cache'])) { // use -1 here to ignore accidental storage of other groups for zero user_id $cache = wpp_cache_get(-1, 'group_membership_for_user'); if (is_array($cache)) { return $cache; } } global $wpdb; if (empty($wpdb->groups_rs)) { return array(); } // include WP metagroup for anonymous user $user_groups = scoper_get_col("SELECT {$wpdb->groups_id_col} FROM {$wpdb->groups_rs} WHERE {$wpdb->groups_rs}.{$wpdb->groups_meta_id_col} = 'wp_anon'"); if ($user_groups && empty($args['no_cache'])) { // users should always be in at least a metagroup. Problem with caching empty result on user creation beginning with WP 2.8 $user_groups = array_fill_keys($user_groups, 1); wpp_cache_set(-1, $user_groups, 'group_membership_for_user'); } return $user_groups; }
function wpp_cache_force_set($key, $data, $flag = '', $expire = 0, $append_blog_suffix = true) { return wpp_cache_set($key, $data, $flag, $expire, $append_blog_suffix, true); }
function get_all_groups($filtering = UNFILTERED_RS, $cols = COLS_ALL_RS, $args = array()) { $defaults = array('include_norole_groups' => false, 'reqd_caps' => 'manage_groups', 'where' => ''); $args = array_merge($defaults, (array) $args); extract($args); if ($filtering && is_user_administrator_rs()) { $filtering = 0; } if ($filtering) { $cache_flag = 'usergroups'; global $current_rs_user; $cache = $current_rs_user->cache_get($cache_flag); } else { $cache_flag = 'all_usergroups'; $cache_id = 'all'; $cache = wpp_cache_get($cache_id, $cache_flag); } $ckey = md5($cols . $reqd_caps); if (!isset($cache[$ckey])) { global $wpdb; if ($filtering && !is_user_administrator_rs() && !cr_user_can($reqd_caps, 0, 0, array('skip_any_object_check' => true, 'skip_any_term_check' => true, 'skip_id_generation' => true))) { $duration_clause = scoper_get_duration_clause(); global $scoper; $role_handles = $scoper->role_defs->qualify_roles($reqd_caps); $role_names = array(); foreach (array_keys($role_handles) as $role_handle) { $role = scoper_explode_role_handle($role_handle); $role_names[] = $role->role_name; } $role_clause = "AND uro.role_name IN ('" . implode("','", $role_names) . "')"; $join = "INNER JOIN {$wpdb->user2role2object_rs} AS uro" . " ON uro.obj_or_term_id = {$wpdb->groups_rs}.{$wpdb->groups_id_col}" . " AND uro.src_or_tx_name = 'group' AND uro.scope = 'object' {$role_clause} {$duration_clause}"; $_where = "WHERE uro.user_id = {$current_rs_user->ID}"; } else { $join = ''; $_where = 'WHERE 1=1 '; } // append supplemental where clause, if any was passed in $_where .= $where; if (COL_ID_RS == $cols) { $query = "SELECT DISTINCT {$wpdb->groups_id_col} FROM {$wpdb->groups_rs} {$join} {$_where}"; } else { $query = "SELECT DISTINCT {$wpdb->groups_id_col} AS ID, {$wpdb->groups_name_col} AS display_name, {$wpdb->groups_descript_col} as descript, {$wpdb->groups_meta_id_col} as meta_id" . " FROM {$wpdb->groups_rs} {$join} {$_where} ORDER BY {$wpdb->groups_name_col}"; } if (COL_ID_RS == $cols) { $cache[$ckey] = scoper_get_col($query); } else { $cache[$ckey] = scoper_get_results($query); } } if ($filtering) { $current_rs_user->cache_set($cache, $cache_flag); } else { wpp_cache_set($cache_id, $cache, $cache_flag); } if (COLS_ALL_RS == $cols) { // strip out anon metagroup if we're not using it (have to do this after cache storage / retrieval) if (!defined('SCOPER_ANON_METAGROUP')) { foreach (array_keys($cache[$ckey]) as $key) { if ('wp_anon' == $cache[$ckey][$key]->meta_id) { unset($cache[$ckey][$key]); break; } } } // strip out groups that don't use roles, unless arg asked for them if (!$include_norole_groups) { foreach (array_keys($cache[$ckey]) as $key) { if (strpos($cache[$ckey][$key]->meta_id, '_nr_')) { unset($cache[$ckey][$key]); } } } } if (!$cache[$ckey]) { $cache[$ckey] = array(); } return $cache[$ckey]; }