/**
 * Send a private message reply to a thread via a POST request.
 *
 * @return string HTML
 * @since BuddyPress (1.2)
 */
function bp_legacy_theme_ajax_messages_send_reply()
{
    // Bail if not a POST action
    if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
        return;
    }
    check_ajax_referer('messages_send_message');
    $result = messages_new_message(array('thread_id' => (int) $_REQUEST['thread_id'], 'content' => $_REQUEST['content']));
    if (!empty($result)) {
        // pretend we're in the message loop
        global $thread_template;
        bp_thread_has_messages(array('thread_id' => (int) $_REQUEST['thread_id']));
        // set the current message to the 2nd last
        $thread_template->message = end($thread_template->thread->messages);
        $thread_template->message = prev($thread_template->thread->messages);
        // set current message to current key
        $thread_template->current_message = key($thread_template->thread->messages);
        // now manually iterate message like we're in the loop
        bp_thread_the_message();
        // manually call oEmbed
        // this is needed because we're not at the beginning of the loop
        bp_messages_embed();
        ?>

		<div class="message-box new-message <?php 
        bp_the_thread_message_css_class();
        ?>
">
			<div class="message-metadata">
				<?php 
        /**
         * Fires before the single message header is displayed.
         *
         * @since BuddyPress (1.1.0)
         */
        do_action('bp_before_message_meta');
        ?>
				<?php 
        echo bp_loggedin_user_avatar('type=thumb&width=30&height=30');
        ?>

				<strong><a href="<?php 
        echo bp_loggedin_user_domain();
        ?>
"><?php 
        bp_loggedin_user_fullname();
        ?>
</a> <span class="activity"><?php 
        printf(__('Sent %s', 'buddypress'), bp_core_time_since(bp_core_current_time()));
        ?>
</span></strong>

				<?php 
        /**
         * Fires after the single message header is displayed.
         *
         * @since BuddyPress (1.1.0)
         */
        do_action('bp_after_message_meta');
        ?>
			</div>

			<?php 
        /**
         * Fires before the message content for a private message.
         *
         * @since BuddyPress (1.1.0)
         */
        do_action('bp_before_message_content');
        ?>

			<div class="message-content">
				<?php 
        bp_the_thread_message_content();
        ?>
			</div>

			<?php 
        /**
         * Fires after the message content for a private message.
         *
         * @since BuddyPress (1.1.0)
         */
        do_action('bp_after_message_content');
        ?>

			<div class="clear"></div>
		</div>
	<?php 
        // clean up the loop
        bp_thread_messages();
    } else {
        echo "-1<div id='message' class='error'><p>" . __('There was a problem sending that reply. Please try again.', 'buddypress') . '</p></div>';
    }
    exit;
}
/**
 * Send a private message reply to a thread via a POST request.
 *
 * @since 1.2.0
 *
 * @return string HTML
 */
function bp_legacy_theme_ajax_messages_send_reply()
{
    // Bail if not a POST action.
    if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
        return;
    }
    check_ajax_referer('messages_send_message');
    $result = messages_new_message(array('thread_id' => (int) $_REQUEST['thread_id'], 'content' => $_REQUEST['content']));
    if (!empty($result)) {
        // Pretend we're in the message loop.
        global $thread_template;
        bp_thread_has_messages(array('thread_id' => (int) $_REQUEST['thread_id']));
        // Set the current message to the 2nd last.
        $thread_template->message = end($thread_template->thread->messages);
        $thread_template->message = prev($thread_template->thread->messages);
        // Set current message to current key.
        $thread_template->current_message = key($thread_template->thread->messages);
        // Now manually iterate message like we're in the loop.
        bp_thread_the_message();
        // Manually call oEmbed
        // this is needed because we're not at the beginning of the loop.
        bp_messages_embed();
        // Add new-message css class.
        add_filter('bp_get_the_thread_message_css_class', create_function('$retval', '
			$retval[] = "new-message";
			return $retval;
		'));
        // Output single message template part.
        bp_get_template_part('members/single/messages/message');
        // Clean up the loop.
        bp_thread_messages();
    } else {
        echo "-1<div id='message' class='error'><p>" . __('There was a problem sending that reply. Please try again.', 'buddypress') . '</p></div>';
    }
    exit;
}