Skip to:
Content

BuddyPress.org

Ticket #7477: 7477.3-use-get-slug.diff

File 7477.3-use-get-slug.diff, 1.4 KB (added by dcavins, 8 years ago)

Use get() to find a group by slug in group_exists().

  • src/bp-groups/classes/class-bp-groups-group.php

    diff --git src/bp-groups/classes/class-bp-groups-group.php src/bp-groups/classes/class-bp-groups-group.php
    index 6b35cb3..2dceead 100644
    class BP_Groups_Group { 
    612612         * @since 1.6.0
    613613         *
    614614         * @param string      $slug       Slug to check.
    615          * @param string|bool $table_name Optional. Name of the table to check
    616          *                                against. Default: $bp->groups->table_name.
     615         * @param string|bool $table_name Deprecated.
    617616         * @return int|null Group ID if found; null if not.
    618617         */
    619618        public static function group_exists( $slug, $table_name = false ) {
    620619                global $wpdb;
    621620
    622                 if ( empty( $table_name ) )
    623                         $table_name = buddypress()->groups->table_name;
    624 
    625                 if ( empty( $slug ) )
     621                if ( empty( $slug ) ) {
    626622                        return false;
     623                }
     624
     625                $args = array(
     626                        'slug'               => $slug,
     627                        'per_page'           => 1,
     628                        'page'               => 1,
     629                        'update_meta_cache'  => false,
     630                        'show_hidden'        => true,
     631                );
     632
     633                $groups = BP_Groups_Group::get( $args );
    627634
    628                 $query = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$table_name} WHERE slug = %s", strtolower( $slug ) ) );
     635                $group_id = null;
     636                if ( $groups['groups'] ) {
     637                        $group_id = current( $groups['groups'] )->id;
     638                }
    629639
    630                 return is_numeric( $query ) ? (int) $query : $query;
     640                return $group_id;
    631641        }
    632642
    633643        /**