function messages_screen_compose()
{
    global $bp;
    if (bp_action_variables()) {
        bp_do_404();
        return;
    }
    // Remove any saved message data from a previous session.
    messages_remove_callback_values();
    // Check if the message form has been submitted
    if (isset($_POST['send'])) {
        // Check the nonce
        check_admin_referer('messages_send_message');
        // Check we have what we need
        if (empty($_POST['subject']) || empty($_POST['content'])) {
            bp_core_add_message(__('There was an error sending that message, please try again', 'buddypress'), 'error');
        } else {
            // If this is a notice, send it
            if (isset($_POST['send-notice'])) {
                if (messages_send_notice($_POST['subject'], $_POST['content'])) {
                    bp_core_add_message(__('Notice sent successfully!', 'buddypress'));
                    bp_core_redirect($bp->loggedin_user->domain . $bp->messages->slug . '/inbox/');
                } else {
                    bp_core_add_message(__('There was an error sending that notice, please try again', 'buddypress'), 'error');
                }
            } else {
                // Filter recipients into the format we need - array( 'username/userid', 'username/userid' )
                $autocomplete_recipients = explode(',', $_POST['send-to-input']);
                $typed_recipients = explode(' ', $_POST['send_to_usernames']);
                $recipients = array_merge((array) $autocomplete_recipients, (array) $typed_recipients);
                $recipients = apply_filters('bp_messages_recipients', $recipients);
                // Send the message
                if ($thread_id = messages_new_message(array('recipients' => $recipients, 'subject' => $_POST['subject'], 'content' => $_POST['content']))) {
                    bp_core_add_message(__('Message sent successfully!', 'buddypress'));
                    bp_core_redirect($bp->loggedin_user->domain . $bp->messages->slug . '/view/' . $thread_id . '/');
                } else {
                    bp_core_add_message(__('There was an error sending that message, please try again', 'buddypress'), 'error');
                }
            }
        }
    }
    do_action('messages_screen_compose');
    bp_core_load_template(apply_filters('messages_template_compose', 'members/single/home'));
}
 /**
  * @group cache
  */
 public function test_get_active_notices()
 {
     // send notice
     $subject = 'Test notice';
     $message = 'This is a notice';
     messages_send_notice($subject, $message);
     // now get the active notice and assert
     $notice = BP_Messages_Notice::get_active();
     $this->assertEquals($subject, $notice->subject);
     $this->assertEquals($message, $notice->message);
     // deactivate notice and make sure cache is invalidated
     $notice->deactivate();
     $this->assertFalse(wp_cache_get('active_notice', 'bp_messages'));
     // create a new notice
     $subject2 = 'Another notice';
     $message2 = 'Say what?';
     messages_send_notice($subject2, $message2);
     // now get the new active notice
     BP_Messages_Notice::get_active();
     // grab the cache and make sure it equals our new notice
     $cache = wp_cache_get('active_notice', 'bp_messages');
     $this->assertEquals($subject2, $cache->subject);
     $this->assertEquals($message2, $cache->message);
 }
