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 {
$is_pro = apply_filters( 'f12_doi_is_pro_active', false );
$is_confirmed = $optin->is_confirmed();
$has_mail = ! empty( $optin->get_mail_optin() );
$disabled = ! $is_pro || $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_pro ) {
$title = __( 'This feature is available in the Pro version.', 'double-opt-in' );
} elseif ( $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' );
}
?>
PRO
__( 'This feature requires the Pro version.', 'double-opt-in' ) ), 403 );
}
if ( ! current_user_can( 'manage_options' ) ) {
wp_send_json_error( array( 'message' => __( '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 {
$container = Container::getInstance();
$repository = $container->get( OptInRepositoryInterface::class );
$entity = $repository->findById( $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 ( ! $entity ) {
wp_send_json_error( array( 'message' => __( 'Opt-In not found.', 'double-opt-in' ) ) );
}
if ( $entity->isConfirmed() ) {
wp_send_json_error( array( 'message' => __( 'This Opt-In is already confirmed.', 'double-opt-in' ) ) );
}
$body = $entity->getMailOptIn();
if ( empty( $body ) ) {
wp_send_json_error( array( 'message' => __( 'No mail body stored for this Opt-In.', 'double-opt-in' ) ) );
}
$email = $entity->getEmail();
if ( empty( $email ) ) {
wp_send_json_error( array( 'message' => __( 'No recipient email found.', 'double-opt-in' ) ) );
}
// Load subject from form settings with fallback
$formId = $entity->getFormId();
$formParameter = \forge12\contactform7\CF7DoubleOptIn\CF7DoubleOptIn::getInstance()->getParameter( $formId );
$subject = ! empty( $formParameter['subject'] )
? $formParameter['subject']
: __( 'Please confirm your opt-in', 'double-opt-in' );
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
if ( ! empty( $formParameter['sender'] ) ) {
if ( ! empty( $formParameter['sender_name'] ) ) {
$headers[] = 'From: ' . $formParameter['sender_name'] . ' <' . $formParameter['sender'] . '>';
} else {
$headers[] = 'From: ' . $formParameter['sender'];
}
}
$sent = wp_mail( $email, $subject, $body, $headers );
if ( $sent ) {
$this->logger->info(
'Confirmation mail resent',
array(
'plugin' => 'double-opt-in',
'optin_id' => $optinId,
'email' => $email,
)
);
wp_send_json_success( array( 'message' => __( 'Confirmation mail has been resent.', 'double-opt-in' ) ) );
} else {
$this->logger->error(
'Failed to resend confirmation mail',
array(
'plugin' => 'double-opt-in',
'optin_id' => $optinId,
'email' => $email,
)
);
wp_send_json_error( array( 'message' => __( 'Failed to send mail. Please check your mail configuration.', 'double-opt-in' ) ) );
}
}
}