# double-opt-in/3.0.62/compatibility/cf7/CF7Frontend.class.php

Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification, version 3.0.62. 282 lines.

- Page: https://pluginprobe.com/plugins/double-opt-in/3.0.62/code/compatibility/cf7/CF7Frontend.class.php
- Raw: https://pluginprobe.com/plugins/double-opt-in/3.0.62/raw/compatibility/cf7/CF7Frontend.class.php
- Modified: 2025-05-23T11:43:56+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/double-opt-in/3.0.62/code/compatibility/cf7/CF7Frontend.class.php#L10-L20`.

```php
<?php

namespace forge12\contactform7\CF7DoubleOptIn {
	if ( ! defined( 'ABSPATH' ) ) {
		exit;
	}

	use forge12\plugins\ContactForm7;

	/**
	 * Class Frontend
	 * Responsible to handle the frontend of the Double OptIn
	 *
	 * @package forge12\contactform7\CF7DoubleOptIn
	 */
	class CF7Frontend extends OptInFrontend {
		/**
		 * Admin constructor.
		 */
		public function __construct() {

			parent::__construct( 'cf7' );
			add_action( 'wpcf7_before_send_mail', array( $this, 'onSubmit' ), 5, 3 );
		}

		/**
		 * Get the recipient email address
		 *
		 * @param string $recipient     The recipient email address
		 * @param array  $formParameter The form parameter array
		 * @param array  $postParameter The post parameter array
		 *
		 * @return string The sanitized recipient email address
		 */
		public function getRecipient( string $recipient, array $formParameter, array $postParameter ): string {
			/**
			 * Ensure the recipient has been defined in the settings
			 */
			if ( ! isset( $formParameter['recipient'] ) ) {
				return $recipient;
			}

			$recipient = $formParameter['recipient'];

			$recipient = str_replace( '[', '', $recipient );
			$recipient = str_replace( ']', '', $recipient );

			/**
			 * Get the Recipient from the post parameter
			 */
			if ( isset( $postParameter[ $recipient ] ) ) {
				$recipient = sanitize_email( $_POST[ $recipient ] );
			}

			return $recipient;
		}

		/**
		 * Send the default email for the given OptIn. (Original Mail after Opt-In Confirmation)
		 *
		 * @param OptIn $OptIn The OptIn object containing the email content and form ID.
		 *
		 * @return void
		 */
		public function sendDefaultMail( OptIn $OptIn ): void {
			if(!function_exists('wpcf7')) {
				return;
			}
			/**
			 * Prepare the Post Data
			 */
			$data = maybe_unserialize( $OptIn->get_content() );

			/**
			 * Sanitize the Data
			 */
			$_POST = SanitizeHelper::sanitize_array( $data );

			/**
			 * Prepare the Contact Form
			 */
			$ContactForm = \WPCF7_ContactForm::get_instance( $OptIn->get_cf_form_id() );

			/**
			 * Prepare the Submission Instance
			 */
			$submission = \WPCF7_Submission::get_instance( $ContactForm );
		}

		/**
		 * On Form Submit add the double optin if enabled
		 *
		 * @param $form       \WPCF7_ContactForm
		 * @param bool
		 * @param $submission \WPCF7_Submission
		 */
		public function onSubmit( $form, &$abort, $submission ) {
			$parameter = CF7DoubleOptIn::getInstance()->getParameter( $form->id() );

			if ( $this->isOptinEnabled( $form->id() ) ) {
				// Remove Contact Form 7 DB hook to ensure the optin mail is not saved
				remove_action( 'wpcf7_before_send_mail', 'cfdb7_before_send_mail' );

				if(apply_filters('f12_cf7_doubleoptin_skip_option', false, $form->id(), $submission->get_posted_data(), $this->type)){
					return; // Skip if requested
				}

				// Get the OptIn Object
				$OptIn = $this->maybeCreateOptIn( $form->id(), $form->form_html(), $submission->get_posted_data(), $submission->uploaded_files() );

				/**
				 * Skip if OptIn not created
				 */
				if ( ! $OptIn ) {
					return;
				}

				// Add the Form URL to the parameters
				$parameter['formUrl'] = $submission->get_meta( 'url' );

				// Get the Body content
				$body = $parameter['body'];

				// Replace the Placeholders within the body content.
				$body = $this->addPlaceholders( $body, $OptIn, $parameter );

				/**
				 * Body
				 *
				 * Filters the Body of the OptIn Mail. Allows developers to adjust the content
				 * before transmission.
				 *
				 * @param string $body The content of the OptIn Mail.
				 *
				 * @since 2.3.3
				 */
				$body = apply_filters( 'f12_cf7_doubleoptin_body', $body );

				/**
				 * Set the OptIn Mail Content to the OptIn Object.
				 */
				$OptIn->set_mail_optin( $body );
				$OptIn->save();

				/**
				 * Recipient
				 */
				$additional_headers = '';

				/**
				 * OptIn Mail Arguments
				 *
				 * Filters the Arguments for the OptIn Mail. This hook allows developers
				 * to add additional content to the Mail.
				 *
				 * @formatter:off
				 *
				 * @param $args {
				 *      @type string    $subject            The Subject of the OptIn Mail
				 *      @type string    $body               The Body of the OptIn Mail,
				 *      @type string    $sender             The Sender of the OptIn Mail
				 *      @type string    $recipient          The recipient of the OptIn Mail
				 *      @type bool      $use_html           Enable/Disable HTML for the OptIn Mail. Default: true
				 *      @type string    $additional_headers Additional Header Information for the Mail.
				 * }
				 *
				 * @formatter:on
				 *
				 * @since    2.3.3
				 */
				$args = apply_filters( 'f12-cf7-doubleoptin-cf7-args', [
					'subject'            => $parameter['subject'],
					'body'               => $body,
					'sender'             => $parameter['sender'],
					'sender_name'        => $parameter['sender_name'],
					'recipient'          => $parameter['recipient'],
					'use_html'           => true,
					'additional_headers' => $additional_headers
				] );

				/**
				 * Set the Header
				 */
				if ( ! empty( $parameter['sender_name'] ) ) {
					$args['additional_headers'] .= 'From: ' . $args['sender_name'] . ' <' . $args['sender'] . '>';
				}

				// Prepare the Opt-In Mail
				\WPCF7_Mail::send( $args, 'mail' );

				// Skip all mails following
				add_filter( 'wpcf7_skip_mail', '__return_true' );

				/**
				 * Action after the OptIn Mail has been sent.
				 *
				 * Allows other developers to do additional tasks after the OptIn Mail has been submitted.
				 *
				 * @formatter:off
				 *
				 * @param \WPCF7_ContactForm $form      The Contact Form 7 Form Object
				 * @param int                $formId    The Post ID of the CF7 Form.
				 *
				 * @formatter:on
				 *
				 * @since    2.3.3
				 */
				do_action( 'f12_cf7_doubleoptin_sent', $form, $form->id() );

			} else if ( isset( $_GET['optin'] ) ) {
				/**
				 * Add Files / Attachments to the OptIn Mail.
				 */
				$hash = sanitize_text_field( $_GET['optin'] );

				/**
				 * Get the OptIn Object
				 */
				$OptIn = OptIn::get_by_hash( $hash );

				/**
				 * Skip if the OptIn has not been found.
				 */
				if ( null == $OptIn ) {
					return;
				}

				/**
				 * Get the Files from the OptIn Object
				 */
				$files = maybe_unserialize( $OptIn->get_files() );

				/**
				 * Skip if no files has been found
				 */
				if ( empty( $files ) ) {
					return;
				}

				/**
				 * Loop through all attachments/files.
				 */
				foreach ( $files as $file ) {
					/**
					 * Skip file if the name is empty
					 */
					if ( ! empty( $file ) ) {
						continue;
					}

					/**
					 * Enable / Disable the OptIn Mail Attachment for Mail 1.
					 *
					 * Allows Developers to Enable/Disable Mail attachments dynamically.
					 *
					 * @param bool  $status Either add attachments (true) or not (false). Default: true
					 * @param OptIn $OptIn  The OptIn Object.
					 *
					 * @since 2.3.3
					 */
					if ( apply_filters( 'f12_cf7_doubleoptin_files_mail_1', true, $OptIn ) ) {
						$submission->add_extra_attachments( $file );
					}

					/**
					 * Enable / Disable the OptIn Mail Attachment for Mail 2.
					 *
					 * Allows Developers to Enable/Disable Mail attachments dynamically.
					 *
					 * @param bool  $status Either add attachments (true) or not (false). Default: true
					 * @param OptIn $OptIn  The OptIn Object.
					 *
					 * @since 2.3.3
					 */
					if ( apply_filters( 'f12_cf7_doubleoptin_files_mail_2', true, $OptIn ) ) {
						$submission->add_extra_attachments( $file, 'mail_2' );
					}
				}
			}
		}
	}
}
```
