logger = $logger;
add_action( 'wp_ajax_doi_resend_optin_mail', array( $this, 'handleResend' ) );
add_action( 'f12_cf7_doubleoptin_ui_view_optin_options', array( $this, 'renderResendButton' ) );
}
/**
* Render the resend button on the view-optin page.
*
* @param \forge12\contactform7\CF7DoubleOptIn\OptIn $optin The OptIn facade instance.
*
* @return void
*/
public function renderResendButton( $optin ): void {
// Free since 5.8, like the resend in the SPA: no licence check.
$is_confirmed = $optin->is_confirmed();
$has_mail = ! empty( $optin->get_mail_optin() );
$disabled = $is_confirmed || ! $has_mail;
$nonce = wp_create_nonce( 'doi_resend_mail' );
$id = $optin->get_id();
// Build tooltip explaining why the button is disabled
$title = '';
if ( $is_confirmed ) {
$title = __( 'This Opt-In has already been confirmed.', 'double-opt-in' );
} elseif ( ! $has_mail ) {
$title = __( 'No mail body stored for this Opt-In.', 'double-opt-in' );
}
?>
__( 'You do not have permission to perform this action.', 'double-opt-in' ) ), 403 );
}
if ( ! check_ajax_referer( 'doi_resend_mail', '_wpnonce', false ) ) {
wp_send_json_error( array( 'message' => __( 'Security check failed.', 'double-opt-in' ) ), 403 );
}
$optinId = isset( $_POST['optin_id'] ) ? (int) $_POST['optin_id'] : 0;
if ( $optinId <= 0 ) {
wp_send_json_error( array( 'message' => __( 'Invalid Opt-In ID.', 'double-opt-in' ) ) );
}
try {
$outcome = Container::getInstance()->get( ConfirmationMailResender::class )->resend( $optinId );
} catch ( \Exception $e ) {
$this->logger->error(
'Failed to load OptIn for resend',
array(
'plugin' => 'double-opt-in',
'id' => $optinId,
'error' => $e->getMessage(),
)
);
wp_send_json_error( array( 'message' => __( 'Failed to load Opt-In.', 'double-opt-in' ) ) );
}
if ( $outcome->isSent() ) {
$this->logger->info(
'Confirmation mail resent',
array(
'plugin' => 'double-opt-in',
'optin_id' => $optinId,
)
);
wp_send_json_success( array( 'message' => __( 'Confirmation mail has been resent.', 'double-opt-in' ) ) );
}
$messages = array(
ResendResult::NOT_FOUND => __( 'Opt-In not found.', 'double-opt-in' ),
ResendResult::CONFIRMED => __( 'This Opt-In is already confirmed.', 'double-opt-in' ),
ResendResult::OPTED_OUT => __( 'This contact has opted out. The confirmation email is not sent again.', 'double-opt-in' ),
ResendResult::NO_BODY => __( 'No mail body stored for this Opt-In.', 'double-opt-in' ),
ResendResult::NO_RECIPIENT => __( 'No recipient email found.', 'double-opt-in' ),
);
if ( $outcome->getReason() === ResendResult::SEND_FAILED ) {
$this->logger->error(
'Failed to resend confirmation mail',
array(
'plugin' => 'double-opt-in',
'optin_id' => $optinId,
)
);
}
wp_send_json_error(
array(
'message' => $messages[ $outcome->getReason() ]
?? __( 'Failed to send mail. Please check your mail configuration.', 'double-opt-in' ),
)
);
}
}