Skip to:
Content

BuddyPress.org

Ticket #8489: 8489.no-prefix.patch

File 8489.no-prefix.patch, 7.0 KB (added by imath, 2 years ago)
  • src/bp-messages/bp-messages-functions.php

    diff --git src/bp-messages/bp-messages-functions.php src/bp-messages/bp-messages-functions.php
    index 83563350e..bd69e0680 100644
    function messages_check_thread_access( $thread_id, $user_id = 0 ) { 
    324324 *
    325325 * Wrapper for {@link BP_Messages_Thread::mark_as_read()}.
    326326 *
    327  * @param int $thread_id ID of the thread.
     327 * @since 9.0.0 Added the `user_id` parameter.
     328 *
     329 * @param int $thread_id The message thread ID.
     330 * @param int $user_id   Optional. The user the thread will be marked as read.
    328331 *
    329332 * @return false|int Number of threads marked as read or false on error.
    330333 */
    331 function messages_mark_thread_read( $thread_id ) {
    332         return BP_Messages_Thread::mark_as_read( $thread_id );
     334function messages_mark_thread_read( $thread_id, $user_id = 0 ) {
     335        return BP_Messages_Thread::mark_as_read( $thread_id, $user_id );
    333336}
    334337
    335338/**
    function messages_mark_thread_read( $thread_id ) { 
    337340 *
    338341 * Wrapper for {@link BP_Messages_Thread::mark_as_unread()}.
    339342 *
    340  * @param int $thread_id ID of the thread.
     343 * @since 9.0.0 Added the `user_id` parameter.
     344 *
     345 * @param int $thread_id The message thread ID.
     346 * @param int $user_ud   Optional. The user the thread will be marked as unread.
    341347 *
    342348 * @return false|int Number of threads marked as unread or false on error.
    343349 */
    344 function messages_mark_thread_unread( $thread_id ) {
    345         return BP_Messages_Thread::mark_as_unread( $thread_id );
     350function messages_mark_thread_unread( $thread_id, $user_id = 0 ) {
     351        return BP_Messages_Thread::mark_as_unread( $thread_id, $user_id );
    346352}
    347353
    348354/**
  • src/bp-messages/classes/class-bp-messages-thread.php

    diff --git src/bp-messages/classes/class-bp-messages-thread.php src/bp-messages/classes/class-bp-messages-thread.php
    index 770e58434..8a87d8faa 100644
    class BP_Messages_Thread { 
    612612         * Mark a thread as read.
    613613         *
    614614         * @since 1.0.0
     615         * @since 9.0.0 Added the `user_id` parameter.
    615616         *
    616617         * @param int $thread_id The message thread ID.
     618         * @param int $user_id   The user the thread will be marked as read.
    617619         *
    618620         * @return false|int Number of threads marked as read or false on error.
    619621         */
    620         public static function mark_as_read( $thread_id = 0 ) {
     622        public static function mark_as_read( $thread_id = 0, $user_id = 0 ) {
    621623                global $wpdb;
    622624
    623                 $user_id =
    624                         bp_displayed_user_id() ?
    625                         bp_displayed_user_id() :
    626                         bp_loggedin_user_id();
     625                if ( empty( $user_id ) ) {
     626                        $user_id =
     627                                bp_displayed_user_id() ?
     628                                bp_displayed_user_id() :
     629                                bp_loggedin_user_id();
     630                }
    627631
    628632                $bp     = buddypress();
    629633                $retval = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 0 WHERE user_id = %d AND thread_id = %d", $user_id, $thread_id ) );
    class BP_Messages_Thread { 
    635639                 * Fires when messages thread was marked as read.
    636640                 *
    637641                 * @since 2.8.0
     642                 * @since 9.0.0 Added the `user_id` parameter.
    638643                 *
    639644                 * @param int $thread_id The message thread ID.
     645                 * @param int $user_id   The user the thread will be marked as read.
    640646                 */
    641                 do_action( 'messages_thread_mark_as_read', $thread_id );
     647                do_action( 'messages_thread_mark_as_read', $thread_id, $user_id );
    642648
    643649                return $retval;
    644650        }
    class BP_Messages_Thread { 
    647653         * Mark a thread as unread.
    648654         *
    649655         * @since 1.0.0
     656         * @since 9.0.0 Added the `user_id` parameter.
    650657         *
    651658         * @param int $thread_id The message thread ID.
     659         * @param int $user_id   The user the thread will be marked as unread.
    652660         *
    653661         * @return false|int Number of threads marked as unread or false on error.
    654662         */
    655         public static function mark_as_unread( $thread_id = 0 ) {
     663        public static function mark_as_unread( $thread_id = 0, $user_id = 0 ) {
    656664                global $wpdb;
    657665
    658                 $user_id =
    659                         bp_displayed_user_id() ?
    660                         bp_displayed_user_id() :
    661                         bp_loggedin_user_id();
     666                if ( empty( $user_id ) ) {
     667                        $user_id =
     668                                bp_displayed_user_id() ?
     669                                bp_displayed_user_id() :
     670                                bp_loggedin_user_id();
     671                }
    662672
    663673                $bp     = buddypress();
    664674                $retval = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 1 WHERE user_id = %d AND thread_id = %d", $user_id, $thread_id ) );
    class BP_Messages_Thread { 
    670680                 * Fires when messages thread was marked as unread.
    671681                 *
    672682                 * @since 2.8.0
     683                 * @since 9.0.0 Added the `user_id` parameter.
    673684                 *
    674685                 * @param int $thread_id The message thread ID.
     686                 * @param int $user_id   The user the thread will be marked as unread.
    675687                 */
    676                 do_action( 'messages_thread_mark_as_unread', $thread_id );
     688                do_action( 'messages_thread_mark_as_unread', $thread_id, $user_id );
    677689
    678690                return $retval;
    679691        }
  • tests/phpunit/testcases/messages/class.bp-messages-thread.php

    diff --git tests/phpunit/testcases/messages/class.bp-messages-thread.php tests/phpunit/testcases/messages/class.bp-messages-thread.php
    index 3827997f0..f9de1d1cd 100644
    class BP_Tests_BP_Messages_Thread extends BP_UnitTestCase { 
    318318                $this->set_current_user( $current_user );
    319319        }
    320320
     321        /**
     322         * @group cache
     323         */
     324        public function test_marking_a_thread_as_read_with_specific_user_id() {
     325                $u1      = self::factory()->user->create();
     326                $u2      = self::factory()->user->create();
     327                $message = self::factory()->message->create_and_get( array(
     328                        'sender_id'  => $u1,
     329                        'recipients' => array( $u2 ),
     330                        'subject'    => 'Foo',
     331                ) );
     332
     333                $thread_id = $message->thread_id;
     334
     335                // Cache should be populated.
     336                $this->assertTrue( (bool) wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
     337
     338                // Mark thread as read.
     339                messages_mark_thread_read( $thread_id, $u2 );
     340
     341                // Cache should be empty.
     342                $this->assertFalse( wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
     343
     344                $thread = new BP_Messages_Thread( $thread_id );
     345
     346                $this->assertFalse( (bool) $thread->unread_count );
     347                $this->assertFalse( (bool) $thread->recipients[ $u1 ]->unread_count );
     348                $this->assertFalse( (bool) $thread->recipients[ $u2 ]->unread_count );
     349        }
     350
     351        /**
     352         * @group cache
     353         */
     354        public function test_marking_a_thread_as_unread_with_specific_user_id() {
     355                $u1      = self::factory()->user->create();
     356                $u2      = self::factory()->user->create();
     357                $message = self::factory()->message->create_and_get( array(
     358                        'sender_id'  => $u1,
     359                        'recipients' => array( $u2 ),
     360                        'subject'    => 'Foo',
     361                ) );
     362
     363                $thread_id = $message->thread_id;
     364
     365                // Cache should be populated.
     366                $this->assertTrue( (bool) wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
     367
     368                // Mark thread as unread.
     369                messages_mark_thread_unread( $thread_id, $u2 );
     370
     371                // Cache should be empty.
     372                $this->assertFalse( wp_cache_get( 'thread_recipients_' . $thread_id, 'bp_messages' ) );
     373
     374                $thread = new BP_Messages_Thread( $thread_id );
     375
     376                $this->assertFalse( (bool) $thread->recipients[ $u1 ]->unread_count );
     377                $this->assertTrue( (bool) $thread->recipients[ $u2 ]->unread_count );
     378        }
     379
    321380        /**
    322381         * @group get_recipients
    323382         * @group cache