Skip to:
Content

BuddyPress.org

Changeset 10585


Ignore:
Timestamp:
02/12/2016 11:29:33 PM (5 years ago)
Author:
r-a-y
Message:

Messages: Return generic WP Error in messages_new_message() when necessary.

In #6535, we added an 'error_type' parameter in messages_new_message()
that supports returning a WP Error object if error type is set to
'wp_error'. However, we did not return a WP error further down in the
messages_new_message() function, which can result in a potential fatal
error to occur.

This commit rectifies this and includes a unit test.

Anti-props r-a-y.

See #6894 (trunk).

Location:
trunk
Files:
2 edited

Legend:

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

    r10417 r10585  
    176176
    177177    // Bail if message failed to send.
    178     if ( ! $message->send() ) {
     178    $send = $message->send();
     179    if ( false === is_int( $send ) ) {
     180        if ( 'wp_error' === $r['error_type'] ) {
     181            if ( is_wp_error( $send ) ) {
     182                return $send;
     183            } else {
     184                return new WP_Error( 'message_generic_error', __( 'Message was not sent. Please try again.', 'buddypress' ) );
     185            }
     186        }
     187
    179188        return false;
    180189    }
  • trunk/tests/phpunit/testcases/messages/functions.php

    r10286 r10585  
    5959        $this->assertSame( 'Message could not be sent because you have entered an invalid username. Please try again.', $t1->get_error_message() );
    6060    }
     61
     62    /**
     63     * @group messages_new_message
     64     */
     65    public function test_messages_new_message_wp_error_generic() {
     66        $u1 = $this->factory->user->create();
     67        $u2 = $this->factory->user->create();
     68
     69        // Emulate a plugin disabling messages.
     70        add_action( 'messages_message_before_save', array( $this, 'remove_recipients_before_save' ) );
     71
     72        // send a private message
     73        $t1 = messages_new_message( array(
     74            'sender_id'  => $u1,
     75            'recipients' => array( $u2 ),
     76            'subject'    => 'A new message',
     77            'content'    => 'Hey there!',
     78            'error_type' => 'wp_error'
     79        ) );
     80
     81        $this->assertNotEmpty( $t1->get_error_code() );
     82
     83        remove_action( 'messages_message_before_save', array( $this, 'remove_recipients_before_save' ) );
     84    }
     85
     86    /**
     87     * Helper method for test_messages_new_message_wp_error_generic().
     88     */
     89    public function remove_recipients_before_save( $message ) {
     90        $message->recipients = array();
     91    }
    6192}
Note: See TracChangeset for help on using the changeset viewer.