Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/23/2014 05:56:57 PM (11 years ago)
Author:
djpaul
Message:

Core: add new search_wildcard parameter to BP_User_Query.

This new parameter controls where BuddyPress places MySQL wildcard characters around the search term (when it's set).
Prior to this change, BuddyPress added a wildcard character to both ends of the search term, but for some use cases, a more restrictive search pattern is required; you can specify "left", "right", or "both" (the default) to pick where the wildcard is added.

Fixes #5769

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-classes.php

    r8608 r8675  
    3131 *           across xprofile fields. Requires XProfile component.
    3232 *           Default: false.
     33 *     @type string $search_wildcard When searching with $search_terms,
     34 *           set where wildcards around the term should be positioned.
     35 *           Default: 'both'. Other values: 'left', 'right'.
    3336 *     @type array|string|bool $include An array or comma-separated list of
    3437 *           user IDs to which query should be limited.
     
    154157                'user_id'         => 0,
    155158                'search_terms'    => false,
     159                'search_wildcard' => 'both',
    156160                'include'         => false,
    157161                'exclude'         => false,
     
    365369        // xprofile field matches happen in bp_xprofile_bp_user_query_search()
    366370        if ( false !== $search_terms ) {
    367             $search_terms_like = '%' . bp_esc_like( $search_terms ) . '%';
     371            $search_terms_like = bp_esc_like( $search_terms );
     372
     373            if ( $search_wildcard === 'left' ) {
     374                $search_terms_like = '%' . $search_terms_like;
     375            } elseif ( $search_wildcard === 'right' ) {
     376                $search_terms_like = $search_terms_like . '%';
     377            } else {
     378                $search_terms_like = '%' . $search_terms_like . '%';
     379            }
     380
    368381            $sql['where']['search'] = $wpdb->prepare( "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE ( user_login LIKE %s OR user_nicename LIKE %s ) )", $search_terms_like, $search_terms_like );
    369382        }
Note: See TracChangeset for help on using the changeset viewer.