Skip to:
Content

BuddyPress.org

Changeset 8675


Ignore:
Timestamp:
07/23/2014 05:56:57 PM (7 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

Location:
trunk
Files:
3 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        }
  • trunk/src/bp-xprofile/bp-xprofile-functions.php

    r8672 r8675  
    596596    $search_terms_clean = esc_sql( esc_sql( $query->query_vars['search_terms'] ) );
    597597
     598    if ( $query->query_vars['search_wildcard'] === 'left' ) {
     599        $search_terms_clean = '%' . $search_terms_clean;
     600    } elseif ( $query->query_vars['search_wildcard'] === 'right' ) {
     601        $search_terms_clean = $search_terms_clean . '%';
     602    } else {
     603        $search_terms_clean = '%' . $search_terms_clean . '%';
     604    }
     605
    598606    // Combine the core search (against wp_users) into a single OR clause
    599607    // with the xprofile_data search
    600608    $search_core     = $sql['where']['search'];
    601     $search_xprofile = "u.{$query->uid_name} IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE '%{$search_terms_clean}%' )";
     609    $search_xprofile = "u.{$query->uid_name} IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE '{$search_terms_clean}' )";
    602610    $search_combined = "( {$search_xprofile} OR {$search_core} )";
    603611
  • trunk/tests/phpunit/testcases/core/class-bp-user-query.php

    r8087 r8675  
    220220        $this->assertEquals( $user_id, $found_user_id );
    221221    }
     222
     223    public function test_bp_user_query_search_wildcards() {
     224        $u1 = $this->create_user( array(
     225            'user_login' => 'xfoo',
     226        ) );
     227        xprofile_set_field_data( 1, $u1, "Bar" );
     228        $q1 = new BP_User_Query( array( 'search_terms' => 'foo', 'search_wildcard' => 'left' ) );
     229
     230        $u2 = $this->create_user( array(
     231            'user_login' => 'foox',
     232        ) );
     233        xprofile_set_field_data( 1, $u2, "Bar" );
     234        $q2 = new BP_User_Query( array( 'search_terms' => 'foo', 'search_wildcard' => 'right' ) );
     235
     236        $u3 = $this->create_user( array(
     237            'user_login' => 'xfoox',
     238        ) );
     239        xprofile_set_field_data( 1, $u3, "Bar" );
     240        $q3 = new BP_User_Query( array( 'search_terms' => 'foo', 'search_wildcard' => 'both' ) );
     241
     242        $this->assertNotEmpty( $q1->results );
     243        $q1 = array_pop( $q1->results );
     244        $this->assertEquals( $u1, $q1->ID );
     245
     246        $this->assertNotEmpty( $q2->results );
     247        $q2 = array_pop( $q2->results );
     248        $this->assertEquals( $u2, $q2->ID );
     249
     250        $this->assertNotEmpty( $q3->results );
     251        foreach ( $q3->results as $user ) {
     252            $this->assertTrue( in_array( $user->ID, array( $u1, $u2, $u3 ) ) );
     253        }
     254    }
     255
    222256    /**
    223257     * @group exclude
Note: See TracChangeset for help on using the changeset viewer.