Skip to:
Content

BuddyPress.org

Changeset 1651


Ignore:
Timestamp:
08/11/2009 10:59:21 PM (17 years ago)
Author:
apeatling
Message:

Re-factored message sending code.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-messages.php

    r1636 r1651  
    5454                message longtext NOT NULL,
    5555                date_sent datetime NOT NULL,
    56                 message_order int(10) NOT NULL,
    57                 sender_is_group tinyint(1) NOT NULL DEFAULT '0',
    58                 KEY sender_id (sender_id),
    59                 KEY message_order (message_order),
    60                 KEY sender_is_group (sender_is_group)
     56                KEY sender_id (sender_id)
    6157               ) {$charset_collate};";
    6258   
     
    161157
    162158function messages_screen_compose() {
     159    global $bp;
     160   
    163161    // Remove any saved message data from a previous session.
    164162    messages_remove_callback_values();
    165163
    166     // Require the auto
    167     $recipients = false;
    168     if ( empty( $_POST['send_to_usernames'] ) ) {
    169         if ( !empty( $_POST['send-to-input'] ) ) {
    170             // Replace commas with places
    171             $recipients = str_replace( ',', ' ', $_POST['send-to-input'] );
    172             $recipients = str_replace( '  ', ' ', $recipients );
    173         }
    174     } else {
    175         $recipients = $_POST['send_to_usernames'];
    176     }
    177    
    178     if ( $recipients || ( isset($_POST['send-notice']) && is_site_admin() ) ) {
    179         if ( !check_admin_referer( 'messages_send_message' ) )
    180             return false;
     164    /* Check if the message form has been submitted */
     165    if ( isset( $_POST['send'] ) ) {
     166       
     167        /* Check the nonce */
     168        check_admin_referer( 'messages_send_message' );
     169       
     170        /* Check we have what we need */
     171        if ( empty( $_POST['subject'] ) || empty( $_POST['content'] ) ) {
     172            bp_core_add_message( __( 'There was an error sending that message, please try again', 'buddypress' ), 'error' );
     173        } else {
     174            /* If this is a notice, send it */
     175            if ( isset($_POST['send-notice']) ) {
     176                messages_send_notice( $_POST['subject'], $_POST['content'] );
     177            } else {
     178                /* Filter recipients into the format we need - array( 'username/userid', 'username/userid' ) */
     179                if ( empty( $_POST['send_to_usernames'] ) ) {
     180                    if ( !empty( $_POST['send-to-input'] ) )
     181                        $recipients = explode( ',', $_POST['send-to-input'] );
     182                } else {
     183                    $recipients = explode( ' ', $_POST['send_to_usernames'] );
     184                }
    181185           
    182         messages_send_message( $recipients, $_POST['subject'], $_POST['content'], $_POST['thread_id'], false, true );
     186                /* Send the message */
     187                if ( $thread_id = messages_new_message( array( 'recipients' => $recipients, 'subject' => $_POST['subject'], 'content' => $_POST['content'] ) ) ) {
     188                    bp_core_add_message( __( 'Message sent successfully!', 'buddypress' ) );
     189                    bp_core_redirect( $bp->loggedin_user->domain . $bp->messages->slug . '/view/' . $thread_id . '/' );
     190                } else {
     191                    bp_core_add_message( __( 'There was an error sending that message, please try again', 'buddypress' ), 'error' );
     192                }
     193            }
     194        }
     195
    183196    }
    184197   
     
    270283       
    271284    $thread_id = $bp->action_variables[0];
    272 
    273     if ( !$thread_id || !is_numeric($thread_id) || !BP_Messages_Thread::check_access($thread_id) ) {
     285   
     286    if ( !$thread_id || ( !messages_check_thread_access($thread_id) && !is_site_admin() ) )
    274287        bp_core_redirect( $bp->displayed_user->domain . $bp->current_component );
    275     } else {
    276 
    277         bp_core_new_subnav_item( array( 'name' => sprintf( __( 'From: %s', 'buddypress'), BP_Messages_Thread::get_last_sender($thread_id) ), 'slug' => 'view', 'parent_url' => $bp->loggedin_user->domain . $bp->messages->slug . '/', 'parent_slug' => $bp->messages->slug, 'screen_function' => true, 'position' => 40, 'user_has_access' => bp_is_home() ) );
    278 
    279         bp_core_load_template( apply_filters( 'messages_template_view_message', 'messages/view' ) );
    280     }
     288       
     289    /* Check if a new reply has been submitted */
     290    if ( isset( $_POST['send'] ) ) {
     291       
     292        /* Check the nonce */
     293        check_admin_referer( 'messages_send_message' );
     294       
     295        /* Send the reply */
     296        if ( messages_new_message( array( 'thread_id' => $thread_id, 'subject' => $_POST['subject'], 'content' => $_POST['content'] ) ) )
     297            bp_core_add_message( __( 'Your reply was sent successfully', 'buddypress' ) );
     298        else
     299            bp_core_add_message( __( 'There was a problem sending your reply, please try again', 'buddypress' ), 'error' );
     300       
     301        bp_core_redirect( $bp->displayed_user->domain . $bp->current_component . '/view/' . $thread_id . '/' );     
     302    }
     303   
     304    bp_core_new_subnav_item( array( 'name' => sprintf( __( 'From: %s', 'buddypress'), BP_Messages_Thread::get_last_sender($thread_id) ), 'slug' => 'view', 'parent_url' => $bp->loggedin_user->domain . $bp->messages->slug . '/', 'parent_slug' => $bp->messages->slug, 'screen_function' => true, 'position' => 40, 'user_has_access' => bp_is_home() ) );
     305    bp_core_load_template( apply_filters( 'messages_template_view_message', 'messages/view' ) );
    281306}
    282307add_action( 'wp', 'messages_action_view_message', 3 );
     
    290315    $thread_id = $bp->action_variables[1];
    291316
    292     if ( !$thread_id || !is_numeric($thread_id) || !BP_Messages_Thread::check_access($thread_id) ) {
     317    if ( !$thread_id || !is_numeric($thread_id) || !messages_check_thread_access($thread_id) ) {
    293318        bp_core_redirect( $bp->displayed_user->domain . $bp->current_component . '/' . $bp->current_action );
    294319    } else {
     
    315340    $thread_ids = $_POST['thread_ids'];
    316341
    317     if ( !$thread_ids || !BP_Messages_Thread::check_access($thread_ids) ) {
     342    if ( !$thread_ids || !messages_check_thread_access($thread_ids) ) {
    318343        bp_core_redirect( $bp->displayed_user->domain . $bp->current_component . '/' . $bp->current_action );           
    319344    } else {
     
    378403 */
    379404
    380 function messages_send_message( $recipients, $subject, $content, $thread_id, $from_ajax = false, $from_template = false, $is_reply = false ) {
    381     global $pmessage;
    382     global $message, $type;
    383     global $bp, $current_user;
    384        
    385     messages_add_callback_values( $recipients, $subject, $content );
    386    
    387     if ( isset( $_POST['send-notice'] ) ) {
    388         if ( messages_send_notice( $subject, $content, $from_template ) ) {
    389             bp_core_add_message( __('Notice posted successfully.', 'buddypress') );
    390         } else {
    391             bp_core_add_message( __('There was an error posting that notice.', 'buddypress'), 'error' );           
    392         }
    393         bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/notices' );
    394         return true;
    395     }
    396    
    397     $recipients = explode( ' ', $recipients );
    398    
    399     // If there are no recipients
    400     if ( count( $recipients ) < 1 ) {
    401         if ( !$from_ajax ) {   
    402             bp_core_add_message( __('Please enter at least one valid user to send this message to.', 'buddypress'), 'error' );
    403             bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/compose' );
    404         } else {
    405             return array('status' => 0, 'message' => __('There was an error sending the reply, please try again.', 'buddypress'));
    406         }
    407        
    408     // If there is only 1 recipient and it is the logged in user.
    409     } else if ( 1 == count( $recipients ) && $recipients[0] == $current_user->user_login ) {
    410         bp_core_add_message( __('You must send your message to one or more users not including yourself.', 'buddypress'), 'error' );
    411         bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/compose' );   
    412    
    413     // If the subject or content boxes are empty.
    414     } else if ( empty( $subject ) || empty( $content ) ) {
    415         if ( !$from_ajax ) {
    416             bp_core_add_message( __('Please make sure you fill in all the fields.', 'buddypress'), 'error' );
    417             bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/compose' );
    418         } else {
    419             return array('status' => 0, 'message' => __('Please make sure you have typed a message before sending a reply.', 'buddypress'));
    420         }
    421        
    422     // Passed validation continue.
     405function messages_new_message( $args = '' ) {
     406    global $bp;
     407   
     408    $defaults = array(
     409        'thread_id' => false, // false for a new message, thread id for a reply to a thread.
     410        'sender_id' => $bp->loggedin_user->id,
     411        'recipients' => false, // Can be an array of usernames, user_ids or mixed.
     412        'subject' => false,
     413        'content' => false,
     414        'date_sent' => time()
     415    );
     416
     417    $r = wp_parse_args( $args, $defaults );
     418    extract( $r, EXTR_SKIP );   
     419   
     420    if ( !$sender_id || !$subject || !$content )
     421        return false;
     422       
     423    /* Create a new message object */   
     424    $message = new BP_Messages_Message;
     425    $message->thread_id = $thread_id;
     426    $message->sender_id = $sender_id;
     427    $message->subject = $subject;
     428    $message->message = $content;
     429    $message->date_sent = $date_sent;
     430
     431    /* If we have a thread ID, use the existing recipients, otherwise use the recipients passed */
     432    if ( $thread_id ) {
     433        $thread = new BP_Messages_Thread($thread_id);
     434        $message->recipients = $thread->get_recipients();       
    423435    } else {
    424 
    425         // Strip the logged in user from the recipient list if they exist
    426         if ( $key = array_search( $current_user->user_login, $recipients ) )
    427             unset( $recipients[$key] );
    428        
    429         $pmessage = new BP_Messages_Message;
    430 
    431         $pmessage->sender_id = $bp->loggedin_user->id;
    432         $pmessage->subject = $subject;
    433         $pmessage->message = $content;
    434         $pmessage->thread_id = $thread_id;
    435         $pmessage->date_sent = time();
    436         $pmessage->message_order = 0; // TODO
    437         $pmessage->sender_is_group = 0;
    438        
    439         if ( $is_reply ) {
    440             $thread = new BP_Messages_Thread($thread_id);
    441             $pmessage->recipients = $thread->get_recipients();
    442         } else {
    443             $pmessage->recipients = BP_Messages_Message::get_recipient_ids( $recipients );
    444         }
    445 
    446         if ( !is_null( $pmessage->recipients ) ) {
    447             if ( !$pmessage->send() ) {
    448                 $message = __('Message could not be sent, please try again.', 'buddypress');
    449                 $type = 'error';
    450        
    451                 if ( $from_ajax ) {
    452                     return array('status' => 0, 'message' => $message);
    453                 } else {
    454                     bp_core_add_message( $message, $type );
    455                     bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/compose' );
    456                 }
    457             } else {
    458                 $message = __('Message sent successfully!', 'buddypress');
    459                 $type = 'success';
    460                
    461                 // Send screen notifications to the recipients
    462                 for ( $i = 0; $i < count($pmessage->recipients); $i++ ) {
    463                     if ( $pmessage->recipients[$i] != $bp->loggedin_user->id ) {
    464                         bp_core_add_notification( $pmessage->id, $pmessage->recipients[$i], 'messages', 'new_message' );   
    465                     }
    466                 }
    467                
    468                 // Send email notifications to the recipients
    469                 require_once( BP_PLUGIN_DIR . '/bp-messages/bp-messages-notifications.php' );
    470                 messages_notification_new_message( array( 'item_id' => $pmessage->id, 'recipient_ids' => $pmessage->recipients, 'thread_id' => $pmessage->thread_id, 'component_name' => $bp->messages->slug, 'component_action' => 'message_sent', 'is_private' => 1 ) );
    471 
    472                 do_action( 'messages_send_message', array( 'item_id' => $pmessage->id, 'recipient_ids' => $pmessage->recipients, 'thread_id' => $pmessage->thread_id, 'component_name' => $bp->messages->slug, 'component_action' => 'message_sent', 'is_private' => 1 ) );
    473        
    474                 if ( $from_ajax ) {
    475                     return array('status' => 1, 'message' => $message, 'reply' => $pmessage);
    476                 } else {
    477                     bp_core_add_message( $message );
    478                     bp_core_redirect( $bp->loggedin_user->domain . $bp->messages->slug . '/view/' . $pmessage->thread_id );
    479                 }
    480             }
    481         } else {
    482             $message = __('Message could not be sent, please try again.', 'buddypress');
    483             $type = 'error';
    484        
    485             if ( $from_ajax ) {
    486                 return array('status' => 0, 'message' => $message);
    487             } else {
    488                 bp_core_add_message( $message, $type );
    489                 bp_core_redirect( $bp->loggedin_user->domain . $bp->messages->slug . '/compose' );
    490             }
    491         }
    492     }
    493 
    494 }
    495 
    496 function messages_add_callback_values( $recipients, $subject, $content ) {
    497     setcookie( 'bp_messages_send_to', $recipients, time()+60*60*24, COOKIEPATH );
    498     setcookie( 'bp_messages_subject', $subject, time()+60*60*24, COOKIEPATH );
    499     setcookie( 'bp_messages_content', $content, time()+60*60*24, COOKIEPATH );
    500 }
    501 
    502 function messages_remove_callback_values() {
    503     setcookie( 'bp_messages_send_to', false, time()-1000, COOKIEPATH );
    504     setcookie( 'bp_messages_subject', false, time()-1000, COOKIEPATH );
    505     setcookie( 'bp_messages_content', false, time()-1000, COOKIEPATH );
    506 }
    507 
    508 function messages_send_notice( $subject, $message, $from_template ) {
     436        if ( empty( $recipients ) )
     437            return false;
     438       
     439        /* Loop the recipients and convert all usernames to user_ids where needed */
     440        foreach( (array) $recipients as $recipient ) {
     441            if ( is_numeric( trim( $recipient ) ) )
     442                $recipient_ids[] = (int)trim( $recipient );
     443           
     444            if ( $recipient_id = bp_core_get_userid( trim( $recipient ) ) )
     445                $recipient_ids[] = (int)$recipient_id;
     446        }
     447
     448        /* Strip the sender from the recipient list if they exist */
     449        if ( $key = array_search( $sender_id, $recipient_ids ) )
     450            unset( $recipient_ids[$key] ); 
     451
     452        $message->recipients = $recipient_ids;
     453    }
     454   
     455    return $message->send();
     456}
     457
     458
     459function messages_send_notice( $subject, $message ) {
    509460    if ( !is_site_admin() || empty( $subject ) || empty( $message ) ) {
    510461        return false;
     
    548499}
    549500
     501function messages_check_thread_access( $thread_id, $user_id = false ) {
     502    global $bp;
     503   
     504    if ( !$user_id )
     505        $user_id = $bp->loggedin_user->id;
     506   
     507    return BP_Messages_Thread::check_access( $thread_id, $user_id );
     508}
     509
     510function messages_add_callback_values( $recipients, $subject, $content ) {
     511    setcookie( 'bp_messages_send_to', $recipients, time()+60*60*24, COOKIEPATH );
     512    setcookie( 'bp_messages_subject', $subject, time()+60*60*24, COOKIEPATH );
     513    setcookie( 'bp_messages_content', $content, time()+60*60*24, COOKIEPATH );
     514}
     515
     516function messages_remove_callback_values() {
     517    setcookie( 'bp_messages_send_to', false, time()-1000, COOKIEPATH );
     518    setcookie( 'bp_messages_subject', false, time()-1000, COOKIEPATH );
     519    setcookie( 'bp_messages_content', false, time()-1000, COOKIEPATH );
     520}
     521
    550522function messages_get_unread_count( $user_id = false ) {
    551523    global $bp;
  • trunk/bp-messages/bp-messages-classes.php

    r1636 r1651  
    278278    }
    279279   
    280     function check_access($id) {
    281         global $wpdb, $bp;
    282        
    283         $sql = $wpdb->prepare("SELECT id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND user_id = %d", $id, $bp->loggedin_user->id );
    284         $has_access = $wpdb->get_var($sql);
    285 
    286         if ( $has_access )
    287             return true;
    288 
    289         return false;
     280    function check_access( $thread_id, $user_id = false ) {
     281        global $wpdb, $bp;
     282       
     283        if ( !$user_id )
     284            $user_id = $bp->loggedin_user->id;
     285       
     286        return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND user_id = %d", $thread_id, $user_id ) );
    290287    }
    291288   
     
    308305    var $message;
    309306    var $date_sent;
    310     var $message_order;
    311     var $sender_is_group;
    312307   
    313308    var $thread_id;
     
    336331            $this->message = $message->message;
    337332            $this->date_sent = $message->date_sent;
    338             $this->message_order = $message->message_order;
    339             $this->sender_is_group = $message->sender_is_group;
    340333        }
    341334
     
    349342        $this->message = apply_filters( 'messages_message_content_before_save', $this->message, $this->id );
    350343        $this->date_sent = apply_filters( 'messages_message_date_sent_before_save', $this->date_sent, $this->id );
    351         $this->message_order = apply_filters( 'messages_message_order_before_save', $this->message_order, $this->id );
    352         $this->sender_is_group = apply_filters( 'messages_message_sender_is_group_before_save', $this->sender_is_group, $this->id );
    353344
    354345        do_action( 'messages_message_before_save', $this );
    355346       
    356347        // First insert the message into the messages table
    357         if ( !$wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_messages} ( sender_id, subject, message, date_sent, message_order, sender_is_group ) VALUES ( %d, %s, %s, FROM_UNIXTIME(%d), %d, %d )", $this->sender_id, $this->subject, $this->message, $this->date_sent, $this->message_order, $this->sender_is_group ) ) )
     348        if ( !$wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_messages} ( sender_id, subject, message, date_sent ) VALUES ( %d, %s, %s, FROM_UNIXTIME(%d) )", $this->sender_id, $this->subject, $this->message, $this->date_sent ) ) )
    358349            return false;
    359350           
     
    418409        do_action( 'messages_message_after_save', $this );
    419410       
    420         return true;
     411        return $this->thread_id;
    421412    }
    422413   
  • trunk/bp-messages/bp-messages-cssjs.php

    r1636 r1651  
    55   
    66    // Include the autocomplete JS for composing a message.
    7     if ( $bp->current_component == $bp->messages->slug && $bp->current_action == 'compose') {
     7    if ( $bp->current_component == $bp->messages->slug && 'compose' == $bp->current_action ) {
    88        add_action( 'wp_head', 'messages_autocomplete_init_jsblock' );
    99       
    10         wp_enqueue_script( 'bp-jquery-autocomplete', BP_PLUGIN_URL . '/bp-messages/js/autocomplete/jquery.autocomplete.js', 'jquery' );
    11         wp_enqueue_script( 'bp-jquery-autocomplete-fb', BP_PLUGIN_URL . '/bp-messages/js/autocomplete/jquery.autocompletefb.js', 'jquery' );
    12         wp_enqueue_script( 'bp-jquery-bgiframe', BP_PLUGIN_URL . '/bp-messages/js/autocomplete/jquery.bgiframe.min.js', 'jquery' );
    13         wp_enqueue_script( 'bp-jquery-dimensions', BP_PLUGIN_URL . '/bp-messages/js/autocomplete/jquery.dimensions.js', 'jquery' );
     10        wp_enqueue_script( 'bp-jquery-autocomplete', BP_PLUGIN_URL . '/bp-messages/js/autocomplete/jquery.autocomplete.js', array( 'jquery' ) );
     11        wp_enqueue_script( 'bp-jquery-autocomplete-fb', BP_PLUGIN_URL . '/bp-messages/js/autocomplete/jquery.autocompletefb.js' );
     12        wp_enqueue_script( 'bp-jquery-bgiframe', BP_PLUGIN_URL . '/bp-messages/js/autocomplete/jquery.bgiframe.min.js' );
     13        wp_enqueue_script( 'bp-jquery-dimensions', BP_PLUGIN_URL . '/bp-messages/js/autocomplete/jquery.dimensions.js' );   
    1414    }
    1515
     
    2020    global $bp;
    2121
    22     if ( $bp->current_component == $bp->messages->slug && $bp->current_action == 'compose') {
    23         wp_enqueue_style( 'bp-messages-autocomplete', BP_PLUGIN_URL . '/bp-messages/deprecated/css/autocomplete/jquery.autocompletefb.css' );   
     22    if ( $bp->current_component == $bp->messages->slug && 'compose' == $bp->current_action ) {
     23        wp_enqueue_style( 'bp-messages-autocomplete', BP_PLUGIN_URL . '/bp-messages/css/autocomplete/jquery.autocompletefb.css' ); 
    2424        wp_print_styles();
    2525    }
  • trunk/bp-messages/bp-messages-templatetags.php

    r1636 r1651  
    159159    function bp_get_message_thread_id() {
    160160        global $messages_template;
     161
    161162        return apply_filters( 'bp_get_message_thread_id', $messages_template->thread->thread_id );
    162163    }
     
    284285        global $bp;
    285286
    286         return apply_filters( 'bp_get_messages_form_action', $bp->loggedin_user->domain . $bp->messages->slug . '/' . $bp->current_action );
     287        return apply_filters( 'bp_get_messages_form_action', $bp->loggedin_user->domain . $bp->messages->slug . '/' . $bp->current_action . '/' . $bp->action_variables[0] . '/' );
    287288    }
    288289
     
    582583}
    583584
     585function bp_the_thread_id() {
     586    echo bp_get_the_thread_id();
     587}
     588    function bp_get_the_thread_id() {
     589        global $thread_template;
     590       
     591        return apply_filters( 'bp_get_the_thread_id', $thread_template->thread->thread_id );
     592    }
     593
    584594function bp_the_thread_subject() {
    585595    echo bp_get_the_thread_subject();
     
    587597    function bp_get_the_thread_subject() {
    588598        global $thread_template;
    589        
    590         return apply_filters( 'bp_get_the_thread_subject', $thread_template->thread->subject );
     599
     600        return apply_filters( 'bp_get_the_thread_subject', $thread_template->thread->last_message_subject );
    591601    }
    592602
  • trunk/bp-messages/css/autocomplete/jquery.autocompletefb.css

    r1366 r1651  
    5353    height  : 1%;
    5454    overflow: hidden;
     55    padding: 0;
     56    list-style: none;
    5557}
    5658    ul.acfb-holder li {
  • trunk/bp-messages/deprecated/bp-messages-deprecated.php

    r1636 r1651  
    162162add_action( 'wp_ajax_messages_close_notice', 'messages_ajax_close_notice' );
    163163
     164/* Deprecated -- use messages_new_message() */
     165function messages_send_message( $recipients, $subject, $content, $thread_id, $from_ajax = false, $from_template = false, $is_reply = false ) {
     166    global $pmessage;
     167    global $message, $type;
     168    global $bp, $current_user;
     169       
     170    messages_add_callback_values( $recipients, $subject, $content );
     171   
     172    if ( isset( $_POST['send-notice'] ) ) {
     173        if ( messages_send_notice( $subject, $content, $from_template ) ) {
     174            bp_core_add_message( __('Notice posted successfully.', 'buddypress') );
     175        } else {
     176            bp_core_add_message( __('There was an error posting that notice.', 'buddypress'), 'error' );           
     177        }
     178        bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/notices' );
     179        return true;
     180    }
     181   
     182    $recipients = explode( ' ', $recipients );
     183   
     184    // If there are no recipients
     185    if ( count( $recipients ) < 1 ) {
     186        if ( !$from_ajax ) {   
     187            bp_core_add_message( __('Please enter at least one valid user to send this message to.', 'buddypress'), 'error' );
     188            bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/compose' );
     189        } else {
     190            return array('status' => 0, 'message' => __('There was an error sending the reply, please try again.', 'buddypress'));
     191        }
     192       
     193    // If there is only 1 recipient and it is the logged in user.
     194    } else if ( 1 == count( $recipients ) && $recipients[0] == $current_user->user_login ) {
     195        bp_core_add_message( __('You must send your message to one or more users not including yourself.', 'buddypress'), 'error' );
     196        bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/compose' );   
     197   
     198    // If the subject or content boxes are empty.
     199    } else if ( empty( $subject ) || empty( $content ) ) {
     200        if ( !$from_ajax ) {
     201            bp_core_add_message( __('Please make sure you fill in all the fields.', 'buddypress'), 'error' );
     202            bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/compose' );
     203        } else {
     204            return array('status' => 0, 'message' => __('Please make sure you have typed a message before sending a reply.', 'buddypress'));
     205        }
     206       
     207    // Passed validation continue.
     208    } else {
     209
     210        // Strip the logged in user from the recipient list if they exist
     211        if ( $key = array_search( $current_user->user_login, $recipients ) )
     212            unset( $recipients[$key] );
     213       
     214        $pmessage = new BP_Messages_Message;
     215
     216        $pmessage->sender_id = $bp->loggedin_user->id;
     217        $pmessage->subject = $subject;
     218        $pmessage->message = $content;
     219        $pmessage->thread_id = $thread_id;
     220        $pmessage->date_sent = time();
     221       
     222        if ( $is_reply ) {
     223            $thread = new BP_Messages_Thread($thread_id);
     224            $pmessage->recipients = $thread->get_recipients();
     225        } else {
     226            $pmessage->recipients = BP_Messages_Message::get_recipient_ids( $recipients );
     227        }
     228
     229        if ( !is_null( $pmessage->recipients ) ) {
     230            if ( !$pmessage->send() ) {
     231                $message = __('Message could not be sent, please try again.', 'buddypress');
     232                $type = 'error';
     233       
     234                if ( $from_ajax ) {
     235                    return array('status' => 0, 'message' => $message);
     236                } else {
     237                    bp_core_add_message( $message, $type );
     238                    bp_core_redirect( $bp->loggedin_user->domain . $bp->current_component . '/compose' );
     239                }
     240            } else {
     241                $message = __('Message sent successfully!', 'buddypress');
     242                $type = 'success';
     243               
     244                // Send screen notifications to the recipients
     245                for ( $i = 0; $i < count($pmessage->recipients); $i++ ) {
     246                    if ( $pmessage->recipients[$i] != $bp->loggedin_user->id ) {
     247                        bp_core_add_notification( $pmessage->id, $pmessage->recipients[$i], 'messages', 'new_message' );   
     248                    }
     249                }
     250               
     251                // Send email notifications to the recipients
     252                require_once( BP_PLUGIN_DIR . '/bp-messages/bp-messages-notifications.php' );
     253                messages_notification_new_message( array( 'item_id' => $pmessage->id, 'recipient_ids' => $pmessage->recipients, 'thread_id' => $pmessage->thread_id, 'component_name' => $bp->messages->slug, 'component_action' => 'message_sent', 'is_private' => 1 ) );
     254
     255                do_action( 'messages_send_message', array( 'item_id' => $pmessage->id, 'recipient_ids' => $pmessage->recipients, 'thread_id' => $pmessage->thread_id, 'component_name' => $bp->messages->slug, 'component_action' => 'message_sent', 'is_private' => 1 ) );
     256       
     257                if ( $from_ajax ) {
     258                    return array('status' => 1, 'message' => $message, 'reply' => $pmessage);
     259                } else {
     260                    bp_core_add_message( $message );
     261                    bp_core_redirect( $bp->loggedin_user->domain . $bp->messages->slug . '/view/' . $pmessage->thread_id );
     262                }
     263            }
     264        } else {
     265            $message = __('Message could not be sent, please try again.', 'buddypress');
     266            $type = 'error';
     267       
     268            if ( $from_ajax ) {
     269                return array('status' => 0, 'message' => $message);
     270            } else {
     271                bp_core_add_message( $message, $type );
     272                bp_core_redirect( $bp->loggedin_user->domain . $bp->messages->slug . '/compose' );
     273            }
     274        }
     275    }
     276
     277}
     278
    164279?>
Note: See TracChangeset for help on using the changeset viewer.