Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure the field name is properly added to the error message when the email address is blank. #74

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading