Skip to:
Content

BuddyPress.org

Ticket #8426: 8426.6.patch

File 8426.6.patch, 8.9 KB (added by oztaser, 2 years ago)
  • src/bp-messages/bp-messages-notifications.php

    diff --git a/src/bp-messages/bp-messages-notifications.php b/src/bp-messages/bp-messages-notifications.php
    index 176a950d6..3a9e1cdd2 100644
    a b 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, $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 a/src/bp-messages/classes/class-bp-messages-thread.php b/src/bp-messages/classes/class-bp-messages-thread.php
    index 8a2845d21..212239c84 100644
    a b class BP_Messages_Thread { 
    763763                                bp_loggedin_user_id();
    764764                }
    765765
    766                 $bp     = buddypress();
    767                 $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 ) );
     766                $bp       = buddypress();
     767                $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 ) );
    768768
    769769                wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' );
    770770                wp_cache_delete( $user_id, 'bp_messages_unread_count' );
    class BP_Messages_Thread { 
    774774                 *
    775775                 * @since 2.8.0
    776776                 * @since 9.0.0 Added the `user_id` parameter.
     777                 * @since 10.0.0 Added the `$num_rows` parameter.
    777778                 *
    778779                 * @param int $thread_id The message thread ID.
    779780                 * @param int $user_id   The user the thread will be marked as read.
     781                 * @param bool|int $num_rows    Number of threads marked as unread or false on error.
    780782                 */
    781                 do_action( 'messages_thread_mark_as_read', $thread_id, $user_id );
     783                do_action( 'messages_thread_mark_as_read', $thread_id, $user_id, $num_rows );
    782784
    783                 return $retval;
     785                return $num_rows;
    784786        }
    785787
    786788        /**
    class BP_Messages_Thread { 
    807809                                bp_loggedin_user_id();
    808810                }
    809811
    810                 $bp     = buddypress();
    811                 $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 ) );
     812                $bp       = buddypress();
     813                $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 ) );
    812814
    813815                wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' );
    814816                wp_cache_delete( $user_id, 'bp_messages_unread_count' );
    class BP_Messages_Thread { 
    818820                 *
    819821                 * @since 2.8.0
    820822                 * @since 9.0.0  Added the `$user_id` parameter.
    821                  * @since 10.0.0 Added the `$retval` parameter.
     823                 * @since 10.0.0 Added the `$num_rows` parameter.
    822824                 *
    823825                 * @param int      $thread_id The message thread ID.
    824826                 * @param int      $user_id   The user the thread will be marked as unread.
    825                  * @param bool|int $retval     =Number of threads marked as unread or false on error.
     827                 * @param bool|int $num_rows  Number of threads marked as unread or false on error.
    826828                 */
    827                 do_action( 'messages_thread_mark_as_unread', $thread_id, $user_id, $retval );
     829                do_action( 'messages_thread_mark_as_unread', $thread_id, $user_id, $num_rows );
    828830
    829                 return $retval;
     831                return $num_rows;
    830832        }
    831833
    832834        /**
  • src/bp-notifications/bp-notifications-functions.php

    diff --git a/src/bp-notifications/bp-notifications-functions.php b/src/bp-notifications/bp-notifications-functions.php
    index b33afb52b..ff7fb9813 100644
    a b function bp_notifications_mark_notifications_from_user( $user_id, $component_nam 
    580580        );
    581581}
    582582
     583/**
     584 * Mark notifications read/unread by item ids and user.
     585 *
     586 * @param  int  $user_id  ID of the user whose notifications are being deleted.
     587 * @param  array  $item_ids  ID of the associated item.
     588 * @param  string  $component_name  Name of the associated component.
     589 * @param  string  $component_action  Name of the associated action.
     590 * @param  int|bool  $is_new  0 for read, 1 for unread.
     591 *
     592 * @return int|false True on success, false on failure.
     593 */
     594function bp_notifications_mark_notifications_by_item_ids( $user_id, $item_ids, $component_name, $component_action, $is_new = false ) {
     595        return BP_Notifications_Notification::update_many(
     596                array(
     597                        'is_new' => $is_new,
     598                ),
     599                array(
     600                        'user_id'          => $user_id,
     601                        'item_id'          => $item_ids,
     602                        'component_name'   => $component_name,
     603                        'component_action' => $component_action
     604                )
     605        );
     606}
     607
    583608/** Helpers *******************************************************************/
    584609
    585610/**
  • src/bp-notifications/classes/class-bp-notifications-notification.php

    diff --git a/src/bp-notifications/classes/class-bp-notifications-notification.php b/src/bp-notifications/classes/class-bp-notifications-notification.php
    index 9e2bee2f9..9437f1ece 100644
    a b class BP_Notifications_Notification { 
    538538
    539539                // The id.
    540540                if ( ! empty( $args['id'] ) ) {
    541                         $where_clauses['data']['id'] = absint( $args['id'] );
    542                         $where_clauses['format'][] = '%d';
     541                        if ( is_array( $args['id'] ) ) {
     542                                $where_clauses['data']['id'] = implode( ',', wp_parse_id_list( $args['id'] ) );
     543                                $where_clauses['format'][]   = 'in';
     544                        } else {
     545                                $where_clauses['data']['id'] = absint( $args['id'] );
     546                        }
    543547                }
    544548
    545549                // The user_id.
    class BP_Notifications_Notification { 
    550554
    551555                // The item_id.
    552556                if ( ! empty( $args['item_id'] ) ) {
    553                         $where_clauses['data']['item_id'] = absint( $args['item_id'] );
    554                         $where_clauses['format'][] = '%d';
     557                        if ( is_array( $args['item_id'] ) ) {
     558                                $where_clauses['data']['item_id'] = implode( ',', wp_parse_id_list( $args['item_id'] ) );
     559                                $where_clauses['format'][]        = 'in';
     560                        } else {
     561                                $where_clauses['data']['item_id'] = absint( $args['item_id'] );
     562                                $where_clauses['format'][]        = '%d';
     563                        }
    555564                }
    556565
    557566                // The secondary_item_id.
    class BP_Notifications_Notification { 
    907916                );
    908917        }
    909918
     919        /**
     920         * Update notifications.
     921         *
     922         * @since 10.0.0
     923         *
     924         * @param  array  $data  Array of notification data to update, passed to
     925         *                            {@link wpdb::update()}. Accepts any property of a
     926         *                            BP_Notification_Notification object.
     927         * @param  array  $where  The WHERE params as passed to wpdb::update().
     928         *                            Typically consists of array( 'ID' => $id ) to specify the ID
     929         *                            of the item being updated. See {@link wpdb::update()}.
     930         *
     931         * @return int|false The number of rows updated, or false on error.
     932         */
     933        public static function update_many( $data = array(), $where = array() ) {
     934                global $wpdb;
     935                $bp = buddypress();
     936
     937                if ( ! is_array( $data ) || ! is_array( $where ) ) {
     938                        return false;
     939                }
     940
     941                $data  = self::get_query_clauses( $data );
     942                $where = self::get_query_clauses( $where );
     943
     944                $fields     = array();
     945                $conditions = array();
     946                $values     = array();
     947
     948                foreach ( $data['data'] as $field => $value ) {
     949                        $index  = array_search( $field, array_keys( $data['data'] ) );
     950                        $format = $data['format'][ $index ];
     951
     952                        $fields[] = "`$field`=" . $format;
     953                        $values[] = $value;
     954                }
     955
     956                foreach ( $where['data'] as $field => $value ) {
     957                        $index  = array_search( $field, array_keys( $where['data'] ) );
     958                        $format = $where['format'][ $index ];
     959
     960                        if ( 'in' === $format ) {
     961                                $conditions[] = " `$field` IN ($value)";
     962
     963                                continue;
     964                        }
     965
     966                        $conditions[] = " `$field`=$format";
     967                        $values[]     = $value;
     968                }
     969
     970                $fields     = implode( ', ', $fields );
     971                $conditions = implode( ' AND ', $conditions );
     972
     973                $sql = "UPDATE `{$bp->notifications->table_name}` SET {$fields} WHERE {$conditions}";
     974
     975                return $wpdb->query( $wpdb->prepare( $sql, $values ) );
     976        }
     977
    910978        /**
    911979         * Delete notifications.
    912980         *