Skip to:
Content

BuddyPress.org

Changeset 9925


Ignore:
Timestamp:
06/11/2015 02:18:26 AM (5 years ago)
Author:
johnjamesjacoby
Message:

Messages: Whitespace & type-casting in class-bp_messages-thread.php

File:
1 edited

Legend:

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

    r9862 r9925  
    1616 */
    1717class BP_Messages_Thread {
     18
    1819    /**
    1920     * The message thread ID.
     
    138139     */
    139140    public function populate( $thread_id = 0, $order = 'ASC', $args = array() ) {
    140         global $wpdb;
    141 
    142         if( 'ASC' != $order && 'DESC' != $order ) {
     141
     142        if ( 'ASC' !== $order && 'DESC' !== $order ) {
    143143            $order = 'ASC';
    144144        }
     
    146146        // merge $args with our defaults
    147147        $r = wp_parse_args( $args, array(
     148            'user_id'           => bp_loggedin_user_id(),
    148149            'update_meta_cache' => true
    149150        ) );
    150151
    151152        $this->messages_order = $order;
    152         $this->thread_id      = $thread_id;
     153        $this->thread_id      = (int) $thread_id;
    153154
    154155        // get messages for thread
    155         $this->messages = self::get_messages( $thread_id );
     156        $this->messages = self::get_messages( $this->thread_id );
    156157
    157158        if ( empty( $this->messages ) || is_wp_error( $this->messages ) ) {
     
    165166
    166167        foreach ( (array) $this->messages as $key => $message ) {
    167             $this->sender_ids[$message->sender_id] = $message->sender_id;
     168            $this->sender_ids[ $message->sender_id ] = $message->sender_id;
    168169        }
    169170
     
    172173
    173174        // Get the unread count for the logged in user
    174         if ( isset( $this->recipients[bp_loggedin_user_id()] ) ) {
    175             $this->unread_count = $this->recipients[bp_loggedin_user_id()]->unread_count;
     175        if ( isset( $this->recipients[ $r['user_id'] ] ) ) {
     176            $this->unread_count = $this->recipients[ $r['user_id'] ]->unread_count;
    176177        }
    177178
     
    230231        }
    231232
     233        $thread_id = (int) $thread_id;
     234
    232235        $recipients = wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' );
    233236        if ( false === $recipients ) {
     
    235238
    236239            $recipients = array();
    237             $results    = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $thread_id ) );
     240            $sql        = $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $thread_id );
     241            $results    = $wpdb->get_results( $sql );
    238242
    239243            foreach ( (array) $results as $recipient ) {
     
    267271     */
    268272    public static function get_messages( $thread_id = 0 ) {
    269         $messages = wp_cache_get( $thread_id, 'bp_messages_threads' );
     273        $thread_id = (int) $thread_id;
     274        $messages  = wp_cache_get( $thread_id, 'bp_messages_threads' );
    270275
    271276        if ( false === $messages ) {
     
    309314     * @return bool
    310315     */
    311     public static function delete( $thread_id ) {
    312         global $wpdb;
     316    public static function delete( $thread_id = 0 ) {
     317        global $wpdb;
     318
     319        $thread_id = (int) $thread_id;
    313320
    314321        /**
     
    353360            // Do something for each message ID
    354361            foreach ( $message_ids as $message_id ) {
     362
    355363                // Delete message meta
    356364                bp_messages_delete_meta( $message_id );
     
    422430        }
    423431
    424         $defaults = array(
     432        $r = bp_parse_args( $args, array(
    425433            'user_id'      => false,
    426434            'box'          => 'inbox',
     
    430438            'search_terms' => '',
    431439            'meta_query'   => array()
    432         );
    433         $r = wp_parse_args( $args, $defaults );
     440        ) );
    434441
    435442        $pag_sql = $type_sql = $search_sql = $user_id_sql = $sender_sql = '';
     
    495502        // Sort threads by date_sent
    496503        foreach( (array) $thread_ids as $thread ) {
    497             $sorted_threads[$thread->thread_id] = strtotime( $thread->date_sent );
     504            $sorted_threads[ $thread->thread_id ] = strtotime( $thread->date_sent );
    498505        }
    499506
    500507        arsort( $sorted_threads );
    501508
    502         $threads = false;
     509        $threads = array();
    503510        foreach ( (array) $sorted_threads as $thread_id => $date_sent ) {
    504511            $threads[] = new BP_Messages_Thread( $thread_id, 'ASC', array(
     
    517524         * }
    518525         */
    519         return apply_filters( 'bp_messages_thread_current_threads', array( 'threads' => &$threads, 'total' => (int) $total_threads ) );
     526        return apply_filters( 'bp_messages_thread_current_threads', array(
     527            'threads' => &$threads,
     528            'total'   => (int) $total_threads
     529        ) );
    520530    }
    521531
     
    561571     * @param int $thread_id The message thread ID.
    562572     */
    563     public static function mark_as_read( $thread_id ) {
     573    public static function mark_as_read( $thread_id = 0 ) {
    564574        global $wpdb;
    565575
    566576        $bp  = buddypress();
    567577        $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 0 WHERE user_id = %d AND thread_id = %d", bp_loggedin_user_id(), $thread_id );
    568         $wpdb->query($sql);
     578        $wpdb->query( $sql );
    569579
    570580        wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' );
     
    579589     * @param int $thread_id The message thread ID.
    580590     */
    581     public static function mark_as_unread( $thread_id ) {
     591    public static function mark_as_unread( $thread_id = 0 ) {
    582592        global $wpdb;
    583593
    584594        $bp  = buddypress();
    585595        $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 1 WHERE user_id = %d AND thread_id = %d", bp_loggedin_user_id(), $thread_id );
    586         $wpdb->query($sql);
     596        $wpdb->query( $sql );
    587597
    588598        wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' );
     
    605615        global $wpdb;
    606616
    607         $exclude_sender = '';
    608         if ( $box != 'sentbox' )
    609             $exclude_sender = ' AND sender_only != 1';
    610 
    611         if ( $type == 'unread' )
    612             $type_sql = " AND unread_count != 0 ";
    613         elseif ( $type == 'read' )
    614             $type_sql = " AND unread_count = 0 ";
     617        $exclude_sender = $type_sql = '';
     618        if ( $box !== 'sentbox' ) {
     619            $exclude_sender = 'AND sender_only != 1';
     620        }
     621
     622        if ( $type === 'unread' ) {
     623            $type_sql = 'AND unread_count != 0';
     624        } elseif ( $type === 'read' ) {
     625            $type_sql = 'AND unread_count = 0';
     626        }
    615627
    616628        $bp = buddypress();
    617629
    618         return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(thread_id) FROM {$bp->messages->table_name_recipients} WHERE user_id = %d AND is_deleted = 0{$exclude_sender} {$type_sql}", $user_id ) );
     630        return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(thread_id) FROM {$bp->messages->table_name_recipients} WHERE user_id = %d AND is_deleted = 0 {$exclude_sender} {$type_sql}", $user_id ) );
    619631    }
    620632
     
    635647        $sender_ids = $wpdb->get_col( $wpdb->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE thread_id = %d", $thread_id ) );
    636648
    637         if ( ! $sender_ids ) {
     649        if ( empty( $sender_ids ) ) {
    638650            return false;
    639651        }
     
    711723     */
    712724    public static function check_access( $thread_id, $user_id = 0 ) {
     725
    713726        if ( empty( $user_id ) ) {
    714727            $user_id = bp_loggedin_user_id();
     
    717730        $recipients = self::get_recipients_for_thread( $thread_id );
    718731
    719         if ( isset( $recipients[$user_id] ) && 0 == $recipients[$user_id]->is_deleted ) {
    720             return $recipients[$user_id]->id;
     732        if ( isset( $recipients[ $user_id ] ) && 0 == $recipients[ $user_id ]->is_deleted ) {
     733            return $recipients[ $user_id ]->id;
    721734        } else {
    722735            return null;
     
    734747     */
    735748    public static function is_valid( $thread_id = 0 ) {
     749
    736750        // Bail if no thread ID is passed
    737751        if ( empty( $thread_id ) ) {
     
    763777     */
    764778    public static function get_recipient_links( $recipients ) {
    765         if ( count( $recipients ) >= 5 )
     779
     780        if ( count( $recipients ) >= 5 ) {
    766781            return sprintf( __( '%s Recipients', 'buddypress' ), number_format_i18n( count( $recipients ) ) );
     782        }
    767783
    768784        $recipient_links = array();
     
    806822            $message_ids = maybe_unserialize( $thread->message_ids );
    807823
    808             if ( !empty( $message_ids ) ) {
     824            if ( ! empty( $message_ids ) ) {
    809825                $message_ids = implode( ',', $message_ids );
    810826
    811827                // Add the thread_id to the messages table
    812                 if ( ! $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_messages} SET thread_id = %d WHERE id IN ({$message_ids})", $thread->id ) ) )
     828                if ( ! $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_messages} SET thread_id = %d WHERE id IN ({$message_ids})", $thread->id ) ) ) {
    813829                    $errors = true;
     830                }
    814831            }
    815832        }
    816833
    817         if ( $errors ) {
    818             return false;
    819         }
    820 
    821         return true;
     834        return (bool) ! $errors;
    822835    }
    823836}
Note: See TracChangeset for help on using the changeset viewer.