Example #1
0
 /**
  * Returns an array with all the groups for which a user has a current, active
  * membership. This takes into account system groups and membership expiration
  * dates.
  *
  * @global wpdb $wpdb
  * @global CTXPSC_Tables $ctxpsdb
  *
  * @param int $user_id The user id of the user to check
  * @param boolean $site_only Optional. If true, array will only include groups with site access.
  *
  * @return array Returns a flat array with all the groups that the specified user is currently a member of.
  */
 public static function get_user_groups($user_id, $site_only = false)
 {
     global $wpdb, $ctxpsdb;
     /**Empty array to be used for building output*/
     $array = array();
     $newArray = array();
     /**Todays date for MySQL comparison*/
     $today = date('Y-m-d');
     /**Assume user is multi-site user*/
     $multisitemember = true;
     //Get membership only if it's not expired
     $query = $wpdb->prepare('SELECT * FROM `' . $ctxpsdb->group_rels . '`
     JOIN `' . $ctxpsdb->groups . '`
         ON `' . $ctxpsdb->group_rels . '`.grel_group_id = `' . $ctxpsdb->groups . '`.ID
     WHERE `' . $ctxpsdb->group_rels . '`.grel_user_id = %s
     AND (grel_expires IS NULL OR grel_expires > %s)', $user_id, $today);
     //If $site_only is true, append extra restriction to query
     if ($site_only) {
         $query .= ' AND (group_site_access = "limited" OR group_site_access = "full")';
     }
     $groups = $wpdb->get_results($query);
     //We only need an ID and a name as a key/value...
     foreach ($groups as $group) {
         $array += array($group->ID => $group->group_title);
     }
     //If multisite is enabled we can better support it...
     if (function_exists('is_user_member_of_blog')) {
         $multisitemember = is_user_member_of_blog($user_id);
     }
     /*** ADD SMART GROUPS (AKA SYSTEM GROUPS ***/
     //Registered Users Smart Group
     if ($user_id != 0 && $multisitemember) {
         //Get the ID for CPS01
         $newArray = CTXPS_Queries::get_system_group('CPS01');
         //Add CPS01 to the current users permissions array
         $array += array($newArray->ID => $newArray->group_title);
     }
     return $array;
 }
 /**
  * Gets an assoc array containing a list of all groups. This can also be used to get
  * a list of only groups belonging to an individual user.
  *
  * @param int $user_id Optional. Include if you want groups a user is attached to. Leave blank for all groups.
  * @return array Associative array with groups. Format: Group_ID => Group_Title
  */
 function psc_get_groups($user_id = null)
 {
     global $wpdb, $current_user;
     $array = array();
     //This function is deprecated
     psc_deprecated('CTXPS_Queries::get_groups()');
     //Determine if we're looking up groups for a user, or all groups
     if (is_numeric($user_id)) {
         $groups = $wpdb->get_results("\n            SELECT * FROM `{$wpdb->prefix}ps_group_relationships`\n            JOIN `{$wpdb->prefix}ps_groups`\n                ON {$wpdb->prefix}ps_group_relationships.grel_group_id = {$wpdb->prefix}ps_groups.ID\n            WHERE {$wpdb->prefix}ps_group_relationships.grel_user_id = '{$user_id}'\n        ");
     } else {
         $groups = $wpdb->get_results("SELECT * FROM `{$wpdb->prefix}ps_groups`");
     }
     //We only need an ID and a name as a key/value..., so we'll build a new array
     foreach ($groups as $group) {
         $array[$group->ID] = $group->group_title;
     }
     //If multisite is enabled we can better support it...
     if (function_exists('is_user_member_of_blog')) {
         //Make sure user is a member of this blog (in addition to being logged in)
         $multisitemember = is_user_member_of_blog($current_user->ID);
     } else {
         //Assume user is member of blog
         $multisitemember = true;
     }
     /*** ADD SMART GROUPS (AKA SYSTEM GROUPS) ***/
     //Registered Users Smart Group
     if ($current_user->ID != 0 && $multisitemember) {
         //Get the ID for CPS01 (added in 1.1, so cant assume 1)
         $newArray = CTXPS_Queries::get_system_group('CPS01');
         //Add CPS01 to the current users permissions array
         $array += array($newArray->ID => $newArray->group_title);
     }
     return $array;
 }