ulgm_pool_seats_add_extra_seats_in_parent
Filters the extra seats added to a parent group when a child group is created or updated.
add_filter( 'ulgm_pool_seats_add_extra_seats_in_parent', $callback, 10, 3 );
Description
Filters the difference in seat count when adding extra seats to a parent group. Developers can modify the `$diff` value to adjust the total number of seats allocated to child groups based on parent group enrollment. This hook is triggered after calculating initial seat differences.
Usage
add_filter( 'ulgm_pool_seats_add_extra_seats_in_parent', 'your_function_name', 10, 3 );
Parameters
-
$diff(mixed) - This parameter represents the difference in the number of seats required for a group.
-
$diff(mixed) - This parameter represents the difference in the number of seats.
-
$group_id(mixed) - This parameter is not used by the `ulgm_pool_seats_add_extra_seats_in_parent` filter.
Return Value
The filtered value.
Examples
/**
* Example of adding extra seats to a parent group based on the difference in total users and available seats.
*
* This filter hook is triggered when the total number of users in a group hierarchy
* exceeds the combined seats available in the parent group and its child groups.
* The function can be used to dynamically add a certain number of extra seats to the
* parent group to accommodate the overflow.
*
* @param int $add The number of extra seats to add to the parent group.
* @param int $diff The difference between the total users and available seats.
* @param int $group_id The ID of the parent group.
*
* @return int The final number of extra seats to add.
*/
add_filter( 'ulgm_pool_seats_add_extra_seats_in_parent', 'my_custom_ulgm_pool_seats_add_extra_seats_in_parent', 10, 3 );
function my_custom_ulgm_pool_seats_add_extra_seats_in_parent( $add, $diff, $group_id ) {
// Let's say we want to add an additional 5 seats on top of the calculated extra seats
// for every 10 users that exceed the current capacity.
$additional_buffer_seats = floor( abs( $diff ) / 10 ) * 5;
// Ensure we always add at least the original calculated extra seats, plus our custom buffer.
$total_seats_to_add = $add + $additional_buffer_seats;
// You might also want to cap the number of extra seats that can be added.
$max_allowed_extra_seats = 50;
if ( $total_seats_to_add > $max_allowed_extra_seats ) {
$total_seats_to_add = $max_allowed_extra_seats;
}
// Log the action for debugging purposes (optional).
error_log( sprintf(
'ULGM: Adding %d extra seats to group ID %d. Original diff: %d. Calculated add: %d. Custom buffer: %d. Total to add: %d.',
$total_seats_to_add,
$group_id,
$diff,
$add,
$additional_buffer_seats,
$total_seats_to_add
) );
// Return the final calculated number of seats to add.
return $total_seats_to_add;
}
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/learndash/learndash-groups-post-edit-additions.php:1313
public static function fix_total_number_of_hierarchy_users( $group_id, $group_users = array(), $group_leaders = array() ) {
$group_leader_take_seat = ! SharedFunctions::group_leaders_dont_use_seats();
if ( true === $group_leader_take_seat && empty( $group_leaders ) ) {
$group_leaders = LearndashFunctionOverrides::learndash_get_groups_administrators( $group_id, true );
}
if ( empty( $group_users ) ) {
$group_users = LearndashFunctionOverrides::learndash_get_groups_users( $group_id, true );
}
global $wpdb;
// Seats in parent group
$seats_in_parent = ulgm()->group_management->seat->total_seats( $group_id );
$seats_across_groups = 0;
$group_children = learndash_get_group_children( $group_id );
$group_children = array_reverse( $group_children );
if ( ! empty( $group_children ) ) {
foreach ( $group_children as $child_group_id ) {
if ( true === $group_leader_take_seat ) {
$group_leaders = array_merge( $group_leaders, LearndashFunctionOverrides::learndash_get_groups_administrators( $child_group_id, true ) );
}
$group_users = array_merge( $group_users, LearndashFunctionOverrides::learndash_get_groups_users( $child_group_id, true ) );
// get seat in each child group
$child_seats = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(c.ID)FROM {$wpdb->prefix}" . ulgm()->db->tbl_group_codes . " c JOIN {$wpdb->prefix}" . ulgm()->db->tbl_group_details . ' d ON c.group_id = d.ID WHERE d.ld_group_id = %d', $child_group_id ) );
$seats_across_groups = (int) $seats_across_groups + $child_seats;
}
}
$group_leader_ids = array_column( $group_leaders, 'ID' );
$group_user_ids = array_column( $group_users, 'ID' );
$total = array_unique( array_merge( $group_leader_ids, $group_user_ids ) );
$count_users = count( $total );
// Parent group seems to be purchased via payment, bail and show error
if (
Utilities::if_woocommerce_active() &&
empty( get_post_meta( GroupManagementInterface::$ulgm_current_managed_group_id, SharedFunctions::$code_group_downgraded, true ) ) &&
empty( get_post_meta( GroupManagementInterface::$ulgm_current_managed_group_id, '_ulgm_is_upgraded', true ) ) &&
empty( get_post_meta( GroupManagementInterface::$ulgm_current_managed_group_id, '_ulgm_is_custom_group_created', true ) ) &&
$count_users > $seats_across_groups
) {
return __( 'Your group does not have enough seats to accommodate all users in the group hierarchy. Please add more seats and try again.', 'uncanny-learndash-groups' );
}
if ( $count_users >= ( $seats_across_groups + $seats_in_parent ) ) {
update_post_meta( $group_id, '_ulgm_seats_before_pooling', $seats_in_parent );
// users across all groups are
// GT seats in all groups
$diff = $count_users - ( $seats_across_groups + $seats_in_parent );
if ( $diff > 0 ) {
$add = (int) apply_filters( 'ulgm_pool_seats_add_extra_seats_in_parent', absint( 10 + $diff ), $diff, $group_id );
self::increase_seat_count( $add, $group_id );
update_post_meta( $group_id, '_ulgm_total_seats', $seats_in_parent + $add );
}
return true;
}
$diff = $seats_across_groups - $seats_in_parent;
if ( $diff > 0 ) {
update_post_meta( $group_id, '_ulgm_seats_before_pooling', $seats_in_parent );
// total seats across all groups in hierarchy
self::increase_seat_count( $diff, $group_id );
update_post_meta( $group_id, '_ulgm_total_seats', $seats_in_parent + $diff );
}
return true;
}