Action uncanny-toolkit-pro

uo_toolkit_csv_import_row_group_leaders_col

Fires after processing a row group's leaders, allowing custom actions with user IDs, group IDs, and row data.

add_action( 'uo_toolkit_csv_import_row_group_leaders_col', $callback, 10, 4 );

Description

Fires after processing a row for LearnDash group leader CSV import. Allows customization of group leader assignment or role addition based on the user ID, group IDs, current row data, and key location during the import process.


Usage

add_action( 'uo_toolkit_csv_import_row_group_leaders_col', 'your_function_name', 10, 4 );

Parameters

$user_id (mixed)
This parameter contains the ID of the user currently being processed for import.
$group_ids (mixed)
The user ID of the user currently being processed for group leader import.
$current_row (mixed)
This parameter contains an array of group IDs that the user is being assigned as a leader to.
$key_location (mixed)
This parameter represents the current row of data being processed from the CSV import.

Examples

add_action( 'uo_toolkit_csv_import_row_group_leaders_col', 'my_custom_group_leader_logic', 10, 4 );

/**
 * Example custom function to process group leader column data during CSV import.
 * This function might add a custom meta field to users assigned as group leaders
 * or log specific actions based on the imported group leader data.
 *
 * @param int   $user_id       The ID of the user being processed.
 * @param array $group_ids     An array of group IDs the user is associated with.
 * @param array $current_row   The current row being processed from the CSV.
 * @param array $key_location  An array mapping CSV column headers to their internal keys.
 */
function my_custom_group_leader_logic( $user_id, $group_ids, $current_row, $key_location ) {

	// Check if the 'group_leader' key exists in the key_location array.
	if ( ! isset( $key_location['group_leader'] ) ) {
		return; // Nothing to do if group_leader column isn't mapped.
	}

	$group_leader_column_index = $key_location['group_leader'];

	// Ensure the group_leader column actually exists in the current row.
	if ( ! isset( $current_row[ $group_leader_column_index ] ) ) {
		return; // Group leader column not found in this row.
	}

	$group_leader_data = $current_row[ $group_leader_column_index ];

	// Example: If the group leader column contains a specific value,
	// add a custom meta field to the user.
	if ( ! empty( $group_leader_data ) && strtolower( $group_leader_data ) === 'yes' ) {
		update_user_meta( $user_id, 'custom_imported_as_group_leader', true );

		// Log this action for debugging or auditing purposes.
		if ( defined( 'UO_DEBUG_MODE' ) && UO_DEBUG_MODE ) {
			error_log( "User ID {$user_id} was marked as a group leader from CSV. Associated Group IDs: " . implode( ',', $group_ids ) );
		}
	}

	// Example: If the group_ids array is not empty, and the user is being imported
	// as a group leader, you might want to trigger a notification.
	if ( ! empty( $group_ids ) && ! empty( $group_leader_data ) ) {
		// You could send an email notification here, for example.
		// wp_mail( get_user_by( 'id', $user_id )->user_email, 'You have been assigned as a Group Leader', 'Details about your new role...' );
	}
}

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/import-learndash-users-from-csv.php:1438

private static function perform_learndash_group_leader_col_import( $user_id, $current_row, $key_location, $options, $group_ids ) {

		$index        = isset( $key_location['group_leader'] ) ? $key_location['group_leader'] : false;
		$index_exists = $index && isset( $current_row[ $index ] );

		if ( ! $index_exists || empty( $group_ids ) ) {
			return $current_row;
		}

		$add_group_leader_role = false;
		$value                 = $current_row[ $index ];
		$csv_group_ids         = empty( $value ) ? '' : array_map( 'intval', explode( ';', $value ) );

		// Remove values that aren't needed anymore so we can loop the rest as meta.
		unset( $current_row[ $index ] );

		if ( empty( $csv_group_ids ) ) {
			return $current_row;
		}

		if ( self::$log_error ) {
			$t1  = wp_json_encode( $csv_group_ids );
			$t2  = wp_json_encode( $group_ids );
			$log = "[ User id: {$user_id}] csv_group_ids: {$t1} group_ids: {$t2}n";
			self::log_error( $log, 'enroll_as_groupleader' );
		}

		foreach ( $csv_group_ids as $csv_group_id ) {
			if ( in_array( absint( $csv_group_id ), $group_ids, true ) ) {
				if ( self::is_learndash_active() ) {
					// add group leader
					ld_update_leader_group_access( $user_id, $csv_group_id );

					$add_group_leader_role = true;
					// Adding group leader role
					$wp_user = new WP_User( $user_id );
					if ( $wp_user instanceof WP_User ) {
						$wp_user->add_role( 'group_leader' );
					}
					if ( class_exists( 'uncanny_learndash_groupsSharedFunctions' ) ) {
						if ( 'yes' !== get_option( 'do_not_add_group_leader_as_member', 'no' ) ) {
							// check if group is converted
							if ( 'no' === (string) get_option( 'group_leaders_dont_use_seats', 'no' ) ) {
								self::ulgm_code_usage( $csv_group_id, $user_id );
							}
						}
					}
				}
				if ( self::$log_error ) {
					$log = "[ User id: {$user_id}] csv_group_id: {$csv_group_id}n";
					self::log_error( $log, 'enroll_course' );
				}
			}
		}

		do_action( 'uo_toolkit_csv_import_row_group_leaders_col', $user_id, $group_ids, $current_row, $key_location );

		return $current_row;
	}

Scroll to Top