Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/19/2015 07:47:01 PM (10 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.