Skip to:
Content

BuddyPress.org

Changeset 11027


Ignore:
Timestamp:
08/22/2016 10:11:20 PM (10 years ago)
Author:
r-a-y
Message:

Groups: Cast properties as integers where appropriate.

See #6977.

Location:
trunk/src/bp-groups
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-functions.php

    r10978 r11027  
    115115        // Pass an existing group ID.
    116116        if ( ! empty( $group_id ) ) {
    117                 $group = groups_get_group( array( 'group_id' => (int) $group_id ) );
     117                $group = groups_get_group( array( 'group_id' => $group_id ) );
    118118                $name  = ! empty( $name ) ? $name : $group->name;
    119119                $slug  = ! empty( $slug ) ? $slug : $group->slug;
     
    416416 *
    417417 * @param string $group_slug The group's slug.
    418  * @return int The ID.
     418 * @return int|null The group ID on success; null on failure.
    419419 */
    420420function groups_get_id( $group_slug ) {
    421         return (int)BP_Groups_Group::group_exists( $group_slug );
     421        return BP_Groups_Group::group_exists( $group_slug );
    422422}
    423423
  • trunk/src/bp-groups/classes/class-bp-groups-group.php

    r10937 r11027  
    176176
    177177                if ( !empty( $id ) ) {
    178                         $this->id = $id;
     178                        $this->id = (int) $id;
    179179                        $this->populate();
    180180                }
     
    209209
    210210                // Group found so setup the object variables.
    211                 $this->id           = $group->id;
    212                 $this->creator_id   = $group->creator_id;
     211                $this->id           = (int) $group->id;
     212                $this->creator_id   = (int) $group->creator_id;
    213213                $this->name         = stripslashes( $group->name );
    214214                $this->slug         = $group->slug;
    215215                $this->description  = stripslashes( $group->description );
    216216                $this->status       = $group->status;
    217                 $this->enable_forum = $group->enable_forum;
     217                $this->enable_forum = (int) $group->enable_forum;
    218218                $this->date_created = $group->date_created;
    219219
     
    232232                        // Add admins and moderators to their respective arrays.
    233233                        foreach ( (array) $admin_mods as $user ) {
     234                                $user->user_id  = (int) $user->user_id;
     235                                $user->is_admin = (int) $user->is_admin;
     236                                $user->is_mod   = (int) $user->is_mod;
     237
    234238                                if ( !empty( $user->is_admin ) ) {
    235239                                        $this->admins[] = $user;
     
    242246                        // from the bp_groups cache because it's cached independently.
    243247                        $this->last_activity      = groups_get_groupmeta( $this->id, 'last_activity' );
    244                         $this->total_member_count = groups_get_groupmeta( $this->id, 'total_member_count' );
     248                        $this->total_member_count = (int) groups_get_groupmeta( $this->id, 'total_member_count' );
    245249
    246250                        // Set user-specific data.
     
    436440         * @param string|bool $table_name Optional. Name of the table to check
    437441         *                                against. Default: $bp->groups->table_name.
    438          * @return string|null ID of the group, if one is found, else null.
     442         * @return int|null Group ID if found; null if not.
    439443         */
    440444        public static function group_exists( $slug, $table_name = false ) {
     
    447451                        return false;
    448452
    449                 return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$table_name} WHERE slug = %s", strtolower( $slug ) ) );
     453                $query = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$table_name} WHERE slug = %s", strtolower( $slug ) ) );
     454
     455                return is_numeric( $query ) ? (int) $query : $query;
    450456        }
    451457
  • trunk/src/bp-groups/classes/class-bp-groups-member.php

    r10794 r11027  
    186186
    187187                if ( !empty( $member ) ) {
    188                         $this->id            = $member->id;
    189                         $this->group_id      = $member->group_id;
    190                         $this->user_id       = $member->user_id;
    191                         $this->inviter_id    = $member->inviter_id;
    192                         $this->is_admin      = $member->is_admin;
    193                         $this->is_mod        = $member->is_mod;
    194                         $this->is_banned     = $member->is_banned;
     188                        $this->id            = (int) $member->id;
     189                        $this->group_id      = (int) $member->group_id;
     190                        $this->user_id       = (int) $member->user_id;
     191                        $this->inviter_id    = (int) $member->inviter_id;
     192                        $this->is_admin      = (int) $member->is_admin;
     193                        $this->is_mod        = (int) $member->is_mod;
     194                        $this->is_banned     = (int) $member->is_banned;
    195195                        $this->user_title    = $member->user_title;
    196196                        $this->date_modified = $member->date_modified;
    197                         $this->is_confirmed  = $member->is_confirmed;
     197                        $this->is_confirmed  = (int) $member->is_confirmed;
    198198                        $this->comments      = $member->comments;
    199                         $this->invite_sent   = $member->invite_sent;
     199                        $this->invite_sent   = (int) $member->invite_sent;
    200200
    201201                        $this->user = new BP_Core_User( $this->user_id );
     
    787787         * @param string $type     If 'sent', results are limited to those invitations
    788788         *                         that have actually been sent (non-draft). Default: 'sent'.
    789          * @return int|null The ID of the invitation if found, otherwise null.
     789         * @return int|null The ID of the invitation if found; null if not found.
    790790         */
    791791        public static function check_has_invite( $user_id, $group_id, $type = 'sent' ) {
     
    801801                        $sql .= " AND invite_sent = 1";
    802802
    803                 return $wpdb->get_var( $wpdb->prepare( $sql, $user_id, $group_id ) );
     803                $query = $wpdb->get_var( $wpdb->prepare( $sql, $user_id, $group_id ) );
     804
     805                return is_numeric( $query ) ? (int) $query : $query;
    804806        }
    805807
     
    932934         * @param int $user_id  ID of the user.
    933935         * @param int $group_id ID of the group.
    934          * @return mixed
     936         * @return int|null int 1 if user is banned; int 0 if user is not banned;
     937         *                  null if user is not part of the group or if group doesn't exist.
    935938         */
    936939        public static function check_is_banned( $user_id, $group_id ) {
     
    942945                $bp = buddypress();
    943946
    944                 return $wpdb->get_var( $wpdb->prepare( "SELECT is_banned FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) );
     947                $query = $wpdb->get_var( $wpdb->prepare( "SELECT is_banned FROM {$bp->groups->table_name_members} WHERE user_id = %d AND group_id = %d", $user_id, $group_id ) );
     948
     949                return is_numeric( $query ) ? (int) $query : $query;
    945950        }
    946951
     
    952957         * @param int $user_id  ID of the user.
    953958         * @param int $group_id ID of the group.
    954          * @return int|null ID of the group if the user is the creator,
    955          *                  otherwise false.
     959         * @return int|null int of group ID if user is the creator; null on failure.
    956960         */
    957961        public static function check_is_creator( $user_id, $group_id ) {
     
    963967                $bp = buddypress();
    964968
    965                 return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name} WHERE creator_id = %d AND id = %d", $user_id, $group_id ) );
     969                $query = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->groups->table_name} WHERE creator_id = %d AND id = %d", $user_id, $group_id ) );
     970
     971                return is_numeric( $query ) ? (int) $query : $query;
    966972        }
    967973
     
    973979         * @param int $user_id  ID of the user.
    974980         * @param int $group_id ID of the group.
    975          * @return int|null ID of the membership if found, otherwise false.
     981         * @return int Database ID of the membership if found; int 0 on failure.
    976982         */
    977983        public static function check_for_membership_request( $user_id, $group_id ) {
     
    10021008                // If the user is logged in and viewing their random groups, we can show hidden and private groups.
    10031009                if ( bp_is_my_profile() ) {
    1004                         return $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT group_id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND is_confirmed = 1 AND is_banned = 0 ORDER BY rand() LIMIT %d", $user_id, $total_groups ) );
     1010                        return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT group_id FROM {$bp->groups->table_name_members} WHERE user_id = %d AND is_confirmed = 1 AND is_banned = 0 ORDER BY rand() LIMIT %d", $user_id, $total_groups ) ) );
    10051011                } else {
    1006                         return $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT m.group_id FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND g.status != 'hidden' AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0 ORDER BY rand() LIMIT %d", $user_id, $total_groups ) );
     1012                        return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT m.group_id FROM {$bp->groups->table_name_members} m, {$bp->groups->table_name} g WHERE m.group_id = g.id AND g.status != 'hidden' AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0 ORDER BY rand() LIMIT %d", $user_id, $total_groups ) ) );
    10071013                }
    10081014        }
     
    10211027                $bp = buddypress();
    10221028
    1023                 return $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 1 AND is_banned = 0", $group_id ) );
     1029                return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 1 AND is_banned = 0", $group_id ) ) );
    10241030        }
    10251031
     
    10441050                }
    10451051
     1052                // Integer casting.
     1053                foreach ( (array) $group_admins as $key => $data ) {
     1054                        $group_admins[ $key ]->user_id = (int) $group_admins[ $key ]->user_id;
     1055                }
     1056
    10461057                return $group_admins;
    10471058        }
     
    10601071                $bp = buddypress();
    10611072
    1062                 return $wpdb->get_results( $wpdb->prepare( "SELECT user_id, date_modified FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_mod = 1 AND is_banned = 0", $group_id ) );
     1073                $group_mods = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, date_modified FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_mod = 1 AND is_banned = 0", $group_id ) );
     1074
     1075                // Integer casting.
     1076                foreach ( (array) $group_mods as $key => $data ) {
     1077                        $group_mods[ $key ]->user_id = (int) $group_mods[ $key ]->user_id;
     1078                }
     1079
     1080                return $group_mods;
    10631081        }
    10641082
     
    10931111                $bp = buddypress();
    10941112
    1095                 return $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 0 AND inviter_id = 0", $group_id ) );
     1113                return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 0 AND inviter_id = 0", $group_id ) ) );
    10961114        }
    10971115
Note: See TracChangeset for help on using the changeset viewer.