Skip to:
Content

BuddyPress.org

Changeset 8753


Ignore:
Timestamp:
08/03/2014 08:51:44 PM (10 years ago)
Author:
djpaul
Message:

Core: adjust the SQL generated by BP_User_Query's search_wildcard parameter for better Suggestions API search results.

Introduced in r8675, the search_wildcard parameter controls where BuddyPress places MySQL wildcard characters around the search term (when it's set).

This change adjusts how the wildcard SQL is constructed; in addition to the wildcard, we now also check for matches that have a single space character at the opposite end of the string (from where the wildcard is). This allows searching data that looks like (for example) a surname in a combined first name/surname field.
e.g. if a displayname is "Paul Gibbs", you can search for "Gi", and with search_wildcard=right, the SQL generated will be LIKE 'Gi%' OR LIKE '% Gi%'.

See #3278

Location:
trunk/src
Files:
3 edited

Legend:

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

    r8677 r8753  
    369369        // xprofile field matches happen in bp_xprofile_bp_user_query_search()
    370370        if ( false !== $search_terms ) {
    371             $search_terms_like = bp_esc_like( $search_terms );
     371            $search_terms = bp_esc_like( $search_terms );
    372372
    373373            if ( $search_wildcard === 'left' ) {
    374                 $search_terms_like = '%' . $search_terms_like;
     374                $search_terms_nospace = '%' . $search_terms;
     375                $search_terms_space   = '%' . $search_terms . ' %';
    375376            } elseif ( $search_wildcard === 'right' ) {
    376                 $search_terms_like = $search_terms_like . '%';
     377                $search_terms_nospace =        $search_terms . '%';
     378                $search_terms_space   = '% ' . $search_terms . '%';
    377379            } else {
    378                 $search_terms_like = '%' . $search_terms_like . '%';
    379             }
    380 
    381             $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 );
     380                $search_terms_nospace = '%' . $search_terms . '%';
     381                $search_terms_space   = '%' . $search_terms . '%';
     382            }
     383
     384            $sql['where']['search'] = $wpdb->prepare(
     385                "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE ( user_login LIKE %s OR user_login LIKE %s OR user_nicename LIKE %s OR user_nicename LIKE %s ) )",
     386                $search_terms_nospace,
     387                $search_terms_space,
     388                $search_terms_nospace,
     389                $search_terms_space
     390            );
    382391        }
    383392
     
    25672576     */
    25682577    protected $default_args = array(
    2569         'limit'        => 16,
     2578        'limit'        => 10,
    25702579        'only_friends' => false,
    25712580        'term'         => '',
     
    26072616            'per_page'        => $this->args['limit'],
    26082617            'search_terms'    => $this->args['term'],
     2618            'search_wildcard' => is_rtl() ? 'left' : 'right',
    26092619        );
    26102620
  • trunk/src/bp-groups/bp-groups-classes.php

    r8669 r8753  
    43884388            'per_page'        => $this->args['limit'],
    43894389            'search_terms'    => $this->args['term'],
     4390            'search_wildcard' => 'right',
    43904391        );
    43914392
  • trunk/src/bp-xprofile/bp-xprofile-functions.php

    r8689 r8753  
    591591    $bp = buddypress();
    592592
    593     $search_terms_clean = esc_sql( esc_sql( $query->query_vars['search_terms'] ) );
     593    $search_terms_clean = bp_esc_like( $query->query_vars['search_terms'] );
    594594
    595595    if ( $query->query_vars['search_wildcard'] === 'left' ) {
    596         $search_terms_clean = '%' . $search_terms_clean;
     596        $search_terms_nospace = '%' . $search_terms_clean;
     597        $search_terms_space   = '%' . $search_terms_clean . ' %';
    597598    } elseif ( $query->query_vars['search_wildcard'] === 'right' ) {
    598         $search_terms_clean = $search_terms_clean . '%';
     599        $search_terms_nospace =        $search_terms_clean . '%';
     600        $search_terms_space   = '% ' . $search_terms_clean . '%';
    599601    } else {
    600         $search_terms_clean = '%' . $search_terms_clean . '%';
     602        $search_terms_nospace = '%' . $search_terms_clean . '%';
     603        $search_terms_space   = '%' . $search_terms_clean . '%';
    601604    }
    602605
    603606    // Combine the core search (against wp_users) into a single OR clause
    604607    // with the xprofile_data search
     608    $search_xprofile = $wpdb->prepare(
     609        "u.{$query->uid_name} IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE %s OR value LIKE %s )",
     610        $search_terms_nospace,
     611        $search_terms_space
     612    );
     613
    605614    $search_core     = $sql['where']['search'];
    606     $search_xprofile = "u.{$query->uid_name} IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE '{$search_terms_clean}' )";
    607615    $search_combined = "( {$search_xprofile} OR {$search_core} )";
    608 
    609616    $sql['where']['search'] = $search_combined;
    610617
Note: See TracChangeset for help on using the changeset viewer.