Skip to:
Content

BuddyPress.org

Changeset 13104


Ignore:
Timestamp:
09/09/2021 02:12:32 PM (6 weeks ago)
Author:
espellcaste
Message:

Update group-related functions so that they use the new bp_get_group helper.

groups_get_slug, groups_leave_group, groups_join_group, and groups_update_last_activity were updated
to get a group using the recently added function bp_get_group, allowing them to be fetched with more than just IDs.

See #7614

File:
1 edited

Legend:

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

    r13103 r13104  
    538538
    539539/**
    540  * Get a group slug by its ID.
    541  *
    542  * @since 1.0.0
    543  *
    544  * @param int $group_id The numeric ID of the group.
    545  * @return string The group's slug.
    546  */
    547 function groups_get_slug( $group_id ) {
    548     $group = groups_get_group( $group_id );
    549     return !empty( $group->slug ) ? $group->slug : '';
     540 * Get slug from a group.
     541 *
     542 * @since 1.0.0
     543 * @since 10.0.0 Updated to use `bp_get_group`.
     544 *
     545 * @param int|string|BP_Groups_Group $group The Group ID, the Group Slug or the Group object.
     546 * @return bool|string The group's slug. False if group doesn't exist.
     547 */
     548function groups_get_slug( $group ) {
     549
     550    $group = bp_get_group( $group );
     551
     552    if ( empty( $group->id ) ) {
     553        return false;
     554    }
     555
     556    return ! empty( $group->slug ) ? $group->slug : '';
    550557}
    551558
     
    580587 *
    581588 * @since 1.0.0
    582  *
    583  * @param int $group_id ID of the group.
    584  * @param int $user_id  Optional. ID of the user. Defaults to the currently
    585  *                      logged-in user.
     589 * @since 10.0.0 Updated to use `bp_get_group`.
     590 *
     591 * @param int|string|BP_Groups_Group $group   The Group ID, the Group Slug or the Group object.
     592 * @param int                        $user_id Optional. ID of the user. Defaults to the currently
     593 *                                            logged-in user.
    586594 * @return bool True on success, false on failure.
    587595 */
    588 function groups_leave_group( $group_id, $user_id = 0 ) {
    589 
    590     if ( empty( $user_id ) )
     596function groups_leave_group( $group, $user_id = 0 ) {
     597
     598    $group = bp_get_group( $group );
     599
     600    if ( empty( $group->id ) ) {
     601        return false;
     602    }
     603
     604    if ( empty( $user_id ) ) {
    591605        $user_id = bp_loggedin_user_id();
     606    }
    592607
    593608    // Don't let single admins leave the group.
    594     if ( count( groups_get_group_admins( $group_id ) ) < 2 ) {
    595         if ( groups_is_user_admin( $user_id, $group_id ) ) {
     609    if ( count( groups_get_group_admins( $group->id ) ) < 2 ) {
     610        if ( groups_is_user_admin( $user_id, $group->id ) ) {
    596611            bp_core_add_message( __( 'As the only admin, you cannot leave the group.', 'buddypress' ), 'error' );
    597612            return false;
     
    599614    }
    600615
    601     if ( ! BP_Groups_Member::delete( $user_id, $group_id ) ) {
     616    if ( ! BP_Groups_Member::delete( $user_id, $group->id ) ) {
    602617        return false;
    603618    }
     
    609624     *
    610625     * @since 1.0.0
    611      *
    612      * @param int $group_id ID of the group.
    613      * @param int $user_id  ID of the user leaving the group.
     626     * @since 10.0.0 Updated to add the `$group` parameter.
     627     *
     628     * @param int             $group_id ID of the group.
     629     * @param int             $user_id  ID of the user leaving the group.
     630     * @param BP_Groups_Group $group    The group object.
    614631     */
    615     do_action( 'groups_leave_group', $group_id, $user_id );
     632    do_action( 'groups_leave_group', $group->id, $user_id, $group );
    616633
    617634    return true;
     
    622639 *
    623640 * @since 1.0.0
    624  *
    625  * @param int $group_id ID of the group.
    626  * @param int $user_id  Optional. ID of the user. Defaults to the currently
    627  *                      logged-in user.
     641 * @since 10.0.0 Updated to use `bp_get_group`.
     642 *
     643 * @param int|string|BP_Groups_Group $group   The Group ID, the Group Slug or the Group object.
     644 * @param int                        $user_id Optional. ID of the user. Defaults to the currently
     645 *                                            logged-in user.
    628646 * @return bool True on success, false on failure.
    629647 */
    630 function groups_join_group( $group_id, $user_id = 0 ) {
    631 
    632     if ( empty( $user_id ) )
     648function groups_join_group( $group, $user_id = 0 ) {
     649
     650    $group = bp_get_group( $group );
     651
     652    if ( empty( $group->id ) ) {
     653        return false;
     654    }
     655
     656    $group_id = $group->id;
     657
     658    if ( empty( $user_id ) ) {
    633659        $user_id = bp_loggedin_user_id();
     660    }
    634661
    635662    // Check if the user has an outstanding invite. If so, delete it.
    636     if ( groups_check_user_has_invite( $user_id, $group_id ) )
     663    if ( groups_check_user_has_invite( $user_id, $group_id ) ) {
    637664        groups_delete_invite( $user_id, $group_id );
     665    }
    638666
    639667    // Check if the user has an outstanding request. If so, delete it.
    640     if ( groups_check_for_membership_request( $user_id, $group_id ) )
     668    if ( groups_check_for_membership_request( $user_id, $group_id ) ) {
    641669        groups_delete_membership_request( null, $user_id, $group_id );
     670    }
    642671
    643672    // User is already a member, just return true.
    644     if ( groups_is_user_member( $user_id, $group_id ) )
     673    if ( groups_is_user_member( $user_id, $group_id ) ) {
    645674        return true;
    646 
    647     $new_member                = new BP_Groups_Member;
     675    }
     676
     677    $new_member                = new BP_Groups_Member();
    648678    $new_member->group_id      = $group_id;
    649679    $new_member->user_id       = $user_id;
     
    654684    $new_member->is_confirmed  = 1;
    655685
    656     if ( !$new_member->save() )
     686    if ( ! $new_member->save() ) {
    657687        return false;
    658 
    659     $bp = buddypress();
    660 
    661     if ( !isset( $bp->groups->current_group ) || !$bp->groups->current_group || $group_id != $bp->groups->current_group->id )
    662         $group = groups_get_group( $group_id );
    663     else
    664         $group = $bp->groups->current_group;
     688    }
    665689
    666690    // Record this in activity streams.
    667691    if ( bp_is_active( 'activity' ) ) {
    668         groups_record_activity( array(
    669             'type'    => 'joined_group',
    670             'item_id' => $group_id,
    671             'user_id' => $user_id,
    672         ) );
     692        groups_record_activity(
     693            array(
     694                'type'    => 'joined_group',
     695                'item_id' => $group_id,
     696                'user_id' => $user_id,
     697            )
     698        );
    673699    }
    674700
     
    677703     *
    678704     * @since 1.0.0
    679      *
    680      * @param int $group_id ID of the group.
    681      * @param int $user_id  ID of the user joining the group.
     705     * @since 10.0.0 Added the `$group` parameter.
     706     *
     707     * @param int             $group_id ID of the group.
     708     * @param int             $user_id  ID of the user joining the group.
     709     * @param BP_Groups_Group $group    The group object.
    682710     */
    683     do_action( 'groups_join_group', $group_id, $user_id );
     711    do_action( 'groups_join_group', $group_id, $user_id, $group );
    684712
    685713    return true;
     
    690718 *
    691719 * @since 1.0.0
    692  *
    693  * @param int $group_id Optional. The ID of the group whose last_activity is
    694  *                      being updated. Default: the current group's ID.
    695  * @return false|null False on failure.
    696  */
    697 function groups_update_last_activity( $group_id = 0 ) {
    698 
    699     if ( empty( $group_id ) ) {
    700         $group_id = buddypress()->groups->current_group->id;
    701     }
    702 
    703     if ( empty( $group_id ) ) {
     720 * @since 10.0.0 Updated to use `bp_get_group`.
     721 *
     722 * @param int|string|BP_Groups_Group $group The Group ID, the Group Slug or the Group object.
     723 *                                          Default: the current group's ID.
     724 * @return bool False on failure.
     725 */
     726function groups_update_last_activity( $group = 0 ) {
     727
     728    $group = bp_get_group( $group );
     729
     730    if ( empty( $group->id ) ) {
    704731        return false;
    705732    }
    706733
    707     groups_update_groupmeta( $group_id, 'last_activity', bp_core_current_time() );
     734    groups_update_groupmeta( $group->id, 'last_activity', bp_core_current_time() );
    708735}
    709736add_action( 'groups_join_group', 'groups_update_last_activity' );
Note: See TracChangeset for help on using the changeset viewer.