Skip to:
Content

BuddyPress.org

Changeset 13146


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

Location:
trunk
Files:
4 edited

Legend:

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

    r13135 r13146  
    17841784            'sort'              => 'DESC',       // sort ASC or DESC.
    17851785            'display_comments'  => false,        // False for no comments. 'stream' for within stream display, 'threaded' for below each activity item.
    1786 
    17871786            'search_terms'      => false,        // Pass search terms as a string.
    17881787            'meta_query'        => false,        // Filter by activity meta. See WP_Meta_Query for format.
     
    17911790            'show_hidden'       => false,        // Show activity items that are hidden site-wide?
    17921791            'exclude'           => false,        // Comma-separated list of activity IDs to exclude.
    1793             'in'                => false,        // Comma-separated list or array of activity IDs to which you.
    1794                                                 // want to limit the query.
     1792            'in'                => false,        // Comma-separated list or array of activity IDs to which you want to limit the query.
    17951793            'spam'              => 'ham_only',   // 'ham_only' (default), 'spam_only' or 'all'.
    17961794            'update_meta_cache' => true,
    17971795            'count_total'       => false,
     1796            'count_total_only'  => false,
    17981797            'scope'             => false,
    17991798
     
    18131812    );
    18141813
    1815     $activity = BP_Activity_Activity::get( array(
    1816         'page'              => $r['page'],
    1817         'per_page'          => $r['per_page'],
    1818         'max'               => $r['max'],
    1819         'sort'              => $r['sort'],
    1820         'search_terms'      => $r['search_terms'],
    1821         'meta_query'        => $r['meta_query'],
    1822         'date_query'        => $r['date_query'],
    1823         'filter_query'      => $r['filter_query'],
    1824         'filter'            => $r['filter'],
    1825         'scope'             => $r['scope'],
    1826         'display_comments'  => $r['display_comments'],
    1827         'show_hidden'       => $r['show_hidden'],
    1828         'exclude'           => $r['exclude'],
    1829         'in'                => $r['in'],
    1830         'spam'              => $r['spam'],
    1831         'update_meta_cache' => $r['update_meta_cache'],
    1832         'count_total'       => $r['count_total'],
    1833         'fields'            => $r['fields'],
    1834     ) );
     1814    $activity = BP_Activity_Activity::get(
     1815        array(
     1816            'page'              => $r['page'],
     1817            'per_page'          => $r['per_page'],
     1818            'max'               => $r['max'],
     1819            'sort'              => $r['sort'],
     1820            'search_terms'      => $r['search_terms'],
     1821            'meta_query'        => $r['meta_query'],
     1822            'date_query'        => $r['date_query'],
     1823            'filter_query'      => $r['filter_query'],
     1824            'filter'            => $r['filter'],
     1825            'scope'             => $r['scope'],
     1826            'display_comments'  => $r['display_comments'],
     1827            'show_hidden'       => $r['show_hidden'],
     1828            'exclude'           => $r['exclude'],
     1829            'in'                => $r['in'],
     1830            'spam'              => $r['spam'],
     1831            'update_meta_cache' => $r['update_meta_cache'],
     1832            'count_total'       => $r['count_total'],
     1833            'count_total_only'  => $r['count_total_only'],
     1834            'fields'            => $r['fields'],
     1835        )
     1836    );
    18351837
    18361838    /**
  • 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'];
  • trunk/src/bp-activity/classes/class-bp-activity-list-table.php

    r12659 r13146  
    157157            'show_hidden'      => true,
    158158            'spam'             => 'spam_only',
    159             'count_total'      => 'count_query',
     159            'count_total_only' => true,
    160160        ) );
    161161        $this->spam_count = $spams['total'];
     
    215215            $count_activities = bp_activity_get(
    216216                array(
    217                     'fields'      => 'ids',
    218                     'show_hidden' => true,
    219                     'count_total' => 'count_query',
     217                    'fields'           => 'ids',
     218                    'show_hidden'      => true,
     219                    'count_total_only' => true,
    220220                )
    221221            );
  • trunk/tests/phpunit/testcases/activity/functions.php

    r13128 r13146  
    19751975        $this->assertSame( $latest_update, bp_get_user_meta( $u1, 'bp_latest_update', true ) );
    19761976    }
     1977
     1978    /**
     1979     * @ticket BP8591
     1980     */
     1981    public function test_activity_admin_screen_count_activities() {
     1982        $u1 = self::factory()->user->create();
     1983        $a1 = self::factory()->activity->create_many(
     1984            5,
     1985            array(
     1986                'user_id'   => $u1,
     1987                'component' => 'activity',
     1988                'type'      => 'activity_update',
     1989            )
     1990        );
     1991        bp_update_user_last_activity( $u1, date( 'Y-m-d H:i:s', bp_core_current_time( true, 'timestamp' ) ) );
     1992
     1993        $count_activities = bp_activity_get(
     1994            array(
     1995                'show_hidden'      => true,
     1996                'count_total_only' => true,
     1997            )
     1998        );
     1999
     2000        $this->assertTrue( 5 === (int) $count_activities['total'] );
     2001    }
    19772002}
Note: See TracChangeset for help on using the changeset viewer.