Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/19/2021 03:58:09 PM (3 years ago)
Author:
imath
Message:

Improve SQL performance when updating/deleting a list of notifications

Instead of looping into a list of notifications to run an update/delete query for each notification, update/delete a list of notifications using a single query. Use primarly this change for message thread notifications and bulk notification actions.

Mainly introduces 2 new static methods to BP_Notifications_Notification:

  • BP_Notifications_Notification::update_id_list() let you update a list of notifications using their notification IDs or component item IDs.
  • BP_Notifications_Notification::delete_id_list() let you delete a list of notifications using their notification IDs or component item IDs.

Props oztaser

Fixes #8426

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-messages/classes/class-bp-messages-thread.php

    r13108 r13112  
    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' );
     
    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 );
    785 
    786         return $retval;
     786        do_action( 'messages_thread_mark_as_read', $thread_id, $user_id, $num_rows );
     787
     788        return $num_rows;
    787789    }
    788790
     
    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' );
     
    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 );
    831 
    832         return $retval;
     832        do_action( 'messages_thread_mark_as_unread', $thread_id, $user_id, $num_rows );
     833
     834        return $num_rows;
    833835    }
    834836
Note: See TracChangeset for help on using the changeset viewer.