@@ -904,14 +904,18 @@ function mailchimp_sf_signup_submit() {
904904 $ url = 'lists/ ' . $ list_id . '/members/ ' . md5 ( strtolower ( $ email ) );
905905 $ status = mailchimp_sf_check_status ( $ url );
906906
907- // If update existing is turned off and the subscriber exists, error out.
908- if ( get_option ( 'mc_update_existing ' ) === false && 'subscribed ' === $ status ) {
909- $ msg = esc_html__ ( 'This email address is already subscribed to the list. ' , 'mailchimp ' );
907+ // If update existing is turned off and the subscriber is not new, error out.
908+ $ is_new_subscriber = false === $ status ;
909+ if ( ! get_option ( 'mc_update_existing ' ) && ! $ is_new_subscriber ) {
910+ $ msg = esc_html__ ( 'This email address has already been subscribed to this list. ' , 'mailchimp ' );
910911 $ error = new WP_Error ( 'mailchimp-update-existing ' , $ msg );
911912 mailchimp_sf_global_msg ( '<strong class="mc_error_msg"> ' . $ msg . '</strong> ' );
912913 return false ;
913914 }
914915
916+ // TODO: If get_option( 'mc_update_existing' ) && 'unsubscribed' === $status then
917+ // make an API request to fetch Mailchimp hosted sign up form and display to user
918+
915919 $ body = mailchimp_sf_subscribe_body ( $ merge , $ igs , $ email_type , $ email , $ status , get_option ( 'mc_double_optin ' ) );
916920 $ retval = $ api ->post ( $ url , $ body , 'PUT ' );
917921
@@ -940,40 +944,42 @@ function mailchimp_sf_signup_submit() {
940944 * Cleans up merge fields and interests to make them
941945 * API 3.0-friendly.
942946 *
943- * @param [type] $merge Merge fields
944- * @param [type] $igs Interest groups
945- * @param string $email_type Email type
946- * @param string $email Email
947- * @param string $status Status
948- * @param bool $double_optin Whether this is double optin
947+ * @param [type] $merge Merge fields
948+ * @param [type] $igs Interest groups
949+ * @param string $email_type Email type
950+ * @param string $email Email
951+ * @param string|false $status Status The subscription status ('subscribed', 'unsubscribed', 'pending', etc.) or false if an error occurred.
952+ * @param string $double_optin Whether double opt-in is enabled. "1" for enabled and "" for disabled.
949953 * @return stdClass
950954 */
951955function mailchimp_sf_subscribe_body ( $ merge , $ igs , $ email_type , $ email , $ status , $ double_optin ) {
952956 $ body = new stdClass ();
953957 $ body ->email_address = $ email ;
954958 $ body ->email_type = $ email_type ;
955959 $ body ->merge_fields = $ merge ;
960+
956961 if ( ! empty ( $ igs ) ) {
957962 $ body ->interests = $ igs ;
958963 }
959964
960- if ( 'subscribed ' !== $ status ) {
961- // single opt-in that covers new subscribers
962- if ( false === ! $ status && $ double_optin ) {
963- $ body ->status = 'subscribed ' ;
964- } else {
965- // anyone else
966- $ body ->status = 'pending ' ;
967- }
965+ // Early return for already subscribed users
966+ if ( 'subscribed ' === $ status ) {
967+ return $ body ;
968968 }
969+
970+ // Subscribe the email immediately unless double opt-in is enabled
971+ // "unsubscribed" and "subscribed" existing emails have been excluded at this stage
972+ // "pending" emails should follow double opt-in rules
973+ $ body ->status = $ double_optin ? 'pending ' : 'subscribed ' ;
974+
969975 return $ body ;
970976}
971977
972978/**
973- * Check status.
979+ * Check the status of a subscriber in the list .
974980 *
975- * @param string $endpoint Endpoint .
976- * @return string
981+ * @param string $endpoint API endpoint to check the status .
982+ * @return string|false The subscription status ('subscribed', 'unsubscribed', 'pending', etc.) or false if the API returned 404 or an error occurred.
977983 */
978984function mailchimp_sf_check_status ( $ endpoint ) {
979985 $ endpoint .= '?fields=status ' ;
0 commit comments