Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/04/2013 03:04:23 AM (12 years ago)
Author:
boonebgorges
Message:

Improves the algorithm for filtering by role in BP_Group_Member_Query

Filtering by group role is tricky, because group role data is declared
explicitly in the database only for 'admin', 'mod', and 'banned' - 'member'
status is implicit, when these other three values are 0. The improved algorithm
assembles a role whitelist in cases where the list of roles being filtered does
not include 'member', and a role blacklist in the case where the list does
contain 'member'.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-groups/bp-groups-classes.php

    r7143 r7144  
    10311031
    10321032        // Role information is stored as follows: admins have
    1033         // is_admin = 1, mods have is_mod = 1, and members have both
    1034         // set to 0.
     1033        // is_admin = 1, mods have is_mod = 1, banned have is_banned =
     1034        // 1, and members have all three set to 0.
    10351035        $roles = !empty( $this->query_vars['group_role'] ) ? $this->query_vars['group_role'] : array();
    10361036        if ( is_string( $roles ) ) {
     
    10381038        }
    10391039
    1040         // Sanitize: Only 'admin', 'mod', and 'member' are valid
     1040        // Sanitize: Only 'admin', 'mod', 'member', and 'banned' are valid
     1041        $allowed_roles = array( 'admin', 'mod', 'member', 'banned' );
    10411042        foreach ( $roles as $role_key => $role_value ) {
    1042             if ( ! in_array( $role_value, array( 'admin', 'mod', 'member' ) ) ) {
     1043            if ( ! in_array( $role_value, $allowed_roles ) ) {
    10431044                unset( $roles[ $role_key ] );
    10441045            }
    10451046        }
    10461047
    1047         // Remove dupes to make the count accurate, and flip for faster
    1048         // isset() lookups
    1049         $roles = array_flip( array_unique( $roles ) );
    1050 
    1051         switch ( count( $roles ) ) {
    1052 
    1053             // All three roles means we don't limit results
    1054             case 3 :
    1055             default :
    1056                 $roles_sql = '';
    1057                 break;
    1058 
    1059             case 2 :
    1060                 // member + mod = no admins
    1061                 // member + admin = no mods
    1062                 if ( isset( $roles['member'] ) ) {
    1063                     $roles_sql = isset( $roles['admin'] ) ? "is_mod = 0" : "is_admin = 0";
    1064 
    1065                 // Two non-member roles are 'admin' and 'mod'
    1066                 } else {
    1067                     $roles_sql = "(is_admin = 1 OR is_mod = 1)";
    1068                 }
    1069                 break;
    1070 
    1071             case 1 :
    1072                 // member only means no admins or mods
    1073                 if ( isset( $roles['member'] ) ) {
    1074                     $roles_sql = "is_admin = 0 AND is_mod = 0";
    1075 
    1076                 // Filter by that role only
    1077                 } else {
    1078                     $roles_sql = isset( $roles['admin'] ) ? "is_admin = 1" : "is_mod = 1";
    1079                 }
    1080                 break;
    1081 
    1082             // No roles means no users should be returned
    1083             case 0 :
    1084                 $roles_sql = $this->no_results['where'];
    1085                 break;
     1048        $roles = array_unique( $roles );
     1049
     1050        // When querying for a set of roles containing 'member' (for
     1051        // which there is no dedicated is_ column), figure out a list
     1052        // of columns *not* to match
     1053        if ( in_array( 'member', $roles ) ) {
     1054            $role_columns = array();
     1055            foreach ( array_diff( $allowed_roles, $roles ) as $excluded_role ) {
     1056                $role_columns[] = 'is_' . $excluded_role . ' = 0';
     1057            }
     1058            $roles_sql = '(' . implode( ' AND ', $role_columns ) . ')';
     1059
     1060        // When querying for a set of roles *not* containing 'member',
     1061        // simply construct a list of is_* = 1 clauses
     1062        } else {
     1063            $role_columns = array();
     1064            foreach ( $roles as $role ) {
     1065                $role_columns[] = 'is_' . $role . ' = 1';
     1066            }
     1067            $roles_sql = '(' . implode( ' OR ', $role_columns ) . ')';
    10861068        }
    10871069
Note: See TracChangeset for help on using the changeset viewer.