Skip to content

Commit 271f3aa

Browse files
Refactor if else statement to switch for readability
1 parent 23fb25b commit 271f3aa

File tree

1 file changed

+57
-41
lines changed

1 file changed

+57
-41
lines changed

mailchimp.php

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,54 +1003,70 @@ function mailchimp_sf_merge_submit( $mv ) {
10031003

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

1006-
/**
1007-
* US Phone validation
1008-
*
1009-
* - Merge field is phone
1010-
* - Merge field is "included" in the Mailchimp admin options
1011-
* - Phone format is set in Mailchimp account
1012-
* - Phone format is US in Mailchimp account
1013-
*/
1014-
if ( 'phone' === $mv_var['type'] && 'on' === get_option( $opt ) && isset( $mv_var['options']['phone_format'] ) && 'US' === $mv_var['options']['phone_format'] ) {
1015-
$opt_val = mailchimp_sf_merge_validate_phone( $opt_val, $mv_var );
1016-
if ( is_wp_error( $opt_val ) ) {
1017-
return $opt_val;
1018-
}
1019-
}
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;
10201027

1021-
/**
1022-
* Address validation
1023-
*
1024-
* - Merge field is address
1025-
* - Merge field is "included" in the Mailchimp admin options
1026-
* - Merge field is an array (address contains multiple <input> elements)
1027-
*/
1028-
elseif ( 'address' === $mv_var['type'] && 'on' === get_option( $opt ) && is_array( $opt_val ) ) { // Handle address logic
1029-
$validate = mailchimp_sf_merge_validate_address( $opt_val, $mv_var );
1030-
if ( is_wp_error( $validate ) ) {
1031-
return $validate;
1032-
}
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+
}
10331041

1034-
if ( $validate ) {
1035-
$merge->$tag = $validate;
1036-
}
1037-
continue;
1038-
}
1042+
if ( $validate ) {
1043+
$merge->$tag = $validate;
1044+
}
1045+
}
1046+
break;
10391047

1040-
/**
1041-
* Not sure what this is for
1042-
*/
1043-
elseif ( is_array( $opt_val ) ) {
1044-
$keys = array_keys( $opt_val );
1045-
$val = new stdClass();
1046-
foreach ( $keys as $key ) {
1047-
$val->$key = $opt_val[ $key ];
1048-
}
1049-
$opt_val = $val;
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;
10501065
}
10511066

10521067
/**
10531068
* Required fields
1069+
*
10541070
* If the field is required and empty, return an error
10551071
*/
10561072
if ( 'Y' === $mv_var['required'] && trim( $opt_val ) === '' ) {

0 commit comments

Comments
 (0)