Action uncanny-learndash-groups

ulgm_seats_freed_for_deleted_user

Fires when seats are freed for a user after they are deleted.

add_action( 'ulgm_seats_freed_for_deleted_user', $callback, 10, 1 );

Description

Fires when seats are freed for a user being deleted. Developers can use this hook to perform custom actions, such as notifying administrators or updating related data when a user's group code becomes available again due to their account deletion.


Usage

add_action( 'ulgm_seats_freed_for_deleted_user', 'your_function_name', 10, 1 );

Parameters

$user_id (mixed)
This parameter contains the ID of the user who has been deleted, and whose seats need to be freed up.

Examples

// Hook into the action fired when seats are freed for a deleted user.
// This example demonstrates how to log the user ID and the number of seats freed.
add_action( 'ulgm_seats_freed_for_deleted_user', 'my_custom_log_freed_seats', 10, 1 );

/**
 * Logs the user ID and the number of seats freed for a deleted user.
 *
 * @param int $user_id The ID of the user for whom seats were freed.
 */
function my_custom_log_freed_seats( $user_id ) {
	// In a real-world scenario, you might want to do more sophisticated logging,
	// such as writing to a custom log file or sending a notification.
	// For this example, we'll just use WordPress's error_log.

	// Check if the user ID is a valid integer.
	if ( ! is_numeric( $user_id ) ) {
		error_log( 'Invalid user ID received in ulgm_seats_freed_for_deleted_user hook: ' . print_r( $user_id, true ) );
		return;
	}

	// In the context of the original function, the action is fired only if $q > 0,
	// meaning at least one seat was updated. However, the hook itself doesn't
	// directly provide the count of seats freed. If you needed that, you'd have
	// to modify the original function to pass it as another argument.
	// For this example, we'll just confirm that the action was triggered for the user.
	error_log( sprintf( 'Seats freed for deleted user ID: %d', absint( $user_id ) ) );

	// Example: You could potentially interact with other systems or perform cleanup tasks here.
	// For instance, if you had a custom table tracking group enrollments, you might
	// want to remove entries related to this user.
}

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/group-management/class-group-management-helpers.php:1887

public static function free_seats_for_deleted_user( $user_id ) {
		global $wpdb;
		$tbl = $wpdb->prefix . ulgm()->db->tbl_group_codes;
		$q   = $wpdb->query(
			$wpdb->prepare(
				"UPDATE {$tbl}
				SET code_status = 'available',
				    student_id  = NULL,
				    user_email  = NULL,
				    first_name  = NULL,
				    last_name   = NULL,
				    used_date   = NULL,
				    ld_group_id = NULL
				WHERE student_id = %d",
				absint( $user_id )
			)
		);
		if ( false === $q ) {
			error_log( sprintf( 'Failed to free seats for deleted user %d: %s', $user_id, $wpdb->last_error ) );
			return false;
		}

		if ( $q > 0 ) {
			do_action( 'ulgm_seats_freed_for_deleted_user', $user_id );
		}

		return true;
	}

Scroll to Top