Skip to content

Commit b86c227

Browse files
authored
Merge pull request #74 from mailchimp/fix/73
Ensure the field name is properly added to the error message when the email address is blank.
2 parents 2a844b7 + b767153 commit b86c227

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/mailchimp/mailchimp.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,15 @@ public function post( $endpoint, $body, $method = 'POST' ) {
190190
// Email address doesn't come back from the API, so if something's wrong, it's that.
191191
$field_name = esc_html__( 'Email Address', 'mailchimp' );
192192
$body['errors'][0]['message'] = esc_html__( 'Please fill out a valid email address.', 'mailchimp' );
193+
} elseif ( ! empty( $body['errors'] ) && isset( $body['errors'][0]['field'] ) && 'email_address' === $body['errors'][0]['field'] ) {
194+
$field_name = esc_html__( 'Email Address', 'mailchimp' );
193195
} elseif ( ! empty( $body['errors'] ) && isset( $body['errors'][0]['field'] ) && $merge['tag'] === $body['errors'][0]['field'] ) {
194196
$field_name = $merge['name'];
195197
}
196198
}
197-
$message = sprintf( $field_name . ': ' . $body['errors'][0]['message'] );
199+
$message = $body['errors'][0]['message'] ?? esc_html__( 'Something went wrong, Please try again later.', 'mailchimp' );
200+
$message = ( ! empty( $field_name ) ) ? $field_name . ': ' . $message : $message;
201+
198202
return new WP_Error( 'mc-subscribe-error-api', $message );
199203
}
200204
}

tests/cypress/e2e/settings.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('Admin can update plugin settings', () => {
3535
cy.get('#mc_signup_submit').should('exist');
3636
cy.get('#mc_signup_submit').click();
3737
cy.get('.mc_error_msg').should('exist');
38-
cy.get('.mc_error_msg').contains(': This value should not be blank.');
38+
cy.get('.mc_error_msg').contains('Email Address: This value should not be blank.');
3939
}
4040
});
4141
});
@@ -55,7 +55,7 @@ describe('Admin can update plugin settings', () => {
5555
cy.get('#mc_signup_submit').should('exist');
5656
cy.get('#mc_signup_submit').click();
5757
cy.get('.mc_error_msg').should('exist');
58-
cy.get('.mc_error_msg').contains(': This value should not be blank.');
58+
cy.get('.mc_error_msg').contains('Email Address: This value should not be blank.');
5959
}
6060
});
6161
});

0 commit comments

Comments
 (0)