Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/09/2021 02:01:40 PM (3 years ago)
Author:
espellcaste
Message:

Improving the group member count routine and the function helper.

The group member count routine was updated to avoid direct, uncached, SQL query and unnecessary cache refresh when a group's Members page was viewed.

is now being used to get the group member count which takes into account users' existence in the site,

the query is now cached and filterable.

was also updated to get the current group from if available.

Props imath
Fixes #7614 and see #6749

File:
1 edited

Legend:

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

    r13101 r13103  
    7878 *                          Possible values are `'id'` or `'slug'`.
    7979 * @param string|int $value (Required) A value for the $field. A Group ID or slug.
    80  * @return BP_Groups_Group|false The Group object if found, false otherwise.
     80 * @return BP_Groups_Group|bool The Group object if found, false otherwise.
    8181 */
    8282function bp_get_group_by( $field, $value ) {
     
    104104 * @since 10.0.0
    105105 *
     106 * @global BP_Groups_Template $groups_template Groups template object.
     107 *
    106108 * @param false|int|string|BP_Groups_Group $group (Optional) The Group ID, the Group Slug or the Group object.
    107109 *                                                Default: false.
    108  * @return BP_Groups_Group|false                  The Group object if found, false otherwise.
     110 * @return BP_Groups_Group|bool The Group object if found, false otherwise.
    109111 */
    110112function bp_get_group( $group = false ) {
     
    115117    if ( $group instanceof BP_Groups_Group ) {
    116118        $group_obj = $group;
    117     } elseif ( is_string( $group ) ) {
    118         $group_obj = bp_get_group_by( 'slug', $group );
    119     } elseif ( is_numeric( $group ) ) {
    120         $group_obj = bp_get_group_by( 'id', $group );
    121     } elseif ( isset( $groups_template->group ) && is_object( $groups_template->group ) ) {
     119
     120        // Nothing requested? Let's use the current Group of the Groups Loop, if available.
     121    } elseif ( ! $group && isset( $groups_template->group ) && is_object( $groups_template->group ) ) {
    122122        $group_obj = $groups_template->group;
     123    } else {
     124        $current_group = null;
     125
     126        // Let's get the current group if we can.
     127        if ( did_action( 'bp_groups_set_current_group' ) ) {
     128            $current_group = groups_get_current_group();
     129        }
     130
     131        $field = '';
     132        if ( is_string( $group ) ) {
     133            $field = 'slug';
     134        } elseif ( is_numeric( $group ) ) {
     135            $field = 'id';
     136            $group = (int) $group;
     137        }
     138
     139        // Let's use the current Group if it matches with the requested field value.
     140        if ( isset( $current_group->{$field} ) && $current_group->{$field} === $group ) {
     141            $group_obj = $current_group;
     142        } else {
     143            $group_obj = bp_get_group_by( $field, $group );
     144        }
    123145    }
    124146
     
    482504    $bp = buddypress();
    483505
    484     return in_array( $status, (array) $bp->groups->valid_status );
     506    return in_array( $status, (array) $bp->groups->valid_status, true );
    485507}
    486508
     
    534556 *
    535557 * @param string $group_slug The group's slug.
    536  * @return int|null The group ID on success; null on failure.
     558 * @return int|null The group ID on success, null on failure.
    537559 */
    538560function groups_get_id( $group_slug ) {
     
    546568 *
    547569 * @param string $group_slug The group's slug.
    548  * @return int|null The group ID on success; null on failure.
     570 * @return int|null The group ID on success, null on failure.
    549571 */
    550572function groups_get_id_by_previous_slug( $group_slug ) {
     
    685707    groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
    686708}
    687 add_action( 'groups_join_group',           'groups_update_last_activity' );
    688 add_action( 'groups_leave_group',          'groups_update_last_activity' );
    689 add_action( 'groups_created_group',        'groups_update_last_activity' );
     709add_action( 'groups_join_group', 'groups_update_last_activity' );
     710add_action( 'groups_leave_group', 'groups_update_last_activity' );
     711add_action( 'groups_created_group', 'groups_update_last_activity' );
    690712
    691713/** General Group Functions ***************************************************/
     
    824846 *
    825847 * @since 1.2.3
    826  *
    827  * @param int $group_id Group ID.
    828  * @return int Count of confirmed members for the group.
    829  */
    830 function groups_get_total_member_count( $group_id ) {
    831     return BP_Groups_Group::get_total_member_count( $group_id );
     848 * @since 10.0.0 Updated to use `bp_get_group`.
     849 *
     850 * @param int|string|BP_Groups_Group $group      The Group ID, the Group Slug or the Group object.
     851 * @param bool                       $skip_cache Optional. Skip grabbing from cache. Defaults to false.
     852 * @return int|bool Count of confirmed members for the group. False if group doesn't exist.
     853 */
     854function groups_get_total_member_count( $group, $skip_cache = false ) {
     855
     856    $group = bp_get_group( $group );
     857
     858    if ( empty( $group->id ) ) {
     859        return false;
     860    }
     861
     862    return (int) BP_Groups_Group::get_total_member_count( $group->id, (bool) $skip_cache );
    832863}
    833864
     
    919950 *
    920951 * @since 1.2.0
    921  *
     952 * @since 10.0.0 Added the `$skip_cache` parameter.
     953 *
     954 * @param bool $skip_cache Optional. Skip getting count from cache.
     955 *                         Defaults to false.
    922956 * @return int
    923957 */
    924 function groups_get_total_group_count() {
    925     $count = wp_cache_get( 'bp_total_group_count', 'bp' );
    926 
    927     if ( false === $count ) {
    928         $count = BP_Groups_Group::get_total_group_count();
    929         wp_cache_set( 'bp_total_group_count', $count, 'bp' );
    930     }
    931 
    932     return $count;
     958function groups_get_total_group_count( $skip_cache = false ) {
     959    return (int) BP_Groups_Group::get_total_group_count( $skip_cache );
    933960}
    934961
     
    950977function groups_get_user_groups( $user_id = 0, $pag_num = 0, $pag_page = 0 ) {
    951978
    952     if ( empty( $user_id ) )
     979    if ( empty( $user_id ) ) {
    953980        $user_id = bp_displayed_user_id();
     981    }
    954982
    955983    return BP_Groups_Member::get_group_ids( $user_id, $pag_num, $pag_page );
     
    11791207 * @since 1.5.0
    11801208 *
    1181  * @return BP_Groups_Group The current group object.
     1209 * @global BuddyPress $bp The one true BuddyPress instance.
     1210 *
     1211 * @return BP_Groups_Group|bool The current group object or false.
    11821212 */
    11831213function groups_get_current_group() {
     
    11931223     * @since 1.5.0
    11941224     *
    1195      * @param BP_Groups_Group $current_group Current BP_Groups_Group object.
     1225     * @param BP_Groups_Group|bool $current_group Current BP_Groups_Group object or false.
    11961226     */
    11971227    return apply_filters( 'groups_get_current_group', $current_group );
Note: See TracChangeset for help on using the changeset viewer.