@@ -904,14 +904,18 @@ function mailchimp_sf_signup_submit() {
904
904
$ url = 'lists/ ' . $ list_id . '/members/ ' . md5 ( strtolower ( $ email ) );
905
905
$ status = mailchimp_sf_check_status ( $ url );
906
906
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 ' );
910
911
$ error = new WP_Error ( 'mailchimp-update-existing ' , $ msg );
911
912
mailchimp_sf_global_msg ( '<strong class="mc_error_msg"> ' . $ msg . '</strong> ' );
912
913
return false ;
913
914
}
914
915
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
+
915
919
$ body = mailchimp_sf_subscribe_body ( $ merge , $ igs , $ email_type , $ email , $ status , get_option ( 'mc_double_optin ' ) );
916
920
$ retval = $ api ->post ( $ url , $ body , 'PUT ' );
917
921
@@ -940,40 +944,42 @@ function mailchimp_sf_signup_submit() {
940
944
* Cleans up merge fields and interests to make them
941
945
* API 3.0-friendly.
942
946
*
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.
949
953
* @return stdClass
950
954
*/
951
955
function mailchimp_sf_subscribe_body ( $ merge , $ igs , $ email_type , $ email , $ status , $ double_optin ) {
952
956
$ body = new stdClass ();
953
957
$ body ->email_address = $ email ;
954
958
$ body ->email_type = $ email_type ;
955
959
$ body ->merge_fields = $ merge ;
960
+
956
961
if ( ! empty ( $ igs ) ) {
957
962
$ body ->interests = $ igs ;
958
963
}
959
964
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 ;
968
968
}
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
+
969
975
return $ body ;
970
976
}
971
977
972
978
/**
973
- * Check status.
979
+ * Check the status of a subscriber in the list .
974
980
*
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.
977
983
*/
978
984
function mailchimp_sf_check_status ( $ endpoint ) {
979
985
$ endpoint .= '?fields=status ' ;
0 commit comments