Skip to content
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
22 changes: 22 additions & 0 deletions lib/mailchimp/mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,28 @@ public function post( $endpoint, $body, $method = 'POST', $list_id = '' ) {
$merges = get_option( 'mailchimp_sf_merge_fields_' . $list_id );
}

// Check if the email address is in compliance state.
if ( ! isset( $body['errors'] ) && isset( $body['status'] ) && isset( $body['title'] ) && 400 === $body['status'] && 'Member In Compliance State' === $body['title'] ) {
$url = mailchimp_sf_signup_form_url( $list_id );
$message = wp_kses(
sprintf(
/* translators: %s: Hosted form Url */
__(
'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>',
'mailchimp'
),
esc_url( $url )
),
[
'a' => [
'href' => [],
'target' => [],
],
]
);
return new WP_Error( 'mc-subscribe-error-compliance', $message );
}

$field_name = '';
foreach ( $merges as $merge ) {
if ( empty( $body['errors'] ) ) {
Expand Down
16 changes: 8 additions & 8 deletions mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,6 @@ function mailchimp_sf_signup_submit() {
return false;
}

// TODO: If get_option( 'mc_update_existing' ) && 'unsubscribed' === $status then
// make an API request to fetch Mailchimp hosted sign up form and display to user

$body = mailchimp_sf_subscribe_body( $merge, $igs, $email_type, $email, $status, get_option( 'mc_double_optin' ) );
$retval = $api->post( $url, $body, 'PUT' );

Expand Down Expand Up @@ -1224,13 +1221,16 @@ function mailchimp_sf_update_profile_url( $email ) {
/**
* Get signup form URL.
*
* @param string $list_id List ID
* @return string
*/
function mailchimp_sf_signup_form_url() {
$dc = get_option( 'mc_datacenter' );
$user = get_option( 'mc_user' );
$list_id = get_option( 'mc_list_id' );
$url = 'http://' . $dc . '.list-manage.com/subscribe?u=' . $user['account_id'] . '&id=' . $list_id;
function mailchimp_sf_signup_form_url( $list_id = '' ) {
$dc = get_option( 'mc_datacenter' );
$user = get_option( 'mc_user' );
if ( empty( $list_id ) ) {
$list_id = get_option( 'mc_list_id' );
}
$url = 'http://' . $dc . '.list-manage.com/subscribe?u=' . $user['account_id'] . '&id=' . $list_id;
return $url;
}

Expand Down
31 changes: 31 additions & 0 deletions tests/cypress/e2e/mailchimp-block.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,37 @@ describe('Block Tests', () => {
});
});

it('Proper error message should display if unsubscribed user try to subscribe', () => {
cy.visit(`/wp-admin/post.php?post=${postId}&action=edit`);
cy.getBlockEditor().find('h2[aria-label="Enter a header (optional)"]').click();
cy.openDocumentSettingsPanel('Form Settings', 'Block');
cy.get('.mailchimp-double-opt-in input.components-form-toggle__input').first().uncheck();
cy.get('.mailchimp-update-existing-subscribers input.components-form-toggle__input')
.first()
.check();
cy.get('button.editor-post-publish-button').click();
cy.wait(500);

// Verify
cy.visit(`/?p=${postId}`);
cy.get('#mc_mv_EMAIL').should('exist');
cy.get('#mc_mv_EMAIL').clear().type('unsubscribed_user@gmail.com');
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(
'The email address cannot be subscribed because it was previously unsubscribed, bounced, or is under review. Please sign up here.',
);

// Reset
cy.visit(`/wp-admin/post.php?post=${postId}&action=edit`);
cy.getBlockEditor().find('h2[aria-label="Enter a header (optional)"]').click();
cy.openDocumentSettingsPanel('Form Settings', 'Block');
cy.get('.mailchimp-double-opt-in input.components-form-toggle__input').first().check();
cy.get('button.editor-post-publish-button').click();
cy.wait(500);
});

// TODO: Add tests for the Double Opt-in and Update existing subscribers settings.
// TODO: Add tests for the block styles settings.
// TODO: Add tests for the form submission.
Expand Down
26 changes: 26 additions & 0 deletions tests/cypress/e2e/settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,30 @@ describe('Admin can update plugin settings', () => {
cy.get('#mc_unsub_link').should('not.exist');
});
});

it('Proper error message should display if unsubscribed user try to subscribe', () => {
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
cy.get('#mc_double_optin').uncheck();
cy.get('#mc_update_existing').check();
cy.get('input[value="Update Subscribe Form Settings"]').first().click();

// Verify
[shortcodePostURL, blockPostPostURL].forEach((url) => {
cy.visit(url);
cy.get('#mc_mv_EMAIL').should('exist');
cy.get('#mc_mv_EMAIL').clear().type('unsubscribed_user@gmail.com');
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(
'The email address cannot be subscribed because it was previously unsubscribed, bounced, or is under review. Please sign up here.',
);
});

// Reset
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
cy.get('#mc_double_optin').check();
cy.get('#mc_update_existing').check();
cy.get('input[value="Update Subscribe Form Settings"]').first().click();
});
});
2 changes: 1 addition & 1 deletion views/css/frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

?>
.mc_error_msg {
.mc_error_msg, .mc_error_msg a {
color: red;
margin-bottom: 1.0em;
}
Expand Down