function messages_send_message($recipients, $subject, $content, $thread_id, $from_ajax = false, $from_template = false, $is_reply = false)
{
    global $pmessage;
    global $message, $type;
    global $bp, $current_user;
    if (!check_admin_referer('messages_send_message')) {
        return false;
    }
    messages_add_callback_values($recipients, $subject, $content);
    if (isset($_POST['send-notice'])) {
        if (messages_send_notice($subject, $content, $from_template)) {
            bp_core_add_message(__('Notice posted successfully.', 'buddypress'));
        } else {
            bp_core_add_message(__('There was an error posting that notice.', 'buddypress'), 'error');
        }
        bp_core_redirect($bp->loggedin_user->domain . $bp->current_component . '/notices');
        return true;
    }
    $recipients = explode(' ', $recipients);
    // If there are no recipients
    if (count($recipients) < 1) {
        if (!$from_ajax) {
            bp_core_add_message(__('Please enter at least one valid user to send this message to.', 'buddypress'), 'error');
            bp_core_redirect($bp->loggedin_user->domain . $bp->current_component . '/compose');
        } else {
            return array('status' => 0, 'message' => __('There was an error sending the reply, please try again.', 'buddypress'));
        }
        // If there is only 1 recipient and it is the logged in user.
    } else {
        if (1 == count($recipients) && $recipients[0] == $current_user->user_login) {
            bp_core_add_message(__('You must send your message to one or more users not including yourself.', 'buddypress'), 'error');
            bp_core_redirect($bp->loggedin_user->domain . $bp->current_component . '/compose');
            // If the subject or content boxes are empty.
        } else {
            if (empty($subject) || empty($content)) {
                if (!$from_ajax) {
                    bp_core_add_message(__('Please make sure you fill in all the fields.', 'buddypress'), 'error');
                    bp_core_redirect($bp->loggedin_user->domain . $bp->current_component . '/compose');
                } else {
                    return array('status' => 0, 'message' => __('Please make sure you have typed a message before sending a reply.', 'buddypress'));
                }
                // Passed validation continue.
            } else {
                // Strip the logged in user from the recipient list if they exist
                if ($key = array_search($current_user->user_login, $recipients)) {
                    unset($recipients[$key]);
                }
                $pmessage = new BP_Messages_Message();
                $pmessage->sender_id = $bp->loggedin_user->id;
                $pmessage->subject = $subject;
                $pmessage->message = $content;
                $pmessage->thread_id = $thread_id;
                $pmessage->date_sent = time();
                $pmessage->message_order = 0;
                // TODO
                $pmessage->sender_is_group = 0;
                if ($is_reply) {
                    $thread = new BP_Messages_Thread($thread_id);
                    $pmessage->recipients = $thread->get_recipients();
                } else {
                    $pmessage->recipients = BP_Messages_Message::get_recipient_ids($recipients);
                }
                if (!is_null($pmessage->recipients)) {
                    if (!$pmessage->send()) {
                        $message = __('Message could not be sent, please try again.', 'buddypress');
                        $type = 'error';
                        if ($from_ajax) {
                            return array('status' => 0, 'message' => $message);
                        } else {
                            bp_core_add_message($message, $type);
                            bp_core_redirect($bp->loggedin_user->domain . $bp->current_component . '/compose');
                        }
                    } else {
                        $message = __('Message sent successfully!', 'buddypress') . ' <a href="' . $bp->loggedin_user->domain . $bp->messages->slug . '/view/' . $pmessage->thread_id . '">' . __('View Message', 'buddypress') . '</a> &raquo;';
                        $type = 'success';
                        // Send screen notifications to the recipients
                        for ($i = 0; $i < count($pmessage->recipients); $i++) {
                            if ($pmessage->recipients[$i] != $bp->loggedin_user->id) {
                                bp_core_add_notification($pmessage->id, $pmessage->recipients[$i], 'messages', 'new_message');
                            }
                        }
                        // Send email notifications to the recipients
                        require_once BP_PLUGIN_DIR . '/bp-messages/bp-messages-notifications.php';
                        messages_notification_new_message(array('item_id' => $pmessage->id, 'recipient_ids' => $pmessage->recipients, 'thread_id' => $pmessage->thread_id, 'component_name' => 'messages', 'component_action' => 'message_sent', 'is_private' => 1));
                        do_action('messages_send_message', array('item_id' => $pmessage->id, 'recipient_ids' => $pmessage->recipients, 'thread_id' => $pmessage->thread_id, 'component_name' => 'messages', 'component_action' => 'message_sent', 'is_private' => 1));
                        if ($from_ajax) {
                            return array('status' => 1, 'message' => $message, 'reply' => $pmessage);
                        } else {
                            bp_core_add_message($message);
                            bp_core_redirect($bp->loggedin_user->domain . $bp->current_component . '/inbox');
                        }
                    }
                } else {
                    $message = __('Message could not be sent, please try again.', 'buddypress');
                    $type = 'error';
                    if ($from_ajax) {
                        return array('status' => 0, 'message' => $message);
                    } else {
                        bp_core_add_message($message, $type);
                        bp_core_redirect($bp->loggedin_user->domain . $bp->messages->slug . '/compose');
                    }
                }
            }
        }
    }
}
/**
 * Load the Messages > Compose screen.
 */
