Filter uncanny-learndash-groups

groups_email_get_group_leader_info_token

Filters the information for a group leader to be used in an email token.

add_filter( 'groups_email_get_group_leader_info_token', $callback, 10, 3 );

Description

Filters the information used to represent a group leader in email notifications. Developers can modify the `$group_leader_info` output to customize how group leader details are displayed or formatted before being included in an email. This filter fires after group leaders are retrieved but before their information is compiled into a string.


Usage

add_filter( 'groups_email_get_group_leader_info_token', 'your_function_name', 10, 3 );

Parameters

$group_leader_info (mixed)
This parameter contains the information about the group leader, which can be filtered by other plugins or themes.
$group_id (mixed)
This parameter contains the information about the group leader, which can be modified by the filter.
$group_leaders (mixed)
This parameter contains the ID of the group for which leader information is being retrieved.

Return Value

The filtered value.


Examples

/**
 * Example of filtering the group leader information.
 * This function will append a unique identifier to the group leader's email address
 * if the group leader is also an administrator of the site.
 *
 * @param string $group_leader_info The original group leader information string.
 * @param int    $group_id          The ID of the group.
 * @param object $group_leaders     An object containing group leader data.
 *
 * @return string The modified group leader information string.
 */
add_filter( 'groups_email_get_group_leader_info_token', function( $group_leader_info, $group_id, $group_leaders ) {

	// Only modify if there are group leaders and the group ID is valid
	if ( ! empty( $group_leaders ) && is_numeric( $group_id ) && $group_id > 0 ) {

		// Check if the current user has administrator privileges
		if ( current_user_can( 'manage_options' ) ) {
			$modified_info = '';
			foreach ( $group_leaders as $group_leader ) {
				// Assuming $group_leader->data->user_email exists and is the email
				if ( isset( $group_leader->data->user_email ) ) {
					$modified_info .= $group_leader->data->user_email . '-siteadmin' . "rn";
				} else {
					// Fallback to original if email is not available
					$modified_info .= $group_leader_info; // This might not be ideal, could parse original if needed
				}
			}
			// Replace the original group_leader_info with the modified one
			// In a real-world scenario, you'd likely want to parse $group_leader_info
			// and modify specific parts rather than a full replacement, but for example,
			// this demonstrates modification.
			// A more robust approach would be to rebuild the string from scratch.
			// For simplicity here, we'll assume we're just adding to the end if the loop didn't cover it.
			// Let's assume the original $group_leader_info is already formatted as we expect.
			// A better approach: build the string from scratch within the filter.
			$new_group_leader_info = '';
			foreach ( $group_leaders as $group_leader ) {
				if ( isset( $group_leader->data ) && isset( $group_leader->data->ID ) ) {
					$user_id = $group_leader->data->ID;
					$first_name = get_user_meta( $user_id, 'first_name', true );
					$last_name = get_user_meta( $user_id, 'last_name', true );
					$email = get_user_meta( $user_id, 'email', true ); // Assuming email might be a meta key if not default user_email

					if ( empty( $email ) && isset( $group_leader->data->user_email ) ) {
						$email = $group_leader->data->user_email;
					}

					$name_parts = [];
					if ( ! empty( $first_name ) ) {
						$name_parts[] = $first_name;
					}
					if ( ! empty( $last_name ) ) {
						$name_parts[] = $last_name;
					}
					$display_name = implode( ' ', $name_parts );

					if ( ! empty( $display_name ) && ! empty( $email ) ) {
						$new_group_leader_info .= $display_name . ' - ';
					}

					if ( ! empty( $email ) ) {
						// Append siteadmin suffix if the user is a site admin
						if ( user_can( $user_id, 'manage_options' ) ) {
							$new_group_leader_info .= $email . '-siteadmin' . "rn";
						} else {
							$new_group_leader_info .= $email . "rn";
						}
					}
				}
			}
			return $new_group_leader_info;
		}
	}

	// Return the original if no modifications were made or conditions weren't met
	return $group_leader_info;

}, 10, 3 );

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:1789

public static function get_group_leader_info( $group_id ) {

		// Default values
		$group_id          = absint( $group_id );
		$group_leaders     = (object) array();
		$group_leader_info = '';

		if ( 0 !== $group_id ) {

			$group_leaders = LearndashFunctionOverrides::learndash_get_groups_administrators( $group_id );

			if ( ! empty( $group_leaders ) ) {
				foreach ( $group_leaders as $group_leader ) {

					if ( isset( $group_leader->data ) && isset( $group_leader->data->ID ) && absint( $group_leader->data->ID ) ) {

						$first_name = get_user_meta( $group_leader->data->ID, 'first_name', true );
						if ( ! empty( $first_name ) ) {
							$first_name = $first_name . ' ';
						} else {
							$first_name = '';
						}

						$last_name = get_user_meta( $group_leader->data->ID, 'last_name', true );
						if ( ! empty( $last_name ) ) {
							$last_name = $last_name . ' ';
						} else {
							$last_name = '';
						}

						$add_dash = '';
						if ( ! empty( $first_name ) || ! empty( $last_name ) ) {
							$add_dash = '- ';
						}

						$email             = $group_leader->data->user_email . "rn";
						$group_leader_info .= $first_name . $last_name . $add_dash . $email;
					}
				}
			}
		}

		$group_leader_info = apply_filters( 'groups_email_get_group_leader_info_token', $group_leader_info, $group_id, $group_leaders );

		return $group_leader_info;
	}

Scroll to Top