Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/19/2014 01:36:57 AM (12 years ago)
Author:
boonebgorges
Message:

Refactor all uses of like_escape() to use bp_esc_like()

WordPress 4.0 will deprecate like_escape(), due to a history of inconsistent
documentation and usage. Its replacement is a new method, $wpdb->esc_like(),
which will be available only in WP 4.0. For this reason, and because the
return value of $wpdb->esc_like() will not always be identical to that of
like_escape(), BP cannot do a straight swap of the old function for the new
one. Instead, we introduce a wrapper function that uses the core method if
available, and otherwise reproduces the logic of that method (for earlier
versions of WordPress).

Fixes #5701
slightly different syntax in some cases

File:
1 edited

Legend:

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

    r8105 r8541  
    291291            $user_id = bp_loggedin_user_id();
    292292
    293         $filter = esc_sql( like_escape( $filter ) );
     293        // Only search for matching strings at the beginning of the
     294        // name (@todo - figure out why this restriction)
     295        $search_terms_like = bp_esc_like( $filter ) . '%';
    294296
    295297        $pag_sql = '';
     
    308310        // filter the user_ids based on the search criteria.
    309311        if ( bp_is_active( 'xprofile' ) ) {
    310             $sql       = "SELECT DISTINCT user_id FROM {$bp->profile->table_name_data} WHERE user_id IN ({$fids}) AND value LIKE '{$filter}%%' {$pag_sql}";
    311             $total_sql = "SELECT COUNT(DISTINCT user_id) FROM {$bp->profile->table_name_data} WHERE user_id IN ({$fids}) AND value LIKE '{$filter}%%'";
     312            $sql       = $wpdb->prepare( "SELECT DISTINCT user_id FROM {$bp->profile->table_name_data} WHERE user_id IN ({$fids}) AND value LIKE %s {$pag_sql}", $search_terms_like );
     313            $total_sql = $wpdb->prepare( "SELECT COUNT(DISTINCT user_id) FROM {$bp->profile->table_name_data} WHERE user_id IN ({$fids}) AND value LIKE %s", $search_terms_like );
    312314        } else {
    313             $sql       = "SELECT DISTINCT user_id FROM {$wpdb->usermeta} WHERE user_id IN ({$fids}) AND meta_key = 'nickname' AND meta_value LIKE '{$filter}%%' {$pag_sql}";
    314             $total_sql = "SELECT COUNT(DISTINCT user_id) FROM {$wpdb->usermeta} WHERE user_id IN ({$fids}) AND meta_key = 'nickname' AND meta_value LIKE '{$filter}%%'";
     315            $sql       = $wpdb->prepare( "SELECT DISTINCT user_id FROM {$wpdb->usermeta} WHERE user_id IN ({$fids}) AND meta_key = 'nickname' AND meta_value LIKE %s' {$pag_sql}", $search_terms_like );
     316            $total_sql = $wpdb->prepare( "SELECT COUNT(DISTINCT user_id) FROM {$wpdb->usermeta} WHERE user_id IN ({$fids}) AND meta_key = 'nickname' AND meta_value LIKE %s", $search_terms_like );
    315317        }
    316318
     
    444446        global $wpdb, $bp;
    445447
    446         $filter = esc_sql( like_escape( $filter ) );
     448        // Only search for matching strings at the beginning of the
     449        // name (@todo - figure out why this restriction)
     450        $search_terms_like = bp_esc_like( $filter ) . '%';
    447451
    448452        $usermeta_table = $wpdb->base_prefix . 'usermeta';
     
    455459        // filter the user_ids based on the search criteria.
    456460        if ( bp_is_active( 'xprofile' ) ) {
    457             $sql = "SELECT DISTINCT d.user_id as id FROM {$bp->profile->table_name_data} d, {$users_table} u WHERE d.user_id = u.id AND d.value LIKE '{$filter}%%' ORDER BY d.value DESC {$pag_sql}";
     461            $sql = $wpdb->prepare( "SELECT DISTINCT d.user_id as id FROM {$bp->profile->table_name_data} d, {$users_table} u WHERE d.user_id = u.id AND d.value LIKE %s ORDER BY d.value DESC {$pag_sql}", $search_terms_like );
    458462        } else {
    459             $sql = "SELECT DISTINCT user_id as id FROM {$usermeta_table} WHERE meta_value LIKE '{$filter}%%' ORDER BY d.value DESC {$pag_sql}";
     463            $sql = $wpdb->prepare( "SELECT DISTINCT user_id as id FROM {$usermeta_table} WHERE meta_value LIKE %s ORDER BY d.value DESC {$pag_sql}", $search_terms_like );
    460464        }
    461465
     
    479483        global $wpdb, $bp;
    480484
    481         $filter = esc_sql( like_escape( $filter ) );
     485        // Only search for matching strings at the beginning of the
     486        // name (@todo - figure out why this restriction)
     487        $search_terms_like = bp_esc_like( $filter ) . '%';
    482488
    483489        $usermeta_table = $wpdb->prefix . 'usermeta';
     
    486492        // filter the user_ids based on the search criteria.
    487493        if ( bp_is_active( 'xprofile' ) ) {
    488             $sql = "SELECT COUNT(DISTINCT d.user_id) FROM {$bp->profile->table_name_data} d, {$users_table} u WHERE d.user_id = u.id AND d.value LIKE '{$filter}%%'";
     494            $sql = $wpdb->prepare( "SELECT COUNT(DISTINCT d.user_id) FROM {$bp->profile->table_name_data} d, {$users_table} u WHERE d.user_id = u.id AND d.value LIKE %s", $search_terms_like );
    489495        } else {
    490             $sql = "SELECT COUNT(DISTINCT user_id) FROM {$usermeta_table} WHERE meta_value LIKE '{$filter}%%'";
     496            $sql = $wpdb->prepare( "SELECT COUNT(DISTINCT user_id) FROM {$usermeta_table} WHERE meta_value LIKE %s", $search_terms_like );
    491497        }
    492498
Note: See TracChangeset for help on using the changeset viewer.