Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
01/14/2017 01:32:34 AM (8 years ago)
Author:
dcavins
Message:

BP_Groups_Group::get(): Add search_column parameter.

Allow developers to specify which column should be searched and where
wildcard characters should be placed in BP_Groups_Group::get().

Fixes #7418.

File:
1 edited

Legend:

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

    r11363 r11383  
    883883     *     @type int          $user_id            Optional. If provided, results will be limited to groups
    884884     *                                            of which the specified user is a member. Default: null.
    885      *     @type string       $search_terms       Optional. If provided, only groups whose names or descriptions
    886      *                                            match the search terms will be returned. Default: false.
     885     *     @type string       $search_terms       Optional. If provided, only groups whose names or descriptions
     886     *                                            match the search terms will be returned. Allows specifying the
     887     *                                            wildcard position using a '*' character before or after the
     888     *                                            string or both. Works in concert with $search_columns.
     889     *                                            Default: false.
     890     *     @type string       $search_columns     Optional. If provided, only apply the search terms to the
     891     *                                            specified columns. Works in concert with $search_terms.
     892     *                                            Default: empty array.
    887893     *     @type array|string $group_type         Array or comma-separated list of group types to limit results to.
    888894     *     @type array|string $group_type__in     Array or comma-separated list of group types to limit results to.
     
    940946            'user_id'            => 0,
    941947            'search_terms'       => false,
     948            'search_columns'     => array(),
    942949            'group_type'         => '',
    943950            'group_type__in'     => '',
     
    974981        }
    975982
    976         if ( ! empty( $r['search_terms'] ) ) {
    977             $search_terms_like = '%' . bp_esc_like( $r['search_terms'] ) . '%';
    978             $where_conditions['search'] = $wpdb->prepare( "( g.name LIKE %s OR g.description LIKE %s )", $search_terms_like, $search_terms_like );
     983        $search = '';
     984        if ( isset( $r['search_terms'] ) ) {
     985            $search = trim( $r['search_terms'] );
     986        }
     987
     988        if ( $search ) {
     989            $leading_wild = ( ltrim( $search, '*' ) != $search );
     990            $trailing_wild = ( rtrim( $search, '*' ) != $search );
     991            if ( $leading_wild && $trailing_wild ) {
     992                $wild = 'both';
     993            } elseif ( $leading_wild ) {
     994                $wild = 'leading';
     995            } elseif ( $trailing_wild ) {
     996                $wild = 'trailing';
     997            } else {
     998                // Default is to wrap in wildcard characters.
     999                $wild = 'both';
     1000            }
     1001            $search = trim( $search, '*' );
     1002
     1003            $searches = array();
     1004            $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : '';
     1005            $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : '';
     1006            $wildcarded = $leading_wild . bp_esc_like( $search ) . $trailing_wild;
     1007
     1008            $search_columns = array( 'name', 'description' );
     1009            if ( $r['search_columns'] ) {
     1010                $search_columns = array_intersect( $r['search_columns'], $search_columns );
     1011            }
     1012
     1013            foreach ( $search_columns as $search_column ) {
     1014                $searches[] = $wpdb->prepare( "$search_column LIKE %s", $wildcarded );
     1015            }
     1016
     1017            $where_conditions['search'] = '(' . implode(' OR ', $searches) . ')';
    9791018        }
    9801019
Note: See TracChangeset for help on using the changeset viewer.