logger = $logger; $this->get_logger()->debug( 'Admin constructor called', [ 'plugin' => 'double-opt-in', 'class' => __CLASS__, 'method' => __METHOD__, ] ); add_action( 'admin_init', [ $this, 'addHooks' ] ); $this->get_logger()->debug( 'Hook admin_init registered', [ 'plugin' => 'double-opt-in', ] ); add_action( 'admin_enqueue_scripts', [ $this, 'addStyles' ] ); $this->get_logger()->debug( 'Hook admin_enqueue_scripts registered', [ 'plugin' => 'double-opt-in', ] ); } public function get_logger() { return $this->logger; } /** * Add the styles for the form */ public function addStyles( $hook ) { // Only load the DOI admin scripts — and their privileged // `f12_doi_details` nonce — on the screens that actually use them // (Contact Form 7's admin/editor and the DOI admin), NOT on every // wp-admin page. Broadcasting the nonce everywhere let a // low-privilege user scrape it from e.g. profile.php. $hook = (string) $hook; $onPluginScreen = ( strpos( $hook, 'wpcf7' ) !== false ) || ( strpos( $hook, 'f12-doi' ) !== false ) || ( strpos( $hook, 'f12-cf7-doubleoptin' ) !== false ); if ( ! $onPluginScreen ) { return; } $this->get_logger()->debug( 'Enqueuing admin styles and scripts', [ 'plugin' => 'double-opt-in', 'class' => __CLASS__, 'method' => __METHOD__, 'hook' => $hook, ] ); wp_enqueue_script( 'f12-cf7-doubleoptin-admin', plugins_url( 'assets/f12-cf7-popup.js', __FILE__ ), [ 'jquery' ], defined( 'FORGE12_OPTIN_VERSION' ) ? FORGE12_OPTIN_VERSION : false, true ); $this->get_logger()->debug( 'Admin popup script enqueued', [ 'plugin' => 'double-opt-in', ] ); wp_localize_script( 'f12-cf7-doubleoptin-admin', 'doi', [ 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'f12_doi_details' ), ] ); $this->get_logger()->debug( 'Admin popup script localized', [ 'plugin' => 'double-opt-in', ] ); wp_enqueue_script( 'f12-cf7-doubleoptin-templateloader', plugins_url( 'assets/f12-cf7-templateloader.js', __FILE__ ), [ 'jquery' ], defined( 'FORGE12_OPTIN_VERSION' ) ? FORGE12_OPTIN_VERSION : false, true ); $this->get_logger()->debug( 'Template loader script enqueued', [ 'plugin' => 'double-opt-in', ] ); wp_localize_script( 'f12-cf7-doubleoptin-templateloader', 'templateloader', [ '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' ), ] ); $this->get_logger()->debug( 'Template loader script localized', [ 'plugin' => 'double-opt-in', ] ); } /** * Add the hooks responsible to handle wordpress functions */ public function addHooks() { $this->get_logger()->debug( 'Registering CF7 admin hooks', [ 'plugin' => 'double-opt-in', 'class' => __CLASS__, 'method' => __METHOD__, ] ); add_filter( 'wpcf7_editor_panels', [ $this, 'addPanel' ], 10, 1 ); $this->get_logger()->debug( 'Filter wpcf7_editor_panels registered', [ 'plugin' => 'double-opt-in', ] ); add_action( 'wpcf7_save_contact_form', [ $this, 'save' ], 10, 3 ); $this->get_logger()->debug( 'Action wpcf7_save_contact_form registered', [ 'plugin' => 'double-opt-in', ] ); } /** * Add a custom panel to the contact form 7 options */ public function addPanel( array $panels ) { $this->get_logger()->debug( 'Adding CF7 editor panel', [ 'plugin' => 'double-opt-in', 'class' => __CLASS__, 'method' => __METHOD__, ] ); $panels['optin'] = [ 'title' => __( 'Double-Opt-in', 'double-opt-in' ), 'callback' => [ $this, 'render' ], ]; $this->get_logger()->debug( 'CF7 editor panel added', [ 'plugin' => 'double-opt-in', ] ); return $panels; } /** * On Contact Form save store the information in the database * * @param \WPCF7_ContactForm $contact_form * @param $args * @param $context */ public function save( $contact_form, $args, $context ) { $postID = $contact_form->id(); $this->get_logger()->debug( 'Saving CF7 Double Opt-In settings', [ 'plugin' => 'double-opt-in', 'class' => __CLASS__, 'method' => __METHOD__, 'post_id' => $postID, ] ); // Validate nonce if ( ! isset( $_POST['f12_cf7_doubleoptin_save_form_nonce'] ) || ! wp_verify_nonce( wp_unslash( $_POST['f12_cf7_doubleoptin_save_form_nonce'] ), 'f12_cf7_doubleoptin_save_form_action' ) ) { $this->get_logger()->warning( 'Nonce validation failed during save', [ 'plugin' => 'double-opt-in', 'post_id' => $postID, ] ); return; } // Check if the double opt-in settings is set if ( ! $postID || ! isset( $_POST['doubleoptin'] ) ) { update_post_meta( $postID, 'f12-cf7-doubleoptin', [] ); $this->get_logger()->debug( 'No doubleoptin data found, cleared settings', [ 'plugin' => 'double-opt-in', 'post_id' => $postID, ] ); return; } $parameter = SanitizeHelper::sanitize_array( $_POST['doubleoptin'] ); $metadata = CF7DoubleOptIn::getInstance()->getParameter( $postID ); foreach ( $metadata as $key => $value ) { if ( isset( $parameter[ $key ] ) ) { if ( $key === 'body' ) { $metadata[ $key ] = $parameter[ $key ]; } elseif ( $key === 'enable' ) { $metadata[ $key ] = (int) $parameter[ $key ]; } elseif ( $key === 'sender' ) { $metadata[ $key ] = $parameter[ $key ]; } else { $metadata[ $key ] = $parameter[ $key ]; } } elseif ( $key === 'enable' ) { $metadata[ $key ] = 0; } } $metadata = apply_filters( 'f12_cf7_doubleoptin_metadata_cf7', $metadata ); $metadata = apply_filters( 'f12_cf7_doubleoptin_save_form', $metadata ); update_post_meta( $postID, 'f12-cf7-doubleoptin', $metadata ); // Save placeholder mapping if ( isset( $_POST['doubleoptin']['placeholder_mapping'] ) && is_array( $_POST['doubleoptin']['placeholder_mapping'] ) ) { $mapping = array_map( 'sanitize_text_field', $_POST['doubleoptin']['placeholder_mapping'] ); PlaceholderMapper::saveCustomMapping( $postID, $mapping, 'cf7' ); $this->get_logger()->debug( 'Placeholder mapping saved', [ 'plugin' => 'double-opt-in', 'post_id' => $postID, 'mapping' => $mapping, ] ); } $this->get_logger()->info( 'Double Opt-In settings saved', [ 'plugin' => 'double-opt-in', 'post_id' => $postID, 'data' => $metadata, ] ); } /** * Get all available templates including custom ones. * * @return array Array of template key => label. */ private function getAvailableTemplates(): array { $templates = [ 'blank' => 'blank', 'newsletter_en' => 'newsletter_en', 'newsletter_en_2' => 'newsletter_en_2', 'newsletter_en_3' => 'newsletter_en_3', ]; // Add custom templates from the Email Template Editor try { $container = \Forge12\DoubleOptIn\Container\Container::getInstance(); $integration = $container->get( \Forge12\DoubleOptIn\EmailTemplates\EmailTemplateIntegration::class ); $customTemplates = $integration->getCustomTemplates(); foreach ( $customTemplates as $template ) { $templates[ 'custom_' . $template['id'] ] = $template['title'] . ' (' . __( 'Custom', 'double-opt-in' ) . ')'; } } catch ( \Exception $e ) { // Silently fail if integration is not available $this->get_logger()->warning( 'Failed to load custom templates: ' . $e->getMessage(), [ 'plugin' => 'double-opt-in', ] ); } return $templates; } /** * Return an option list containing all tags for the condition and a default field to disable the condition. * * @param \WPCF7_ContactForm $post * * @return array */ private function getTags( $post ) { $this->get_logger()->debug( 'Fetching CF7 form tags', [ 'plugin' => 'double-opt-in', 'class' => __CLASS__, 'method' => __METHOD__, 'post_id' => $post->id() ?? null, ] ); $tags = []; $arrayTags = $post->scan_form_tags(); foreach ( $arrayTags as $formTag /** @var \WPCF7_FormTag $formTag */ ) { if ( ! empty( $formTag->name ) ) { $tags[ $formTag->name ] = $formTag->name; } } $this->get_logger()->debug( 'Form tags fetched', [ 'plugin' => 'double-opt-in', 'count' => count( $tags ), ] ); return $tags; } /** * Show the backend double opt in options * * Displays a notice with link to central form management. * Full settings are now managed centrally in the Forms admin page. * * @param \WPCF7_ContactForm $post */ public function render( $post ) { if ( ! $post ) { return; } $metadata = CF7DoubleOptIn::getInstance()->getParameter( $post->id() ); $centralUrl = admin_url( 'admin.php?page=f12-doi-admin#/forms' ); $isEnabled = isset( $metadata['enable'] ) && $metadata['enable'] == 1; $this->get_logger()->debug( 'Rendering CF7 panel notice', [ 'plugin' => 'double-opt-in', 'form_id' => $post->id(), 'enabled' => $isEnabled, ] ); ?>

getTags( $post ) ); $standardPlaceholders = PlaceholderMapper::getStandardPlaceholders(); $autoMapping = PlaceholderMapper::autoDetectMapping( $formFields ); $customMapping = PlaceholderMapper::getCustomMapping( $post->id(), 'cf7' ); // Merge for display (custom takes precedence) $effectiveMapping = array_merge( $autoMapping, $customMapping ); ?>

$config ) : $autoDetected = isset( $autoMapping[ $placeholder ] ) ? $autoMapping[ $placeholder ] : ''; $customValue = isset( $customMapping[ $placeholder ] ) ? $customMapping[ $placeholder ] : ''; ?>
[]
[]

$config ) : ?> []


[doubleoptinlink], [doubleoptoutlink], [doubleoptin_form_date], [doubleoptin_form_time], [doubleoptin_form_url]