bp_messages_action_create_message()

Handle creating of private messages or sitewide notices

Description

Return

(boolean)

Source

File: bp-messages/actions/compose.php

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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.', 'buddyboss' );
        } else {
            $feedback = __( 'Your message was not sent. Please enter some content.', 'buddyboss' );
        }
 
    // 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.', 'buddyboss' );
                $redirect_to = trailingslashit( $member_messages . 'notices' );
 
            // Notice could not be sent.
            } else {
                $success  = false;
                $feedback = __( 'Notice was not created. Please try again.', 'buddyboss' );
            }
 
        // 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.
            $send = messages_new_message( array(
                'recipients' => $recipients,
                'subject'    => $_POST['subject'],
                'content'    => $_POST['content'],
                'error_type' => 'wp_error'
            ) );
 
            // Send the message and redirect to it.
            if ( true === is_int( $send ) ) {
                $success     = true;
                $feedback    = __( 'Message successfully sent.', 'buddyboss' );
                $view        = trailingslashit( $member_messages . 'view' );
                $redirect_to = trailingslashit( $view . $send );
 
            // Message could not be sent.
            } else {
                $success  = false;
                $feedback = $send->get_error_message();
            }
        }
    }
 
    // 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 );
    }
}

Changelog

Changelog
Version Description
BuddyPress 2.4.0 This function was split from messages_screen_compose(). See #6505. Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.