function messages_screen_compose()
{
    if (bp_action_variables()) {
        bp_do_404();
        return;
    }
    // Remove any saved message data from a previous session.
    messages_remove_callback_values();
    // Check if the message form has been submitted
    if (isset($_POST['send'])) {
        // Check the nonce
        check_admin_referer('messages_send_message');
        // Check we have what we need
        if (empty($_POST['subject']) || empty($_POST['content'])) {
            bp_core_add_message(__('There was an error sending that message. Please try again.', 'buddypress'), 'error');
        } else {
            // If this is a notice, send it
            if (isset($_POST['send-notice'])) {
                if (messages_send_notice($_POST['subject'], $_POST['content'])) {
                    bp_core_add_message(__('Notice sent successfully!', 'buddypress'));
                    bp_core_redirect(bp_loggedin_user_domain() . bp_get_messages_slug() . '/inbox/');
                } else {
                    bp_core_add_message(__('There was an error sending that notice. Please try again.', 'buddypress'), 'error');
                }
            } else {
                // Filter recipients into the format we need - array( 'username/userid', 'username/userid' )
                $autocomplete_recipients = explode(',', $_POST['send-to-input']);
                $typed_recipients = explode(' ', $_POST['send_to_usernames']);
                $recipients = array_merge((array) $autocomplete_recipients, (array) $typed_recipients);
                /**
                 * Filters the array of recipients to receive the composed message.
                 *
                 * @since BuddyPress (1.2.10)
                 *
                 * @param array $recipients Array of recipients to receive message.
                 */
                $recipients = apply_filters('bp_messages_recipients', $recipients);
                $thread_id = messages_new_message(array('recipients' => $recipients, 'subject' => $_POST['subject'], 'content' => $_POST['content']));
                // Send the message
                if (!empty($thread_id)) {
                    bp_core_add_message(__('Message sent successfully!', 'buddypress'));
                    bp_core_redirect(bp_loggedin_user_domain() . bp_get_messages_slug() . '/view/' . $thread_id . '/');
                } else {
                    bp_core_add_message(__('There was an error sending that message. Please try again.', 'buddypress'), 'error');
                }
            }
        }
    }
    /**
     * Fires right before the loading of the Messages compose screen template file.
     *
     * @since BuddyPress (1.0.0)
     */
    do_action('messages_screen_compose');
    /**
     * Filters the template to load for the Messages compose screen.
     *
     * @since BuddyPress (1.0.0)
     *
     * @param string $template Path to the messages template to load.
     */
    bp_core_load_template(apply_filters('messages_template_compose', 'members/single/home'));
}
/**
 * Handle creating of private messages or sitewide notices
 *
 * @since BuddyPress (2.4.0) This function was split from messages_screen_compose(). See #6505.
 *
 * @return boolean
 */
function bp_messages_action_create_message()
{
    // Bail if not posting to the compose message screen
    if (!bp_is_post_request() || !bp_is_messages_component() || !bp_is_current_action('compose')) {
        return false;
    }
    // Check the nonce
    check_admin_referer('messages_send_message');
    // Define local variables
    $redirect_to = '';
    $feedback = '';
    $success = false;
    // Missing subject or content
    if (empty($_POST['subject']) || empty($_POST['content'])) {
        $success = false;
        if (empty($_POST['subject'])) {
            $feedback = __('Your message was not sent. Please enter a subject line.', 'buddypress');
        } else {
            $feedback = __('Your message was not sent. Please enter some content.', 'buddypress');
        }
        // Subject and content present
    } else {
        // Setup the link to the logged-in user's messages
        $member_messages = trailingslashit(bp_loggedin_user_domain() . bp_get_messages_slug());
        // Site-wide notice
        if (isset($_POST['send-notice'])) {
            // Attempt to save the notice and redirect to notices
            if (messages_send_notice($_POST['subject'], $_POST['content'])) {
                $success = true;
                $feedback = __('Notice successfully created.', 'buddypress');
                $redirect_to = trailingslashit($member_messages . 'notices');
                // Notice could not be sent
            } else {
                $success = false;
                $feedback = __('Notice was not created. Please try again.', 'buddypress');
            }
            // Private conversation
        } else {
            // Filter recipients into the format we need - array( 'username/userid', 'username/userid' )
            $autocomplete_recipients = (array) explode(',', $_POST['send-to-input']);
            $typed_recipients = (array) explode(' ', $_POST['send_to_usernames']);
            $recipients = array_merge($autocomplete_recipients, $typed_recipients);
            /**
             * Filters the array of recipients to receive the composed message.
             *
             * @since BuddyPress (1.2.10)
             *
             * @param array $recipients Array of recipients to receive message.
             */
            $recipients = apply_filters('bp_messages_recipients', $recipients);
            // Attempt to send the message
            $thread_id = messages_new_message(array('recipients' => $recipients, 'subject' => $_POST['subject'], 'content' => $_POST['content']));
            // Send the message and redirect to it
            if (!empty($thread_id)) {
                $success = true;
                $feedback = __('Message successfully sent.', 'buddypress');
                $view = trailingslashit($member_messages . 'view');
                $redirect_to = trailingslashit($view . $thread_id);
                // Message could not be sent
            } else {
                $success = false;
                $feedback = __('Message was not sent. Please try again.', 'buddypress');
            }
        }
    }
    // Feedback
    if (!empty($feedback)) {
        // Determine message type
        $type = true === $success ? 'success' : 'error';
        // Add feedback message
        bp_core_add_message($feedback, $type);
    }
    // Maybe redirect
    if (!empty($redirect_to)) {
        bp_core_redirect($redirect_to);
    }
}