Filter uncanny-learndash-groups

ulgm_do_not_allow_parent_group_leaders_to_manage_children

Filters whether parent group leaders are allowed to manage their children groups, firing before management actions.

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

Description

Filters whether a parent group leader can manage child groups. By default, parent group leaders can manage their children. Return `true` to prevent parent group leaders from managing their child groups. Useful for granular access control in hierarchical group structures.


Usage

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

Parameters

$ld_group_id (mixed)
This parameter is the default return value for the filter, which is `true` if the user is allowed to manage the group, and `false` otherwise.
$group_leader_id (mixed)
This parameter contains the ID of the group for which the user's management capabilities are being checked.
$user_group_ids (mixed)
This parameter contains the ID of the user who is attempting to manage the group.

Return Value

The filtered value.


Examples

/**
 * Prevent parent group leaders from managing their direct children groups if this filter returns true.
 * This is useful if you want to enforce a strict hierarchy where only specific users (e.g., site admins)
 * can manage parent groups and their children, rather than the immediate leaders of those child groups.
 *
 * @param bool $prevent_management Whether to prevent parent group leaders from managing children. Default is false.
 * @param int|null $ld_group_id The ID of the group being checked for management permissions.
 * @param int $group_leader_id The ID of the user attempting to manage the group.
 * @param array $user_group_ids An array of group IDs the user is a member of.
 * @return bool True to prevent parent group leaders from managing their children, false otherwise.
 */
add_filter( 'ulgm_do_not_allow_parent_group_leaders_to_manage_children', function( $prevent_management, $ld_group_id, $group_leader_id, $user_group_ids ) {
    // Example scenario: Only allow site administrators to override the default group leader management.
    // If the current user is NOT a site administrator, and they are a leader of a parent group,
    // we want to prevent them from managing their direct children.
    // This assumes the original function already checks if the user is a direct leader of the child.

    // If the filter is already set to prevent management, we don't need to do anything further.
    if ( true === $prevent_management ) {
        return true;
    }

    // If this is not a group management check for a specific group, exit early.
    if ( null === $ld_group_id ) {
        return false;
    }

    // Check if the user is a site administrator. If so, they should always be able to manage.
    if ( user_can( $group_leader_id, 'manage_options' ) ) {
        return false; // Site admins can always manage, so don't prevent it.
    }

    // Let's assume we have a function to check if a user is a leader of a specific group.
    // Replace this with the actual logic from your plugin or theme.
    // For demonstration, we'll simulate this check.
    // In a real scenario, $user_group_ids might already represent groups the user leads.
    // This example assumes $user_group_ids contains all groups the user is associated with,
    // and we need to check if they are a leader of any parent group.

    // A hypothetical function to get groups the user *leads*.
    // You would need to implement or use an existing function for this.
    // Example: $groups_user_leads = my_plugin_get_groups_led_by_user( $group_leader_id );
    $groups_user_leads = array(); // Replace with actual data

    // If the user is not leading any groups, they can't be a parent leader to prevent.
    if ( empty( $groups_user_leads ) ) {
        return false;
    }

    // Now, let's check if the current group ($ld_group_id) is a child of any group the user leads.
    // And also if the user is the leader of the parent group.
    // This logic can be complex depending on how your group hierarchy is stored.
    // For this example, let's simplify: if the user is a leader of ANY group,
    // AND that group is a PARENT of the current group ($ld_group_id),
    // then we prevent them.

    // We need to check if $ld_group_id has a parent group, and if $group_leader_id is the leader of that parent group.
    // This requires functions to get parent groups and check leadership.
    // Let's assume we have:
    // learndash_get_parent_group_id( $child_group_id )
    // my_plugin_is_user_leader_of_group( $user_id, $group_id )

    $parent_group_id = learndash_get_parent_group_id( $ld_group_id );

    if ( $parent_group_id && my_plugin_is_user_leader_of_group( $group_leader_id, $parent_group_id ) ) {
        // The user is a leader of the parent group, and we want to prevent them from managing the child.
        return true;
    }

    // By default, if none of the above conditions are met, don't prevent management.
    return false;
}, 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:1343

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