Skip to content

Commit f675ce1

Browse files
authored
Merge pull request #140 from mailchimp/fix/117
Display an error message informing the user to subscribe through the hosted form if they were previously unsubscribed.
2 parents 8e7fcc5 + 0b88fc5 commit f675ce1

File tree

5 files changed

+88
-9
lines changed

5 files changed

+88
-9
lines changed

lib/mailchimp/mailchimp.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,28 @@ public function post( $endpoint, $body, $method = 'POST', $list_id = '' ) {
190190
$merges = get_option( 'mailchimp_sf_merge_fields_' . $list_id );
191191
}
192192

193+
// Check if the email address is in compliance state.
194+
if ( ! isset( $body['errors'] ) && isset( $body['status'] ) && isset( $body['title'] ) && 400 === $body['status'] && 'Member In Compliance State' === $body['title'] ) {
195+
$url = mailchimp_sf_signup_form_url( $list_id );
196+
$message = wp_kses(
197+
sprintf(
198+
/* translators: %s: Hosted form Url */
199+
__(
200+
'The email address cannot be subscribed because it was previously unsubscribed, bounced, or is under review. Please <a href="%s" target="_blank">sign up here.</a>',
201+
'mailchimp'
202+
),
203+
esc_url( $url )
204+
),
205+
[
206+
'a' => [
207+
'href' => [],
208+
'target' => [],
209+
],
210+
]
211+
);
212+
return new WP_Error( 'mc-subscribe-error-compliance', $message );
213+
}
214+
193215
$field_name = '';
194216
foreach ( $merges as $merge ) {
195217
if ( empty( $body['errors'] ) ) {

mailchimp.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -883,9 +883,6 @@ function mailchimp_sf_signup_submit() {
883883
return false;
884884
}
885885

886-
// TODO: If get_option( 'mc_update_existing' ) && 'unsubscribed' === $status then
887-
// make an API request to fetch Mailchimp hosted sign up form and display to user
888-
889886
$body = mailchimp_sf_subscribe_body( $merge, $igs, $email_type, $email, $status, get_option( 'mc_double_optin' ) );
890887
$retval = $api->post( $url, $body, 'PUT' );
891888

@@ -1248,13 +1245,16 @@ function mailchimp_sf_update_profile_url( $email ) {
12481245
/**
12491246
* Get signup form URL.
12501247
*
1248+
* @param string $list_id List ID
12511249
* @return string
12521250
*/
1253-
function mailchimp_sf_signup_form_url() {
1254-
$dc = get_option( 'mc_datacenter' );
1255-
$user = get_option( 'mc_user' );
1256-
$list_id = get_option( 'mc_list_id' );
1257-
$url = 'http://' . $dc . '.list-manage.com/subscribe?u=' . $user['account_id'] . '&id=' . $list_id;
1251+
function mailchimp_sf_signup_form_url( $list_id = '' ) {
1252+
$dc = get_option( 'mc_datacenter' );
1253+
$user = get_option( 'mc_user' );
1254+
if ( empty( $list_id ) ) {
1255+
$list_id = get_option( 'mc_list_id' );
1256+
}
1257+
$url = 'http://' . $dc . '.list-manage.com/subscribe?u=' . $user['account_id'] . '&id=' . $list_id;
12581258
return $url;
12591259
}
12601260

tests/cypress/e2e/mailchimp-block.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,37 @@ describe('Block Tests', () => {
246246
});
247247
});
248248

249+
it('Proper error message should display if unsubscribed user try to subscribe', () => {
250+
cy.visit(`/wp-admin/post.php?post=${postId}&action=edit`);
251+
cy.getBlockEditor().find('h2[aria-label="Enter a header (optional)"]').click();
252+
cy.openDocumentSettingsPanel('Form Settings', 'Block');
253+
cy.get('.mailchimp-double-opt-in input.components-form-toggle__input').first().uncheck();
254+
cy.get('.mailchimp-update-existing-subscribers input.components-form-toggle__input')
255+
.first()
256+
.check();
257+
cy.get('button.editor-post-publish-button').click();
258+
cy.wait(500);
259+
260+
// Verify
261+
cy.visit(`/?p=${postId}`);
262+
cy.get('#mc_mv_EMAIL').should('exist');
263+
cy.get('#mc_mv_EMAIL').clear().type('unsubscribed_user@gmail.com');
264+
cy.get('#mc_signup_submit').should('exist');
265+
cy.get('#mc_signup_submit').click();
266+
cy.get('.mc_error_msg').should('exist');
267+
cy.get('.mc_error_msg').contains(
268+
'The email address cannot be subscribed because it was previously unsubscribed, bounced, or is under review. Please sign up here.',
269+
);
270+
271+
// Reset
272+
cy.visit(`/wp-admin/post.php?post=${postId}&action=edit`);
273+
cy.getBlockEditor().find('h2[aria-label="Enter a header (optional)"]').click();
274+
cy.openDocumentSettingsPanel('Form Settings', 'Block');
275+
cy.get('.mailchimp-double-opt-in input.components-form-toggle__input').first().check();
276+
cy.get('button.editor-post-publish-button').click();
277+
cy.wait(500);
278+
});
279+
249280
it('Form data should persist if validation fails', () => {
250281
// Verify
251282
const firstName = 'John';

tests/cypress/e2e/settings.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,32 @@ describe('Admin can update plugin settings', () => {
154154
});
155155
});
156156

157+
it('Proper error message should display if unsubscribed user try to subscribe', () => {
158+
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
159+
cy.get('#mc_double_optin').uncheck();
160+
cy.get('#mc_update_existing').check();
161+
cy.get('input[value="Update Subscribe Form Settings"]').first().click();
162+
163+
// Verify
164+
[shortcodePostURL, blockPostPostURL].forEach((url) => {
165+
cy.visit(url);
166+
cy.get('#mc_mv_EMAIL').should('exist');
167+
cy.get('#mc_mv_EMAIL').clear().type('unsubscribed_user@gmail.com');
168+
cy.get('#mc_signup_submit').should('exist');
169+
cy.get('#mc_signup_submit').click();
170+
cy.get('.mc_error_msg').should('exist');
171+
cy.get('.mc_error_msg').contains(
172+
'The email address cannot be subscribed because it was previously unsubscribed, bounced, or is under review. Please sign up here.',
173+
);
174+
});
175+
176+
// Reset
177+
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
178+
cy.get('#mc_double_optin').check();
179+
cy.get('#mc_update_existing').check();
180+
cy.get('input[value="Update Subscribe Form Settings"]').first().click();
181+
});
182+
157183
it('Form data should persist if validation fails', () => {
158184
// Verify
159185
[shortcodePostURL, blockPostPostURL].forEach((url) => {

views/css/frontend.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
?>
9-
.mc_error_msg {
9+
.mc_error_msg, .mc_error_msg a {
1010
color: red;
1111
margin-bottom: 1.0em;
1212
}

0 commit comments

Comments
 (0)