Skip to:
Content

BuddyPress.org

Ticket #6535: 6535.01.patch

File 6535.01.patch, 3.8 KB (added by r-a-y, 10 years ago)
  • src/bp-messages/bp-messages-actions.php

     
    8585                        $recipients = apply_filters( 'bp_messages_recipients', $recipients );
    8686
    8787                        // Attempt to send the message
    88                         $thread_id = messages_new_message( array(
     88                        $send = messages_new_message( array(
    8989                                'recipients' => $recipients,
    9090                                'subject'    => $_POST['subject'],
    91                                 'content'    => $_POST['content']
     91                                'content'    => $_POST['content'],
     92                                'error_type' => 'wp_error'
    9293                        ) );
    9394
    9495                        // Send the message and redirect to it
    95                         if ( ! empty( $thread_id ) ) {
     96                        if ( true === is_int( $send ) ) {
    9697                                $success     = true;
    9798                                $feedback    = __( 'Message successfully sent.', 'buddypress' );
    9899                                $view        = trailingslashit( $member_messages . 'view' );
    99                                 $redirect_to = trailingslashit( $view . $thread_id );
     100                                $redirect_to = trailingslashit( $view . $send );
    100101
    101102                        // Message could not be sent
    102103                        } else {
    103104                                $success  = false;
    104                                 $feedback = __( 'Message was not sent. Please try again.', 'buddypress' );
     105                                $feedback = $send->get_error_message();
    105106                        }
    106107                }
    107108        }
  • src/bp-messages/bp-messages-functions.php

     
    3232 *                              threads, 'No Subject' will be used if no $subject is provided.
    3333 *     @type string $content    Content of the message. Cannot be empty.
    3434 *     @type string $date_sent  Date sent, in 'Y-m-d H:i:s' format. Default: current date/time.
     35 *     @type string $error_type Optional. Error type. Either 'bool' or 'wp_error'. Default: 'bool'.
    3536 * }
    3637 * @return int|bool ID of the message thread on success, false on failure.
    3738 */
     
    4445                'recipients' => array(), // Can be an array of usernames, user_ids or mixed.
    4546                'subject'    => false,
    4647                'content'    => false,
    47                 'date_sent'  => bp_core_current_time()
     48                'date_sent'  => bp_core_current_time(),
     49                'error_type' => 'bool'
    4850        ), 'messages_new_message' );
    4951
    5052        // Bail if no sender or no content
    5153        if ( empty( $r['sender_id'] ) || empty( $r['content'] ) ) {
    52                 return false;
     54                if ( 'wp_error' === $r['error_type'] ) {
     55                        if ( empty( $r['sender_id'] ) ) {
     56                                $error_code = 'messages_empty_sender';
     57                                $feedback = __( 'Your message was not sent. Please use a valid sender.', 'buddypress' );
     58                        } else {
     59                                $error_code = 'messages_empty_content';
     60                                $feedback = __( 'Your message was not sent. Please enter some content.', 'buddypress' );
     61                        }
     62
     63                        return new WP_Error( $error_code, $feedback );
     64
     65                } else {
     66                        return false;
     67                }
    5368        }
    5469
    5570        // Create a new message object
     
    8398
    8499                // Bail if no recipients
    85100                if ( empty( $r['recipients'] ) ) {
    86                         return false;
     101                        if ( 'wp_error' === $r['error_type'] ) {
     102                                return new WP_Error( 'message_empty_recipients', __( 'Message could not be sent. Please enter a recipient.', 'buddypress' ) );
     103                        } else {
     104                                return false;
     105                        }
    87106                }
    88107
    89108                // Set a default subject if none exists
     
    139158                // Remove duplicates & bail if no recipients
    140159                $recipient_ids = array_unique( $recipient_ids );
    141160                if ( empty( $recipient_ids ) ) {
    142                         return false;
     161                        if ( 'wp_error' === $r['error_type'] ) {
     162                                return new WP_Error( 'message_invalid_recipients', __( 'Message could not be sent because you have entered an invalid username. Please try again.', 'buddypress' ) );
     163                        } else {
     164                                return false;
     165                        }
    143166                }
    144167
    145168                // Format this to match existing recipients
     
    150173        }
    151174
    152175        // Bail if message failed to send
     176        // @todo Should we use WP_Error higher up the stack in BP_Messages_Message::save()?
    153177        if ( ! $message->send() ) {
    154178                return false;
    155179        }