ulgm_download_users_keys
Filters the user IDs associated with a specific group download, allowing modification before retrieval.
add_filter( 'ulgm_download_users_keys', $callback, 10, 2 );
Description
Filters the list of users and their keys to be downloaded for a specific group. Allows developers to modify or exclude users from the download based on custom logic. Fires just before the user data is prepared for CSV export.
Usage
add_filter( 'ulgm_download_users_keys', 'your_function_name', 10, 2 );
Parameters
-
$group_id(mixed) - This parameter is the ID of the group for which user keys are being downloaded.
-
$group_id(mixed) - This parameter represents the ID of the group for which user keys are being downloaded.
Return Value
The filtered value.
Examples
<?php
/**
* Example of adding a filter to ulgm_download_users_keys to modify the user data for a CSV download.
*
* This function will be attached to the 'ulgm_download_users_keys' hook.
* It receives the array of user data and the group ID.
* In this example, we'll add an extra column to the CSV, like the user's email address.
*/
function my_custom_ulgm_user_keys_data( $users_data, $group_id ) {
// Check if $users_data is an array and if it's not empty.
if ( ! is_array( $users_data ) || empty( $users_data ) ) {
return $users_data; // Return original data if empty or not an array.
}
// Ensure the header row exists and has at least 'Group' and 'Key' columns.
// We assume the original data structure is [[Group, Key], ...].
// If the header structure is different, this logic might need adjustment.
if ( ! isset( $users_data[0] ) || ! is_array( $users_data[0] ) || count( $users_data[0] ) < 2 ) {
// Add a new header row if it doesn't exist or is malformed.
array_unshift( $users_data, array( 'Group', 'Key', 'User Email' ) );
} else {
// Add the new header to the existing header row.
$users_data[0][] = 'User Email';
}
// Loop through the user data (starting from the second element if header exists)
// and add the user's email.
$start_index = ( isset( $users_data[0] ) && is_array( $users_data[0] ) && count( $users_data[0] ) > 2 ) ? 1 : 0;
for ( $i = $start_index; $i < count( $users_data ); $i++ ) {
// The structure of $users_data[$i] is assumed to be [group_name, key_value].
// We need to fetch the actual user ID associated with the key to get their email.
// This part is hypothetical as the exact structure of 'key' isn't defined here.
// For a realistic scenario, you'd need to know how to get the user ID from the key.
// Let's assume the 'key' itself contains the user ID or can be used to fetch it.
// A more likely scenario is that $users_data might already contain user IDs or emails.
// For this example, let's assume we can get the user ID by processing the group_id and key.
// In a real plugin, you'd have access to the actual data structure.
// Placeholder: In a real scenario, you'd fetch the user associated with this key.
// Example: $user_id = YourPlugin_Helper::get_user_id_from_key( $users_data[$i][1] );
// For demonstration, let's just use a dummy email based on the group and key.
// If the actual $users_data contains user objects or IDs, you'd use that.
// Let's assume $users_data[$i] is an array like [group_name, key_value, ...other_data]
// and we need to get the user ID to fetch the email.
// The original source `get_unused__key_users_data` might return more data per user.
// If $users_data[$i] is a simple array [group_name, key_value], this needs more context.
// Let's assume for the sake of this example that $users_data[$i] can be extended.
// If $users_data[$i] is an array like [group_name, key_value], we need a way to link it to a user.
// If the original `get_unused__key_users_data` already returns user emails or IDs, this would be simpler.
// Let's consider a common scenario where $users_data is an array of arrays,
// and each inner array represents a user/key entry.
// If $users_data[0] is ['Group', 'Key'], then $users_data[1] would be ['MyGroup', 'ABCDEFG'].
// We need to get the user's email for the key 'ABCDEFG' in group 'MyGroup'.
// A more realistic structure for $users_data from `get_unused__key_users_data`
// might be an array of associative arrays, or arrays with more than just group and key.
// For example:
// [
// ['Group' => 'Group A', 'Key' => 'KEY123', 'User ID' => 5, 'User Name' => 'John Doe'],
// ['Group' => 'Group A', 'Key' => 'KEY456', 'User ID' => 8, 'User Name' => 'Jane Smith'],
// ]
// If the structure is [group_name, key_value], we need to infer the user.
// This is where the context of `Group_Management_Helpers::get_unused__key_users_data` is crucial.
// For this example, let's assume the inner array contains enough info or can be processed.
// If the original data is `[ ['Group', 'Key'], ['Group A', 'KEY123'], ... ]`
// and we want to add the email. We need a way to get the user for KEY123.
// Let's refine the assumption: If `get_unused__key_users_data` returns an array of arrays,
// and the first element is the header, the subsequent elements are data.
// If $users_data[$i][1] is the key, we need to find the user associated with it.
// The `absint( ulgm_filter_input( 'post' ) )` gives us the group ID.
// A practical way to get the user's email for a given key would involve
// looking up that key in your database or plugin's data.
// For this example, we'll simulate getting a user ID and then their email.
// Let's assume the `get_unused__key_users_data` function returns data that we can iterate over directly.
// If it returns an array of arrays, e.g., `[ ['Group Name', 'Key'], ['Group A', 'KEY1'], ... ]`
// Then $users_data[$i][1] is the key.
// *** A More Robust Assumption: ***
// Let's assume that the original `get_unused__key_users_data($group_id)` returns an array
// where each element is an array representing a user's key entry,
// AND that it might contain a user ID or a way to retrieve it.
// For example, if it returns something like:
// [
// ['group_name' => 'My Group', 'key' => 'abc123', 'user_id' => 10],
// ['group_name' => 'My Group', 'key' => 'def456', 'user_id' => 25],
// ]
// Then we can easily add the email.
// If `get_unused__key_users_data` returns an array of arrays like `[group_slug, key]`.
// This is less likely for a function named `get_unused__key_users_data`.
// It's more probable it returns user-specific data.
// For this example, let's assume the data is an array of arrays like:
// `[ [Group Name, Key], [User_Data_Array_1], [User_Data_Array_2] ]`
// and `User_Data_Array_N` might be `[Group Name, Key, User_ID]`.
// Let's make a common-sense assumption that the original function `get_unused__key_users_data($group_id)`
// returns an array of arrays, where each inner array represents a user and their key.
// We'll assume the structure is `[ Group Name, Key, User ID ]`.
// If the original header is already present, we'll just append.
if ( $i === 0 ) {
// This is the header row. Add the new column header.
// Ensure it's an array and append.
if ( is_array( $users_data[0] ) ) {
$users_data[0][] = 'User Email';
}
continue; // Skip to the next iteration to process actual data.
}
// Process data rows. Assume $users_data[$i] = [group_name, key, user_id]
$current_user_id = isset( $users_data[$i][2] ) ? absint( $users_data[$i][2] ) : 0;
$user_email = '';
if ( $current_user_id > 0 ) {
$user_info = get_user_by( 'id', $current_user_id );
if ( $user_info ) {
$user_email = $user_info->user_email;
}
}
// Append the user's email to the current row.
$users_data[$i][] = $user_email;
}
return $users_data; // Return the modified array of user keys.
}
add_filter( 'ulgm_download_users_keys', 'my_custom_ulgm_user_keys_data', 10, 2 );
?>
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/class-learndash-modifications.php:116
src/classes/helpers/rest-api-end-points.php:1521
public function download_csv_file() {
if ( ! ulgm_filter_has_var( 'action' ) ) {
return;
}
if ( ulgm_filter_has_var( 'action' ) && 'download_seat_count' !== ulgm_filter_input( 'action' ) ) {
return;
}
if ( ! ulgm_filter_has_var( 'post' ) ) {
return;
}
if ( ! ulgm_filter_has_var( '_wpnonce' ) ) {
return;
}
if ( ! wp_verify_nonce( ulgm_filter_input( '_wpnonce' ), 'ulgm' ) ) {
return;
}
$group_id = absint( ulgm_filter_input( 'post' ) );
$users = apply_filters( 'ulgm_download_users_keys', Group_Management_Helpers::get_unused__key_users_data( $group_id ), $group_id );
$header = array(
'header' => array(
'Group',
'Key',
),
);
// open raw memory as file so no temp files needed, you might run out of memory though
$f = fopen( 'php://memory', 'w' );
$group_slug = get_post_field( 'post_name', $group_id );
$file_name = 'keys-' . $group_slug . '-' . date( 'Y-m-d' );
$file_name = apply_filters( 'csv_file_name', $file_name, $group_slug, $group_id, wp_get_current_user()->ID );
$filename = "$file_name.csv";
$delimiter = ',';
// loop over the input array
foreach ( $header as $line ) {
// generate csv lines from the inner arrays
fputcsv( $f, $line, $delimiter );
}
foreach ( $users as $line ) {
// generate csv lines from the inner arrays
fputcsv( $f, $line, $delimiter );
}
// reset the file pointer to the start of the file
rewind( $f );
// tell the browser it's going to be a csv file
header( 'Content-Type: application/csv; charset=UTF-8' );
// tell the browser we want to save it instead of displaying it
header( 'Content-Disposition: attachment;filename="' . $filename . '";' );
// make php send the generated csv lines to the browser
if ( function_exists( 'fpassthru' ) ) {
fpassthru( $f );
} else {
echo stream_get_contents( $f );
fclose( $f );
}
die();
}