@@ -105,8 +105,6 @@ function () {
105105 */
106106function mailchimp_sf_plugin_init () {
107107
108- // Remove Sopresto check. If user does not have API key, make them authenticate.
109-
110108 if ( get_option ( 'mc_list_id ' ) && get_option ( 'mc_merge_field_migrate ' ) !== '1 ' && mailchimp_sf_get_api () !== false ) {
111109 mailchimp_sf_update_merge_fields ();
112110 }
@@ -247,7 +245,7 @@ function mailchimp_sf_request_handler() {
247245 }
248246
249247 // erase auth information
250- $ options = array ( 'mc_api_key ' , 'mailchimp_sf_access_token ' , 'mc_datacenter ' , 'mailchimp_sf_auth_error ' , 'mailchimp_sf_waiting_for_login ' , ' mc_sopresto_user ' , ' mc_sopresto_public_key ' , ' mc_sopresto_secret_key ' );
248+ $ options = array ( 'mc_api_key ' , 'mailchimp_sf_access_token ' , 'mc_datacenter ' , 'mailchimp_sf_auth_error ' , 'mailchimp_sf_waiting_for_login ' );
251249 mailchimp_sf_delete_options ( $ options );
252250 break ;
253251 case 'change_form_settings ' :
@@ -291,58 +289,6 @@ function mailchimp_sf_request_handler() {
291289}
292290add_action ( 'init ' , 'mailchimp_sf_request_handler ' );
293291
294- /**
295- * Migrate Sopresto
296- *
297- * @return void
298- */
299- function mailchimp_sf_migrate_sopresto () {
300- $ sopresto = get_option ( 'mc_sopresto_secret_key ' );
301- if ( ! $ sopresto ) {
302- return ;
303- }
304-
305- // Talk to Sopresto, make exchange, delete old sopresto things.
306- $ body = array (
307- 'public_key ' => get_option ( 'mc_sopresto_public_key ' ),
308- 'hash ' => sha1 ( get_option ( 'mc_sopresto_public_key ' ) . get_option ( 'mc_sopresto_secret_key ' ) ),
309- );
310-
311- $ url = 'https://sopresto.socialize-this.com/mailchimp/exchange ' ;
312- $ args = array (
313- 'method ' => 'POST ' ,
314- 'timeout ' => 500 ,
315- 'redirection ' => 5 ,
316- 'httpversion ' => '1.0 ' ,
317- 'user-agent ' => 'Mailchimp WordPress Plugin/ ' . get_bloginfo ( 'url ' ),
318- 'body ' => $ body ,
319- );
320-
321- // post to sopresto
322- $ key = wp_remote_post ( $ url , $ args );
323- if ( ! is_wp_error ( $ key ) && 200 === $ key ['response ' ]['code ' ] ) {
324- $ key = json_decode ( $ key ['body ' ] );
325- try {
326- $ api = new MailChimp_API ( $ key ->response );
327- } catch ( Exception $ e ) {
328- $ msg = '<strong class="mc_error_msg"> ' . $ e ->getMessage () . '</strong> ' ;
329- mailchimp_sf_global_msg ( $ msg );
330- return ;
331- }
332-
333- $ verify = mailchimp_sf_verify_key ( $ api );
334-
335- // something went wrong with the key that we had
336- if ( is_wp_error ( $ verify ) ) {
337- return ;
338- }
339-
340- delete_option ( 'mc_sopresto_public_key ' );
341- delete_option ( 'mc_sopresto_secret_key ' );
342- delete_option ( 'mc_sopresto_user ' );
343- }
344- }
345-
346292/**
347293 * Update merge fields
348294 *
@@ -421,13 +367,13 @@ function mailchimp_sf_needs_upgrade() {
421367
422368/**
423369 * Deletes all Mailchimp options
370+ *
371+ * TODO: The options names should be moved to a config file
372+ * or to a class dedicated to options
424373 **/
425374function mailchimp_sf_delete_setup () {
426375 $ options = array (
427376 'mc_user_id ' ,
428- 'mc_sopresto_user ' ,
429- 'mc_sopresto_public_key ' ,
430- 'mc_sopresto_secret_key ' ,
431377 'mc_use_javascript ' ,
432378 'mc_use_datepicker ' ,
433379 'mc_use_unsub_link ' ,
@@ -934,14 +880,18 @@ function mailchimp_sf_signup_submit() {
934880 $ url = 'lists/ ' . $ list_id . '/members/ ' . md5 ( strtolower ( $ email ) );
935881 $ status = mailchimp_sf_check_status ( $ url );
936882
937- // If update existing is turned off and the subscriber exists, error out.
938- if ( get_option ( 'mc_update_existing ' ) === false && 'subscribed ' === $ status ) {
939- $ msg = esc_html__ ( 'This email address is already subscribed to the list. ' , 'mailchimp ' );
883+ // If update existing is turned off and the subscriber is not new, error out.
884+ $ is_new_subscriber = false === $ status ;
885+ if ( ! get_option ( 'mc_update_existing ' ) && ! $ is_new_subscriber ) {
886+ $ msg = esc_html__ ( 'This email address has already been subscribed to this list. ' , 'mailchimp ' );
940887 $ error = new WP_Error ( 'mailchimp-update-existing ' , $ msg );
941888 mailchimp_sf_global_msg ( '<strong class="mc_error_msg"> ' . $ msg . '</strong> ' );
942889 return false ;
943890 }
944891
892+ // TODO: If get_option( 'mc_update_existing' ) && 'unsubscribed' === $status then
893+ // make an API request to fetch Mailchimp hosted sign up form and display to user
894+
945895 $ body = mailchimp_sf_subscribe_body ( $ merge , $ igs , $ email_type , $ email , $ status , get_option ( 'mc_double_optin ' ) );
946896 $ retval = $ api ->post ( $ url , $ body , 'PUT ' );
947897
@@ -970,40 +920,42 @@ function mailchimp_sf_signup_submit() {
970920 * Cleans up merge fields and interests to make them
971921 * API 3.0-friendly.
972922 *
973- * @param [type] $merge Merge fields
974- * @param [type] $igs Interest groups
975- * @param string $email_type Email type
976- * @param string $email Email
977- * @param string $status Status
978- * @param bool $double_optin Whether this is double optin
923+ * @param [type] $merge Merge fields
924+ * @param [type] $igs Interest groups
925+ * @param string $email_type Email type
926+ * @param string $email Email
927+ * @param string|false $status Status The subscription status ('subscribed', 'unsubscribed', 'pending', etc.) or false if an error occurred.
928+ * @param string $double_optin Whether double opt-in is enabled. "1" for enabled and "" for disabled.
979929 * @return stdClass
980930 */
981931function mailchimp_sf_subscribe_body ( $ merge , $ igs , $ email_type , $ email , $ status , $ double_optin ) {
982932 $ body = new stdClass ();
983933 $ body ->email_address = $ email ;
984934 $ body ->email_type = $ email_type ;
985935 $ body ->merge_fields = $ merge ;
936+
986937 if ( ! empty ( $ igs ) ) {
987938 $ body ->interests = $ igs ;
988939 }
989940
990- if ( 'subscribed ' !== $ status ) {
991- // single opt-in that covers new subscribers
992- if ( false === ! $ status && $ double_optin ) {
993- $ body ->status = 'subscribed ' ;
994- } else {
995- // anyone else
996- $ body ->status = 'pending ' ;
997- }
941+ // Early return for already subscribed users
942+ if ( 'subscribed ' === $ status ) {
943+ return $ body ;
998944 }
945+
946+ // Subscribe the email immediately unless double opt-in is enabled
947+ // "unsubscribed" and "subscribed" existing emails have been excluded at this stage
948+ // "pending" emails should follow double opt-in rules
949+ $ body ->status = $ double_optin ? 'pending ' : 'subscribed ' ;
950+
999951 return $ body ;
1000952}
1001953
1002954/**
1003- * Check status.
955+ * Check the status of a subscriber in the list .
1004956 *
1005- * @param string $endpoint Endpoint .
1006- * @return string
957+ * @param string $endpoint API endpoint to check the status .
958+ * @return string|false The subscription status ('subscribed', 'unsubscribed', 'pending', etc.) or false if the API returned 404 or an error occurred.
1007959 */
1008960function mailchimp_sf_check_status ( $ endpoint ) {
1009961 $ endpoint .= '?fields=status ' ;
@@ -1033,32 +985,72 @@ function mailchimp_sf_merge_submit( $mv ) {
1033985
1034986 $ opt_val = isset ( $ _POST [ $ opt ] ) ? map_deep ( stripslashes_deep ( $ _POST [ $ opt ] ), 'sanitize_text_field ' ) : '' ;
1035987
1036- // Handle phone number logic
1037- if ( isset ( $ mv_var ['options ' ]['phone_format ' ] ) && 'phone ' === $ mv_var ['type ' ] && 'US ' === $ mv_var ['options ' ]['phone_format ' ] ) {
1038- $ opt_val = mailchimp_sf_merge_validate_phone ( $ opt_val , $ mv_var );
1039- if ( is_wp_error ( $ opt_val ) ) {
1040- return $ opt_val ;
1041- }
1042- } elseif ( is_array ( $ opt_val ) && 'address ' === $ mv_var ['type ' ] ) { // Handle address logic
1043- $ validate = mailchimp_sf_merge_validate_address ( $ opt_val , $ mv_var );
1044- if ( is_wp_error ( $ validate ) ) {
1045- return $ validate ;
1046- }
988+ switch ( $ mv_var ['type ' ] ) {
989+ /**
990+ * US Phone validation
991+ *
992+ * - Merge field is phone
993+ * - Merge field is "included" in the Mailchimp admin options
994+ * - Phone format is set in Mailchimp account
995+ * - Phone format is US in Mailchimp account
996+ */
997+ case 'phone ' :
998+ if (
999+ 'on ' === get_option ( $ opt )
1000+ && isset ( $ mv_var ['options ' ]['phone_format ' ] )
1001+ && 'US ' === $ mv_var ['options ' ]['phone_format ' ]
1002+ ) {
1003+ $ opt_val = mailchimp_sf_merge_validate_phone ( $ opt_val , $ mv_var );
1004+ if ( is_wp_error ( $ opt_val ) ) {
1005+ return $ opt_val ;
1006+ }
1007+ }
1008+ break ;
10471009
1048- if ( $ validate ) {
1049- $ merge ->$ tag = $ validate ;
1050- }
1051- continue ;
1010+ /**
1011+ * Address validation
1012+ *
1013+ * - Merge field is address
1014+ * - Merge field is "included" in the Mailchimp admin options
1015+ * - Merge field is an array (address contains multiple <input> elements)
1016+ */
1017+ case 'address ' :
1018+ if ( 'on ' === get_option ( $ opt ) && is_array ( $ opt_val ) ) {
1019+ $ validate = mailchimp_sf_merge_validate_address ( $ opt_val , $ mv_var );
1020+ if ( is_wp_error ( $ validate ) ) {
1021+ return $ validate ;
1022+ }
10521023
1053- } elseif ( is_array ( $ opt_val ) ) {
1054- $ keys = array_keys ( $ opt_val );
1055- $ val = new stdClass ();
1056- foreach ( $ keys as $ key ) {
1057- $ val ->$ key = $ opt_val [ $ key ];
1058- }
1059- $ opt_val = $ val ;
1024+ if ( $ validate ) {
1025+ $ merge ->$ tag = $ validate ;
1026+ }
1027+ }
1028+ break ;
1029+
1030+ /**
1031+ * Handle generic array values
1032+ *
1033+ * Not sure what this does or is for
1034+ *
1035+ * - Merge field is an array, not specifically phone or address
1036+ */
1037+ default :
1038+ if ( is_array ( $ opt_val ) ) {
1039+ $ keys = array_keys ( $ opt_val );
1040+ $ val = new stdClass ();
1041+ foreach ( $ keys as $ key ) {
1042+ $ val ->$ key = $ opt_val [ $ key ];
1043+ }
1044+ $ opt_val = $ val ;
1045+ }
1046+ break ;
10601047 }
10611048
1049+ /**
1050+ * Required fields
1051+ *
1052+ * If the field is required and empty, return an error
1053+ */
10621054 if ( 'Y ' === $ mv_var ['required ' ] && trim ( $ opt_val ) === '' ) {
10631055 /* translators: %s: field name */
10641056 $ message = sprintf ( esc_html__ ( 'You must fill in %s. ' , 'mailchimp ' ), esc_html ( $ mv_var ['name ' ] ) );
0 commit comments