Ticket #8591: 8591.2.patch
File 8591.2.patch, 9.9 KB (added by , 3 years ago) |
---|
-
src/bp-activity/bp-activity-functions.php
diff --git src/bp-activity/bp-activity-functions.php src/bp-activity/bp-activity-functions.php index e8487a78f..3b7187b67 100644
function bp_activity_get( $args = '' ) { 1783 1783 'per_page' => false, // results per page. 1784 1784 'sort' => 'DESC', // sort ASC or DESC. 1785 1785 'display_comments' => false, // False for no comments. 'stream' for within stream display, 'threaded' for below each activity item. 1786 1787 1786 'search_terms' => false, // Pass search terms as a string. 1788 1787 'meta_query' => false, // Filter by activity meta. See WP_Meta_Query for format. 1789 1788 'date_query' => false, // Filter by date. See first parameter of WP_Date_Query for format. 1790 1789 'filter_query' => false, 1791 1790 'show_hidden' => false, // Show activity items that are hidden site-wide? 1792 1791 '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. 1795 1793 'spam' => 'ham_only', // 'ham_only' (default), 'spam_only' or 'all'. 1796 1794 'update_meta_cache' => true, 1797 1795 'count_total' => false, 1796 'count_total_only' => false, 1798 1797 'scope' => false, 1799 1798 1800 1799 /** … … function bp_activity_get( $args = '' ) { 1812 1811 'activity_get' 1813 1812 ); 1814 1813 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 ); 1835 1837 1836 1838 /** 1837 1839 * Filters the requested activity item(s). -
src/bp-activity/classes/class-bp-activity-activity.php
diff --git src/bp-activity/classes/class-bp-activity-activity.php src/bp-activity/classes/class-bp-activity-activity.php index cf084c16f..ebd28e5d7 100644
class BP_Activity_Activity { 341 341 * @since 1.2.0 342 342 * @since 2.4.0 Introduced the `$fields` parameter. 343 343 * @since 2.9.0 Introduced the `$order_by` parameter. 344 * @since 10.0.0 Introduced the `$count_total_only` parameter. 344 345 * 345 346 * @see BP_Activity_Activity::get_filter_sql() for a description of the 346 347 * 'filter' parameter. … … class BP_Activity_Activity { 372 373 * @type bool $update_meta_cache Whether to pre-fetch metadata for queried activity items. Default: true. 373 374 * @type string|bool $count_total If true, an additional DB query is run to count the total activity items 374 375 * 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. 375 378 * } 376 379 * @return array The array returned has two keys: 377 380 * - 'total' is the count of located activities … … class BP_Activity_Activity { 435 438 'spam' => 'ham_only', // Spam status. 436 439 'update_meta_cache' => true, // Whether or not to update meta cache. 437 440 'count_total' => false, // Whether or not to use count_total. 441 'count_total_only' => false, // Whether to only get the total count. 438 442 ) 439 443 ); 440 444 … … class BP_Activity_Activity { 629 633 'has_more_items' => null, 630 634 ); 631 635 636 // Init the activity list. 637 $activities = array(); 638 $only_get_count = (bool) $r['count_total_only']; 639 632 640 /** 633 641 * Filters if BuddyPress should use legacy query structure over current structure for version 2.0+. 634 642 * … … class BP_Activity_Activity { 640 648 * @param BP_Activity_Activity $value Current method being called. 641 649 * @param array $r Parsed arguments passed into method. 642 650 */ 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 ) ) { 644 652 645 653 // Legacy queries joined against the user table. 646 654 $select_sql = "SELECT DISTINCT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name"; … … class BP_Activity_Activity { 692 700 $activities[ $i ]->is_spam = (int) $ac->is_spam; 693 701 } 694 702 695 } else {703 } elseif ( ! $only_get_count ) { 696 704 // Query first for activity IDs. 697 705 $activity_ids_sql = "{$select_sql} {$from_sql} {$join_sql} {$where_sql} ORDER BY {$order_by} {$sort}, a.id {$sort}"; 698 706 … … class BP_Activity_Activity { 745 753 } 746 754 } 747 755 748 if ( 'ids' !== $r['fields'] ) {756 if ( $activities && 'ids' !== $r['fields'] ) { 749 757 // Get the fullnames of users so we don't have to query in the loop. 750 758 $activities = self::append_user_fullnames( $activities ); 751 759 … … class BP_Activity_Activity { 772 780 773 781 $retval['activities'] = $activities; 774 782 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 ) { 778 785 /** 779 786 * Filters the total activities MySQL statement. 780 787 * … … class BP_Activity_Activity { 785 792 * @param string $sort Sort direction for query. 786 793 */ 787 794 $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 788 806 $cached = bp_core_get_incremented_cache( $total_activities_sql, $cache_group ); 789 807 if ( false === $cached ) { 790 808 $total_activities = $wpdb->get_var( $total_activities_sql ); … … class BP_Activity_Activity { 793 811 $total_activities = $cached; 794 812 } 795 813 796 if ( !empty( $r['max'] ) ) { 814 // If $max is set, only return up to the max results. 815 if ( ! empty( $r['max'] ) ) { 797 816 if ( (int) $total_activities > (int) $r['max'] ) { 798 817 $total_activities = $r['max']; 799 818 } -
src/bp-activity/classes/class-bp-activity-list-table.php
diff --git src/bp-activity/classes/class-bp-activity-list-table.php src/bp-activity/classes/class-bp-activity-list-table.php index e735f6445..9494f2144 100644
class BP_Activity_List_Table extends WP_List_Table { 156 156 'display_comments' => 'stream', 157 157 'show_hidden' => true, 158 158 'spam' => 'spam_only', 159 'count_total ' => 'count_query',159 'count_total_only' => true, 160 160 ) ); 161 161 $this->spam_count = $spams['total']; 162 162 unset( $spams ); … … class BP_Activity_List_Table extends WP_List_Table { 214 214 } elseif ( 'single' !== $this->view ) { 215 215 $count_activities = bp_activity_get( 216 216 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, 220 220 ) 221 221 ); 222 222 -
tests/phpunit/testcases/activity/functions.php
diff --git tests/phpunit/testcases/activity/functions.php tests/phpunit/testcases/activity/functions.php index 4e1700f02..50e584af5 100644
Bar!'; 1974 1974 $this->assertEqualSets( [ $a1 ], bp_activity_get_user_favorites( $u1 ) ); 1975 1975 $this->assertSame( $latest_update, bp_get_user_meta( $u1, 'bp_latest_update', true ) ); 1976 1976 } 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 } 1977 2002 }