Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
01/02/2015 04:32:04 AM (11 years ago)
Author:
r-a-y
Message:

Add $user_id parameter to bp_groups_user_can_send_invites().

This allows developers to check if a specific user can send group invites
rather than just do checks against the logged-in user.

This commit also:

  • Updates the PHPDoc for the function and the filter hook.
  • Adds unit tests.

Props dcavins.

Fixes #6031.

File:
1 edited

Legend:

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

    r9291 r9293  
    18821882
    18831883/**
    1884  * Can the logged-in user send invitations in the specified group?
     1884 * Can a user send invitations in the specified group?
    18851885 *
    18861886 * @since BuddyPress (1.5.0)
    1887  *
    1888  * @param int $group_id Optional. The ID of the group whose status you want to
    1889  *        check. Default: the ID of the current group.
    1890  * @return bool $can_send_invites
    1891  */
    1892 function bp_groups_user_can_send_invites( $group_id = false ) {
    1893     global $bp;
    1894 
     1887 * @since BuddyPress (2.2.0) Added the $user_id parameter.
     1888 *
     1889 * @param int $group_id The group ID to check.
     1890 * @param int $user_id  The user ID to check.
     1891 * @return bool
     1892 */
     1893function bp_groups_user_can_send_invites( $group_id = 0, $user_id = 0 ) {
    18951894    $can_send_invites = false;
    18961895    $invite_status    = false;
    18971896
    1898     if ( is_user_logged_in() ) {
    1899         if ( bp_current_user_can( 'bp_moderate' ) ) {
    1900             // Super admins can always send invitations
     1897    // If $user_id isn't specified, we check against the logged-in user.
     1898    if ( ! $user_id ) {
     1899        $user_id = bp_loggedin_user_id();
     1900    }
     1901
     1902    // If $group_id isn't specified, use existing one if available.
     1903    if ( ! $group_id ) {
     1904        $group_id = bp_get_current_group_id();
     1905    }
     1906
     1907    if ( $user_id ) {
     1908        // Users with the 'bp_moderate' cap can always send invitations
     1909        if ( user_can( $user_id, 'bp_moderate' ) ) {
    19011910            $can_send_invites = true;
    1902 
    19031911        } else {
    1904             // If no $group_id is provided, default to the current group id
    1905             if ( !$group_id )
    1906                 $group_id = isset( $bp->groups->current_group->id ) ? $bp->groups->current_group->id : 0;
    1907 
    1908             // If no group has been found, bail
    1909             if ( !$group_id )
    1910                 return false;
    1911 
    19121912            $invite_status = bp_group_get_invite_status( $group_id );
    1913             if ( !$invite_status )
    1914                 return false;
    19151913
    19161914            switch ( $invite_status ) {
    19171915                case 'admins' :
    1918                     if ( groups_is_user_admin( bp_loggedin_user_id(), $group_id ) )
     1916                    if ( groups_is_user_admin( $user_id, $group_id ) ) {
    19191917                        $can_send_invites = true;
     1918                    }
    19201919                    break;
    19211920
    19221921                case 'mods' :
    1923                     if ( groups_is_user_mod( bp_loggedin_user_id(), $group_id ) || groups_is_user_admin( bp_loggedin_user_id(), $group_id ) )
     1922                    if ( groups_is_user_mod( $user_id, $group_id ) || groups_is_user_admin( $user_id, $group_id ) ) {
    19241923                        $can_send_invites = true;
     1924                    }
    19251925                    break;
    19261926
    19271927                case 'members' :
    1928                     if ( groups_is_user_member( bp_loggedin_user_id(), $group_id ) )
     1928                    if ( groups_is_user_member( $user_id, $group_id ) ) {
    19291929                        $can_send_invites = true;
     1930                    }
    19301931                    break;
    19311932            }
     
    19331934    }
    19341935
    1935     return apply_filters( 'bp_groups_user_can_send_invites', $can_send_invites, $group_id, $invite_status );
     1936    /**
     1937     * Filters whether a user can send invites in a group.
     1938     *
     1939     * @since BuddyPress (1.5.0)
     1940     * @since BuddyPress (2.2.0) Added the $user_id parameter.
     1941     *
     1942     * @param bool $can_send_invites Whether the user can send invites
     1943     * @param int  $group_id         The group ID being checked
     1944     * @param bool $invite_status    The group's current invite status
     1945     * @param int  $user_id          The user ID being checked
     1946     */
     1947    return apply_filters( 'bp_groups_user_can_send_invites', $can_send_invites, $group_id, $invite_status, $user_id );
    19361948}
    19371949
Note: See TracChangeset for help on using the changeset viewer.