Skip to:
Content

BuddyPress.org

Changeset 9331


Ignore:
Timestamp:
01/09/2015 07:24:02 PM (10 years ago)
Author:
r-a-y
Message:

Messages: Do not duplicate SQL statements in BP_Messages_Thread::get_current_threads_for_user()

Previously, to query the thread IDs and the total threads, two separate SQL
statements were generated. Both of these statements are almost identical.
Therefore, this commit uses an array to house the SQL clauses, which is
later adjusted in the total thread query.

See #6063.

File:
1 edited

Legend:

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

    r9318 r9331  
    364364        $r = wp_parse_args( $args, $defaults );
    365365
    366         $pag_sql = $type_sql = $search_sql = '';
     366        $pag_sql = $type_sql = $search_sql = $user_id_sql = $sender_sql = '';
    367367
    368368        if ( $r['limit'] && $r['page'] ) {
     
    381381        }
    382382
    383         if ( 'sentbox' == $r['box'] ) {
    384             $user_id_sql = $wpdb->prepare( 'm.sender_id = %d', $r['user_id'] );
    385             $thread_ids  = $wpdb->get_results( "SELECT m.thread_id, MAX(m.date_sent) AS date_sent FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND m.sender_id = r.user_id AND {$user_id_sql} AND r.is_deleted = 0 {$search_sql} GROUP BY m.thread_id ORDER BY date_sent DESC {$pag_sql}" );
    386             $total_threads = $wpdb->get_var( "SELECT COUNT( DISTINCT m.thread_id ) FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND m.sender_id = r.user_id AND {$user_id_sql} AND r.is_deleted = 0 {$search_sql} " );
    387         } else {
    388             $user_id_sql = $wpdb->prepare( 'r.user_id = %d', $r['user_id'] );
    389             $thread_ids = $wpdb->get_results( "SELECT m.thread_id, MAX(m.date_sent) AS date_sent FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND r.is_deleted = 0 AND {$user_id_sql} AND r.sender_only = 0 {$type_sql} {$search_sql} GROUP BY m.thread_id ORDER BY date_sent DESC {$pag_sql}" );
    390             $total_threads = $wpdb->get_var( "SELECT COUNT( DISTINCT m.thread_id ) FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m WHERE m.thread_id = r.thread_id AND r.is_deleted = 0 AND {$user_id_sql} AND r.sender_only = 0 {$type_sql} {$search_sql}" );
    391         }
    392 
     383        if ( ! empty( $r['user_id'] ) ) {
     384            if ( 'sentbox' == $r['box'] ) {
     385                $user_id_sql = 'AND ' . $wpdb->prepare( 'm.sender_id = %d', $r['user_id'] );
     386                $sender_sql  = ' AND m.sender_id = r.user_id';
     387            } else {
     388                $user_id_sql = 'AND ' . $wpdb->prepare( 'r.user_id = %d', $r['user_id'] );
     389                $sender_sql  = ' AND r.sender_only = 0';
     390            }
     391        }
     392
     393        // set up SQL array
     394        $sql = array();
     395        $sql['select'] = 'SELECT m.thread_id, MAX(m.date_sent) AS date_sent';
     396        $sql['from']   = "FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_messages} m";
     397        $sql['where']  = "WHERE m.thread_id = r.thread_id AND r.is_deleted = 0 {$user_id_sql} {$sender_sql} {$type_sql} {$search_sql}";
     398        $sql['misc']   = "GROUP BY m.thread_id ORDER BY date_sent DESC {$pag_sql}";
     399
     400        // get thread IDs
     401        $thread_ids = $wpdb->get_results( implode( ' ', $sql ) );
    393402        if ( empty( $thread_ids ) ) {
    394403            return false;
    395404        }
     405
     406        // adjust $sql to work for thread total
     407        $sql['select'] = 'SELECT COUNT( DISTINCT m.thread_id )';
     408        unset( $sql['misc'] );
     409        $total_threads = $wpdb->get_var( implode( ' ', $sql ) );
    396410
    397411        // Sort threads by date_sent
Note: See TracChangeset for help on using the changeset viewer.