Skip to:
Content

BuddyPress.org

Changeset 13873


Ignore:
Timestamp:
05/25/2024 04:47:42 AM (13 months ago)
Author:
imath
Message:

Groups: improve group membership management functions

  • Make the functions handling the 5 available group membership actions (promote, demote, ban, unban, remove) usable outside of the BuddyPress Web version. Doing so, the BP REST API will be able to use them so that current hooks used to perform custom code/clear group cache are still fired within this specific context.
  • In BP Nouveau, the group members management interface (which is using the BP REST API) will soon benefit from this work once the BP REST API has been updated. The hook used to notify a group member has been promoted will then be fired and BP Nouveau behavior will be consistent with the BP Legacy one about group membership management.
  • Deprecate some misleading hooks, which were firing even if the group membership action had failed, in favor of new ones which do fire at the right "place".

Props needle

Fixes #9158

Location:
trunk
Files:
4 edited

Legend:

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

    r13808 r13873  
    20072007 *
    20082008 * @since 1.0.0
    2009  *
    2010  * @param int    $user_id  ID of the user.
    2011  * @param int    $group_id ID of the group.
    2012  * @param string $status   The new status. 'mod' or 'admin'.
     2009 * @since 14.0.0 Adds the `$group_admin_id` parameter.
     2010 *
     2011 * @param int    $user_id        ID of the user.
     2012 * @param int    $group_id       ID of the group.
     2013 * @param string $status         The new status. 'mod' or 'admin'.
     2014 * @param int    $group_admin_id Optional. The group admin user ID.
    20132015 * @return bool True on success, false on failure.
    20142016 */
    2015 function groups_promote_member( $user_id, $group_id, $status ) {
    2016 
    2017     if ( ! bp_is_item_admin() )
     2017function groups_promote_member( $user_id, $group_id, $status, $group_admin_id = 0 ) {
     2018    // Carry on using the item admin set by the Web version.
     2019    if ( ! $group_admin_id ) {
     2020        $user_can = bp_is_item_admin();
     2021
     2022        // Use the provided Group Admin ID (eg: during a REST API request).
     2023    } else {
     2024        $user_can = bp_current_user_can( 'bp_moderate' ) || groups_is_user_admin( $group_admin_id, $group_id );
     2025    }
     2026
     2027    if ( ! $user_can ) {
    20182028        return false;
     2029    }
    20192030
    20202031    $member = new BP_Groups_Member( $user_id, $group_id );
    20212032
    20222033    // Don't use this action. It's deprecated as of BuddyPress 1.6.
    2023     do_action( 'groups_premote_member', $group_id, $user_id, $status );
     2034    do_action_deprecated( 'groups_premote_member', array( $group_id, $user_id, $status ), '1.6' );
    20242035
    20252036    /**
     
    20342045    do_action( 'groups_promote_member', $group_id, $user_id, $status );
    20352046
    2036     return $member->promote( $status );
     2047    if ( ! $member->promote( $status ) ) {
     2048        return false;
     2049    }
     2050
     2051    /**
     2052     * Fires once the group member has been successfully promoted.
     2053     *
     2054     * @since 14.0.0
     2055     *
     2056     * @param int $user_id  ID of the user being promoted.
     2057     * @param int $group_id ID of the group being promoted in.
     2058     */
     2059    do_action( 'group_member_promoted', $user_id, $group_id );
     2060
     2061    return true;
    20372062}
    20382063
     
    20412066 *
    20422067 * @since 1.0.0
     2068 * @since 14.0.0 Adds the `$group_admin_id` parameter.
    20432069 *
    20442070 * @param int $user_id  ID of the user.
    20452071 * @param int $group_id ID of the group.
     2072 * @param int $group_admin_id Optional. The group admin user ID.
    20462073 * @return bool True on success, false on failure.
    20472074 */
    2048 function groups_demote_member( $user_id, $group_id ) {
    2049 
    2050     if ( ! bp_is_item_admin() ) {
     2075function groups_demote_member( $user_id, $group_id, $group_admin_id = 0 ) {
     2076    // Carry on using the item admin set by the Web version.
     2077    if ( ! $group_admin_id ) {
     2078        $user_can = bp_is_item_admin();
     2079
     2080        // Use the provided Group Admin ID (eg: during a REST API request).
     2081    } else {
     2082        $user_can = bp_current_user_can( 'bp_moderate' ) || groups_is_user_admin( $group_admin_id, $group_id );
     2083    }
     2084
     2085    if ( ! $user_can ) {
    20512086        return false;
    20522087    }
     
    20642099    do_action( 'groups_demote_member', $group_id, $user_id );
    20652100
    2066     return $member->demote();
     2101    if ( ! $member->demote() ) {
     2102        return false;
     2103    }
     2104
     2105    /**
     2106     * Fires once the group member has been successfully demoted.
     2107     *
     2108     * @since 14.0.0
     2109     *
     2110     * @param int $user_id  ID of the user being demoted.
     2111     * @param int $group_id ID of the group being demoted in.
     2112     */
     2113    do_action( 'group_member_demoted', $user_id, $group_id );
     2114
     2115    return true;
    20672116}
    20682117
     
    20712120 *
    20722121 * @since 1.0.0
     2122 * @since 14.0.0 Adds the `$group_admin_id` parameter.
    20732123 *
    20742124 * @param int $user_id  ID of the user.
    20752125 * @param int $group_id ID of the group.
     2126 * @param int $group_admin_id Optional. The group admin user ID.
    20762127 * @return bool True on success, false on failure.
    20772128 */
    2078 function groups_ban_member( $user_id, $group_id ) {
    2079 
    2080     if ( ! bp_is_item_admin() ) {
     2129function groups_ban_member( $user_id, $group_id, $group_admin_id = 0 ) {
     2130    // Carry on using the item admin set by the Web version.
     2131    if ( ! $group_admin_id ) {
     2132        $user_can = bp_is_item_admin();
     2133
     2134        // Use the provided Group Admin ID (eg: during a REST API request).
     2135    } else {
     2136        $user_can = bp_current_user_can( 'bp_moderate' ) || groups_is_user_admin( $group_admin_id, $group_id );
     2137    }
     2138
     2139    if ( ! $user_can ) {
    20812140        return false;
    20822141    }
     
    20942153    do_action( 'groups_ban_member', $group_id, $user_id );
    20952154
    2096     return $member->ban();
     2155    if ( ! $member->ban() ) {
     2156        return false;
     2157    }
     2158
     2159    /**
     2160     * Fires once the group member has been successfully banned.
     2161     *
     2162     * @since 14.0.0
     2163     *
     2164     * @param int $user_id  ID of the user being banned.
     2165     * @param int $group_id ID of the group being banned from.
     2166     */
     2167    do_action( 'group_member_banned', $user_id, $group_id );
     2168
     2169    return true;
    20972170}
    20982171
     
    21012174 *
    21022175 * @since 1.0.0
     2176 * @since 14.0.0 Adds the `$group_admin_id` parameter.
    21032177 *
    21042178 * @param int $user_id  ID of the user.
    21052179 * @param int $group_id ID of the group.
     2180 * @param int $group_admin_id Optional. The group admin user ID.
    21062181 * @return bool True on success, false on failure.
    21072182 */
    2108 function groups_unban_member( $user_id, $group_id ) {
    2109 
    2110     if ( ! bp_is_item_admin() ) {
     2183function groups_unban_member( $user_id, $group_id, $group_admin_id = 0 ) {
     2184    // Carry on using the item admin set by the Web version.
     2185    if ( ! $group_admin_id ) {
     2186        $user_can = bp_is_item_admin();
     2187
     2188        // Use the provided Group Admin ID (eg: during a REST API request).
     2189    } else {
     2190        $user_can = bp_current_user_can( 'bp_moderate' ) || groups_is_user_admin( $group_admin_id, $group_id );
     2191    }
     2192
     2193    if ( ! $user_can ) {
    21112194        return false;
    21122195    }
     
    21242207    do_action( 'groups_unban_member', $group_id, $user_id );
    21252208
    2126     return $member->unban();
     2209    if ( ! $member->unban() ) {
     2210        return false;
     2211    }
     2212
     2213    /**
     2214     * Fires once the group member has been successfully unbanned.
     2215     *
     2216     * @since 14.0.0
     2217     *
     2218     * @param int $user_id  ID of the user being unbanned.
     2219     * @param int $group_id ID of the group being unbanned from.
     2220     */
     2221    do_action( 'group_member_unbanned', $user_id, $group_id );
     2222
     2223    return true;
    21272224}
    21282225
     
    21332230 *
    21342231 * @since 1.2.6
     2232 * @since 14.0.0 Adds the `$group_admin_id` parameter.
    21352233 *
    21362234 * @param int $user_id  ID of the user.
    21372235 * @param int $group_id ID of the group.
     2236 * @param int $group_admin_id Optional. The group admin user ID.
    21382237 * @return bool True on success, false on failure.
    21392238 */
    2140 function groups_remove_member( $user_id, $group_id ) {
    2141 
    2142     if ( ! bp_is_item_admin() ) {
     2239function groups_remove_member( $user_id, $group_id, $group_admin_id = 0 ) {
     2240    // Carry on using the item admin set by the Web version.
     2241    if ( ! $group_admin_id ) {
     2242        $user_can = bp_is_item_admin();
     2243
     2244        // Use the provided Group Admin ID (eg: during a REST API request).
     2245    } else {
     2246        $user_can = bp_current_user_can( 'bp_moderate' ) || groups_is_user_admin( $group_admin_id, $group_id );
     2247    }
     2248
     2249    if ( ! $user_can ) {
    21432250        return false;
    21442251    }
     
    21562263    do_action( 'groups_remove_member', $group_id, $user_id );
    21572264
    2158     return $member->remove();
     2265    if ( ! $member->remove() ) {
     2266        return false;
     2267    }
     2268
     2269    /**
     2270     * Fires once the group member has been successfully removed.
     2271     *
     2272     * @since 14.0.0
     2273     *
     2274     * @param int $user_id  ID of the user being unbanned.
     2275     * @param int $group_id ID of the group being removed from.
     2276     */
     2277    do_action( 'group_member_removed', $user_id, $group_id );
    21592278}
    21602279
  • trunk/src/bp-groups/bp-groups-notifications.php

    r13808 r13873  
    301301    bp_send_email( 'groups-member-promoted', (int) $user_id, $args );
    302302}
    303 add_action( 'groups_promoted_member', 'groups_notification_promoted_member', 10, 2 );
     303add_action( 'group_member_promoted', 'groups_notification_promoted_member', 10, 2 );
    304304
    305305/**
     
    10561056    }
    10571057}
    1058 add_action( 'groups_demoted_member', 'bp_groups_delete_promotion_notifications', 10, 2 );
     1058add_action( 'group_member_demoted', 'bp_groups_delete_promotion_notifications', 10, 2 );
    10591059
    10601060/**
  • trunk/src/bp-groups/screens/single/admin/manage-members.php

    r13762 r13873  
    4949
    5050            /**
    51              * Fires before the redirect after a group member has been promoted.
    52              *
    53              * @since 1.0.0
     51             * Fires before the redirect.
     52             *
     53             * @since 1.0.0
     54             * @deprecated 14.0.0
    5455             *
    5556             * @param int $user_id ID of the user being promoted.
    56              * @param int $id      ID of the group user is promoted within.
    57              */
    58             do_action( 'groups_promoted_member', $user_id, $bp->groups->current_group->id );
     57             * @param int $id      ID of the group is promoted within.
     58             */
     59            do_action_deprecated( 'groups_promoted_member', array( $user_id, $bp->groups->current_group->id ), '14.0.0', 'group_member_promoted' );
    5960
    6061            bp_core_redirect( $redirect );
     
    8788             *
    8889             * @since 1.0.0
     90             * @deprecated 14.0.0
    8991             *
    9092             * @param int $user_id ID of the user being demoted.
    91              * @param int $id      ID of the group user is demoted within.
    92              */
    93             do_action( 'groups_demoted_member', $user_id, $bp->groups->current_group->id );
     93             * @param int $id      ID of the group is demoted within.
     94             */
     95            do_action_deprecated( 'groups_demoted_member', array( $user_id, $bp->groups->current_group->id ), '14.0.0', 'group_member_demoted' );
    9496
    9597            bp_core_redirect( $redirect );
     
    115117             *
    116118             * @since 1.0.0
     119             * @deprecated 14.0.0
    117120             *
    118121             * @param int $user_id ID of the user being banned.
    119122             * @param int $id      ID of the group user is banned from.
    120123             */
    121             do_action( 'groups_banned_member', $user_id, $bp->groups->current_group->id );
     124            do_action_deprecated( 'groups_banned_member', array( $user_id, $bp->groups->current_group->id ), '14.0.0', 'group_member_banned' );
    122125
    123126            bp_core_redirect( $redirect );
     
    143146             *
    144147             * @since 1.0.0
     148             * @deprecated 14.0.0
    145149             *
    146150             * @param int $user_id ID of the user being unbanned.
    147151             * @param int $id      ID of the group user is unbanned from.
    148152             */
    149             do_action( 'groups_unbanned_member', $user_id, $bp->groups->current_group->id );
     153            do_action_deprecated( 'groups_unbanned_member', array( $user_id, $bp->groups->current_group->id ), '14.0.0', 'group_member_unbanned' );
    150154
    151155            bp_core_redirect( $redirect );
     
    171175             *
    172176             * @since 1.2.6
     177             * @deprecated 14.0.0
    173178             *
    174179             * @param int $user_id ID of the user being removed.
    175180             * @param int $id      ID of the group the user is removed from.
    176181             */
    177             do_action( 'groups_removed_member', $user_id, $bp->groups->current_group->id );
     182            do_action_deprecated( 'groups_removed_member', array( $user_id, $bp->groups->current_group->id ), '14.0.0', 'group_member_removed' );
    178183
    179184            bp_core_redirect( $redirect );
  • trunk/tests/phpunit/testcases/groups/notifications.php

    r13314 r13873  
    182182
    183183        // fire the hook
    184         do_action( 'groups_demoted_member', $u, $g );
     184        do_action( 'group_member_demoted', $u, $g );
    185185
    186186        $notifications = BP_Notifications_Notification::get( array(
     
    206206
    207207        // fire the hook
    208         do_action( 'groups_demoted_member', $u, $g );
     208        do_action( 'group_member_demoted', $u, $g );
    209209
    210210        $notifications = BP_Notifications_Notification::get( array(
Note: See TracChangeset for help on using the changeset viewer.