# double-opt-in/3.0.5/compatibility/avada/AvadaFormOptions.class.php

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

- Page: https://pluginprobe.com/plugins/double-opt-in/3.0.5/code/compatibility/avada/AvadaFormOptions.class.php
- Raw: https://pluginprobe.com/plugins/double-opt-in/3.0.5/raw/compatibility/avada/AvadaFormOptions.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.5/code/compatibility/avada/AvadaFormOptions.class.php#L10-L20`.

```php
<?php

namespace forge12\contactform7\CF7DoubleOptIn {
	if ( ! defined( 'ABSPATH' ) ) {
		exit;
	}

	/**
	 * Class AvadaFormOptions
	 * Add the OptIn Options to the Avada Form
	 *
	 * @package forge12\contactform7\CF7OptIn
	 */
	class AvadaFormOptions {
		/**
		 * Admin constructor.
		 */
		public function __construct() {
			add_action( 'admin_init', array( $this, 'addHooks' ) );
			add_action( 'admin_enqueue_scripts', array( $this, 'addStyles' ) );
			add_action( 'save_post', array( $this, 'save' ), 20, 1 );
			add_filter( 'awb_po_get_value', array( $this, 'get_value' ), 10, 2 );
		}

		public function get_value( $ret, $field_id ) {
			global $post;

			if ( ! $post || ! isset( $post->ID ) ) {
				return $ret;
			}

			$metadata = CF7DoubleOptIn::getInstance()->getParameter( $post->ID );

			foreach ( $metadata as $key => $value ) {
				$metadata[ 'doubleoptin[' . $key . ']' ] = $value;
			}

			if ( isset( $metadata[ $field_id ] ) ) {
				$ret = $metadata[ $field_id ];
			}

			return $ret;
		}

		/**
		 * Add the styles for the form
		 */
		public function addStyles( $hook ) {
			wp_enqueue_style( 'f12-avada-doubleoptin-admin-avada', plugins_url( 'assets/admin-avada-style.css', __FILE__ ) );

			wp_enqueue_script( 'f12-avada-doubleoptin-templateloader', plugins_url( 'assets/f12-avada-templateloader.js', __FILE__ ), array( 'jquery' ) );
			wp_localize_script( 'f12-avada-doubleoptin-templateloader', 'templateloader', array(
				'ajax_url'          => admin_url( 'admin-ajax.php' ),
				'nonce'             => wp_create_nonce( 'f12_doi_templateloader' ),
				'label_placeholder' => __( 'Please wait while we load the template...', 'double-opt-in' )
			) );
		}

		/**
		 * Add the hooks responsible to handle wordpress functions
		 */
		public function addHooks() {
			add_filter( 'avada_metabox_tabs', array( $this, 'addPanel' ), 10, 2 );
			add_filter( 'f12_cf7_doubleoptin_avada_form_panel', array( $this, 'render' ), 10, 1 );
			add_action( 'wpcf7_save_contact_form', array( $this, 'save' ), 10, 3 );
		}

		/**
		 * Add a custom panel to the contact form 7 options
		 */
		public function addPanel( array $panels, $post_type ) {
			global $post;

			if ( $post && $post->post_type == 'fusion_form' ) {
				if ( $post_type == 'default' ) {
					$panels['tabs_names']['f12cf7doubleoptin'] = __( 'Double-Opt-in', 'double-opt-in' );
					$panels['requested_tabs'][]                = 'f12cf7doubleoptin';
				}
			}

			return $panels;
		}

		/**
		 * On Contact Form save store the information in the database
		 *
		 * @param $postID
		 */
		public function save( $postID ) {
			if ( ! class_exists( '\Fusion_Data_PostMeta' ) ) {
				return;
			}

			if ( ! isset( $_POST[ \Fusion_Data_PostMeta::ROOT ] ) || ! isset( $_POST[ \Fusion_Data_PostMeta::ROOT ]['doubleoptin'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
				return;
			}

			$parameter = SanitizeHelper::sanitize_array( $_POST[ \Fusion_Data_PostMeta::ROOT ]['doubleoptin'] );
			$parameter = array_merge( $parameter, SanitizeHelper::sanitize_array( $_POST[ \Fusion_Data_PostMeta::ROOT ]['_doubleoptin'] ) );

			// load the default parameter
			$metadata = CF7DoubleOptIn::getInstance()->getParameter( $postID );

			// validated all parameter defined in the default parameter options and  sanitize them.
			foreach ( $metadata as $key => $value ) {
				if ( isset( $parameter[ $key ] ) ) {
					if ( $key == 'body' ) {
						$metadata[ $key ] = $parameter[ $key ];
					} else if ( $key == 'sender' ) {
						$metadata[ $key ] = $parameter[ $key ];
					} else if ( $key == 'page' || $key == 'enable' ) {
						$metadata[ $key ] = (int) $parameter[ $key ];
					} else {
						$metadata[ $key ] = sanitize_text_field( $parameter[ $key ] );
					}
				} else if ( $key == 'enable' ) {
					$metadata[ $key ] = 0;
				}
			}

			/**
			 * Adjust the Metadata before saving
			 *
			 * @param array $metadata
			 */
			$metadata = apply_filters( 'f12_cf7_doubleoptin_metadata_avada', $metadata );

			/**
			 * Adjust the Metadata before saving
			 *
			 * @param array $metadata
			 */
			$metadata = apply_filters( 'f12_cf7_doubleoptin_save_form', $metadata );

			update_post_meta( $postID, 'f12-cf7-doubleoptin', $metadata );
		}

		/**
		 * @return array
		 */
		private function getPages() {
			$value = array();
			$pages = get_pages();
			foreach ( $pages as $page ) {
				$value[ $page->ID ] = $page->post_title;
			}

			return $value;
		}

		/**
		 * @return array
		 */
		private function getCategories() {
			$atts = array(
				'perPage' => - 1,
				'orderBy' => 'name',
				'order'   => 'ASC'
			);

			$listOfCategories = Category::get_list( $atts, $numberOfPages );

			$value = array(
				0 => '---'
			);

			foreach ( $listOfCategories as $Category /** @var Category $Category */ ) {
				$value[ $Category->get_id() ] = $Category->get_name();
			}

			return $value;
		}

		/**
		 * Show the backend double opt in options
		 *
		 * @param array $tab_data
		 *
		 * @return array
		 */
		public function render( $tab_data ) {
			global $post;
			$metadata = CF7DoubleOptIn::getInstance()->getParameter( $post->ID );

			$checked = '';
			if ( $metadata['enable'] == 1 ) {
				$checked = 'checked="checked"';
			}

			$TemplateList = new HTMLSelect( '_fusion[doubleoptin][template]', array(
				'blank'           => 'blank',
				'newsletter_en'   => 'newsletter_en',
				'newsletter_en_2' => 'newsletter_en_2',
				'newsletter_en_3' => 'newsletter_en_3'
			), array(
				'class' => array( 'f12-cf7-templateloader' ),
				'style' => array( 'display:none;' )
			) );
			$TemplateList->setOptionSelectedByKey( $metadata['template'] );


			$fields = [
				[
					'id'          => 'doubleoptin[enable]',
					'label'       => __( 'Enable', 'double-opt-in' ),
					'value'       => [ 1 => __( 'Enable', 'double-opt-in' ) ],
					'description' => __( 'Enable this checkbox to activate the Double-Opt-In Mail for this formular.', 'double-opt-in' ) . '</p></div><div class="pyre_field"><p><input type="checkbox" id="pyre_doubleoptin[enable]" name="_fusion[doubleoptin][enable]" value="1" ' . $checked . '>',
					'default'     => 0,
					'dependency'  => '',
					'type'        => 'custom'
				],
				[
					'id'          => 'doubleoptin[category]',
					'label'       => __( 'Category', 'double-opt-in' ),
					'choices'     => $this->getCategories(),
					'description' => __( 'Select the category for the Opt-In.', 'double-opt-in' ),
					'default'     => 0,
					'dependency'  => '',
					'type'        => 'select'
				],
				[
					'id'          => 'doubleoptin[page]',
					'label'       => __( 'Confirmation Page', 'double-opt-in' ),
					'choices'     => $this->getPages(),
					'description' => __( 'Select the page that should be displayed after the user clicks on the confirmation link', 'double-opt-in' ),
					'default'     => 0,
					'dependency'  => '',
					'type'        => 'select'
				],
				[
					'id'          => 'doubleoptin[recipient]',
					'label'       => __( 'To', 'double-opt-in' ),
					'description' => __( 'This field defines who will receive the Double-Opt-In mail. Use the "Field Name" defined for the User E-Mail.', 'double-opt-in' ),
					'default'     => '',
					'dependency'  => '',
					'type'        => 'text'
				],
				[
					'id'          => 'doubleoptin[sender]',
					'label'       => __( 'From E-Mail', 'double-opt-in' ),
					'choices'     => $metadata['sender'],
					'description' => __( 'This field defines who will sent the mail. You can either enter the e-Mail (e.g.: your-email@yourdomain.de) or use a placeholder (e.g.: [_site_admin_email]).', 'double-opt-in' ),
					'default'     => '',
					'dependency'  => '',
					'type'        => 'text'
				],
				[
					'id'          => 'doubleoptin[sender_name]',
					'label'       => __( 'From Name', 'double-opt-in' ),
					'choices'     => $metadata['sender_name'],
					'description' => __( 'This field defines the name for outgoing mails.', 'double-opt-in' ),
					'default'     => '',
					'dependency'  => '',
					'type'        => 'text'
				],
				[
					'id'          => 'doubleoptin[subject]',
					'label'       => __( 'Subject', 'double-opt-in' ),
					'choices'     => $metadata['subject'],
					'description' => __( 'Enter a custom subject for the Double-Opt-In mail (e.g.: Please confirm your registration).', 'double-opt-in' ),
					'default'     => '',
					'dependency'  => '',
					'type'        => 'text'
				],
				[
					'id'          => 'doubleoptin[template]',
					'label'       => __( 'Template', 'double-opt-in' ),
					'description' => __( 'Pick the Template for your double opt-in Mail.', 'double-opt-in' ) . '</p></div><div class="pyre_field"><p>' . $TemplateList->get(),
					'default'     => 0,
					'dependency'  => '',
					'type'        => 'custom'
				],
				/*[
					'id' => 'doubleoptin[template]',
					'label' => __('Template', 'double-opt-in'),
					'choices' => [
						'blank' => 'Custom',
						'newsletter_en' => 'Newsletter 1',
						'newsletter_en_2' => 'Newsletter 2',
						'newsletter_en_3' => 'Newsletter 3'
					],
					'description' => __('Pick the Template for your double opt in Mail.', 'double-opt-in'),
					'default' => 'blank',
					'dependency' => '',
					'type' => 'select'
				],*/
				[
					'id'          => 'doubleoptin[body]',
					'label'       => __( 'Message body', 'double-opt-in' ),
					'choices'     => $metadata['body'],
					'description' => __( 'Enter the Message you want to sent to your customer. Do not forget to add the Double-Opt-In tag ([doubleoptinlink]) e.g.: <pre><code>&lt;a href="[doubleoptinlink]"&gt;Confirm the registration&lt;/a&gt;</code></pre>', 'double-opt-in' ),
					'default'     => '',
					'dependency'  => '',
					'type'        => 'textarea'
				],
			];

			/**
			 * Used to render settings for Avada
			 *
			 * @param array $fields
			 * @param array $metadata
			 *
			 * @since 3.0.0
			 */
			$tab_data['f12cf7doubleoptin']['fields'] = apply_filters( 'f12_cf7_formular_settings_after_avada', $fields, $metadata );

			return $tab_data;
		}
	}
}

/**
 * Extra Function to use the Avada From Options
 */

namespace {
	function avada_page_options_tab_f12cf7doubleoptin() {
		return apply_filters( 'f12_cf7_doubleoptin_avada_form_panel', [] );
	}
}
```
