group_management_add_group_leader_permission
Filters whether a user can be added as a group leader, allowing modification of the permission.
add_filter( 'group_management_add_group_leader_permission', $callback, 10, 1 );
Description
Allows developers to modify or conditionally grant 'group_leader' permissions when adding a group leader via the REST API. Use this filter to override default permission checks or add custom validation before a group leader is assigned. The hook passes the intended 'group_leader' value for manipulation.
Usage
add_filter( 'group_management_add_group_leader_permission', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
/**
* Filters the permission required to add a group leader.
*
* This function checks if the current user has a specific capability.
* It's designed to be hooked into 'group_management_add_group_leader_permission'.
*
* @param string $required_capability The capability currently required to add a group leader.
* @return string The modified required capability.
*/
function my_custom_group_leader_permission( $required_capability ) {
// Example: Allow users with the 'edit_others_courses' capability to add group leaders
// in addition to the default 'group_leader' capability.
// The original check in the source code also includes 'manage_options' and 'ulgm_group_management'.
// This filter allows us to add more flexibility.
// If the default capability is 'group_leader', we might want to add more.
if ( 'group_leader' === $required_capability ) {
// Check if the current user already has 'manage_options' or 'ulgm_group_management'
// which are checked later in the function. If so, no need to modify.
if ( current_user_can( 'manage_options' ) || current_user_can( 'ulgm_group_management' ) ) {
return $required_capability;
}
// Let's say we want to also allow users who can 'edit_others_courses'.
// This filter doesn't directly check the user, but it modifies what the
// core function will check against using current_user_can().
// So, we return a capability that is either the original or a more specific one
// that a user might have.
// A more realistic scenario might be to add a custom capability.
// For demonstration, let's assume we want to allow 'edit_others_courses'
// and the original check will handle the `current_user_can()` calls.
// We can return a new string that represents a higher level of access or
// a combined set of permissions if the core logic supported it.
// However, since the core logic checks against a single capability string,
// the most practical use of this filter is to *change* the required capability
// to a different one, or to return the original if no change is needed.
// Let's assume a scenario where a premium feature grants an additional role.
// If the 'premium_group_leader_access' capability exists, we'll use that instead.
if ( current_user_can( 'premium_group_leader_access' ) ) {
return 'premium_group_leader_access';
}
}
// Always return the capability (either modified or original)
return $required_capability;
}
add_filter( 'group_management_add_group_leader_permission', 'my_custom_group_leader_permission', 10, 1 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/helpers/rest-api-end-points.php:1339
public static function add_group_leader( WP_REST_Request $request ) {
// Actions permitted by the pi call (collected from input element with name action )
$permitted_actions = array( 'add-leader' );
// Was an action received, and is the actions allowed
if ( $request->has_param( 'action' ) && in_array( $request->get_param( 'action' ), $permitted_actions ) ) {
$action = (string) $request->get_param( 'action' );
} else {
$action = '';
$data['message'] = __( 'Select an action.', 'uncanny-learndash-groups' );
wp_send_json_error( $data );
}
// Does the current user have permission
$permission = apply_filters( 'group_management_add_group_leader_permission', 'group_leader' );
if ( ! current_user_can( $permission ) && ! current_user_can( 'manage_options' ) && ! current_user_can( 'ulgm_group_management' ) ) {
$data['message'] = __( 'You do not have permission to add group leaders.', 'uncanny-learndash-groups' );
wp_send_json_error( $data );
}
// If a single part of data doesn't validate, it dies and sends back the validation error
$user_data = self::validate_new_user_data( $action, $request );
$ld_group_id = (int) $request->get_param( 'group-id' );
$order_id = SharedFunctions::get_order_id_from_group_id( $ld_group_id );
//returns true or false if group leader allowed to add user
//wp_get_current_user() = (object) of current logged in user details
//(int) $request->get_param( 'group-id' ) = (int) LD Group ID
//$user_data = (object) of user being added to group as group leader
$ulgm_gdpr_compliance = apply_filters( 'ulgm_gdpr_is_group_leader_allowed', true, wp_get_current_user(), $ld_group_id, (object) $user_data, $request->get_param( 'action' ) );
// Add group leader and send out welcome email
if ( $ulgm_gdpr_compliance && 'add-leader' === $action ) {
// Add the user and send out a welcome email NOTE group id has already been validate and the script would return error so its safe :)
$data = Group_Management_Helpers::create_group_leader( $user_data, $ld_group_id, false );
if ( ! key_exists( 'error', $data ) ) {
if ( 'yes' !== get_option( 'do_not_add_group_leader_as_member', 'no' ) ) {
$user = get_user_by( 'email', $user_data['user_email'] );
if ( 'no' === ulgm()->group_management->is_user_already_member_of_group( $user->ID, $ld_group_id ) ) {
$user_data['user_id'] = $user->ID;
//if ( 'yes' !== get_option( 'do_not_add_group_leader_as_member', 'no' ) ) {
Group_Management_Helpers::add_existing_user( $user_data, true, $ld_group_id, $order_id, SharedFunctions::$redeem_status, false, false, false, true );
//}
SharedFunctions::delete_transient( null, $ld_group_id );
}
}
do_action( 'ulgm_group_leader_added', $user_data, $ld_group_id, $order_id );
wp_send_json_success( $data );
} else {
wp_send_json_error( $data );
}
}
}