Skip to:
Content

BuddyPress.org

Changeset 9328


Ignore:
Timestamp:
01/09/2015 04:06:51 PM (5 years ago)
Author:
boonebgorges
Message:

Search consistently against blog title and description in BP_Blogs_Blog::get().

Previously, the description field was only being matched for the 'total' query,
with the result that pagination and other count-based results did not match the
blogs actually returned by the query.

This changeset reworks the syntax a bit, to avoid the duplicated logic when
using search terms, and to use proper JOIN syntax instead of commas.

Props jreeve.
Fixes #5858.

Location:
trunk
Files:
2 edited

Legend:

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

    r9218 r9328  
    174174        }
    175175
    176         if ( !empty( $search_terms ) ) {
     176        if ( ! empty( $search_terms ) ) {
    177177            $search_terms_like = '%' . bp_esc_like( $search_terms ) . '%';
    178             $search_terms_sql  = $wpdb->prepare( 'bm2.meta_value LIKE %s', $search_terms_like );
    179             $paged_blogs = $wpdb->get_results( "SELECT b.blog_id, b.user_id as admin_user_id, u.user_email as admin_user_email, wb.domain, wb.path, bm.meta_value as last_activity, bm2.meta_value as name FROM {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm, {$bp->blogs->table_name_blogmeta} bm2, {$wpdb->base_prefix}blogs wb, {$wpdb->users} u WHERE b.blog_id = wb.blog_id AND b.user_id = u.ID AND b.blog_id = bm.blog_id AND b.blog_id = bm2.blog_id AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} AND bm.meta_key = 'last_activity' AND bm2.meta_key = 'name' AND {$search_terms_sql} {$user_sql} {$include_sql} GROUP BY b.blog_id {$order_sql} {$pag_sql}" );
    180             $total_blogs = $wpdb->get_var( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name_blogmeta} bm, {$bp->blogs->table_name_blogmeta} bm2 WHERE b.blog_id = wb.blog_id AND bm.blog_id = b.blog_id AND bm2.blog_id = b.blog_id AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} AND bm.meta_key = 'name' AND bm2.meta_key = 'description' AND {$search_terms_sql} {$user_sql} {$include_sql}" );
     178            $search_terms_sql  = $wpdb->prepare( 'AND (bm_name.meta_value LIKE %s OR bm_description.meta_value LIKE %s)', $search_terms_like, $search_terms_like );
    181179        } else {
    182             $paged_blogs = $wpdb->get_results( "SELECT b.blog_id, b.user_id as admin_user_id, u.user_email as admin_user_email, wb.domain, wb.path, bm.meta_value as last_activity, bm2.meta_value as name FROM {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm, {$bp->blogs->table_name_blogmeta} bm2, {$wpdb->base_prefix}blogs wb, {$wpdb->users} u WHERE b.blog_id = wb.blog_id AND b.user_id = u.ID AND b.blog_id = bm.blog_id AND b.blog_id = bm2.blog_id {$user_sql} AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} AND bm.meta_key = 'last_activity' AND bm2.meta_key = 'name' {$include_sql} GROUP BY b.blog_id {$order_sql} {$pag_sql}" );
    183             $total_blogs = $wpdb->get_var( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb WHERE b.blog_id = wb.blog_id {$user_sql} AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$include_sql} {$hidden_sql}" );
    184         }
     180            $search_terms_sql = '';
     181        }
     182
     183        $paged_blogs = $wpdb->get_results( "
     184            SELECT b.blog_id, b.user_id as admin_user_id, u.user_email as admin_user_email, wb.domain, wb.path, bm.meta_value as last_activity, bm_name.meta_value as name
     185            FROM
     186              {$bp->blogs->table_name} b
     187              LEFT JOIN {$bp->blogs->table_name_blogmeta} bm ON (b.blog_id = bm.blog_id)
     188              LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_name ON (b.blog_id = bm_name.blog_id)
     189              LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_description ON (b.blog_id = bm_description.blog_id)
     190              LEFT JOIN {$wpdb->base_prefix}blogs wb ON (b.blog_id = wb.blog_id)
     191              LEFT JOIN {$wpdb->users} u ON (b.user_id = u.ID)
     192            WHERE
     193              wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql}
     194              AND bm.meta_key = 'last_activity' AND bm_name.meta_key = 'name' AND bm_description.meta_key = 'description'
     195              {$search_terms_sql} {$user_sql} {$include_sql}
     196            GROUP BY b.blog_id {$order_sql} {$pag_sql}
     197        " );
     198
     199        $total_blogs = $wpdb->get_var( "
     200            SELECT COUNT(DISTINCT b.blog_id)
     201            FROM
     202              {$bp->blogs->table_name} b
     203              LEFT JOIN {$wpdb->base_prefix}blogs wb ON (b.blog_id = wb.blog_id)
     204              LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_name ON (b.blog_id = bm_name.blog_id)
     205              LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_description ON (b.blog_id = bm_description.blog_id)
     206            WHERE
     207              wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql}
     208              AND
     209              bm_name.meta_key = 'name' AND bm_description.meta_key = 'description'
     210              {$search_terms_sql} {$user_sql} {$include_sql}
     211        " );
    185212
    186213        $blog_ids = array();
  • trunk/tests/phpunit/testcases/blogs/class-bp-blogs-blog.php

    r9139 r9328  
    3838
    3939        $this->set_current_user( $old_user );
     40        wpmu_delete_blog( $b, true );
     41    }
     42
     43    /**
     44     * @ticket BP5858
     45     */
     46    public function test_get_with_search_terms_should_match_description() {
     47        if ( ! is_multisite() ) {
     48            return;
     49        }
     50
     51        $old_user = get_current_user_id();
     52
     53        $u = $this->factory->user->create();
     54        $this->set_current_user( $u );
     55        $b = $this->factory->blog->create( array(
     56            'title' => 'The Foo Bar Blog',
     57            'domain' => __METHOD__,
     58            'user_id' => $u,
     59        ) );
     60        update_blog_option( $b, 'blogdescription', 'Full of foorificness' );
     61        bp_blogs_record_existing_blogs();
     62
     63        // make the blog public or it won't turn up in generic results
     64        update_blog_option( $b, 'blog_public', '1' );
     65
     66        $blogs = BP_Blogs_Blog::get( 'active', false, false, 0, 'Full' );
     67        $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' );
     68
     69        $this->assertEquals( array( $b ), $blog_ids );
     70        $this->assertEquals( 1, $blogs['total'] );
     71
     72        $this->set_current_user( $old_user );
     73        wpmu_delete_blog( $b, true );
    4074    }
    4175
     
    6599
    66100        $this->set_current_user( $old_user );
     101        wpmu_delete_blog( $b, true );
    67102    }
    68103
     
    95130
    96131        $this->set_current_user( $old_user );
     132        wpmu_delete_blog( $b, true );
    97133    }
    98134}
Note: See TracChangeset for help on using the changeset viewer.