From a5d52940f6e7a6e8fb4139c8425511e97ef4aa61 Mon Sep 17 00:00:00 2001 From: Rodrigo Ziviani de Souza Date: Fri, 9 Oct 2020 18:38:41 +1300 Subject: [PATCH 1/5] [IN-1646_DOUBLEOPTIN] IN-1646 Fixed broken Double Opt-In --- views/admin.functions.subscribe.options.php | 19 ++- widget.subscribe.php | 137 +++++++++----------- 2 files changed, 79 insertions(+), 77 deletions(-) diff --git a/views/admin.functions.subscribe.options.php b/views/admin.functions.subscribe.options.php index 84a3a56..f260bdc 100644 --- a/views/admin.functions.subscribe.options.php +++ b/views/admin.functions.subscribe.options.php @@ -12,8 +12,22 @@ function welcome_template_callback( $args ) { echo '
'; echo '
'; - echo '

Welcome Template

'; - echo '

Choose a template to send after a user signs up using the Sailthru Subscribe Widget or shortcode.'; + echo '

Welcome Email

'; + echo ' +

+ Optional. Choose a template to send after a user signs up using the Sailthru Subscribe Widget. + The email will only be sent when a user to at least one of the lists selected in the subscribe widget. +

+

+ If using double opt-in, customers can be added to the appropriate list via + + signup_confirm + . + For more information visit the + + Wordpress Integration Documentation + . +

'; } function welcome_template( $args ) { @@ -62,7 +76,6 @@ function sailthru_double_opt_in_callback() { $options = get_option( 'sailthru_forms_options' ); $sailthru_double_opt_in = isset( $options['sailthru_double_opt_in'] ) ? $options['sailthru_double_opt_in'] : ''; echo ''; - echo 'Important: Ensure the template uses signup_confirm or the user will not be added to a list

The welcome email will only be sent when a user does not belong to a list selected in the subscribe widget.

'; } function sailthru_forms_callback( $args ) { diff --git a/widget.subscribe.php b/widget.subscribe.php index 644f0f5..ce419ef 100644 --- a/widget.subscribe.php +++ b/widget.subscribe.php @@ -359,33 +359,8 @@ function add_subscriber() { } } //end for loop - - $subscribe_to_lists = []; - if ( isset($_POST['sailthru_email_list']) ){ - $sailthru_email_list = sanitize_text_field( $_POST['sailthru_email_list'] ); - } - if ( ! empty( $sailthru_email_list ) ) { - - // check for double opt in setting - if ( isset( $customfields['sailthru_double_opt_in'] ) && true === $customfields['sailthru_double_opt_in'] ) { - $double_opt_in = true; - } else { - $double_opt_in = false; - } - - $lists = explode( ',', $sailthru_email_list ); - - foreach ( $lists as $key => $list ) { - $subscribe_to_lists[ $list ] = 1; - } - - $options['lists'] = $subscribe_to_lists; - - } else { - - $options['lists'] = array( 'Sailthru Subscribe Widget' => 1 ); // subscriber is an orphan - - } + $double_opt_in = !empty( $customfields['sailthru_double_opt_in'] ); + $subscribe_to_lists = $this->extract_subscribe_to_list(); // clean up vars unset( $vars['email'] ); @@ -441,7 +416,7 @@ function add_subscriber() { 'id' => $email, 'key' => 'email', 'vars' => $options['vars'], - 'lists' => $options['lists'], + 'lists' => $double_opt_in ? '' : $subscribe_to_lists, ]; $should_update_optout = isset( $_POST['reset_optout_status'] ) && ! empty( $_POST['reset_optout_status'] ) ? 'none': ''; @@ -449,54 +424,15 @@ function add_subscriber() { $profile_data['optout_email'] = 'none'; } - if ( ! empty( $profile ) ) { - - if ( isset( $profile['lists'] ) && count( $profile['lists'] ) > 0 ) { - - // now we need to check which lists they are being subscribed to which they are not a member of. - foreach ( $options['lists'] as $list_name => $list_val ) { - - if ( ! array_key_exists( $list_name, $profile['lists'] ) ) { - $new_lists[] = $list_name; - } - } - } else { - // all of the lists are new lists - $new_lists = $options['lists']; - } - - try { - $client->apiPost( 'user', $profile_data ); - $new_lists = $options['lists']; - - } catch ( Sailthru_Client_Exception $e ) { - write_log( $e ); - } - - } else { - - try { - $client->apiPost( 'user', $profile_data ); - $new_lists = $options['lists']; - - } catch ( Sailthru_Client_Exception $e ) { - write_log( $e ); - } - } - - // check if the user is a new subscriber to any lists. - $new_subscriber = count( $new_lists ) > 0 ? true : false; + $this->post_user_profile( $client, $profile_data ); - if ( isset( $customfields['sailthru_welcome_template'] ) && ! empty( $customfields['sailthru_welcome_template'] ) ) { + $new_lists = empty( $profile ) + ? array_keys( $subscribe_to_lists ) + : $this->filter_out_existing_lists( $profile, $subscribe_to_lists ); - if ( $new_subscriber ) { - - try { - $client->send( $customfields['sailthru_welcome_template'], $email, $vars ); - } catch ( Sailthru_Client_Exception $e ) { - write_log( $e ); - } - } + if ( $this->should_send_welcome_template( $customfields, $new_lists ) ) { + $vars['signup_lists'] = $new_lists; + $this->send_welcome_template( $client, $customfields, $email, $vars ); } // Handle the Event If it's been set to fire. @@ -548,8 +484,61 @@ function add_subscriber() { } + private function extract_subscribe_to_list(): array + { + if ( isset($_POST['sailthru_email_list']) ) { + $sailthru_email_list = sanitize_text_field( $_POST['sailthru_email_list'] ); + } + + if ( empty($sailthru_email_list) ) { + return array('Sailthru Subscribe Widget' => 1); // subscriber is an orphan + } + + return $this->create_subscribe_to_list( $sailthru_email_list ); + } + + private function create_subscribe_to_list( $sailthru_email_list ): array { + $lists = explode(',', $sailthru_email_list); + $subscribe_to_lists = []; + foreach (array_values($lists) as $list_name) { + $subscribe_to_lists[$list_name] = 1; + } + + return $subscribe_to_lists; + } + private function post_user_profile( $client, $profile_data ): void + { + try { + $client->apiPost( 'user', $profile_data ); + } catch ( Sailthru_Client_Exception $e ) { + write_log( $e ); + } + } + + private function should_send_welcome_template( array $custom_fields, array $new_lists ): bool + { + return ! empty( $custom_fields['sailthru_welcome_template'] ) && !empty( $new_lists ); + } + + private function send_welcome_template( $client, $custom_fields, $email, $vars ): void + { + try { + $client->send( $custom_fields['sailthru_welcome_template'], $email, $vars ); + } catch ( Sailthru_Client_Exception $e ) { + write_log( $e ); + } + } + + private function filter_out_existing_lists( array $profile, array $subscribe_to_lists ): array + { + $new_list_diff = empty( $profile['lists'] ) + ? $subscribe_to_lists + : array_diff_key( $subscribe_to_lists, $profile['lists'] ); + + return array_keys( $new_list_diff ); + } } // end class From 666eb7eedfb2e0589ed78483304d4d1f217ddb41 Mon Sep 17 00:00:00 2001 From: Rodrigo Ziviani de Souza Date: Tue, 13 Oct 2020 18:28:27 +1300 Subject: [PATCH 2/5] [IN-1646_DOUBLEOPTIN] IN-1646 Fixed broken Double Opt-In - Code review improvements --- views/admin.functions.subscribe.options.php | 2 +- widget.subscribe.php | 41 ++++++++++----------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/views/admin.functions.subscribe.options.php b/views/admin.functions.subscribe.options.php index f260bdc..d6c94bd 100644 --- a/views/admin.functions.subscribe.options.php +++ b/views/admin.functions.subscribe.options.php @@ -16,7 +16,7 @@ function welcome_template_callback( $args ) { echo '

Optional. Choose a template to send after a user signs up using the Sailthru Subscribe Widget. - The email will only be sent when a user to at least one of the lists selected in the subscribe widget. + The email will only be sent when a user does not belong to at least one of the lists selected in the subscribe widget.

If using double opt-in, customers can be added to the appropriate list via diff --git a/widget.subscribe.php b/widget.subscribe.php index ce419ef..dd4489b 100644 --- a/widget.subscribe.php +++ b/widget.subscribe.php @@ -359,9 +359,6 @@ function add_subscriber() { } } //end for loop - $double_opt_in = !empty( $customfields['sailthru_double_opt_in'] ); - $subscribe_to_lists = $this->extract_subscribe_to_list(); - // clean up vars unset( $vars['email'] ); unset( $vars['sailthru_nonce'] ); @@ -412,6 +409,9 @@ function add_subscriber() { } } + $double_opt_in = !empty( $customfields['sailthru_double_opt_in'] ); + $subscribe_to_lists = $this->extract_subscribe_to_list(); + $profile_data = [ 'id' => $email, 'key' => 'email', @@ -426,13 +426,11 @@ function add_subscriber() { $this->post_user_profile( $client, $profile_data ); - $new_lists = empty( $profile ) - ? array_keys( $subscribe_to_lists ) - : $this->filter_out_existing_lists( $profile, $subscribe_to_lists ); + $new_lists = $this->filter_out_existing_lists( $profile, $subscribe_to_lists ); if ( $this->should_send_welcome_template( $customfields, $new_lists ) ) { $vars['signup_lists'] = $new_lists; - $this->send_welcome_template( $client, $customfields, $email, $vars ); + $this->send_template( $client, $customfields['sailthru_welcome_template'], $email, $vars ); } // Handle the Event If it's been set to fire. @@ -484,8 +482,7 @@ function add_subscriber() { } - private function extract_subscribe_to_list(): array - { + private function extract_subscribe_to_list(): array { if ( isset($_POST['sailthru_email_list']) ) { $sailthru_email_list = sanitize_text_field( $_POST['sailthru_email_list'] ); } @@ -494,11 +491,11 @@ private function extract_subscribe_to_list(): array return array('Sailthru Subscribe Widget' => 1); // subscriber is an orphan } - return $this->create_subscribe_to_list( $sailthru_email_list ); + return $this->create_email_list_array( $sailthru_email_list ); } - private function create_subscribe_to_list( $sailthru_email_list ): array { - $lists = explode(',', $sailthru_email_list); + private function create_email_list_array( $email_list ): array { + $lists = explode(',', $email_list); $subscribe_to_lists = []; foreach (array_values($lists) as $list_name) { @@ -508,8 +505,7 @@ private function create_subscribe_to_list( $sailthru_email_list ): array { return $subscribe_to_lists; } - private function post_user_profile( $client, $profile_data ): void - { + private function post_user_profile( $client, $profile_data ): void { try { $client->apiPost( 'user', $profile_data ); } catch ( Sailthru_Client_Exception $e ) { @@ -517,22 +513,23 @@ private function post_user_profile( $client, $profile_data ): void } } - private function should_send_welcome_template( array $custom_fields, array $new_lists ): bool - { - return ! empty( $custom_fields['sailthru_welcome_template'] ) && !empty( $new_lists ); + private function should_send_welcome_template( array $custom_fields, array $new_lists ): bool { + return ! empty($custom_fields['sailthru_welcome_template']) && ! empty($new_lists); } - private function send_welcome_template( $client, $custom_fields, $email, $vars ): void - { + private function send_template( $client, string $template, string $email, array $vars ): void { try { - $client->send( $custom_fields['sailthru_welcome_template'], $email, $vars ); + $client->send( $template, $email, $vars ); } catch ( Sailthru_Client_Exception $e ) { write_log( $e ); } } - private function filter_out_existing_lists( array $profile, array $subscribe_to_lists ): array - { + private function filter_out_existing_lists( array $profile, array $subscribe_to_lists ): array { + if (empty($profile)) { + return array_keys( $subscribe_to_lists ); + } + $new_list_diff = empty( $profile['lists'] ) ? $subscribe_to_lists : array_diff_key( $subscribe_to_lists, $profile['lists'] ); From db7702f3de76911bc1b03b18908232f854231266 Mon Sep 17 00:00:00 2001 From: Rodrigo Ziviani de Souza Date: Wed, 14 Oct 2020 12:28:34 +1300 Subject: [PATCH 3/5] [IN-1646_DOUBLEOPTIN] IN-1646 Fixed broken Double Opt-In - Code review, improved function name --- widget.subscribe.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/widget.subscribe.php b/widget.subscribe.php index dd4489b..f4a95f5 100644 --- a/widget.subscribe.php +++ b/widget.subscribe.php @@ -491,10 +491,10 @@ private function extract_subscribe_to_list(): array { return array('Sailthru Subscribe Widget' => 1); // subscriber is an orphan } - return $this->create_email_list_array( $sailthru_email_list ); + return $this->create_user_api_list_update( $sailthru_email_list ); } - private function create_email_list_array( $email_list ): array { + private function create_user_api_list_update( $email_list ): array { $lists = explode(',', $email_list); $subscribe_to_lists = []; From f5a6c082c3b333b2f9916e737c988541c9ffdc3e Mon Sep 17 00:00:00 2001 From: Lindsay Cooper Date: Wed, 14 Oct 2020 11:35:44 -0600 Subject: [PATCH 4/5] [IN-1646_DOUBLEOPTIN] Fix typo --- views/widget.subscribe.admin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/widget.subscribe.admin.php b/views/widget.subscribe.admin.php index c2b6006..0bd5ed6 100644 --- a/views/widget.subscribe.admin.php +++ b/views/widget.subscribe.admin.php @@ -243,7 +243,7 @@ // if no lists are checked, show a warning. if ( empty( $instance['sailthru_list'] ) ) { echo '

 

'; - echo '

If you do not select as least one list to subscribe to, this widget will not display to the user.

'; + echo '

If you do not select at least one list to subscribe to, this widget will not display to the user.

'; } ?> From da38ac17225522099e6aaa56e418d90d7deebcf1 Mon Sep 17 00:00:00 2001 From: Rodrigo Ziviani de Souza Date: Thu, 15 Oct 2020 11:29:37 +1300 Subject: [PATCH 5/5] [IN-1646_DOUBLEOPTIN] IN-1646 Fixed broken Double Opt-In - Version number change --- README.txt | 2 +- changelog.md | 3 +++ plugin.php | 4 ++-- views/admin.functions.subscribe.options.php | 4 +--- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.txt b/README.txt index a422949..d68ab17 100755 --- a/README.txt +++ b/README.txt @@ -3,7 +3,7 @@ Contributors: brownac, lcooper, asilverman, nickgundry, sailthru-wp, automattic, Tags: personalization, email, Requires at least: 3.6 Tested up to: 5.4 -Stable tag: 3.6.0 +Stable tag: 3.6.1 This plugin provides fast and easy integration of the core Sailthru features into your Wordpress site. diff --git a/changelog.md b/changelog.md index ec22f96..fab5615 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ # Changelog +## v3.6.1 (2020-10-14) +Fixed bug where double opt-in option adds users to list immediately + ## v3.6.0 (2020-08-04) Added option for users to have Sailthru Subscription widget title to disappear after user sign-up Changed code which produced some errors and warnings in Wordpress PHP Codesniffer diff --git a/plugin.php b/plugin.php index 6bd1c06..112b143 100644 --- a/plugin.php +++ b/plugin.php @@ -3,7 +3,7 @@ Plugin Name: Sailthru for WordPress Plugin URI: http://sailthru.com/ Description: Add the power of Sailthru to your WordPress set up. -Version: 3.6.0 +Version: 3.6.1 Author: Sailthru Author URI: http://sailthru.com Author Email: integrations@sailthru.com @@ -35,7 +35,7 @@ * @var const $version The current version of the plugin. */ if ( ! defined( 'SAILTHRU_PLUGIN_VERSION' ) ) { - define( 'SAILTHRU_PLUGIN_VERSION', '3.6.0' ); + define( 'SAILTHRU_PLUGIN_VERSION', '3.6.1' ); } if ( ! defined( 'SAILTHRU_PLUGIN_PATH' ) ) { diff --git a/views/admin.functions.subscribe.options.php b/views/admin.functions.subscribe.options.php index d6c94bd..d80c9d0 100644 --- a/views/admin.functions.subscribe.options.php +++ b/views/admin.functions.subscribe.options.php @@ -20,9 +20,7 @@ function welcome_template_callback( $args ) {

If using double opt-in, customers can be added to the appropriate list via - - signup_confirm - . + signup_confirm. For more information visit the Wordpress Integration Documentation