Skip to content

Commit

Permalink
Merge pull request #115 from mailchimp/bug/double-opt-in-triggered-de…
Browse files Browse the repository at this point in the history
…spite-being-disabled-in-settings

Bug/double opt in triggered despite being disabled in settings
  • Loading branch information
vikrampm1 authored Jan 28, 2025
2 parents 59b1a97 + 8e1e3f4 commit 6099727
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ tests/cypress/reports
tests/cypress/downloads

mailchimp.zip

# IDE
.vscode
46 changes: 26 additions & 20 deletions mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -904,14 +904,18 @@ function mailchimp_sf_signup_submit() {
$url = 'lists/' . $list_id . '/members/' . md5( strtolower( $email ) );
$status = mailchimp_sf_check_status( $url );

// If update existing is turned off and the subscriber exists, error out.
if ( get_option( 'mc_update_existing' ) === false && 'subscribed' === $status ) {
$msg = esc_html__( 'This email address is already subscribed to the list.', 'mailchimp' );
// If update existing is turned off and the subscriber is not new, error out.
$is_new_subscriber = false === $status;
if ( ! get_option( 'mc_update_existing' ) && ! $is_new_subscriber ) {
$msg = esc_html__( 'This email address has already been subscribed to this list.', 'mailchimp' );
$error = new WP_Error( 'mailchimp-update-existing', $msg );
mailchimp_sf_global_msg( '<strong class="mc_error_msg">' . $msg . '</strong>' );
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 @@ -940,40 +944,42 @@ function mailchimp_sf_signup_submit() {
* Cleans up merge fields and interests to make them
* API 3.0-friendly.
*
* @param [type] $merge Merge fields
* @param [type] $igs Interest groups
* @param string $email_type Email type
* @param string $email Email
* @param string $status Status
* @param bool $double_optin Whether this is double optin
* @param [type] $merge Merge fields
* @param [type] $igs Interest groups
* @param string $email_type Email type
* @param string $email Email
* @param string|false $status Status The subscription status ('subscribed', 'unsubscribed', 'pending', etc.) or false if an error occurred.
* @param string $double_optin Whether double opt-in is enabled. "1" for enabled and "" for disabled.
* @return stdClass
*/
function mailchimp_sf_subscribe_body( $merge, $igs, $email_type, $email, $status, $double_optin ) {
$body = new stdClass();
$body->email_address = $email;
$body->email_type = $email_type;
$body->merge_fields = $merge;

if ( ! empty( $igs ) ) {
$body->interests = $igs;
}

if ( 'subscribed' !== $status ) {
// single opt-in that covers new subscribers
if ( false === ! $status && $double_optin ) {
$body->status = 'subscribed';
} else {
// anyone else
$body->status = 'pending';
}
// Early return for already subscribed users
if ( 'subscribed' === $status ) {
return $body;
}

// Subscribe the email immediately unless double opt-in is enabled
// "unsubscribed" and "subscribed" existing emails have been excluded at this stage
// "pending" emails should follow double opt-in rules
$body->status = $double_optin ? 'pending' : 'subscribed';

return $body;
}

/**
* Check status.
* Check the status of a subscriber in the list.
*
* @param string $endpoint Endpoint.
* @return string
* @param string $endpoint API endpoint to check the status.
* @return string|false The subscription status ('subscribed', 'unsubscribed', 'pending', etc.) or false if the API returned 404 or an error occurred.
*/
function mailchimp_sf_check_status( $endpoint ) {
$endpoint .= '?fields=status';
Expand Down

0 comments on commit 6099727

Please sign in to comment.