Filter uncanny-learndash-groups

ulgm_maybe_resend_redemption_email

Filters whether a redemption email should be resent, allowing modification of the decision and email content.

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

Description

Filters whether to resend a redemption email. Allows modification of the email recipient, subject, and group ID before sending. Useful for customizing redemption email logic or conditionally preventing resends.


Usage

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

Parameters

$to (mixed)
This parameter is a boolean that indicates whether the redemption email should be resent.
$subject (mixed)
This parameter represents the email address of the recipient.
$group_id (mixed)
This parameter contains the subject of the redemption email.

Return Value

The filtered value.


Examples

/**
 * Example of a custom filter for the ulgm_maybe_resend_redemption_email hook.
 * This example adds a condition to prevent resending the redemption email
 * if the user has already redeemed their item within a specific timeframe.
 */
add_filter( 'ulgm_maybe_resend_redemption_email', 'my_custom_resend_redemption_email_logic', 10, 4 );

function my_custom_resend_redemption_email_logic( $send_mail, $to, $subject, $group_id ) {

	// Retrieve user meta to check for redemption status and timestamp.
	// This assumes user meta is stored in a specific way by your plugin.
	// You'll need to adapt this based on how your plugin actually stores redemption data.
	$user = get_user_by_email( $to ); // You might need a function to get user by email if not standard

	if ( $user ) {
		$last_redemption_timestamp = get_user_meta( $user->ID, 'last_redemption_timestamp', true );
		$redemption_cooldown_hours = apply_filters( 'my_redemption_cooldown_hours', 24 ); // Allow filtering the cooldown period

		if ( ! empty( $last_redemption_timestamp ) ) {
			// Convert the stored timestamp to a datetime object
			$last_redemption_date = new DateTime( "@" . $last_redemption_timestamp );
			$current_date         = new DateTime();
			$interval             = $last_redemption_date->diff( $current_date );

			// Check if the cooldown period has passed
			if ( $interval->h < $redemption_cooldown_hours ) {
				// If within the cooldown period, prevent resending the email.
				$send_mail = false;
			}
		}
	}

	// Return the boolean value, which will either allow or deny the email send.
	return $send_mail;
}

/**
 * Helper function to get a user object by email.
 * This is a hypothetical function and might need to be implemented or adapted
 * based on your WordPress setup and any user management plugins.
 */
function get_user_by_email( $email ) {
	$user = get_users( array( 'email' => $email ) );
	if ( ! empty( $user ) && is_array( $user ) ) {
		return $user[0];
	}
	return false;
}

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

public static function resend_redemption_email( $user_data, $redemption_template_subject, $redemption_template_body, $group_id ) {

		// Set up user data
		$user_email = $user_data['user_email'];
		$first_name = $user_data['first_name'];
		$last_name  = $user_data['last_name'];

		// Get the redemption key
		$redemption_key = $user_data['key'];

		// Filter #EmailEncoded variable
		$redemption_template_subject = str_ireplace( '#EmailEncoded', urlencode( $user_email ), $redemption_template_subject );
		$redemption_template_body    = str_ireplace( '#EmailEncoded', urlencode( $user_email ), $redemption_template_body );

		// Filter #email variable
		$redemption_template_subject = str_ireplace( '#Email', $user_email, $redemption_template_subject );
		$redemption_template_body    = str_ireplace( '#Email', $user_email, $redemption_template_body );

		// Filter #first_name variable
		$redemption_template_subject = str_ireplace( '#FirstName', $first_name, $redemption_template_subject );
		$redemption_template_body    = str_ireplace( '#FirstName', $first_name, $redemption_template_body );

		// Filter #last_name variable
		$redemption_template_subject = str_ireplace( '#LastName', $last_name, $redemption_template_subject );
		$redemption_template_body    = str_ireplace( '#LastName', $last_name, $redemption_template_body );

		// Filter #redemption_key variable
		$redemption_template_subject = str_ireplace( '#RedemptionKey', $redemption_key, $redemption_template_subject );
		$redemption_template_body    = str_ireplace( '#RedemptionKey', $redemption_key, $redemption_template_body );

		// Filter #SiteUrl variable
		$redemption_template_subject = str_ireplace( '#SiteUrl', site_url(), $redemption_template_subject );
		$redemption_template_body    = str_ireplace( '#SiteUrl', site_url(), $redemption_template_body );

		// Filter #LoginUrl variable
		$redemption_template_subject = str_ireplace( '#LoginUrl', wp_login_url(), $redemption_template_subject );
		$redemption_template_body    = str_ireplace( '#LoginUrl', wp_login_url(), $redemption_template_body );

		// Filter #GroupLeaderInfo variable
		$redemption_template_subject = str_ireplace( '#GroupLeaderInfo', self::get_group_leader_info( $group_id ), $redemption_template_subject );
		$redemption_template_body    = str_ireplace( '#GroupLeaderInfo', self::get_group_leader_info( $group_id ), $redemption_template_body );

		// Filter #Courses variable
		$redemption_template_subject = str_ireplace( '#Courses', self::get_group_courses( $group_id ), $redemption_template_subject );
		$redemption_template_body    = str_ireplace( '#Courses', self::get_group_courses( $group_id ), $redemption_template_body );

		// Remove escaped apostrophes
		$redemption_template_subject = str_replace( "'", "'", $redemption_template_subject );
		$redemption_template_body    = str_replace( "'", "'", $redemption_template_body );

		$to      = apply_filters( 'ulgm_redemption_email_to', $user_email, $user_data );
		$subject = apply_filters( 'ulgm_redemption_email_subject', $redemption_template_subject, $user_data );
		$body    = apply_filters( 'ulgm_redemption_email_body', $redemption_template_body, $user_data );

		if ( ! class_exists( 'WP_Better_Emails' ) || ( false === preg_match( '/<DOCTYPE/', $body ) && false === preg_match( '/<head>/', $body ) ) ) {
			$body = wpautop( $body );
		}
		$send_mail = apply_filters( 'ulgm_maybe_resend_redemption_email', true, $to, $subject, $group_id );
		if ( $send_mail ) {
			$redemption_email = SharedFunctions::wp_mail( $to, $subject, $body, self::get_headers() );

			//If the mail is successful let a a fake user and group meta
			if ( is_wp_error( $redemption_email ) ) {
				return false;
			}
		}

		return true;
	}


Scroll to Top