Ticket #8426: 8426.6.patch
File 8426.6.patch, 8.9 KB (added by , 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 261 261 * @since 3.0.0 262 262 * 263 263 * @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. 264 266 */ 265 function bp_messages_mark_notification_on_mark_thread( $thread_id ) { 267 function bp_messages_mark_notification_on_mark_thread( $thread_id, $user_id, $num_rows = 0 ) { 268 if ( ! $num_rows ) { 269 return; 270 } 271 266 272 $thread_messages = BP_Messages_Thread::get_messages( $thread_id ); 273 $message_ids = wp_list_pluck( $thread_messages, 'id' ); 267 274 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 ); 271 276 } 272 add_action( 'messages_thread_mark_as_read', 'bp_messages_mark_notification_on_mark_thread' );277 add_action( 'messages_thread_mark_as_read', 'bp_messages_mark_notification_on_mark_thread', 10, 3 ); 273 278 274 279 /** 275 280 * 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 { 763 763 bp_loggedin_user_id(); 764 764 } 765 765 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 ) ); 768 768 769 769 wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' ); 770 770 wp_cache_delete( $user_id, 'bp_messages_unread_count' ); … … class BP_Messages_Thread { 774 774 * 775 775 * @since 2.8.0 776 776 * @since 9.0.0 Added the `user_id` parameter. 777 * @since 10.0.0 Added the `$num_rows` parameter. 777 778 * 778 779 * @param int $thread_id The message thread ID. 779 780 * @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. 780 782 */ 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 ); 782 784 783 return $ retval;785 return $num_rows; 784 786 } 785 787 786 788 /** … … class BP_Messages_Thread { 807 809 bp_loggedin_user_id(); 808 810 } 809 811 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 ) ); 812 814 813 815 wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' ); 814 816 wp_cache_delete( $user_id, 'bp_messages_unread_count' ); … … class BP_Messages_Thread { 818 820 * 819 821 * @since 2.8.0 820 822 * @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. 822 824 * 823 825 * @param int $thread_id The message thread ID. 824 826 * @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. 826 828 */ 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 ); 828 830 829 return $ retval;831 return $num_rows; 830 832 } 831 833 832 834 /** -
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 580 580 ); 581 581 } 582 582 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 */ 594 function 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 583 608 /** Helpers *******************************************************************/ 584 609 585 610 /** -
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 { 538 538 539 539 // The id. 540 540 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 } 543 547 } 544 548 545 549 // The user_id. … … class BP_Notifications_Notification { 550 554 551 555 // The item_id. 552 556 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 } 555 564 } 556 565 557 566 // The secondary_item_id. … … class BP_Notifications_Notification { 907 916 ); 908 917 } 909 918 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 910 978 /** 911 979 * Delete notifications. 912 980 *