Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/13/2021 03:16:36 PM (3 years ago)
Author:
imath
Message:

Add a new param to bp_activity_get() to only get the number of items

Using the count_total_only parameter with bp_activity_get() will only run the query to count the number of activity items. Using this new parameter into the Activity WP Admin Screen improves the performance of the query to get the total number of activities.

Props oztaser

Fixes #8591

File:
1 edited

Legend:

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

    r13108 r13146  
    342342     * @since 2.4.0 Introduced the `$fields` parameter.
    343343     * @since 2.9.0 Introduced the `$order_by` parameter.
     344     * @since 10.0.0 Introduced the `$count_total_only` parameter.
    344345     *
    345346     * @see BP_Activity_Activity::get_filter_sql() for a description of the
     
    373374     *     @type string|bool  $count_total       If true, an additional DB query is run to count the total activity items
    374375     *                                           for the query. Default: false.
     376     *     @type bool         $count_total_only  If true, only the DB query to count the total activity items is run.
     377     *                                           Default: false.
    375378     * }
    376379     * @return array The array returned has two keys:
     
    436439                'update_meta_cache' => true,            // Whether or not to update meta cache.
    437440                'count_total'       => false,           // Whether or not to use count_total.
     441                'count_total_only'  => false,           // Whether to only get the total count.
    438442            )
    439443        );
     
    630634        );
    631635
     636        // Init the activity list.
     637        $activities     = array();
     638        $only_get_count = (bool) $r['count_total_only'];
     639
    632640        /**
    633641         * Filters if BuddyPress should use legacy query structure over current structure for version 2.0+.
     
    641649         * @param array                $r     Parsed arguments passed into method.
    642650         */
    643         if ( apply_filters( 'bp_use_legacy_activity_query', false, __METHOD__, $r ) ) {
     651        if ( ! $only_get_count && apply_filters( 'bp_use_legacy_activity_query', false, __METHOD__, $r ) ) {
    644652
    645653            // Legacy queries joined against the user table.
     
    693701            }
    694702
    695         } else {
     703        } elseif ( ! $only_get_count ) {
    696704            // Query first for activity IDs.
    697705            $activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY {$order_by} {$sort}, a.id {$sort}";
     
    746754        }
    747755
    748         if ( 'ids' !== $r['fields'] ) {
     756        if ( $activities && 'ids' !== $r['fields'] ) {
    749757            // Get the fullnames of users so we don't have to query in the loop.
    750758            $activities = self::append_user_fullnames( $activities );
     
    773781        $retval['activities'] = $activities;
    774782
    775         // If $max is set, only return up to the max results.
    776         if ( ! empty( $r['count_total'] ) ) {
    777 
     783        // Only query the count total if requested.
     784        if ( ! empty( $r['count_total'] ) || $only_get_count ) {
    778785            /**
    779786             * Filters the total activities MySQL statement.
     
    786793             */
    787794            $total_activities_sql = apply_filters( 'bp_activity_total_activities_sql', "SELECT count(DISTINCT a.id) FROM {$bp->activity->table_name} a {$join_sql} {$where_sql}", $where_sql, $sort );
     795
     796            /*
     797             * Queries that include 'last_activity' are cached separately,
     798             * since they are generally much less long-lived.
     799             */
     800            if ( preg_match( '/a\.type NOT IN \([^\)]*\'last_activity\'[^\)]*\)/', $total_activities_sql ) ) {
     801                $cache_group = 'bp_activity';
     802            } else {
     803                $cache_group = 'bp_activity_with_last_activity';
     804            }
     805
    788806            $cached = bp_core_get_incremented_cache( $total_activities_sql, $cache_group );
    789807            if ( false === $cached ) {
     
    794812            }
    795813
    796             if ( !empty( $r['max'] ) ) {
     814            // If $max is set, only return up to the max results.
     815            if ( ! empty( $r['max'] ) ) {
    797816                if ( (int) $total_activities > (int) $r['max'] ) {
    798817                    $total_activities = $r['max'];
Note: See TracChangeset for help on using the changeset viewer.