LLMS_Engagements::handle_email( mixed[] $args )

Send an email engagement


Description Description

This is called via do_action() by the ‘maybe_trigger_engagement’ function in this class.


Top ↑

Parameters Parameters

$args

(mixed[]) (Required) An array of arguments from the triggering hook.

  • (int) WP_User ID.
  • '1'
    (int) WP_Post ID of the email.
  • '2'
    (int|string) WP_Post ID of the related triggering post or an empty string for engagements with no related post.


Top ↑

Return Return

(bool|WP_Error) Returns true on success or a WP_Error when the email has failed or is prevented.


Top ↑

Source Source

File: includes/class.llms.engagements.php

		$related_id = $args[2];
		$meta_key   = '_email_sent';

		$msg = sprintf( __( 'Email #%1$d to user #%2$d triggered by %3$s', 'lifterlms' ), $email_id, $person_id, $related_id ? '#' . $related_id : 'N/A' );

		if ( $related_id ) {

			if ( in_array( get_post_type( $related_id ), llms_get_enrollable_status_check_post_types(), true ) && ! llms_is_user_enrolled( $person_id, $related_id ) ) {

				// User is no longer enrolled in the triggering post. We should skip the send.
				llms_log( $msg . ' ' . __( 'not sent due to user enrollment issues.', 'lifterlms' ), 'engagement-emails' );
				return new WP_Error( 'llms_engagement_email_not_sent_enrollment', $msg, $args );
			} elseif ( absint( $email_id ) === absint( llms_get_user_postmeta( $person_id, $related_id, $meta_key ) ) ) {

				// User has already received this email, don't send it again.
				llms_log( $msg . ' ' . __( 'not sent because of dupcheck.', 'lifterlms' ), 'engagement-emails' );
				return new WP_Error( 'llms_engagement_email_not_sent_dupcheck', $msg, $args );
			}
		}

		// Setup the email.
		$email = LLMS()->mailer()->get_email( 'engagement', compact( 'person_id', 'email_id', 'related_id' ) );
		if ( $email && $email->send() ) {

			if ( $related_id ) {
				llms_update_user_postmeta( $person_id, $related_id, $meta_key, $email_id );
			}

			llms_log( $msg . ' ' . __( 'sent successfully.', 'lifterlms' ), 'engagement-emails' );
			return true;
		}

		// Error sending email.
		llms_log( $msg . ' ' . __( 'not sent due to email sending issues.', 'lifterlms' ), 'engagement-emails' );
		return new WP_Error( 'llms_engagement_email_not_sent_error', $msg, $args );

	}

	/**
	 * Log debug data to the WordPress debug.log file
	 *
	 * @since 2.7.9
	 * @since 3.12.0 Unknown.
	 *


Top ↑

Changelog Changelog

Changelog
Version Description
4.4.3 Fixed different emails triggered by the same related post not sent because of a wrong duplicate check. Fixed dupcheck log message and error message which reversed the email and person order.
4.4.1 Use postmeta helpers for dupcheck and postmeta insertion. Add a return value in favor of void. Log successes and failures to the engagement-emails log file instead of the main llms log.
3.8.0 Unknown.
2.3.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.