Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/05/2018 12:57:24 PM (7 years ago)
Author:
boonebgorges
Message:

Move notification-grouping logic to MySQL.

The notification-grouping logic required to show the collapsed Notifications
toolbar - "You have 3 pending friend requests", etc - is currently done in
PHP, in a manner that requires all notifications to be loaded into memory
and processed. On sites where users have large numbers of notifications, this
operation requires extensive resources. By leaving the grouping to MySQL,
we can speed up processing in almost all situations.

This changeset also adds specific caching for grouped notifications.

Props m_uysl.
See #7130.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-notifications/classes/class-bp-notifications-notification.php

    r11840 r11888  
    11321132        return self::update( $update_args, $where_args );
    11331133    }
     1134
     1135    /**
     1136     * Get a user's unread notifications, grouped by component and action.
     1137     *
     1138     * Multiple notifications of the same type (those that share the same component_name
     1139     * and component_action) are collapsed for formatting as "You have 5 pending
     1140     * friendship requests", etc. See bp_notifications_get_notifications_for_user().
     1141     * For a full-fidelity list of user notifications, use
     1142     * bp_notifications_get_all_notifications_for_user().
     1143     *
     1144     * @since 3.0.0
     1145     *
     1146     * @param int $user_id ID of the user whose notifications are being fetched.
     1147     * @return array Notifications items for formatting into a list.
     1148     */
     1149    public static function get_grouped_notifications_for_user( $user_id ) {
     1150        global $wpdb;
     1151
     1152        // Load BuddyPress.
     1153        $bp = buddypress();
     1154
     1155        // SELECT.
     1156        $select_sql = "SELECT id, user_id, item_id, secondary_item_id, component_name, component_action, date_notified, is_new, COUNT(id) as total_count ";
     1157
     1158        // FROM.
     1159        $from_sql = "FROM {$bp->notifications->table_name} n ";
     1160
     1161        // WHERE.
     1162        $where_sql = self::get_where_sql( array(
     1163            'user_id'        => $user_id,
     1164            'is_new'         => 1,
     1165            'component_name' => bp_notifications_get_registered_components(),
     1166        ), $select_sql, $from_sql );
     1167
     1168        // GROUP
     1169        $group_sql = "GROUP BY user_id, component_name, component_action";
     1170
     1171        // SORT
     1172        $order_sql = "ORDER BY date_notified desc";
     1173
     1174        // Concatenate query parts.
     1175        $sql = "{$select_sql} {$from_sql} {$where_sql} {$group_sql} {$order_sql}";
     1176
     1177        // Return the queried results.
     1178        return $wpdb->get_results( $sql );
     1179    }
    11341180}
Note: See TracChangeset for help on using the changeset viewer.