Email_Notifications::handle( awsmug\Torro_Forms\DB_Objects\Submissions\Submission $submission, awsmug\Torro_Forms\DB_Objects\Forms\Form $form )

Handles the action for a specific form submission.

Description

Parameters

$submission

(awsmug\Torro_Forms\DB_Objects\Submissions\Submission) (Required) Submission to handle by the action.

$form

(awsmug\Torro_Forms\DB_Objects\Forms\Form) (Required) Form the submission applies to.

Return

(bool|WP_Error) True on success, error object on failure.

Source

File: src/modules/actions/email-notifications.php

	public function handle( $submission, $form ) {
		$notifications = $this->get_form_option( $form->id, 'notifications', array() );
		if ( empty( $notifications ) ) {
			return true;
		}

		$dynamic_template_tags = $this->get_dynamic_template_tags( $form, true );

		foreach ( $dynamic_template_tags as $slug => $data ) {
			if ( isset( $data['email_support'] ) ) {
				$email_support = (bool) $data['email_support'];

				unset( $data['email_support'] );

				if ( $email_support ) {
					$this->template_tag_handler_email_only->add_tag( $slug, $data );
				}
			}

			$this->template_tag_handler->add_tag( $slug, $data );
			$this->template_tag_handler_complex->add_tag( $slug, $data );
		}

		$error = new WP_Error();

		add_filter( 'wp_mail_content_type', array( $this, 'override_content_type' ) );
		add_filter( 'wp_mail_from_name', array( $this, 'override_from_name' ) );
		add_filter( 'wp_mail_from', array( $this, 'override_from_email' ) );
		add_action( 'wp_mail_failed', array( $this, 'store_phpmailer_error' ) );

		$email_header_fields = array(
			'reply_email' => 'Reply-To',
			'cc_email'    => 'Cc',
			'bcc_email'   => 'Bcc',
		);

		foreach ( $notifications as $notification ) {
			if ( empty( $notification['to_email'] ) || empty( $notification['subject'] ) || empty( $notification['message'] ) ) {
				continue;
			}

			foreach ( $notification as $key => $value ) {
				switch ( $key ) {
					case 'from_email':
					case 'reply_email':
					case 'to_email':
					case 'cc_email':
					case 'bcc_email':
						$notification[ $key ] = $this->template_tag_handler_email_only->process_content( $value, array( $form, $submission ) );
						break;
					case 'message':
						$notification[ $key ] = $this->template_tag_handler_complex->process_content( $value, array( $form, $submission ) );
						break;
					default:
						$notification[ $key ] = $this->template_tag_handler->process_content( $value, array( $form, $submission ) );
				}
			}

			$notification['message'] = wpautop( $notification['message'] );

			$this->from_name  = $notification['from_name'];
			$this->from_email = $notification['from_email'];

			$headers = array();
			foreach ( $email_header_fields as $field => $header ) {
				if ( ! empty( $notification[ $field ] ) ) {
					$headers[] = $header . ': ' . $notification[ $field ];
				}
			}

			$sent = wp_mail( $notification['to_email'], $notification['subject'], $notification['message'], $headers );
			if ( ! $sent ) {
				/* translators: %s: email address */
				$error_message = sprintf( __( 'Email notification to %s could not be sent.', 'torro-forms' ), $notification['to_email'] );
				if ( $this->phpmailer_error ) {
					/* translators: %s: error message */
					$error_message .= ' ' . sprintf( __( 'Original error message: %s', 'torro-forms' ), $this->phpmailer_error->get_error_message() );

					$this->phpmailer_error = null;
				}

				$error->add( 'email_notification_not_sent', $error_message );
			}
		}

		$this->from_name  = '';
		$this->from_email = '';

		remove_filter( 'wp_mail_content_type', array( $this, 'override_content_type' ) );
		remove_filter( 'wp_mail_from_name', array( $this, 'override_from_name' ) );
		remove_filter( 'wp_mail_from', array( $this, 'override_from_email' ) );
		remove_action( 'wp_mail_failed', array( $this, 'store_phpmailer_error' ) );

		foreach ( $dynamic_template_tags as $slug => $data ) {
			if ( isset( $data['email_support'] ) && $data['email_support'] ) {
				$this->template_tag_handler_email_only->remove_tag( $slug );
			}

			$this->template_tag_handler->remove_tag( $slug );
			$this->template_tag_handler_complex->remove_tag( $slug );
		}

		if ( ! empty( $error->errors ) ) {
			return $error;
		}

		return true;
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.