LLMS_Engagement_Handler::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.
  • '3'
    (int) WP_Post ID of the engagement post.


Top ↑

Return Return

(bool|WP_Error[]) Returns true on success and array of error objects when the email has failed or is prevented.


Top ↑

Source Source

File: includes/class-llms-engagement-handler.php

	public static function handle_email( $args ) {

		$can_process = self::can_process( 'email', ...$args );
		if ( true !== $can_process ) {
			return $can_process;
		}

		list( $person_id, $email_id, $related_id ) = $args;

		$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 && 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 array( 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 array( new WP_Error( 'llms_engagement_email_not_sent_error', $msg, $args ) );

	}


Top ↑

Changelog Changelog

Changelog
Version Description
6.0.0 Moved from LLMS_Engagements class. Removed engagement debug logging. Ensure related post, email template, and engagement all exist and are published before processing.
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.