Skip to:
Content

BuddyPress.org

Ticket #8426: 8426.8.patch

File 8426.8.patch, 10.8 KB (added by imath, 2 years ago)
  • src/bp-messages/bp-messages-notifications.php

    diff --git src/bp-messages/bp-messages-notifications.php src/bp-messages/bp-messages-notifications.php
    index 176a950d6..a4fb8440c 100644
    add_action( 'thread_loop_start', 'bp_messages_screen_conversation_mark_notificat 
    261261 * @since 3.0.0
    262262 *
    263263 * @param int $thread_id ID of the thread being marked as read.
     264 * @param int $user_id   ID of the user thread will be marked as unread.
     265 * @param int $num_rows  The number of affected rows by the "mark read" update query.
    264266 */
    265 function bp_messages_mark_notification_on_mark_thread( $thread_id ) {
     267function bp_messages_mark_notification_on_mark_thread( $thread_id, $user_id = 0, $num_rows = 0 ) {
     268        if ( ! $num_rows ) {
     269                return;
     270        }
     271
    266272        $thread_messages = BP_Messages_Thread::get_messages( $thread_id );
     273        $message_ids     = wp_list_pluck( $thread_messages, 'id' );
    267274
    268         foreach ( $thread_messages as $thread_message ) {
    269                 bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), $thread_message->id, buddypress()->messages->id, 'new_message' );
    270         }
     275        bp_notifications_mark_notifications_by_item_ids( $user_id, $message_ids, 'messages', 'new_message', false );
    271276}
    272 add_action( 'messages_thread_mark_as_read', 'bp_messages_mark_notification_on_mark_thread' );
     277add_action( 'messages_thread_mark_as_read', 'bp_messages_mark_notification_on_mark_thread', 10, 3 );
    273278
    274279/**
    275280 * When a message is deleted, delete corresponding notifications.
  • 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 ebdf8bd1e..e1a7f1891 100644
    class BP_Messages_Thread { 
    766766                                bp_loggedin_user_id();
    767767                }
    768768
    769                 $bp     = buddypress();
    770                 $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 ) );
     769                $bp       = buddypress();
     770                $num_rows = $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 ) );
    771771
    772772                wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' );
    773773                wp_cache_delete( $user_id, 'bp_messages_unread_count' );
    class BP_Messages_Thread { 
    777777                 *
    778778                 * @since 2.8.0
    779779                 * @since 9.0.0 Added the `user_id` parameter.
     780                 * @since 10.0.0 Added the `$num_rows` parameter.
    780781                 *
    781782                 * @param int $thread_id The message thread ID.
    782783                 * @param int $user_id   The user the thread will be marked as read.
     784                 * @param bool|int $num_rows    Number of threads marked as unread or false on error.
    783785                 */
    784                 do_action( 'messages_thread_mark_as_read', $thread_id, $user_id );
     786                do_action( 'messages_thread_mark_as_read', $thread_id, $user_id, $num_rows );
    785787
    786                 return $retval;
     788                return $num_rows;
    787789        }
    788790
    789791        /**
    class BP_Messages_Thread { 
    810812                                bp_loggedin_user_id();
    811813                }
    812814
    813                 $bp     = buddypress();
    814                 $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 ) );
     815                $bp       = buddypress();
     816                $num_rows = $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 ) );
    815817
    816818                wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' );
    817819                wp_cache_delete( $user_id, 'bp_messages_unread_count' );
    class BP_Messages_Thread { 
    821823                 *
    822824                 * @since 2.8.0
    823825                 * @since 9.0.0  Added the `$user_id` parameter.
    824                  * @since 10.0.0 Added the `$retval` parameter.
     826                 * @since 10.0.0 Added the `$num_rows` parameter.
    825827                 *
    826828                 * @param int      $thread_id The message thread ID.
    827829                 * @param int      $user_id   The user the thread will be marked as unread.
    828                  * @param bool|int $retval     =Number of threads marked as unread or false on error.
     830                 * @param bool|int $num_rows  Number of threads marked as unread or false on error.
    829831                 */
    830                 do_action( 'messages_thread_mark_as_unread', $thread_id, $user_id, $retval );
     832                do_action( 'messages_thread_mark_as_unread', $thread_id, $user_id, $num_rows );
    831833
    832                 return $retval;
     834                return $num_rows;
    833835        }
    834836
    835837        /**
  • src/bp-notifications/bp-notifications-functions.php

    diff --git src/bp-notifications/bp-notifications-functions.php src/bp-notifications/bp-notifications-functions.php
    index b818ab01b..642923981 100644
    function bp_notifications_mark_notifications_from_user( $user_id, $component_nam 
    584584        );
    585585}
    586586
     587/**
     588 * Mark notifications read/unread by item ids and user.
     589 *
     590 * @since 10.0.0
     591 *
     592 * @param  int       $user_id          ID of the user whose notifications are being deleted.
     593 * @param  array     $item_ids         ID of the associated item.
     594 * @param  string    $component_name   Name of the associated component.
     595 * @param  string    $component_action Name of the associated action.
     596 * @param  int|false $is_new           0 for read, 1 for unread.
     597 * @return int|false                   The number of rows updated. False on error.
     598 */
     599function bp_notifications_mark_notifications_by_item_ids( $user_id, $item_ids, $component_name, $component_action, $is_new = false ) {
     600        return BP_Notifications_Notification::update_id_list(
     601                'item_id',
     602                $item_ids,
     603                array(
     604                        'is_new' => $is_new,
     605                ),
     606                array(
     607                        'user_id'          => $user_id,
     608                        'component_name'   => $component_name,
     609                        'component_action' => $component_action
     610                )
     611        );
     612}
     613
    587614/** Helpers *******************************************************************/
    588615
    589616/**
  • src/bp-notifications/classes/class-bp-notifications-notification.php

    diff --git src/bp-notifications/classes/class-bp-notifications-notification.php src/bp-notifications/classes/class-bp-notifications-notification.php
    index bc3b49821..12e4b40c7 100644
    class BP_Notifications_Notification { 
    910910                );
    911911        }
    912912
     913        /**
     914         * Update notifications using a list of ids/items_ids.
     915         *
     916         * @since 10.0.0
     917         *
     918         * @param string $field The name of the db field of the items to update.
     919         *                      Possible values are `id` or `item_id`.
     920         * @param int[]  $items The list of items to update.
     921         * @param array  $data  Array of notification data to update, passed to
     922         *                      {@link wpdb::update()}. Accepts any property of a
     923         *                      BP_Notification_Notification object.
     924         * @param array  $where The WHERE params as passed to wpdb::update().
     925         *                      Typically consists of array( 'ID' => $id ) to specify the ID
     926         *                      of the item being updated. See {@link wpdb::update()}.
     927         * @return int|false    The number of rows updated. False on error.
     928         */
     929        public static function update_id_list( $field, $items = array(), $data = array(), $where = array() ) {
     930                global $wpdb;
     931                $bp = buddypress();
     932
     933                $supported_fields = array( 'id', 'item_id' );
     934
     935                if ( false === in_array( $field, $supported_fields, true ) ) {
     936                        return false;
     937                }
     938
     939                if ( ! is_array( $items ) || ! is_array( $data ) || ! is_array( $where ) ) {
     940                        return false;
     941                }
     942
     943                $data  = self::get_query_clauses( $data );
     944                $where = self::get_query_clauses( $where );
     945
     946                $fields     = array();
     947                $conditions = array();
     948                $values     = array();
     949
     950                $_items       = implode( ',', wp_parse_id_list( $items ) );
     951                $conditions[] = "{$field} IN ({$_items})";
     952
     953                foreach ( $data['data'] as $field => $value ) {
     954                        $index  = array_search( $field, array_keys( $data['data'] ) );
     955                        $format = $data['format'][ $index ];
     956
     957                        $fields[] = "{$field} = {$format}";
     958                        $values[] = $value;
     959                }
     960
     961                foreach ( $where['data'] as $field => $value ) {
     962                        $index  = array_search( $field, array_keys( $where['data'] ) );
     963                        $format = $where['format'][ $index ];
     964
     965                        $conditions[] = "{$field} = {$format}";
     966                        $values[]     = $value;
     967                }
     968
     969                $fields     = implode( ', ', $fields );
     970                $conditions = implode( ' AND ', $conditions );
     971
     972                $sql = "UPDATE {$bp->notifications->table_name} SET {$fields} WHERE {$conditions}";
     973
     974                return $wpdb->query( $wpdb->prepare( $sql, $values ) );
     975        }
     976
    913977        /**
    914978         * Delete notifications.
    915979         *
  • tests/phpunit/testcases/messages/notifications.php

    diff --git tests/phpunit/testcases/messages/notifications.php tests/phpunit/testcases/messages/notifications.php
    index 8e348a830..522175780 100644
    class BP_Tests_Messages_Notifications extends BP_UnitTestCase { 
    1111        public function setUp() {
    1212                parent::setUp();
    1313
     14                $this->reset_user_id = get_current_user_id();
     15
    1416                $this->filter_fired = '';
    1517        }
    1618
     19        public function tearDown() {
     20                parent::tearDown();
     21
     22                $this->set_current_user( $this->reset_user_id );
     23        }
     24
    1725        /**
    1826         * @group messages_format_notifications
    1927         */
    class BP_Tests_Messages_Notifications extends BP_UnitTestCase { 
    4048
    4149        /**
    4250         * @group messages_format_notifications
    43          * @group imath
    4451         */
    4552        public function test_friends_format_notifications_bp_messages_single_new_message_notification_nonstring_filter() {
    4653                // Dummy thread ID
    class BP_Tests_Messages_Notifications extends BP_UnitTestCase { 
    145152                $this->assertSame( array(), $n2 );
    146153        }
    147154
     155        /**
     156         * @ticket BP8426
     157         */
     158        public function test_bp_messages_mark_notification_on_mark_thread() {
     159                global $wpdb;
     160
     161                $u1 = self::factory()->user->create();
     162                $u2 = self::factory()->user->create();
     163                $m1 = self::factory()->message->create_and_get( array(
     164                        'sender_id'  => $u1,
     165                        'recipients' => array( $u2 ),
     166                        'subject'    => 'Foo',
     167                ) );
     168
     169                self::factory()->message->create_many(
     170                        9,
     171                        array(
     172                                'thread_id' => $m1->thread_id,
     173                                'sender_id' => $u2,
     174                                'recipients' => array( $u1 ),
     175                                'subject' => 'Bar',
     176                        )
     177                );
     178
     179                $unreadn = wp_list_pluck(
     180                        BP_Notifications_Notification::get(
     181                                array(
     182                                        'user_id'           => $u1,
     183                                        'component_name'    => buddypress()->messages->id,
     184                                        'component_action'  => 'new_message',
     185                                        'is_new'            => 1,
     186                                )
     187                        ),
     188                        'user_id',
     189                        'id'
     190                );
     191
     192                $this->set_current_user( $u1 );
     193
     194                // Mark a thread read.
     195                bp_messages_mark_notification_on_mark_thread( $m1->thread_id, $u1, count( $unreadn ) );
     196
     197                $readn = wp_list_pluck(
     198                        BP_Notifications_Notification::get(
     199                                array(
     200                                        'user_id'           => $u1,
     201                                        'component_name'    => buddypress()->messages->id,
     202                                        'component_action'  => 'new_message',
     203                                        'is_new'            => 0,
     204                                )
     205                        ),
     206                        'user_id',
     207                        'id'
     208                );
     209
     210                $this->assertSame( $unreadn, $readn );
     211        }
     212
    148213        public function notification_filter_callback( $value ) {
    149214                $this->filter_fired = current_filter();
    150215                return $value;