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 '| '; Helpers::smartcodes_button( Helpers::dom_id( $n . '[message]' ), $form_id ); echo ' |
';
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 ) ) . '