| @@ -67,8 +67,34 @@ | ||
| 67 | 67 | */ |
| 68 | 68 | protected ?OptInError $lastCreationError = null; |
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | + * Why the last maybeCreateOptIn() returned null, or null if it did not. | |
| 72 | + * | |
| 73 | + * Callers use it to answer in the form plugin's own words — Elementor | |
| 74 | + * adds the message to its AJAX response when the error must reach the | |
| 75 | + * visitor (OptInError::shouldShowToVisitor()). | |
| 76 | + * | |
| 77 | + * @return OptInError|null | |
| 78 | + */ | |
| 79 | + public function getLastCreationError(): ?OptInError { | |
| 80 | + return $this->lastCreationError; | |
| 81 | + } | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Remember the refusal and hand it to the frontend toast. | |
| 85 | + * | |
| 86 | + * @param OptInError $error The reason. | |
| 87 | + * @param int $formId The form. | |
| 88 | + * | |
| 89 | + * @return void | |
| 90 | + */ | |
| 91 | + protected function storeCreationError( OptInError $error, int $formId ): void { | |
| 92 | + $this->lastCreationError = $error; | |
| 93 | + ErrorNotification::store( $error, $formId ); | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 71 | 97 | * Get the validation status from the last validateOptIn() call. |
| 72 | 98 | * |
| 73 | 99 | * @return string One of: '', 'confirmed', 'already_confirmed', 'expired', 'not_found'. |
| 74 | 100 | */ |
| @@ -812,8 +838,10 @@ | ||
| 812 | 838 | * |
| 813 | 839 | * @return OptIn|null |
| 814 | 840 | */ |
| 815 | 841 | protected function maybeCreateOptIn( int $formId, string $formHtml, array $parameter, array $files = array(), array $knownFields = array() ): ?OptIn { |
| 842 | + $this->lastCreationError = null; | |
| 843 | + | |
| 816 | 844 | $this->get_logger()->debug( 'maybeCreateOptIn called', [ |
| 817 | 845 | 'plugin' => 'double-opt-in', |
| 818 | 846 | 'class' => __CLASS__, |
| 819 | 847 | 'method' => __METHOD__, |
| @@ -856,9 +884,9 @@ | ||
| 856 | 884 | if ( empty( $recipient ) ) { |
| 857 | 885 | $this->get_logger()->warning( 'No recipient found, skipping OptIn creation', [ |
| 858 | 886 | 'plugin' => 'double-opt-in', |
| 859 | 887 | ] ); |
| 860 | - ErrorNotification::store( | |
| 888 | + $this->storeCreationError( | |
| 861 | 889 | OptInError::fromCode( OptInError::NO_RECIPIENT, [ 'form_id' => $formId ] ), |
| 862 | 890 | $formId |
| 863 | 891 | ); |
| 864 | 892 | return null; |
| @@ -905,9 +933,9 @@ | ||
| 905 | 933 | 'integration' => $this->type, |
| 906 | 934 | 'consent_field' => $consentField, |
| 907 | 935 | ] ); |
| 908 | 936 | do_action( 'f12_cf7_doubleoptin_consent_not_given', $formId, $consentField ); |
| 909 | - ErrorNotification::store( | |
| 937 | + $this->storeCreationError( | |
| 910 | 938 | OptInError::fromCode( |
| 911 | 939 | OptInError::CONSENT_NOT_GIVEN, |
| 912 | 940 | [ 'form_id' => $formId, 'consent_field' => $consentField ] |
| 913 | 941 | ), |
| @@ -942,9 +970,9 @@ | ||
| 942 | 970 | 'ip' => $ip, |
| 943 | 971 | 'formId' => $formId, |
| 944 | 972 | ] ); |
| 945 | 973 | do_action( 'f12_cf7_doubleoptin_rate_limited', 'ip', $ip, $formId ); |
| 946 | - ErrorNotification::store( | |
| 974 | + $this->storeCreationError( | |
| 947 | 975 | OptInError::fromCode( OptInError::RATE_LIMIT_IP, [ 'ip' => $ip, 'form_id' => $formId ] ), |
| 948 | 976 | $formId |
| 949 | 977 | ); |
| 950 | 978 | return null; |
| @@ -956,9 +984,9 @@ | ||
| 956 | 984 | 'email' => $recipient, |
| 957 | 985 | 'formId' => $formId, |
| 958 | 986 | ] ); |
| 959 | 987 | do_action( 'f12_cf7_doubleoptin_rate_limited', 'email', $recipient, $formId ); |
| 960 | - ErrorNotification::store( | |
| 988 | + $this->storeCreationError( | |
| 961 | 989 | OptInError::fromCode( OptInError::RATE_LIMIT_EMAIL, [ 'email' => $recipient, 'form_id' => $formId ] ), |
| 962 | 990 | $formId |
| 963 | 991 | ); |
| 964 | 992 | return null; |
| @@ -1017,9 +1045,9 @@ | ||
| 1017 | 1045 | ! empty( $errorMsg ) ? $errorMsg : OptInError::fromCode( OptInError::RECIPIENT_INVALID )->getMessage(), |
| 1018 | 1046 | [ 'email' => $recipient, 'form_id' => $formId ] |
| 1019 | 1047 | ); |
| 1020 | 1048 | |
| 1021 | - ErrorNotification::store( $this->lastCreationError, $formId ); | |
| 1049 | + $this->storeCreationError( $this->lastCreationError, $formId ); | |
| 1022 | 1050 | return null; |
| 1023 | 1051 | } |
| 1024 | 1052 | |
| 1025 | 1053 | /** |
| @@ -1084,9 +1112,9 @@ | ||
| 1084 | 1112 | ] ); |
| 1085 | 1113 | |
| 1086 | 1114 | do_action( 'f12_cf7_doubleoptin_creation_failed', $formId, $recipient ); |
| 1087 | 1115 | |
| 1088 | - ErrorNotification::store( | |
| 1116 | + $this->storeCreationError( | |
| 1089 | 1117 | OptInError::fromCode( OptInError::SAVE_FAILED, [ 'form_id' => $formId ] ), |
| 1090 | 1118 | $formId |
| 1091 | 1119 | ); |
| 1092 | 1120 | |