Skip to:
Content

BuddyPress.org

Changeset 10286


Ignore:
Timestamp:
10/19/2015 07:47:01 PM (5 years ago)
Author:
r-a-y
Message:

Messages: Return context-appropriate error messages for messages_new_message().

This commit introduces the error_type parameter in the
messages_new_message() function, which allows for us to return
descriptive error messages to the user when 'error_type' is set to
'wp_error'.

This most notably addresses an issue when entering an invalid username
when composing a new private message.

Commit also includes a unit test.

Fixes #6535.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/bp-messages-actions.php

    r10139 r10286  
    8585
    8686            // Attempt to send the message
    87             $thread_id = messages_new_message( array(
     87            $send = messages_new_message( array(
    8888                'recipients' => $recipients,
    8989                'subject'    => $_POST['subject'],
    90                 'content'    => $_POST['content']
     90                'content'    => $_POST['content'],
     91                'error_type' => 'wp_error'
    9192            ) );
    9293
    9394            // Send the message and redirect to it
    94             if ( ! empty( $thread_id ) ) {
     95            if ( true === is_int( $send ) ) {
    9596                $success     = true;
    9697                $feedback    = __( 'Message successfully sent.', 'buddypress' );
    9798                $view        = trailingslashit( $member_messages . 'view' );
    98                 $redirect_to = trailingslashit( $view . $thread_id );
     99                $redirect_to = trailingslashit( $view . $send );
    99100
    100101            // Message could not be sent
    101102            } else {
    102103                $success  = false;
    103                 $feedback = __( 'Message was not sent. Please try again.', 'buddypress' );
     104                $feedback = $send->get_error_message();
    104105            }
    105106        }
  • trunk/src/bp-messages/bp-messages-functions.php

    r10139 r10286  
    1717/**
    1818 * Create a new message.
     19 *
     20 * @since 2.4.0 Added 'error_type' as an additional $args parameter.
    1921 *
    2022 * @param array|string $args {
     
    3234 *     @type string $content    Content of the message. Cannot be empty.
    3335 *     @type string $date_sent  Date sent, in 'Y-m-d H:i:s' format. Default: current date/time.
     36 *     @type string $error_type Optional. Error type. Either 'bool' or 'wp_error'. Default: 'bool'.
    3437 * }
    3538 * @return int|bool ID of the message thread on success, false on failure.
     
    4447        'subject'    => false,
    4548        'content'    => false,
    46         'date_sent'  => bp_core_current_time()
     49        'date_sent'  => bp_core_current_time(),
     50        'error_type' => 'bool'
    4751    ), 'messages_new_message' );
    4852
    4953    // Bail if no sender or no content
    5054    if ( empty( $r['sender_id'] ) || empty( $r['content'] ) ) {
    51         return false;
     55        if ( 'wp_error' === $r['error_type'] ) {
     56            if ( empty( $r['sender_id'] ) ) {
     57                $error_code = 'messages_empty_sender';
     58                $feedback = __( 'Your message was not sent. Please use a valid sender.', 'buddypress' );
     59            } else {
     60                $error_code = 'messages_empty_content';
     61                $feedback = __( 'Your message was not sent. Please enter some content.', 'buddypress' );
     62            }
     63
     64            return new WP_Error( $error_code, $feedback );
     65
     66        } else {
     67            return false;
     68        }
    5269    }
    5370
     
    83100        // Bail if no recipients
    84101        if ( empty( $r['recipients'] ) ) {
    85             return false;
     102            if ( 'wp_error' === $r['error_type'] ) {
     103                return new WP_Error( 'message_empty_recipients', __( 'Message could not be sent. Please enter a recipient.', 'buddypress' ) );
     104            } else {
     105                return false;
     106            }
    86107        }
    87108
     
    139160        $recipient_ids = array_unique( $recipient_ids );
    140161        if ( empty( $recipient_ids ) ) {
    141             return false;
     162            if ( 'wp_error' === $r['error_type'] ) {
     163                return new WP_Error( 'message_invalid_recipients', __( 'Message could not be sent because you have entered an invalid username. Please try again.', 'buddypress' ) );
     164            } else {
     165                return false;
     166            }
    142167        }
    143168
  • trunk/tests/phpunit/testcases/messages/functions.php

    r9819 r10286  
    4242    }
    4343
     44    /**
     45     * @group messages_new_message
     46     */
     47    public function test_messages_new_message_invalid_recipient_error_message() {
     48        $u1 = $this->factory->user->create();
     49
     50        // attempt to send a private message to an invalid username
     51        $t1 = messages_new_message( array(
     52            'sender_id'  => $u1,
     53            'recipients' => array( 'homerglumpkin' ),
     54            'subject'    => 'A new message',
     55            'content'    => 'Hey there!',
     56            'error_type' => 'wp_error'
     57        ) );
     58
     59        $this->assertSame( 'Message could not be sent because you have entered an invalid username. Please try again.', $t1->get_error_message() );
     60    }
    4461}
Note: See TracChangeset for help on using the changeset viewer.