diff --git a/lib/mailchimp/mailchimp.php b/lib/mailchimp/mailchimp.php index 03b35c1..212b97c 100644 --- a/lib/mailchimp/mailchimp.php +++ b/lib/mailchimp/mailchimp.php @@ -190,11 +190,15 @@ public function post( $endpoint, $body, $method = 'POST' ) { // Email address doesn't come back from the API, so if something's wrong, it's that. $field_name = esc_html__( 'Email Address', 'mailchimp' ); $body['errors'][0]['message'] = esc_html__( 'Please fill out a valid email address.', 'mailchimp' ); + } elseif ( ! empty( $body['errors'] ) && isset( $body['errors'][0]['field'] ) && 'email_address' === $body['errors'][0]['field'] ) { + $field_name = esc_html__( 'Email Address', 'mailchimp' ); } elseif ( ! empty( $body['errors'] ) && isset( $body['errors'][0]['field'] ) && $merge['tag'] === $body['errors'][0]['field'] ) { $field_name = $merge['name']; } } - $message = sprintf( $field_name . ': ' . $body['errors'][0]['message'] ); + $message = $body['errors'][0]['message'] ?? esc_html__( 'Something went wrong, Please try again later.', 'mailchimp' ); + $message = ( ! empty( $field_name ) ) ? $field_name . ': ' . $message : $message; + return new WP_Error( 'mc-subscribe-error-api', $message ); } } diff --git a/tests/cypress/e2e/settings.test.js b/tests/cypress/e2e/settings.test.js index ceb4406..0466b10 100644 --- a/tests/cypress/e2e/settings.test.js +++ b/tests/cypress/e2e/settings.test.js @@ -35,7 +35,7 @@ describe('Admin can update plugin settings', () => { cy.get('#mc_signup_submit').should('exist'); cy.get('#mc_signup_submit').click(); cy.get('.mc_error_msg').should('exist'); - cy.get('.mc_error_msg').contains(': This value should not be blank.'); + cy.get('.mc_error_msg').contains('Email Address: This value should not be blank.'); } }); }); @@ -55,7 +55,7 @@ describe('Admin can update plugin settings', () => { cy.get('#mc_signup_submit').should('exist'); cy.get('#mc_signup_submit').click(); cy.get('.mc_error_msg').should('exist'); - cy.get('.mc_error_msg').contains(': This value should not be blank.'); + cy.get('.mc_error_msg').contains('Email Address: This value should not be blank.'); } }); });