Filter uncanny-learndash-groups

ulgm_user_can_manage_this_group

Filters if the current user can manage a specific group, allowing modification of the management permission.

add_filter( 'ulgm_user_can_manage_this_group', $callback, 10, 3 );

Description

Filters whether a user can manage a specific group. Developers can return `false` to deny management access. Pass `null` for `$ld_group_id` to check general group management permissions. `$user_group_ids` contains the current user's group IDs.


Usage

add_filter( 'ulgm_user_can_manage_this_group', 'your_function_name', 10, 3 );

Parameters

$ld_group_id (mixed)
This parameter is the boolean result of whether the user can manage the group.
$group_leader_id (mixed)
This parameter represents the ID of the specific group being checked.
$user_group_ids (mixed)
This parameter contains the ID of the user who is currently the leader of the group being checked.

Return Value

The filtered value.


Examples

/**
 * Custom logic to determine if a user can manage a specific group.
 *
 * This example demonstrates how to override the default group management permissions.
 * For instance, we can allow users with a specific custom role (e.g., 'group_manager_advanced')
 * to manage any group, regardless of whether they are an admin or a leader of that group.
 *
 * @param bool   $can_manage The current value of whether the user can manage the group.
 * @param int    $ld_group_id The ID of the LearnDash group.
 * @param int    $group_leader_id The ID of the user who is potentially managing the group.
 * @param array  $user_group_ids An array of group IDs the user is a member of.
 * @return bool True if the user can manage the group, false otherwise.
 */
add_filter( 'ulgm_user_can_manage_this_group', function( $can_manage, $ld_group_id, $group_leader_id, $user_group_ids ) {

	// If the user already has permission (e.g., they are a WordPress admin), keep it.
	if ( true === $can_manage ) {
		return true;
	}

	// Check if the user has a custom role that grants universal group management.
	$user_roles = get_userdata( $group_leader_id )->roles;
	if ( in_array( 'group_manager_advanced', $user_roles, true ) ) {
		// This user role can manage any group.
		return true;
	}

	// If the user is a leader of the specific group they are trying to manage, allow it.
	if ( in_array( $ld_group_id, $user_group_ids, true ) ) {
		return true;
	}

	// If none of the above conditions are met, respect the default logic (which would have returned false
	// if the original $can_manage was false and no other conditions were met).
	return $can_manage;

}, 10, 4 );

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/shared-functions.php:1340
src/classes/helpers/shared-functions.php:1346
src/classes/helpers/shared-functions.php:1358
src/classes/helpers/shared-functions.php:1365
src/classes/helpers/shared-functions.php:1376
src/classes/helpers/shared-functions.php:1379

public static function can_user_manage_this_group( $group_leader_id, $ld_group_id = null, $user_group_ids = array() ) {
		// Looks like an admin
		if ( user_can( $group_leader_id, 'manage_options' ) ) {
			return apply_filters( 'ulgm_user_can_manage_this_group', true, $ld_group_id, $group_leader_id, $user_group_ids );
		}
		// Allow users to disable setting
		if ( true === apply_filters( 'ulgm_do_not_allow_parent_group_leaders_to_manage_children', false, $ld_group_id, $group_leader_id, $user_group_ids ) ) {
			$r = in_array( $ld_group_id, array_map( 'absint', $user_group_ids ), true );

			return apply_filters( 'ulgm_user_can_manage_this_group', $r, $ld_group_id, $group_leader_id, $user_group_ids );
		}
		// if null, return
		if ( null === $group_leader_id || null === $ld_group_id ) {
			return false;
		}
		// check if hierarchy is enabled
		// Enabled = move on to next logic
		// Disabled = check if group ID exists
		if ( ! self::is_hierarchy_enabled( $ld_group_id ) ) {
			$r = in_array( $ld_group_id, array_map( 'absint', $user_group_ids ), true );

			return apply_filters( 'ulgm_user_can_manage_this_group', $r, $ld_group_id, $group_leader_id, $user_group_ids );
		}

		$overrides_group_admin_ids = LearndashFunctionOverrides::learndash_get_administrators_group_ids( $group_leader_id );
		$group_admin_ids           = array_merge( learndash_get_administrators_group_ids( $group_leader_id ), $overrides_group_admin_ids );
		$group_admin_ids           = array_unique( $group_admin_ids );
		if ( empty( $group_admin_ids ) ) {
			return apply_filters( 'ulgm_user_can_manage_this_group', false, $ld_group_id, $group_leader_id, $user_group_ids );
		}
		// Hierarchy is enabled, return all children groups as well
		$new_groups = array();
		foreach ( $group_admin_ids as $group_id ) {
			$new_groups[] = $group_id;
			$new_groups   = array_merge( $new_groups, learndash_get_group_children( $group_id ) );
		}

		$new_groups = array_map( 'absint', array_unique( $new_groups ) );
		if ( ! in_array( $ld_group_id, $new_groups, true ) ) {
			return apply_filters( 'ulgm_user_can_manage_this_group', false, $ld_group_id, $group_leader_id, $new_groups );
		}

		return apply_filters( 'ulgm_user_can_manage_this_group', true, $ld_group_id, $group_leader_id, $new_groups );
	}


Scroll to Top