Skip to:
Content

BuddyPress.org

Changeset 3760


Ignore:
Timestamp:
01/20/2011 11:43:41 AM (16 years ago)
Author:
johnjamesjacoby
Message:

Some messages code clean-up

Location:
trunk/bp-messages
Files:
2 edited

Legend:

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

    r3713 r3760  
    2121
    2222                $this->messages_order = $order;
    23                 $this->thread_id = $thread_id;
     23                $this->thread_id      = $thread_id;
    2424
    2525                if ( !$this->messages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE thread_id = %d ORDER BY date_sent " . $order, $this->thread_id ) ) )
    … …  
    2929                        $this->sender_ids[$message->sender_id] = $message->sender_id;
    3030
    31                 /* Fetch the recipients */
     31                // Fetch the recipients
    3232                $this->recipients = $this->get_recipients();
    3333
    34                 /* Get the unread count for the logged in user */
     34                // Get the unread count for the logged in user
    3535                if ( isset( $this->recipients[$bp->loggedin_user->id] ) )
    3636                        $this->unread_count = $this->recipients[$bp->loggedin_user->id]->unread_count;
    … …  
    6868
    6969                if ( empty( $recipients ) ) {
    70                         /* Delete all the messages */
     70                        // Delete all the messages
    7171                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_messages} WHERE thread_id = %d", $thread_id ) );
    7272
    73                         /* Delete all the recipients */
     73                        // Delete all the recipients
    7474                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $thread_id ) );
    7575                }
    … …  
    8181                global $wpdb, $bp;
    8282
    83                 $pag_sql = '';
     83                $pag_sql = $type_sql = '';
    8484                if ( $limit && $page )
    8585                        $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
    8686
    87                 $type_sql = '';
    8887                if ( $type == 'unread' )
    8988                        $type_sql = $wpdb->prepare( " AND r.unread_count != 0 " );
    … …  
    102101                        return false;
    103102
    104                 /* Sort threads by date_sent */
    105                 foreach( (array)$thread_ids as $thread ) {
    106                         $sorted_threads[$thread->thread_id] = strtotime($thread->date_sent);
    107                 }
    108                 arsort($sorted_threads);
     103                // Sort threads by date_sent
     104                foreach( (array)$thread_ids as $thread )
     105                        $sorted_threads[$thread->thread_id] = strtotime( $thread->date_sent );
     106
     107                arsort( $sorted_threads );
    109108
    110109                $threads = false;
    … …  
    173172
    174173                $count = 0;
    175                 for ( $i = 0; $i < count($unread_counts); $i++ ) {
     174                for ( $i = 0; $i < count( $unread_counts ); $i++ ) {
    176175                        $count += $unread_counts[$i]->unread_count;
    177176                }
    … …  
    180179        }
    181180
    182         function check_access( $thread_id, $user_id = false ) {
    183                 global $wpdb, $bp;
    184 
    185                 if ( !$user_id )
     181        function check_access( $thread_id, $user_id = 0 ) {
     182                global $wpdb, $bp;
     183
     184                if ( empty( $user_id ) )
    186185                        $user_id = $bp->loggedin_user->id;
    187186
    … …  
    199198                        return count( $recipients ) . __(' Recipients', 'buddypress');
    200199
    201                 foreach ( (array)$recipients as $recipient ) {
     200                foreach ( (array)$recipients as $recipient )
    202201                        $recipient_links[] = bp_core_get_userlink( $recipient->user_id );
    203                 }
    204202
    205203                return implode( ', ', (array) $recipient_links );
    206204        }
    207205
    208         /* Update Functions */
     206        // Update Functions
    209207
    210208        function update_tables() {
    … …  
    214212                $threads = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->base_prefix}bp_messages_threads" ) );
    215213
    216                 /* Nothing to update, just return true to remove the table */
     214                // Nothing to update, just return true to remove the table
    217215                if ( empty( $threads ) )
    218216                        return true;
    … …  
    224222                                $message_ids = implode( ',', $message_ids );
    225223
    226                                 /* Add the thread_id to the messages table */
     224                                // Add the thread_id to the messages table
    227225                                if ( !$wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_messages} SET thread_id = %d WHERE id IN ({$message_ids})", $thread->id ) ) )
    228226                                        $errors = true;
    … …  
    253251                $this->sender_id = $bp->loggedin_user->id;
    254252
    255                 if ( $id ) {
    256                         $this->populate($id);
    257                 }
     253                if ( !empty( $id ) )
     254                        $this->populate( $id );
    258255        }
    259256
    … …  
    262259
    263260                if ( $message = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE id = %d", $id ) ) ) {
    264                         $this->id = $message->id;
     261                        $this->id        = $message->id;
    265262                        $this->thread_id = $message->thread_id;
    266263                        $this->sender_id = $message->sender_id;
    267                         $this->subject = $message->subject;
    268                         $this->message = $message->message;
     264                        $this->subject   = $message->subject;
     265                        $this->message   = $message->message;
    269266                        $this->date_sent = $message->date_sent;
    270267                }
    … …  
    276273                $this->sender_id = apply_filters( 'messages_message_sender_id_before_save', $this->sender_id, $this->id );
    277274                $this->thread_id = apply_filters( 'messages_message_thread_id_before_save', $this->thread_id, $this->id );
    278                 $this->subject = apply_filters( 'messages_message_subject_before_save', $this->subject, $this->id );
    279                 $this->message = apply_filters( 'messages_message_content_before_save', $this->message, $this->id );
     275                $this->subject   = apply_filters( 'messages_message_subject_before_save', $this->subject, $this->id );
     276                $this->message   = apply_filters( 'messages_message_content_before_save', $this->message, $this->id );
    280277                $this->date_sent = apply_filters( 'messages_message_date_sent_before_save', $this->date_sent, $this->id );
    281278
    282279                do_action( 'messages_message_before_save', $this );
    283280
    284                 /* Make sure we have at least one recipient before sending. */
     281                // Make sure we have at least one recipient before sending.
    285282                if ( empty( $this->recipients ) )
    286283                        return false;
    … …  
    288285                $new_thread = false;
    289286
    290                 /* If we have no thread_id then this is the first message of a new thread. */
     287                // If we have no thread_id then this is the first message of a new thread.
    291288                if ( empty( $this->thread_id ) ) {
    292289                        $this->thread_id = (int)$wpdb->get_var( $wpdb->prepare( "SELECT MAX(thread_id) FROM {$bp->messages->table_name_messages}" ) ) + 1;
    … …  
    299296
    300297                if ( $new_thread ) {
    301                         /* Add an recipient entry for all recipients */
     298                        // Add an recipient entry for all recipients
    302299                        foreach ( (array)$this->recipients as $recipient )
    303300                                $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_recipients} ( user_id, thread_id, unread_count ) VALUES ( %d, %d, 1 )", $recipient->user_id, $this->thread_id ) );
    304301
    305                         /* Add a sender recipient entry if the sender is not in the list of recipients */
     302                        // Add a sender recipient entry if the sender is not in the list of recipients
    306303                        if ( !in_array( $this->sender_id, $this->recipients ) )
    307304                                $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_recipients} ( user_id, thread_id, sender_only ) VALUES ( %d, %d, 1 )", $this->sender_id, $this->thread_id ) );
    308305                } else {
    309                         /* Update the unread count for all recipients */
     306                        // Update the unread count for all recipients
    310307                        $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = unread_count + 1, sender_only = 0, is_deleted = 0 WHERE thread_id = %d AND user_id != %d", $this->thread_id, $this->sender_id ) );
    311308                }
    … …  
    365362        var $is_active;
    366363
    367         function bp_messages_notice($id = null) {
     364        function bp_messages_notice( $id = null ) {
    368365                if ( $id ) {
    369366                        $this->id = $id;
    … …  
    378375
    379376                if ( $notice ) {
    380                         $this->subject = $notice->subject;
    381                         $this->message = $notice->message;
     377                        $this->subject   = $notice->subject;
     378                        $this->message   = $notice->message;
    382379                        $this->date_sent = $notice->date_sent;
    383380                        $this->is_active = $notice->is_active;
    … …  
    393390                do_action( 'messages_notice_before_save', $this );
    394391
    395                 if ( !$this->id ) {
     392                if ( empty( $this->id ) )
    396393                        $sql = $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_notices} (subject, message, date_sent, is_active) VALUES (%s, %s, %s, %d)", $this->subject, $this->message, $this->date_sent, $this->is_active );
    397                 } else {
     394                else
    398395                        $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_notices} SET subject = %s, message = %s, is_active = %d WHERE id = %d", $this->subject, $this->message, $this->is_active, $this->id );
    399                 }
    400 
    401                 if ( !$wpdb->query($sql) )
     396
     397                if ( !$wpdb->query( $sql ) )
    402398                        return false;
    403399
    … …  
    436432                $sql = $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_notices} WHERE id = %d", $this->id );
    437433
    438                 if ( !$wpdb->query($sql) )
     434                if ( !$wpdb->query( $sql ) )
    439435                        return false;
    440436
    … …  
    463459
    464460                $notice_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_notices} WHERE is_active = 1") );
    465                 return new BP_Messages_Notice($notice_id);
     461                return new BP_Messages_Notice( $notice_id );
    466462        }
    467463}
  • trunk/bp-messages/bp-messages-functions.php

    r3757 r3760  
    2323        extract( $r, EXTR_SKIP );
    2424
    25         if ( !$sender_id || !$content )
     25        if ( empty( $sender_id ) || empty( $content ) )
    2626                return false;
    2727
    … …  
    3535
    3636        // If we have a thread ID, use the existing recipients, otherwise use the recipients passed
    37         if ( $thread_id ) {
     37        if ( !empty( $thread_id ) ) {
    3838                $thread = new BP_Messages_Thread( $thread_id );
    3939                $message->recipients = $thread->get_recipients();
    … …  
    7575                // Format this to match existing recipients
    7676                foreach( (array)$recipient_ids as $i => $recipient_id ) {
    77                         $message->recipients[$i] = new stdClass;
     77                        $message->recipients[$i]          = new stdClass;
    7878                        $message->recipients[$i]->user_id = $recipient_id;
    7979                }
    … …  
    127127                }
    128128
    129                 if ( $error )
     129                if ( !empty( $error ) )
    130130                        return false;
    131131
    … …  
    146146        global $bp;
    147147
    148         if ( !$user_id )
     148        if ( empty( $user_id ) )
    149149                $user_id = $bp->loggedin_user->id;
    150150
    … …  
    161161
    162162function messages_add_callback_values( $recipients, $subject, $content ) {
    163         setcookie( 'bp_messages_send_to', $recipients, time()+60*60*24, COOKIEPATH );
    164         setcookie( 'bp_messages_subject', $subject, time()+60*60*24, COOKIEPATH );
    165         setcookie( 'bp_messages_content', $content, time()+60*60*24, COOKIEPATH );
     163        @setcookie( 'bp_messages_send_to', $recipients, time() + 60 * 60 * 24, COOKIEPATH );
     164        @setcookie( 'bp_messages_subject', $subject,    time() + 60 * 60 * 24, COOKIEPATH );
     165        @setcookie( 'bp_messages_content', $content,    time() + 60 * 60 * 24, COOKIEPATH );
    166166}
    167167
    168168function messages_remove_callback_values() {
    169         setcookie( 'bp_messages_send_to', false, time()-1000, COOKIEPATH );
    170         setcookie( 'bp_messages_subject', false, time()-1000, COOKIEPATH );
    171         setcookie( 'bp_messages_content', false, time()-1000, COOKIEPATH );
     169        @setcookie( 'bp_messages_send_to', false, time() - 1000, COOKIEPATH );
     170        @setcookie( 'bp_messages_subject', false, time() - 1000, COOKIEPATH );
     171        @setcookie( 'bp_messages_content', false, time() - 1000, COOKIEPATH );
    172172}
    173173
    … …  
    175175        global $bp;
    176176
    177         if ( !$user_id )
     177        if ( empty( $user_id ) )
    178178                $user_id = $bp->loggedin_user->id;
    179179
Note: See TracChangeset for help on using the changeset viewer.