Skip to:
Content

BuddyPress.org

Changeset 7008


Ignore:
Timestamp:
05/06/2013 07:51:30 PM (12 years ago)
Author:
boonebgorges
Message:

Improve search_terms SQL clause in BP_User_Query

Standardizing the way that apostrophes and other special characters are
escaped in the LIKE claues means that we won't miss items "O'Conner" in member
search.

Fixes #4933

Props dontdream

Location:
trunk
Files:
2 edited

Legend:

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

    r6996 r7008  
    318318        // @todo remove need for bp_is_active() check
    319319        if ( false !== $search_terms && bp_is_active( 'xprofile' ) ) {
    320             $found_user_ids = $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE %s", '%%' . like_escape( $search_terms ) . '%%' ) );
     320            $search_terms_clean = mysql_real_escape_string( mysql_real_escape_string( $search_terms ) );
     321            $search_terms_clean = like_escape( $search_terms_clean );
     322            $found_user_ids_query = "SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE '%" . $search_terms_clean . "%'";
     323            $found_user_ids = $wpdb->get_col( $found_user_ids_query );
    321324
    322325            if ( ! empty( $found_user_ids ) ) {
  • trunk/tests/testcases/core/classes.php

    r6996 r7008  
    101101        $this->assertEquals( $expected, $user_ids );
    102102    }
     103
     104    public function test_bp_user_query_search_with_apostrophe() {
     105        // Apostrophe. Search_terms must escaped to mimic POST payload
     106        $user_id = $this->create_user();
     107        xprofile_set_field_data( 1, $user_id, "Foo'Bar" );
     108        $q = new BP_User_Query( array( 'search_terms' => "oo\'Ba", ) );
     109
     110        $found_user_id = null;
     111        if ( ! empty( $q->results ) ) {
     112            $found_user = array_pop( $q->results );
     113            $found_user_id = $found_user->ID;
     114        }
     115
     116        $this->assertEquals( $user_id, $found_user_id );
     117    }
     118
     119    public function test_bp_user_query_search_with_percent_sign() {
     120
     121        // LIKE special character: %
     122        $user_id = $this->create_user();
     123        xprofile_set_field_data( 1, $user_id, "Foo%Bar" );
     124        $q = new BP_User_Query( array( 'search_terms' => "oo%Bar", ) );
     125
     126        $found_user_id = null;
     127        if ( ! empty( $q->results ) ) {
     128            $found_user = array_pop( $q->results );
     129            $found_user_id = $found_user->ID;
     130        }
     131
     132        $this->assertEquals( $user_id, $found_user_id );
     133
     134    }
     135
     136    public function test_bp_user_query_search_with_underscore() {
     137
     138        // LIKE special character: _
     139        $user_id = $this->create_user();
     140        xprofile_set_field_data( 1, $user_id, "Foo_Bar" );
     141        $q = new BP_User_Query( array( 'search_terms' => "oo_Bar", ) );
     142
     143        $found_user_id = null;
     144        if ( ! empty( $q->results ) ) {
     145            $found_user = array_pop( $q->results );
     146            $found_user_id = $found_user->ID;
     147        }
     148
     149        $this->assertEquals( $user_id, $found_user_id );
     150
     151    }
     152
    103153}
Note: See TracChangeset for help on using the changeset viewer.