Skip to:
Content

BuddyPress.org

Changeset 10586


Ignore:
Timestamp:
02/12/2016 11:30:39 PM (9 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.

Fixes #6894 (2.4-branch).

Location:
branches/2.4
Files:
2 edited

Legend:

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

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

    r10286 r10586  
    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.