ulgm_download_keys_header
Filters the header for download keys, allowing modification of the group and key format before the keys are generated.
add_filter( 'ulgm_download_keys_header', $callback, 10, 1 );
Description
Filters the CSV header row for downloaded license keys. Modify this hook to customize the column names or add/remove fields in the downloaded CSV file. The default header includes "Group,Key".
Usage
add_filter( 'ulgm_download_keys_header', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the ulgm_download_keys_header filter to modify the CSV header.
* This example adds the 'Group ID' column to the header if it's not already present.
*/
add_filter( 'ulgm_download_keys_header', 'my_custom_ulgm_download_keys_header', 10, 2 );
function my_custom_ulgm_download_keys_header( $header_string, $group_id ) {
// Check if 'Group ID' is already in the header.
// We assume the first column is 'Group' and look for it.
if ( strpos( $header_string, 'Group,' ) !== 0 ) {
// If 'Group ID' is not the first part of the header, prepend it.
// We add the group_id as the very first column.
$new_header = 'Group ID,' . $header_string;
} else {
// If 'Group' is already the first column, we don't need to add 'Group ID' separately.
// We can assume the current data might include the group identifier.
// For this example, we'll still ensure 'Group ID' is present and at the beginning.
// A more robust solution would parse the existing header and insert accordingly.
// For simplicity, we'll prepend it if it's not already there.
$new_header = 'Group ID,' . $header_string;
}
// Ensure the header ends with a newline for proper CSV formatting.
if ( substr( $new_header, -1 ) !== "n" ) {
$new_header .= "n";
}
return $new_header;
}
?>
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/helpers/rest-api-end-points.php:1525
src/classes/helpers/rest-api-end-points.php:1531
public static function download_keys_csv( WP_REST_Request $request ) {
// Actions permitted by the pi call (collected from input element with name action )
$permitted_actions = array( 'download' );
// Was an action received, and is the actions allowed
if ( $request->has_param( 'action' ) && in_array( $request->get_param( 'action' ), $permitted_actions ) ) {
$action = (string) $request->get_param( 'action' );
} else {
$action = '';
$data['message'] = __( 'Select an action.', 'uncanny-learndash-groups' );
wp_send_json_error( $data );
}
// Does the current user have permission
$allowed_roles = apply_filters(
'ulgm_gm_allowed_roles',
array(
'administrator',
'group_leader',
'ulgm_group_management',
'super_admin',
)
);
$permission = apply_filters( 'download_keys_csv_permission', 'group_leader' );
if ( ! current_user_can( $permission ) && ! array_intersect( wp_get_current_user()->roles, $allowed_roles ) ) {
$data['message'] = __( 'You do not have permission to download csv keys.', 'uncanny-learndash-groups' );
wp_send_json_error( $data );
}
$group_leader_id = get_current_user_id();
$user_group_ids = LearndashFunctionOverrides::learndash_get_administrators_group_ids( $group_leader_id );
$can_the_user_manage_this_group = SharedFunctions::can_user_manage_this_group( $group_leader_id, absint( $request->get_param( 'group-id' ) ), $user_group_ids );
// is the current user able to administer this group
if ( false === $can_the_user_manage_this_group ) {
$data['message'] = __( 'You do not have permission to manage this group.', 'uncanny-learndash-groups' );
$data['error'] = 'invalid-group-id';
wp_send_json_error( $data );
}
$group_id = absint( $request->get_param( 'group-id' ) );
$users = apply_filters( 'ulgm_download_users_keys', Group_Management_Helpers::get_unused__key_users_data( $group_id ), $group_id );
$csv = '';
if ( ! empty( $users ) ) {
$headers = apply_filters( 'ulgm_download_keys_header', "Group,Keyn", $group_id );
$csv .= $headers;
foreach ( $users as $row ) {
$csv .= implode( ',', $row ) . "n";
}
} else {
$headers = apply_filters( 'ulgm_download_keys_header', "Group,Keyn", $group_id );
$csv .= $headers;
}
// File name
$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, $group_leader_id );
/// Trigger file creation to frontend
$data['reload'] = false;
$data['call_function'] = 'downloadCsv';
$data['function_vars'] = array(
'csvDataString' => $csv,
'fileName' => $file_name,
);
wp_send_json_success( $data );
}