# double-opt-in/3.0.2/compatibility/avada/AvadaFrontend.class.php

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

- Page: https://pluginprobe.com/plugins/double-opt-in/3.0.2/code/compatibility/avada/AvadaFrontend.class.php
- Raw: https://pluginprobe.com/plugins/double-opt-in/3.0.2/raw/compatibility/avada/AvadaFrontend.class.php
- Modified: 2024-08-13T13:52:36+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.2/code/compatibility/avada/AvadaFrontend.class.php#L10-L20`.

```php
<?php

namespace forge12\contactform7\CF7DoubleOptIn {
	if ( ! defined( 'ABSPATH' ) ) {
		exit;
	}

	/**
	 * Class Frontend
	 * Responsible to handle the frontend of the Double OptIn
	 *
	 * @package forge12\contactform7\CF7DoubleOptIn
	 */
	class AvadaFrontend extends OptInFrontend {
		/**
		 * Admin constructor.
		 */
		public function __construct() {
			parent::__construct( 'avada' );

			if ( ! isset( $_GET['optin'] ) ) {
				add_filter( 'fusion_form_send_mail_args', array( $this, 'onSubmit' ), 10, 3 );
			}
		}

		/**
		 * 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 {
			/**
			 * Get the Form Data
			 */
			$formData = maybe_unserialize( $OptIn->get_content() );

			/**
			 * Sanitize the data
			 */
			$formData = SanitizeHelper::sanitize_array( $formData );

			/**
			 * check if the formdata is available
			 */
			if ( ! isset( $formData['data'] ) ) {
				return;
			}

			/**
			 * Transform the Data to a key=>value string.
			 *
			 * @example
			 *         $data = ['e-mail' => 'info@forge12.com', 'subject' => 'Betreff']
			 *         to
			 *         $output = e-mail=info@forge12.com&subject=Betreff
			 */
			$data           = $formData['data'];
			$formDataString = [];
			foreach ( $data as $key => $value ) {
				$formDataString[] = $key . '=' . $value;
			}
			$formDataString = implode( "&", $formDataString );

			/**
			 * Reformat them to work with avada submit
			 */
			$_POST = array(
				'formData'           => $formDataString,
				'field_labels'       => json_encode( $formData['field_labels'] ),
				'field_types'        => json_encode( $formData['field_types'] ),
				'hidden_field_names' => json_encode( $formData['hidden_field_names'] )
			);

			$_POST['form_id'] = $OptIn->get_cf_form_id();

			/**
			 * Load the Avada Form Submit Object
			 */
			require_once( 'AvadaFormSubmit.class.php' );

			$AvadaFormSubmit = new AvadaFormSubmit();

			/**
			 * Submit the form
			 */
			$AvadaFormSubmit->submit( $OptIn->get_cf_form_id(), [] );
		}

		/**
		 * On Form Submit add the double optin if enabled
		 *
		 * @param array $parameter
		 * @param int   $post_id
		 * @param array $data
		 */
		public function onSubmit( $form_parameter, $submission_id, $form_data ) {
			$post_id = isset( $_POST['form_id'] ) ? absint( sanitize_text_field( wp_unslash( $_POST['form_id'] ) ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification

			if ( ! $post_id ) {
				return $form_parameter;
			}

			$parameter = CF7DoubleOptIn::getInstance()->getParameter( $post_id );

			if ( $this->isOptinEnabled( $post_id ) ) {

				// Get the Post Object
				$form = get_post( $post_id );

				if ( ! $form ) {
					return $form_parameter;
				}

				$form_data['form_parameter'] = $form_parameter;

				// Get the OptIn Object
				$OptIn = $this->maybeCreateOptIn( $post_id, do_shortcode( $form->post_content ), $form_data, $form_parameter['attachments'] );

				/**
				 * Skip if OptIn not created
				 */
				if ( ! $OptIn ) {
					return $form_parameter;
				}

				// Add the Form URL
				$parameter['formUrl'] = $form_data['submission']['source_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
				 */
				$headers = 'MIME-Version: 1.0' . "\r\n";
				$headers .= 'Content-type: text/html; charset=UTF8' . "\r\n";

				/**
				 * 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'          => $form_data['data'][ $parameter['recipient'] ],
					'use_html'           => true,
					'additional_headers' => $headers
				] );

				/*
				 * Adjust the header
				 */
				$args['additional_headers'] .= sprintf( 'From: %s <%s>' . "\r\n", $args['sender_name'], $args['sender'] );

				$args['additional_headers'] .= 'From: ' . $args['sender'] . "\r\n";

				\wp_mail(
					$args['recipient'],
					$args['subject'],
					$args['body'],
					$args['additional_headers']
				);

				/**
				 * 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, $post_id );

				/**
				 * Die after sending the form
				 */
				die( wp_json_encode( [ 'status' => 'success', 'info' => 'opt-in send' ] ) );
			}

			return $form_parameter;
		}

		/**
		 * Retrieves the recipient value from the given parameters.
		 *
		 * @param string $recipient     The recipient parameter.
		 * @param array  $formParameter The form parameters.
		 * @param array  $postParameter The post parameters.
		 *
		 * @return string The recipient value if it exists in the post parameters, or an empty string if it doesn't.
		 */
		public function getRecipient( string $recipient, array $formParameter, array $postParameter ): string {
			if ( ! isset( $postParameter['data'][ $formParameter['recipient'] ] ) ) {
				return '';
			}

			return $postParameter['data'][ $formParameter['recipient'] ];
		}
	}
}
```
