Skip to content

Commit 478adea

Browse files
authored
Merge pull request #108 from mailchimp/bug/us-phone-not-included-in-mailchimp-form-causes-fatal-error-on-submission
Bug/when US phone is not included as a merge field in a Mailchimp form it causes fatal error on submission
2 parents cba6c8b + 428c4dc commit 478adea

File tree

2 files changed

+74
-22
lines changed

2 files changed

+74
-22
lines changed

mailchimp.php

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,32 +1003,72 @@ function mailchimp_sf_merge_submit( $mv ) {
10031003

10041004
$opt_val = isset( $_POST[ $opt ] ) ? map_deep( stripslashes_deep( $_POST[ $opt ] ), 'sanitize_text_field' ) : '';
10051005

1006-
// Handle phone number logic
1007-
if ( isset( $mv_var['options']['phone_format'] ) && 'phone' === $mv_var['type'] && 'US' === $mv_var['options']['phone_format'] ) {
1008-
$opt_val = mailchimp_sf_merge_validate_phone( $opt_val, $mv_var );
1009-
if ( is_wp_error( $opt_val ) ) {
1010-
return $opt_val;
1011-
}
1012-
} elseif ( is_array( $opt_val ) && 'address' === $mv_var['type'] ) { // Handle address logic
1013-
$validate = mailchimp_sf_merge_validate_address( $opt_val, $mv_var );
1014-
if ( is_wp_error( $validate ) ) {
1015-
return $validate;
1016-
}
1006+
switch ( $mv_var['type'] ) {
1007+
/**
1008+
* US Phone validation
1009+
*
1010+
* - Merge field is phone
1011+
* - Merge field is "included" in the Mailchimp admin options
1012+
* - Phone format is set in Mailchimp account
1013+
* - Phone format is US in Mailchimp account
1014+
*/
1015+
case 'phone':
1016+
if (
1017+
'on' === get_option( $opt )
1018+
&& isset( $mv_var['options']['phone_format'] )
1019+
&& 'US' === $mv_var['options']['phone_format']
1020+
) {
1021+
$opt_val = mailchimp_sf_merge_validate_phone( $opt_val, $mv_var );
1022+
if ( is_wp_error( $opt_val ) ) {
1023+
return $opt_val;
1024+
}
1025+
}
1026+
break;
10171027

1018-
if ( $validate ) {
1019-
$merge->$tag = $validate;
1020-
}
1021-
continue;
1028+
/**
1029+
* Address validation
1030+
*
1031+
* - Merge field is address
1032+
* - Merge field is "included" in the Mailchimp admin options
1033+
* - Merge field is an array (address contains multiple <input> elements)
1034+
*/
1035+
case 'address':
1036+
if ( 'on' === get_option( $opt ) && is_array( $opt_val ) ) {
1037+
$validate = mailchimp_sf_merge_validate_address( $opt_val, $mv_var );
1038+
if ( is_wp_error( $validate ) ) {
1039+
return $validate;
1040+
}
10221041

1023-
} elseif ( is_array( $opt_val ) ) {
1024-
$keys = array_keys( $opt_val );
1025-
$val = new stdClass();
1026-
foreach ( $keys as $key ) {
1027-
$val->$key = $opt_val[ $key ];
1028-
}
1029-
$opt_val = $val;
1042+
if ( $validate ) {
1043+
$merge->$tag = $validate;
1044+
}
1045+
}
1046+
break;
1047+
1048+
/**
1049+
* Handle generic array values
1050+
*
1051+
* Not sure what this does or is for
1052+
*
1053+
* - Merge field is an array, not specifically phone or address
1054+
*/
1055+
default:
1056+
if ( is_array( $opt_val ) ) {
1057+
$keys = array_keys( $opt_val );
1058+
$val = new stdClass();
1059+
foreach ( $keys as $key ) {
1060+
$val->$key = $opt_val[ $key ];
1061+
}
1062+
$opt_val = $val;
1063+
}
1064+
break;
10301065
}
10311066

1067+
/**
1068+
* Required fields
1069+
*
1070+
* If the field is required and empty, return an error
1071+
*/
10321072
if ( 'Y' === $mv_var['required'] && trim( $opt_val ) === '' ) {
10331073
/* translators: %s: field name */
10341074
$message = sprintf( esc_html__( 'You must fill in %s.', 'mailchimp' ), esc_html( $mv_var['name'] ) );

tests/cypress/e2e/connect.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ describe('Admin can connect to "Mailchimp" Account', () => {
2323
cy.get('#mailchimp_sf_oauth_connect').click();
2424
cy.wait(6000);
2525

26+
// Accept cookie consent popup window (if present)
27+
cy.popup().then(($popup) => {
28+
const acceptButtonSelector = '#onetrust-accept-btn-handler';
29+
30+
// Check if the accept button is visible and click it
31+
if ($popup.find(acceptButtonSelector).length > 0 && $popup.find(acceptButtonSelector).is(':visible')) {
32+
$popup.find(acceptButtonSelector).click();
33+
} else {
34+
cy.log('Cookie consent popup not found or not visible.');
35+
}
36+
});
37+
2638
cy.popup()
2739
.find('input#username')
2840
.clear()

0 commit comments

Comments
 (0)