Skip to content

Commit

Permalink
Merge pull request #74 from mailchimp/fix/73
Browse files Browse the repository at this point in the history
Ensure the field name is properly added to the error message when the email address is blank.
  • Loading branch information
dkotter authored Oct 29, 2024
2 parents 2a844b7 + b767153 commit b86c227
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/mailchimp/mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/cypress/e2e/settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
});
});
Expand All @@ -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.');
}
});
});
Expand Down

0 comments on commit b86c227

Please sign in to comment.