Ticket #8426: 8426.9.patch
File 8426.9.patch, 14.3 KB (added by , 3 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..33149dcc6 100644
a b function bp_messages_screen_conversation_mark_notifications() { 245 245 246 246 // Get the unread message ids for this thread only. 247 247 $message_ids = array_intersect( $unread_message_ids, wp_list_pluck( $thread_template->thread->messages, 'id' ) ); 248 249 // Mark each notification for each PM message as read. 250 foreach ( $message_ids as $message_id ) { 251 bp_notifications_mark_notifications_by_item_id( bp_loggedin_user_id(), (int) $message_id, buddypress()->messages->id, 'new_message' ); 252 } 248 bp_notifications_mark_notifications_by_item_ids( bp_loggedin_user_id(), $message_ids, 'messages', 'new_message', false ); 253 249 } 254 250 add_action( 'thread_loop_start', 'bp_messages_screen_conversation_mark_notifications', 10 ); 255 251 … … add_action( 'thread_loop_start', 'bp_messages_screen_conversation_mark_notificat 261 257 * @since 3.0.0 262 258 * 263 259 * @param int $thread_id ID of the thread being marked as read. 260 * @param int $user_id ID of the user thread will be marked as unread. 261 * @param int $num_rows The number of affected rows by the "mark read" update query. 264 262 */ 265 function bp_messages_mark_notification_on_mark_thread( $thread_id ) { 263 function bp_messages_mark_notification_on_mark_thread( $thread_id, $user_id = 0, $num_rows = 0 ) { 264 if ( ! $num_rows ) { 265 return; 266 } 267 266 268 $thread_messages = BP_Messages_Thread::get_messages( $thread_id ); 269 $message_ids = wp_list_pluck( $thread_messages, 'id' ); 267 270 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 } 271 bp_notifications_mark_notifications_by_item_ids( $user_id, $message_ids, buddypress()->messages->id, 'new_message', false ); 271 272 } 272 add_action( 'messages_thread_mark_as_read', 'bp_messages_mark_notification_on_mark_thread' );273 add_action( 'messages_thread_mark_as_read', 'bp_messages_mark_notification_on_mark_thread', 10, 3 ); 273 274 274 275 /** 275 276 * When a message is deleted, delete corresponding notifications. … … add_action( 'messages_thread_mark_as_read', 'bp_messages_mark_notification_on_ma 282 283 function bp_messages_message_delete_notifications( $thread_id, $message_ids ) { 283 284 // For each recipient, delete notifications corresponding to each message. 284 285 $thread = new BP_Messages_Thread( $thread_id ); 286 285 287 foreach ( $thread->get_recipients() as $recipient ) { 286 foreach ( $message_ids as $message_id ) { 287 if ( ! empty( $recipient->user_id ) ) { 288 bp_notifications_delete_notifications_by_item_id( 289 $recipient->user_id, 290 (int) $message_id, 291 buddypress()->messages->id, 292 'new_message' 293 ); 294 } 288 if ( true === empty( $recipient->user_id ) ) { 289 continue; 295 290 } 291 292 bp_notifications_delete_notifications_by_item_ids( $recipient->user_id, $message_ids, buddypress()->messages->id, 'new_message' ); 296 293 } 297 294 } 298 295 add_action( 'bp_messages_thread_after_delete', 'bp_messages_message_delete_notifications', 10, 2 ); -
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 ebdf8bd1e..e1a7f1891 100644
a b class BP_Messages_Thread { 766 766 bp_loggedin_user_id(); 767 767 } 768 768 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 ) ); 771 771 772 772 wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' ); 773 773 wp_cache_delete( $user_id, 'bp_messages_unread_count' ); … … class BP_Messages_Thread { 777 777 * 778 778 * @since 2.8.0 779 779 * @since 9.0.0 Added the `user_id` parameter. 780 * @since 10.0.0 Added the `$num_rows` parameter. 780 781 * 781 782 * @param int $thread_id The message thread ID. 782 783 * @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. 783 785 */ 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 ); 785 787 786 return $ retval;788 return $num_rows; 787 789 } 788 790 789 791 /** … … class BP_Messages_Thread { 810 812 bp_loggedin_user_id(); 811 813 } 812 814 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 ) ); 815 817 816 818 wp_cache_delete( 'thread_recipients_' . $thread_id, 'bp_messages' ); 817 819 wp_cache_delete( $user_id, 'bp_messages_unread_count' ); … … class BP_Messages_Thread { 821 823 * 822 824 * @since 2.8.0 823 825 * @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. 825 827 * 826 828 * @param int $thread_id The message thread ID. 827 829 * @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. 829 831 */ 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 ); 831 833 832 return $ retval;834 return $num_rows; 833 835 } 834 836 835 837 /** -
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 b818ab01b..d343fbaca 100644
a b function bp_notifications_delete_notifications_by_item_id( $user_id, $item_id, $ 384 384 ) ); 385 385 } 386 386 387 /** 388 * Delete notifications by notification ids and user. 389 * 390 * @since 10.0.0 391 * 392 * @param int $user_id ID of the user whose notifications are being deleted. 393 * @param array $ids IDs of the associated notifications. 394 * @param string $component_name Name of the associated component. 395 * @param string $component_action Name of the associated action. 396 * @return int|false The number of rows updated. False on error. 397 */ 398 function bp_notifications_delete_notifications_by_ids( $user_id, $ids, $component_name, $component_action ) { 399 return BP_Notifications_Notification::delete_by_id_list( 400 'id', 401 $ids, 402 array( 403 'user_id' => $user_id, 404 'component_name' => $component_name, 405 'component_action' => $component_action 406 ) 407 ); 408 } 409 410 /** 411 * Delete notifications by item ids and user. 412 * 413 * @since 10.0.0 414 * 415 * @param int $user_id ID of the user whose notifications are being deleted. 416 * @param array $item_ids ID of the associated item. 417 * @param string $component_name Name of the associated component. 418 * @param string $component_action Name of the associated action. 419 * @return int|false The number of rows updated. False on error. 420 */ 421 function bp_notifications_delete_notifications_by_item_ids( $user_id, $item_ids, $component_name, $component_action ) { 422 return BP_Notifications_Notification::delete_by_id_list( 423 'item_id', 424 $item_ids, 425 array( 426 'user_id' => $user_id, 427 'component_name' => $component_name, 428 'component_action' => $component_action 429 ) 430 ); 431 } 432 387 433 /** 388 434 * Delete all notifications by type. 389 435 * … … function bp_notifications_mark_notifications_from_user( $user_id, $component_nam 584 630 ); 585 631 } 586 632 633 /** 634 * Mark notifications read/unread by item ids and user. 635 * 636 * @since 10.0.0 637 * 638 * @param int $user_id ID of the user whose notifications are being deleted. 639 * @param array $item_ids ID of the associated item. 640 * @param string $component_name Name of the associated component. 641 * @param string $component_action Name of the associated action. 642 * @param int|false $is_new 0 for read, 1 for unread. 643 * @return int|false The number of rows updated. False on error. 644 */ 645 function bp_notifications_mark_notifications_by_item_ids( $user_id, $item_ids, $component_name, $component_action, $is_new = false ) { 646 return BP_Notifications_Notification::update_id_list( 647 'item_id', 648 $item_ids, 649 array( 650 'is_new' => $is_new, 651 ), 652 array( 653 'user_id' => $user_id, 654 'component_name' => $component_name, 655 'component_action' => $component_action 656 ) 657 ); 658 } 659 660 /** 661 * Mark notifications read/unread by item ids. 662 * 663 * @since 10.0.0 664 * 665 * @param array $ids IDs of the associated notification items. 666 * @param string $component_name Name of the associated component. 667 * @param string $component_action Name of the associated action. 668 * @param int|false $is_new 0 for read, 1 for unread. 669 * @return int|false The number of rows updated. False on error. 670 */ 671 function bp_notifications_mark_notifications_by_ids( $ids, $component_name, $component_action, $is_new = false ) { 672 return BP_Notifications_Notification::update_id_list( 673 'id', 674 $ids, 675 array( 676 'is_new' => $is_new, 677 ), 678 array( 679 'component_name' => $component_name, 680 'component_action' => $component_action 681 ) 682 ); 683 } 684 587 685 /** Helpers *******************************************************************/ 588 686 589 687 /** -
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 bc3b49821..e746351cc 100644
a b class BP_Notifications_Notification { 910 910 ); 911 911 } 912 912 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 $conditions[] = "{$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 913 977 /** 914 978 * Delete notifications. 915 979 * … … class BP_Notifications_Notification { 940 1004 return self::_delete( $where['data'], $where['format'] ); 941 1005 } 942 1006 1007 /** 1008 * Delete notifications using a list of ids/items_ids. 1009 * 1010 * @since 10.0.0 1011 * 1012 * @param string $field The name of the db field of the items to update. 1013 * Possible values are `id` or `item_id`. 1014 * @param int[] $items The list of items to update. 1015 * @param array $where The WHERE params as passed to wpdb::update(). 1016 * Typically consists of array( 'ID' => $id ) to specify the ID 1017 * of the item being updated. See {@link wpdb::update()}. 1018 * @return int|false The number of rows updated. False on error. 1019 */ 1020 public static function delete_by_id_list( $field, $items = array(), $where = array() ) { 1021 global $wpdb; 1022 $bp = buddypress(); 1023 1024 $supported_fields = array( 'id', 'item_id' ); 1025 1026 if ( false === in_array( $field, $supported_fields, true ) ) { 1027 return false; 1028 } 1029 1030 if ( ! is_array( $items ) || ! is_array( $where ) ) { 1031 return false; 1032 } 1033 1034 $where = self::get_query_clauses( $where ); 1035 1036 $conditions = array(); 1037 $values = array(); 1038 1039 $_items = implode( ',', wp_parse_id_list( $items ) ); 1040 $conditions[] = "{$field} IN ({$_items})"; 1041 1042 foreach ( $where['data'] as $field => $value ) { 1043 $index = array_search( $field, array_keys( $where['data'] ) ); 1044 $format = $where['format'][ $index ]; 1045 1046 $conditions[] = "{$field} = {$format}"; 1047 $values[] = $value; 1048 } 1049 1050 $conditions = implode( ' AND ', $conditions ); 1051 1052 $sql = "DELETE FROM {$bp->notifications->table_name} WHERE {$conditions}"; 1053 1054 return $wpdb->query( $wpdb->prepare( $sql, $values ) ); 1055 } 1056 943 1057 /** Convenience methods ***************************************************/ 944 1058 945 1059 /**