| @@ -8,9 +8,10 @@ | ||
| 8 | 8 | |
| 9 | 9 | namespace Forge12\DoubleOptIn\Admin; |
| 10 | 10 | |
| 11 | 11 | use Forge12\DoubleOptIn\Container\Container; |
| 12 | -use Forge12\DoubleOptIn\Repository\OptInRepositoryInterface; | |
| 12 | +use Forge12\DoubleOptIn\Service\ConfirmationMailResender; | |
| 13 | +use Forge12\DoubleOptIn\Service\ResendResult; | |
| 13 | 14 | use Forge12\Shared\LoggerInterface; |
| 14 | 15 | |
| 15 | 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | 17 | exit; |
| @@ -39,12 +40,12 @@ | ||
| 39 | 40 | * |
| 40 | 41 | * @return void |
| 41 | 42 | */ |
| 42 | 43 | public function renderResendButton( $optin ): void { |
| 43 | - $is_pro = apply_filters( 'f12_doi_is_pro_active', false ); | |
| 44 | + // Free since 5.8, like the resend in the SPA: no licence check. | |
| 44 | 45 | $is_confirmed = $optin->is_confirmed(); |
| 45 | 46 | $has_mail = ! empty( $optin->get_mail_optin() ); |
| 46 | - $disabled = ! $is_pro || $is_confirmed || ! $has_mail; | |
| 47 | + $disabled = $is_confirmed || ! $has_mail; | |
| 47 | 48 | |
| 48 | 49 | $nonce = wp_create_nonce( 'doi_resend_mail' ); |
| 49 | 50 | $id = $optin->get_id(); |
| 50 | 51 | |
| @@ -49,11 +50,9 @@ | ||
| 49 | 50 | $id = $optin->get_id(); |
| 50 | 51 | |
| 51 | 52 | // Build tooltip explaining why the button is disabled |
| 52 | 53 | $title = ''; |
| 53 | - if ( ! $is_pro ) { | |
| 54 | - $title = __( 'This feature is available in the Pro version.', 'double-opt-in' ); | |
| 55 | - } elseif ( $is_confirmed ) { | |
| 54 | + if ( $is_confirmed ) { | |
| 56 | 55 | $title = __( 'This Opt-In has already been confirmed.', 'double-opt-in' ); |
| 57 | 56 | } elseif ( ! $has_mail ) { |
| 58 | 57 | $title = __( 'No mail body stored for this Opt-In.', 'double-opt-in' ); |
| 59 | 58 | } |
| @@ -69,14 +68,8 @@ | ||
| 69 | 68 | <?php |
| 70 | 69 | _e( 'Resend Confirmation Mail', 'double-opt-in' ); |
| 71 | 70 | ?> |
| 72 | 71 | </button> |
| 73 | - <?php | |
| 74 | - if ( ! $is_pro ) : | |
| 75 | - ?> | |
| 76 | - <span style="display:inline-block;background:linear-gradient(135deg,#e6a817,#d4941a);color:#fff;font-size:10px;font-weight:700;line-height:1;padding:3px 6px;border-radius:3px;letter-spacing:.5px;text-transform:uppercase;" | |
| 77 | - title="<?php esc_attr_e( 'This feature is available in the Pro version.', 'double-opt-in' ); ?>">PRO</span> | |
| 78 | - <?php endif; ?> | |
| 79 | 72 | <span class="doi-tooltip"> |
| 80 | 73 | <span class="dashicons dashicons-info-outline"></span> |
| 81 | 74 | <span class="doi-tooltip-text"> |
| 82 | 75 | <?php |
| @@ -84,9 +77,9 @@ | ||
| 84 | 77 | ?> |
| 85 | 78 | </span> |
| 86 | 79 | </span> |
| 87 | 80 | <span id="doi-resend-feedback" style="font-size:13px;"></span> |
| 88 | - <?php if ( $is_pro && ! $disabled ) : ?> | |
| 81 | + <?php if ( ! $disabled ) : ?> | |
| 89 | 82 | <script> |
| 90 | 83 | (function() { |
| 91 | 84 | var btn = document.getElementById('doi-resend-btn'); |
| 92 | 85 | var feedback = document.getElementById('doi-resend-feedback'); |
| @@ -137,12 +130,8 @@ | ||
| 137 | 130 | * |
| 138 | 131 | * @return void |
| 139 | 132 | */ |
| 140 | 133 | public function handleResend(): void { |
| 141 | - if ( ! apply_filters( 'f12_doi_is_pro_active', false ) ) { | |
| 142 | - wp_send_json_error( array( 'message' => __( 'This feature requires the Pro version.', 'double-opt-in' ) ), 403 ); | |
| 143 | - } | |
| 144 | - | |
| 145 | 134 | if ( ! current_user_can( 'manage_options' ) ) { |
| 146 | 135 | wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'double-opt-in' ) ), 403 ); |
| 147 | 136 | } |
| 148 | 137 | |
| @@ -156,11 +145,9 @@ | ||
| 156 | 145 | wp_send_json_error( array( 'message' => __( 'Invalid Opt-In ID.', 'double-opt-in' ) ) ); |
| 157 | 146 | } |
| 158 | 147 | |
| 159 | 148 | try { |
| 160 | - $container = Container::getInstance(); | |
| 161 | - $repository = $container->get( OptInRepositoryInterface::class ); | |
| 162 | - $entity = $repository->findById( $optinId ); | |
| 149 | + $outcome = Container::getInstance()->get( ConfirmationMailResender::class )->resend( $optinId ); | |
| 163 | 150 | } catch ( \Exception $e ) { |
| 164 | 151 | $this->logger->error( |
| 165 | 152 | 'Failed to load OptIn for resend', |
| 166 | 153 | array( |
| @@ -171,64 +158,41 @@ | ||
| 171 | 158 | ); |
| 172 | 159 | wp_send_json_error( array( 'message' => __( 'Failed to load Opt-In.', 'double-opt-in' ) ) ); |
| 173 | 160 | } |
| 174 | 161 | |
| 175 | - if ( ! $entity ) { | |
| 176 | - wp_send_json_error( array( 'message' => __( 'Opt-In not found.', 'double-opt-in' ) ) ); | |
| 177 | - } | |
| 178 | - | |
| 179 | - if ( $entity->isConfirmed() ) { | |
| 180 | - wp_send_json_error( array( 'message' => __( 'This Opt-In is already confirmed.', 'double-opt-in' ) ) ); | |
| 181 | - } | |
| 182 | - | |
| 183 | - $body = $entity->getMailOptIn(); | |
| 184 | - if ( empty( $body ) ) { | |
| 185 | - wp_send_json_error( array( 'message' => __( 'No mail body stored for this Opt-In.', 'double-opt-in' ) ) ); | |
| 186 | - } | |
| 187 | - | |
| 188 | - $email = $entity->getEmail(); | |
| 189 | - if ( empty( $email ) ) { | |
| 190 | - wp_send_json_error( array( 'message' => __( 'No recipient email found.', 'double-opt-in' ) ) ); | |
| 191 | - } | |
| 192 | - | |
| 193 | - // Load subject from form settings with fallback | |
| 194 | - $formId = $entity->getFormId(); | |
| 195 | - $formParameter = \forge12\contactform7\CF7DoubleOptIn\CF7DoubleOptIn::getInstance()->getParameter( $formId ); | |
| 196 | - $subject = ! empty( $formParameter['subject'] ) | |
| 197 | - ? $formParameter['subject'] | |
| 198 | - : __( 'Please confirm your opt-in', 'double-opt-in' ); | |
| 199 | - | |
| 200 | - $headers = array( 'Content-Type: text/html; charset=UTF-8' ); | |
| 201 | - | |
| 202 | - if ( ! empty( $formParameter['sender'] ) ) { | |
| 203 | - if ( ! empty( $formParameter['sender_name'] ) ) { | |
| 204 | - $headers[] = 'From: ' . $formParameter['sender_name'] . ' <' . $formParameter['sender'] . '>'; | |
| 205 | - } else { | |
| 206 | - $headers[] = 'From: ' . $formParameter['sender']; | |
| 207 | - } | |
| 208 | - } | |
| 209 | - | |
| 210 | - $sent = wp_mail( $email, $subject, $body, $headers ); | |
| 211 | - | |
| 212 | - if ( $sent ) { | |
| 162 | + if ( $outcome->isSent() ) { | |
| 213 | 163 | $this->logger->info( |
| 214 | 164 | 'Confirmation mail resent', |
| 215 | 165 | array( |
| 216 | 166 | 'plugin' => 'double-opt-in', |
| 217 | 167 | 'optin_id' => $optinId, |
| 218 | - 'email' => $email, | |
| 219 | 168 | ) |
| 220 | 169 | ); |
| 221 | 170 | wp_send_json_success( array( 'message' => __( 'Confirmation mail has been resent.', 'double-opt-in' ) ) ); |
| 222 | - } else { | |
| 171 | + } | |
| 172 | + | |
| 173 | + $messages = array( | |
| 174 | + ResendResult::NOT_FOUND => __( 'Opt-In not found.', 'double-opt-in' ), | |
| 175 | + ResendResult::CONFIRMED => __( 'This Opt-In is already confirmed.', 'double-opt-in' ), | |
| 176 | + ResendResult::OPTED_OUT => __( 'This contact has opted out. The confirmation email is not sent again.', 'double-opt-in' ), | |
| 177 | + ResendResult::NO_BODY => __( 'No mail body stored for this Opt-In.', 'double-opt-in' ), | |
| 178 | + ResendResult::NO_RECIPIENT => __( 'No recipient email found.', 'double-opt-in' ), | |
| 179 | + ); | |
| 180 | + | |
| 181 | + if ( $outcome->getReason() === ResendResult::SEND_FAILED ) { | |
| 223 | 182 | $this->logger->error( |
| 224 | 183 | 'Failed to resend confirmation mail', |
| 225 | 184 | array( |
| 226 | 185 | 'plugin' => 'double-opt-in', |
| 227 | 186 | 'optin_id' => $optinId, |
| 228 | - 'email' => $email, | |
| 229 | 187 | ) |
| 230 | 188 | ); |
| 231 | - wp_send_json_error( array( 'message' => __( 'Failed to send mail. Please check your mail configuration.', 'double-opt-in' ) ) ); | |
| 232 | 189 | } |
| 190 | + | |
| 191 | + wp_send_json_error( | |
| 192 | + array( | |
| 193 | + 'message' => $messages[ $outcome->getReason() ] | |
| 194 | + ?? __( 'Failed to send mail. Please check your mail configuration.', 'double-opt-in' ), | |
| 195 | + ) | |
| 196 | + ); | |
| 233 | 197 | } |
| 234 | 198 | } |