Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/22/2021 12:38:43 AM (4 years ago)
Author:
espellcaste
Message:

We are introducing in this commit two new helper functions to get a group: bp_get_group_by and bp_get_group. The former allows for getting a group by id/ID or slug.
And the latter allows for getting a group by id/ID, group object, slug. And when used in the context of a Group loop built by the BP_Groups_Template class, it defaults to the Group being iterated on.

  • The new helper functions were applied to several functions in the Group component.
  • bp_group_is_visible was updated to check against a specific user ID. Previously, only the current logged in user was verified.
  • bp_get_group_avatar was updated to use the new functions and new arguments that are passed to bp_core_fetch_avatar were added.
  • PHPDoc were updated to reflect those changes.

Props imath, boonebgorges and DJPaul

Fixes #6749 (trunk)

File:
1 edited

Legend:

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

    r12991 r13085  
    6868     */
    6969    return apply_filters( 'groups_get_group', $group );
     70}
     71
     72/**
     73 * Retrieve group by a given field.
     74 *
     75 * @since 10.0.0
     76 *
     77 * @param string     $field (Required) The field to use to retrieve the group.
     78 *                          Possible values are `'id'` or `'slug'`.
     79 * @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.
     81 */
     82function bp_get_group_by( $field, $value ) {
     83    $group_id = $value;
     84
     85    if ( 'slug' === $field && is_string( $value ) ) {
     86        $group_id = groups_get_id( $value );
     87    }
     88
     89    $group = groups_get_group( array( 'group_id' => (int) $group_id ) );
     90
     91    if ( empty( $group->id ) ) {
     92        return false;
     93    }
     94
     95    return $group;
     96}
     97
     98/**
     99 * Retrieve a Group.
     100 *
     101 * When used into the context of a Groups loop built by the `BP_Groups_Template` class, it defaults to the
     102 * Group being iterated on.
     103 *
     104 * @since 10.0.0
     105 *
     106 * @param false|int|string|BP_Groups_Group $group (Optional) The Group ID, the Group Slug or the Group object.
     107 *                                                Default: false.
     108 * @return BP_Groups_Group|false                  The Group object if found, false otherwise.
     109 */
     110function bp_get_group( $group = false ) {
     111    global $groups_template;
     112
     113    $group_obj = false;
     114
     115    if ( $group instanceof BP_Groups_Group ) {
     116        $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 ) ) {
     122        $group_obj = $groups_template->group;
     123    }
     124
     125    return $group_obj;
    70126}
    71127
Note: See TracChangeset for help on using the changeset viewer.