@@ -178,6 +178,64 @@ public function handle_form_submission() {
178178 return $ message ;
179179 }
180180
181+ /**
182+ * Validate phone
183+ *
184+ * @param string $opt_val Option value.
185+ * @param array $data Data.
186+ * @return string|WP_Error Option value or error.
187+ */
188+ public function validate_phone ( $ opt_val , $ data ) {
189+ if ( empty ( $ opt_val ) ) {
190+ return '' ;
191+ }
192+
193+ // Backwards compatibility for old phone format.
194+ if ( is_array ( $ opt_val ) ) {
195+ $ opt_val = implode ( '- ' , $ opt_val );
196+ }
197+
198+ $ opt_val = trim ( $ opt_val );
199+
200+ // Validate phone number.
201+ if ( preg_match ( '/^\+?[\d\s\-\(\)\.]*$/ ' , $ opt_val ) ) {
202+ return $ opt_val ;
203+ } else {
204+ /* translators: %s: field name */
205+ $ message = sprintf ( esc_html__ ( 'Please enter a valid %s. ' , 'mailchimp ' ), esc_html ( $ data ['name ' ] ) );
206+ return new WP_Error ( 'mc_phone_validation ' , $ message );
207+ }
208+ }
209+
210+ /**
211+ * Validate address
212+ *
213+ * @param array $opt_val Option value
214+ * @param array $data Data
215+ * @return mixed
216+ */
217+ public function validate_address ( $ opt_val , $ data ) {
218+ if ( true === (bool ) $ data ['required ' ] ) {
219+ if ( empty ( $ opt_val ['addr1 ' ] ) || empty ( $ opt_val ['city ' ] ) ) {
220+ /* translators: %s: field name */
221+ $ message = sprintf ( esc_html__ ( '%s: Please enter a complete address. ' , 'mailchimp ' ), esc_html ( $ data ['name ' ] ) );
222+ $ error = new WP_Error ( 'invalid_address_merge ' , $ message );
223+ return $ error ;
224+ }
225+ } elseif ( empty ( $ opt_val ['addr1 ' ] ) || empty ( $ opt_val ['city ' ] ) ) {
226+ return false ;
227+ }
228+
229+ $ merge = new stdClass ();
230+ $ merge ->addr1 = $ opt_val ['addr1 ' ];
231+ $ merge ->addr2 = $ opt_val ['addr2 ' ];
232+ $ merge ->city = $ opt_val ['city ' ];
233+ $ merge ->state = $ opt_val ['state ' ];
234+ $ merge ->zip = $ opt_val ['zip ' ];
235+ $ merge ->country = $ opt_val ['country ' ];
236+ return $ merge ;
237+ }
238+
181239 /**
182240 * Prepare the merge fields body for the API request.
183241 *
@@ -193,29 +251,20 @@ public function prepare_merge_fields_body( $merge_fields, $skip_merge_validation
193251 $ opt = 'mc_mv_ ' . $ tag ;
194252
195253 // Skip if the field is not required and not submitted.
196- if ( ( ' Y ' !== $ merge_field ['required ' ] && ! isset ( $ _POST [ $ opt ] ) ) || $ skip_merge_validation ) {
254+ if ( ( true !== ( bool ) $ merge_field ['required ' ] && ! isset ( $ _POST [ $ opt ] ) ) || $ skip_merge_validation ) {
197255 continue ;
198256 }
199257
200258 $ opt_val = isset ( $ _POST [ $ opt ] ) ? map_deep ( stripslashes_deep ( $ _POST [ $ opt ] ), 'sanitize_text_field ' ) : '' ;
201259
202260 switch ( $ merge_field ['type ' ] ) {
203261 /**
204- * US Phone validation
205- *
206- * - Merge field is phone
207- * - Phone format is set in Mailchimp account
208- * - Phone format is US in Mailchimp account
262+ * US/International Phone validation
209263 */
210264 case 'phone ' :
211- if (
212- isset ( $ merge_field ['options ' ]['phone_format ' ] )
213- && 'US ' === $ merge_field ['options ' ]['phone_format ' ]
214- ) {
215- $ opt_val = mailchimp_sf_merge_validate_phone ( $ opt_val , $ merge_field );
216- if ( is_wp_error ( $ opt_val ) ) {
217- return $ opt_val ;
218- }
265+ $ opt_val = $ this ->validate_phone ( $ opt_val , $ merge_field );
266+ if ( is_wp_error ( $ opt_val ) ) {
267+ return $ opt_val ;
219268 }
220269 break ;
221270
@@ -227,7 +276,7 @@ public function prepare_merge_fields_body( $merge_fields, $skip_merge_validation
227276 */
228277 case 'address ' :
229278 if ( is_array ( $ opt_val ) ) {
230- $ validate = mailchimp_sf_merge_validate_address ( $ opt_val , $ merge_field );
279+ $ validate = $ this -> validate_address ( $ opt_val , $ merge_field );
231280 if ( is_wp_error ( $ validate ) ) {
232281 return $ validate ;
233282 }
@@ -260,9 +309,9 @@ public function prepare_merge_fields_body( $merge_fields, $skip_merge_validation
260309 /**
261310 * Required fields
262311 *
263- * If the field is required and empty, return an error
312+ * If the field is required and empty, + return an error
264313 */
265- if ( ' Y ' === $ merge_field ['required ' ] && trim ( $ opt_val ) === '' ) {
314+ if ( true === ( bool ) $ merge_field ['required ' ] && empty ( $ opt_val ) ) {
266315 /* translators: %s: field name */
267316 $ message = sprintf ( esc_html__ ( 'You must fill in %s. ' , 'mailchimp ' ), esc_html ( $ merge_field ['name ' ] ) );
268317 $ error = new WP_Error ( 'missing_required_field ' , $ message );
@@ -512,6 +561,16 @@ protected function validate_form_submission() {
512561 return new WP_Error ( 'spam ' , $ spam_message );
513562 }
514563
564+ // Early return if the email is not set
565+ if ( empty ( $ _POST ['mc_mv_EMAIL ' ] ) ) {
566+ return new WP_Error ( 'email_required ' , esc_html__ ( 'Please enter your email address. ' , 'mailchimp ' ) );
567+ }
568+
569+ // Check if the email is valid
570+ if ( ! is_email ( sanitize_email ( wp_unslash ( $ _POST ['mc_mv_EMAIL ' ] ) ) ) ) {
571+ return new WP_Error ( 'invalid_email ' , esc_html__ ( 'Please enter a valid email address. ' , 'mailchimp ' ) );
572+ }
573+
515574 /**
516575 * Filter to allow for custom validation of the form submission.
517576 *
0 commit comments