uo_toolkit_csv_import_row_groups_col
Fires after a user's group memberships are processed during CSV import, allowing modification of group IDs.
add_action( 'uo_toolkit_csv_import_row_groups_col', $callback, 10, 4 );
Description
Fires after processing the LearnDash groups column during CSV user import. Developers can use this action to modify or extend the group association logic for imported users. It's crucial to understand the provided user ID, current row data, and any pre-determined group IDs before implementing custom actions.
Usage
add_action( 'uo_toolkit_csv_import_row_groups_col', 'your_function_name', 10, 4 );
Parameters
-
$user_id(mixed) - The ID of the user currently being imported.
-
$group_ids(mixed) - The ID of the user being imported.
-
$current_row(mixed) - This parameter contains an array of group IDs that have already been associated with the user during the import process.
-
$key_location(mixed) - This parameter contains the current row of data being processed from the CSV file.
Examples
add_action( 'uo_toolkit_csv_import_row_groups_col', 'my_custom_learndash_group_enrollment', 10, 4 );
/**
* Custom function to handle LearnDash group enrollment during CSV import.
*
* This function intercepts the default LearnDash group enrollment process
* for CSV imports. It can be used to perform additional checks, logging,
* or to modify the group enrollment logic based on custom requirements.
*
* @param int $user_id The ID of the user being imported.
* @param array $group_ids An array of existing group IDs for the user (if any).
* @param array $current_row The current row of data from the CSV file.
* @param array $key_location An array mapping column names to their indices in the CSV row.
*/
function my_custom_learndash_group_enrollment( $user_id, $group_ids, $current_row, $key_location ) {
// Get the column index for LearnDash groups from the key_location.
$learndash_groups_index = isset( $key_location['learndash_groups'] ) ? $key_location['learndash_groups'] : false;
// If the 'learndash_groups' column isn't found in the mapping, do nothing.
if ( ! $learndash_groups_index || ! isset( $current_row[ $learndash_groups_index ] ) ) {
return;
}
// Get the raw CSV value for LearnDash groups.
$csv_groups_value = $current_row[ $learndash_groups_index ];
// Parse the CSV group IDs, assuming they are semicolon-separated.
// If the value is empty, use a default option if it exists.
// Otherwise, an empty array is returned.
$csv_group_ids_raw = empty( $csv_groups_value ) ? ( ! empty( $options['uo_import_add_to_group'] ) ? $options['uo_import_add_to_group'] : array() ) : array_map( 'intval', explode( ';', $csv_groups_value ) );
// Convert existing group IDs to integers for reliable comparison.
$existing_group_ids_int = array_map( 'absint', $group_ids );
// Filter the CSV group IDs to only include those that are valid and exist in the system.
$valid_csv_group_ids = array_filter( $csv_group_ids_raw, function( $csv_group_id ) use ( $existing_group_ids_int ) {
// Ensure the CSV group ID is a positive integer.
if ( ! is_numeric( $csv_group_id ) || absint( $csv_group_id ) !== $csv_group_id ) {
return false;
}
// Check if the group ID exists in the system (you might need a more robust check here,
// like querying the 'groups' table or using a specific LearnDash function if available).
// For this example, we'll just check if it's in the existing_group_ids_int array,
// assuming $group_ids passed to this hook are representative of existing groups.
return in_array( absint( $csv_group_id ), $existing_group_ids_int, true );
} );
// If there are no valid groups to enroll the user into, stop here.
if ( empty( $valid_csv_group_ids ) ) {
// Optionally, log this situation if debugging.
// error_log( "No valid LearnDash groups found for user ID: {$user_id} from CSV data: {$csv_groups_value}" );
return;
}
// Log the enrollment process for debugging purposes if error logging is enabled.
if ( defined( 'UO_ENABLE_IMPORT_LOGGING' ) && UO_ENABLE_IMPORT_LOGGING ) {
$log_message = sprintf(
'[User ID: %d] Enrolling user into LearnDash groups. CSV Groups: %s. Existing System Groups (filtered): %s',
$user_id,
implode( ',', $csv_group_ids_raw ),
implode( ',', $valid_csv_group_ids )
);
error_log( $log_message );
}
// Iterate through the valid CSV group IDs and enroll the user.
foreach ( $valid_csv_group_ids as $group_id ) {
$group_id_int = absint( $group_id );
// Check if LearnDash is active before attempting to update group access.
if ( function_exists( 'ld_update_group_access' ) ) {
ld_update_group_access( $user_id, $group_id_int );
} else {
// Fallback for when LearnDash might not be active, or for custom tracking.
// This meta key should be consistent with how the default import handles this.
add_user_meta( $user_id, 'uo_learndash_group_enrollment_attempt', $group_id_int, false );
}
// You could also add custom checks or actions here, for example:
// - Check if the user is already in the group.
// - Log successful enrollments.
// - Trigger other custom hooks or processes.
// Example of a custom check: ensure the group actually exists before enrolling.
// This would typically involve a database query or a specific plugin function.
// if ( ! my_custom_check_if_group_exists( $group_id_int ) ) {
// error_log( "Attempted to enroll user {$user_id} into non-existent group {$group_id_int}." );
// continue; // Skip to the next group.
// }
}
}
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:1366
public static function perform_learndash_groups_col_import( $user_id, $current_row, $key_location, $options, $group_ids ) {
$index = $key_location['learndash_groups'];
$index_exists = isset( $current_row[ $index ] );
$value = $index_exists ? $current_row[ $index ] : '';
$csv_group_ids = empty( $value ) ? $options['uo_import_add_to_group'] : array_map( 'intval', explode( ';', $value ) );
// Remove values that aren't needed anymore so we can loop the rest as meta.
if ( $index_exists ) {
unset( $current_row[ $index ] );
}
// Return data for the loop.
$results = array(
'current_row' => $current_row,
'index' => $index,
'value' => '',
);
if ( empty( $csv_group_ids ) || empty( $group_ids ) ) {
return $results;
}
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_groups' );
}
foreach ( $csv_group_ids as $k => $csv_group_id ) {
if ( ! in_array( absint( $csv_group_id ), $group_ids, true ) ) {
unset( $csv_group_ids[ $k ] );
continue;
}
if ( self::is_learndash_active() ) {
// add group access
ld_update_group_access( $user_id, $csv_group_id );
} else {
add_user_meta( $user_id, 'import_user_csv_learndash_groups', $csv_group_id );
}
if ( self::is_learndash_active() && class_exists( 'uncanny_learndash_groupsSharedFunctions' ) ) {
// check if group is converted
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' );
}
}
// TODO REVIEW - shouldn't we pass the $csv_group_ids IDs that got added only?
do_action( 'uo_toolkit_csv_import_row_groups_col', $user_id, $group_ids, $current_row, $key_location );
// Implode final results for CSV.
$results['value'] = implode( ';', $csv_group_ids );
return $results;
}