Skip to:
Content

BuddyPress.org

Changeset 12984


Ignore:
Timestamp:
07/02/2021 01:10:12 AM (3 years ago)
Author:
espellcaste
Message:

Adding a user ID parameter to the messages_mark_thread_read and messages_mark_thread_unread functions.

Allowing those functions to be used in other BuddyPress API.

Props imath
Fixes #8489 (trunk)

Location:
trunk
Files:
3 edited

Legend:

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

    r12592 r12984  
    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
     
    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_id   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
  • trunk/src/bp-messages/classes/class-bp-messages-thread.php

    r12592 r12984  
    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 ) {
    621         global $wpdb;
    622 
    623         $user_id =
    624             bp_displayed_user_id() ?
    625             bp_displayed_user_id() :
    626             bp_loggedin_user_id();
     622    public static function mark_as_read( $thread_id = 0, $user_id = 0 ) {
     623        global $wpdb;
     624
     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();
     
    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;
     
    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 ) {
    656         global $wpdb;
    657 
    658         $user_id =
    659             bp_displayed_user_id() ?
    660             bp_displayed_user_id() :
    661             bp_loggedin_user_id();
     663    public static function mark_as_unread( $thread_id = 0, $user_id = 0 ) {
     664        global $wpdb;
     665
     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();
     
    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;
  • trunk/tests/phpunit/testcases/messages/class.bp-messages-thread.php

    r11737 r12984  
    320320
    321321    /**
     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
     380    /**
    322381     * @group get_recipients
    323382     * @group cache
Note: See TracChangeset for help on using the changeset viewer.