confirms the entry once and releases the * postponed actions (Submission::complete( $id, 'confirmed' )). When admin approval is also on, * a confirmed entry moves on to `pending` instead and the approval module takes over. * * Entry meta: _optin_token (32 hex), _optin_confirmed (UTC datetime), _optin_sent (count). * * @package FWREN */ namespace FWREN\Modules; use FWREN\Entries; use FWREN\Form; use FWREN\Notifications; use FWREN\Schema; use FWREN\Smartcodes; use FWREN\Submission; defined( 'ABSPATH' ) || exit; class Double_Optin { /** @var string Notice printed on the page the visitor returns to. */ private static $notice = ''; /** @var bool */ private static $notice_done = false; /** Hooks. */ public static function init() { Helpers::boot(); add_filter( 'fwren/form_defaults', array( __CLASS__, 'defaults' ) ); add_action( 'fwren/submission/gated', array( __CLASS__, 'on_gated' ), 10, 4 ); add_action( 'template_redirect', array( __CLASS__, 'maybe_confirm' ), 0 ); add_action( 'template_redirect', array( __CLASS__, 'prepare_notice' ), 2 ); add_filter( 'fwren/form_html', array( __CLASS__, 'notice_in_form' ), 5, 2 ); add_action( 'wp_footer', array( __CLASS__, 'notice_footer' ) ); add_filter( 'fwren/smartcodes', array( __CLASS__, 'catalog' ) ); add_filter( 'fwren/admin/form_tabs', array( __CLASS__, 'tab' ), 20, 2 ); add_action( 'fwren/admin/form_tab/double_optin', array( __CLASS__, 'render_tab' ), 10, 2 ); add_action( 'fwren/admin/entry_actions', array( __CLASS__, 'entry_actions' ) ); add_action( 'fwren/admin/entry_sidebar', array( __CLASS__, 'entry_sidebar' ) ); add_action( 'admin_post_fwren_optin_resend', array( __CLASS__, 'resend' ) ); } /** Extra defaults. */ public static function defaults( $d ) { $d['double_optin'] = array_merge( array( 'after_confirm' => 'redirect' ), // redirect | page. (array) ( $d['double_optin'] ?? array() ) ); return $d; } /** Smartcodes picker entry. */ public static function catalog( $groups ) { $groups[ __( 'Double opt-in', 'fieldwren' ) ] = array( '{confirmation_link}' => __( 'Confirmation link (opt-in email only)', 'fieldwren' ), ); return $groups; } /* ------------------------------------------------------------------ * * Email * ------------------------------------------------------------------ */ /** * Entry held back: send the confirmation email. * * @param array $row Entry row. * @param array $values Values. * @param string $status Entry status. * @param mixed $payment Payment hand-off. */ public static function on_gated( $row, $values, $status, $payment = null ) { if ( 'unconfirmed' !== $status || empty( $row['id'] ) ) { return; } self::send( (array) $row, (array) $values ); } /** @return string Confirmation URL of an entry (creates the token once). */ public static function link( $entry_id ) { $token = (string) Entries::get_meta( $entry_id, '_optin_token' ); if ( ! Helpers::is_token( $token ) ) { $token = Helpers::token(); Entries::set_meta( $entry_id, '_optin_token', $token ); } return add_query_arg( 'fwren_confirm', $token, home_url( '/' ) ); } /** * Send (or resend) the confirmation email. * * @param array $entry Entry row. * @param array $values Values (full, incl. values that are not stored). * @return bool */ public static function send( array $entry, array $values ) { $form_id = (int) $entry['form_id']; $s = Form::settings( $form_id )['double_optin']; $to = Helpers::email_of( $form_id, $values, (string) $s['email_field'] ); if ( ! $to ) { return false; } $url = self::link( (int) $entry['id'] ); $message = str_replace( array( '{confirmation_link}', '{confirmation_url}', '#confirmation_url#' ), esc_url( $url ), (string) $s['message'] ); $message = make_clickable( $message ); $n = array( 'id' => 'double_optin', 'enabled' => true, 'name' => __( 'Double opt-in confirmation', 'fieldwren' ), 'to_type' => 'email', 'to' => $to, 'to_field' => '', 'routing' => array(), 'cc' => '', 'bcc' => '', 'reply_to' => '', 'from_name' => '', 'from_email' => '', 'subject' => (string) $s['subject'], 'message' => $message, 'attach_uploads' => false, ); $n = apply_filters( 'fwren/double_optin/email', $n, $entry, $values ); $ok = Notifications::send( $n, $entry, $values ); Entries::set_meta( (int) $entry['id'], '_optin_sent', (int) Entries::get_meta( (int) $entry['id'], '_optin_sent' ) + 1 ); return $ok; } /* ------------------------------------------------------------------ * * Confirmation * ------------------------------------------------------------------ */ /** Handle ?fwren_confirm=token. */ public static function maybe_confirm() { if ( empty( $_GET['fwren_confirm'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification return; } $token = sanitize_key( wp_unslash( $_GET['fwren_confirm'] ) ); // phpcs:ignore WordPress.Security.NonceVerification $id = Helpers::is_token( $token ) ? Entries::find_by_meta( '_optin_token', $token ) : 0; $entry = $id ? Entries::get( $id ) : null; if ( ! $entry || ! hash_equals( (string) Entries::get_meta( $id, '_optin_token' ), $token ) || in_array( $entry['status'], array( 'trash', 'spam' ), true ) ) { Helpers::page( __( 'Link expired', 'fieldwren' ), '

' . esc_html__( 'This confirmation link is invalid or has expired.', 'fieldwren' ) . '

', 404 ); } $form_id = (int) $entry['form_id']; $settings = Form::settings( $form_id ); $s = $settings['double_optin']; if ( 'unconfirmed' === $entry['status'] && ! Entries::get_meta( $id, '_optin_confirmed' ) && add_option( 'fwren_lock_optin_' . $id, time(), '', 'no' ) ) { try { Entries::set_meta( $id, '_optin_confirmed', current_time( 'mysql', true ) ); self::release( $entry ); } finally { delete_option( 'fwren_lock_optin_' . $id ); } } $ctx = array( 'form_id' => $form_id, 'schema' => Schema::get( $form_id ), 'values' => (array) $entry['response'], 'entry' => $entry ); $msg = wpautop( Smartcodes::parse( (string) $s['confirmed_message'], $ctx, 'html' ) ); $source = (string) $entry['source_url']; if ( 'page' !== ( $s['after_confirm'] ?? 'redirect' ) && $source && wp_validate_redirect( $source, '' ) ) { wp_safe_redirect( add_query_arg( array( 'fwren_notice' => 'optin', 'fwren_form' => $form_id ), remove_query_arg( array( 'fwren_confirm', 'fwren_notice', 'fwren_form' ), $source ) ) ); exit; } Helpers::page( get_the_title( $form_id ), $msg ); } /** * Release the entry: approval next, or complete. * * @param array $entry Entry row. */ public static function release( array $entry ) { $id = (int) $entry['id']; $settings = Form::settings( (int) $entry['form_id'] ); if ( ! empty( $settings['admin_approval']['enabled'] ) ) { Entries::update( $id, array( 'status' => 'pending' ) ); $entry['status'] = 'pending'; Notifications::send_for_event( $entry, (array) $entry['response'], 'confirmed' ); do_action( 'fwren/submission/gated', $entry, (array) $entry['response'], 'pending', null ); } else { Submission::complete( $id, 'confirmed' ); } do_action( 'fwren/double_optin/confirmed', $entry ); } /** Notice after the redirect back to the form page. */ public static function prepare_notice() { if ( empty( $_GET['fwren_notice'] ) || 'optin' !== $_GET['fwren_notice'] || empty( $_GET['fwren_form'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification return; } $form_id = absint( $_GET['fwren_form'] ); // phpcs:ignore WordPress.Security.NonceVerification if ( ! Form::exists( $form_id ) ) { return; } $s = Form::settings( $form_id )['double_optin']; self::$notice = wp_kses_post( wpautop( Smartcodes::parse_default( (string) $s['confirmed_message'], $form_id ) ) ); nocache_headers(); } /** Print the notice above the form. */ public static function notice_in_form( $html, $form_id ) { if ( ! self::$notice || self::$notice_done || absint( $_GET['fwren_form'] ?? 0 ) !== (int) $form_id ) { // phpcs:ignore WordPress.Security.NonceVerification return $html; } self::$notice_done = true; return '
' . self::$notice . '
' . $html; } /** Fallback toast when the page does not show the form. */ public static function notice_footer() { if ( ! self::$notice || self::$notice_done ) { return; } echo '
' . self::$notice . '
'; // phpcs:ignore WordPress.Security.EscapeOutput } /* ------------------------------------------------------------------ * * Admin * ------------------------------------------------------------------ */ /** Per-form tab. */ public static function tab( $tabs, $form_id = 0 ) { $tabs['double_optin'] = __( 'Double opt-in', 'fieldwren' ); return $tabs; } /** Tab content. */ public static function render_tab( $form_id, $settings ) { $s = $settings['double_optin']; $n = 'fwren[double_optin]'; echo ''; Helpers::row_toggle( $n . '[enabled]', ! empty( $s['enabled'] ), __( 'Enable double opt-in', 'fieldwren' ), __( 'Entries wait as “Unconfirmed” until the submitter clicks the link in the email. Notifications and integrations run after confirmation (notifications set to send on “confirmed” run at that moment too).', 'fieldwren' ) ); Helpers::row_field( $n . '[email_field]', $form_id, $s['email_field'], __( 'Email field', 'fieldwren' ), Helpers::types( array( 'email' ) ) ); Helpers::row_text( $n . '[subject]', $s['subject'], __( 'Email subject', 'fieldwren' ) ); Helpers::row_textarea( $n . '[message]', $s['message'], __( 'Email message', 'fieldwren' ), __( 'Use {confirmation_link} where the link should go. Other smartcodes work too.', 'fieldwren' ), 8 ); echo ''; Helpers::row_textarea( $n . '[pending_message]', $s['pending_message'], __( 'Message after submitting', 'fieldwren' ), __( 'Replaces the confirmation message while the entry is unconfirmed.', 'fieldwren' ), 3 ); Helpers::row_textarea( $n . '[confirmed_message]', $s['confirmed_message'], __( 'Message after confirming', 'fieldwren' ), '', 3 ); Helpers::row_select( $n . '[after_confirm]', $s['after_confirm'] ?? 'redirect', __( 'After confirming', 'fieldwren' ), array( 'redirect' => __( 'Return to the form page and show the message', 'fieldwren' ), 'page' => __( 'Show the message on a simple page', 'fieldwren' ), ) ); Helpers::row_text( $n . '[delete_after_days]', (string) $s['delete_after_days'], __( 'Delete unconfirmed entries after (days)', 'fieldwren' ), __( '0 keeps them.', 'fieldwren' ), 'number' ); Helpers::row_toggle( $n . '[skip_for_logged_in]', ! empty( $s['skip_for_logged_in'] ), __( 'Skip for logged-in users', 'fieldwren' ) ); echo ''; } /** Resend button on the entry screen. */ public static function entry_actions( $entry ) { if ( empty( $entry['id'] ) || 'unconfirmed' !== ( $entry['status'] ?? '' ) || ! current_user_can( 'fwren_manage_entries' ) ) { return; } $url = wp_nonce_url( admin_url( 'admin-post.php?action=fwren_optin_resend&entry=' . (int) $entry['id'] ), 'fwren_optin_resend_' . (int) $entry['id'] ); echo '' . esc_html__( 'Resend confirmation email', 'fieldwren' ) . ' '; } /** Opt-in state in the entry sidebar. */ public static function entry_sidebar( $entry ) { if ( empty( $entry['id'] ) ) { return; } $sent = (int) Entries::get_meta( (int) $entry['id'], '_optin_sent' ); $done = (string) Entries::get_meta( (int) $entry['id'], '_optin_confirmed' ); if ( ! $sent && ! $done ) { return; } echo '

' . esc_html__( 'Double opt-in', 'fieldwren' ) . '

'; if ( $done ) { /* translators: %s date */ echo esc_html( sprintf( __( 'Confirmed on %s', 'fieldwren' ), wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $done . ' UTC' ) ) ) ); } else { esc_html_e( 'Waiting for confirmation.', 'fieldwren' ); } /* translators: %d emails */ echo '
' . esc_html( sprintf( _n( '%d email sent', '%d emails sent', $sent, 'fieldwren' ), $sent ) ) . '

'; } /** Resend handler. */ public static function resend() { $id = absint( $_GET['entry'] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification if ( ! current_user_can( 'fwren_manage_entries' ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ?? '' ) ), 'fwren_optin_resend_' . $id ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'fieldwren' ), 403 ); } $entry = Entries::get( $id ); $ok = $entry && 'unconfirmed' === $entry['status'] && self::send( $entry, (array) $entry['response'] ); wp_safe_redirect( add_query_arg( 'fwren_notice', $ok ? 'optin_sent' : 'optin_failed', wp_get_referer() ? wp_get_referer() : admin_url( 'admin.php?page=fwren-entries&entry=' . $id ) ) ); exit; } }