Skip to content

Commit

Permalink
Merge pull request #87 from sailthru/IN-1646_DoubleOptIn
Browse files Browse the repository at this point in the history
[IN-1646_DOUBLEOPTIN] IN-1646 Fixed Double Opt-In
  • Loading branch information
rzsouza-st authored Oct 14, 2020
2 parents bdd5770 + 04cacaf commit 8dcbe63
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 82 deletions.
2 changes: 1 addition & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]
Expand Down Expand Up @@ -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' ) ) {
Expand Down
17 changes: 14 additions & 3 deletions views/admin.functions.subscribe.options.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,20 @@ function welcome_template_callback( $args ) {
echo '<div class="postbox">';
echo '<div class="inside">';

echo '<h3>Welcome Template</h3>';
echo '<p>Choose a template to send after a user signs up using the Sailthru Subscribe Widget or shortcode.';
echo '<h3>Welcome Email</h3>';
echo '
<p>
<strong>Optional. </strong> Choose a template to send after a user signs up using the Sailthru 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.
</p>
<p>
If using double opt-in, customers can be added to the appropriate list via
<code><a href="https://getstarted.sailthru.com/developers/zephyr-functions-library/signup_confirm/">signup_confirm</a></code>.
For more information visit the
<a href="https://getstarted.sailthru.com/integrations/wordpress/wordpress-plugin/">
Wordpress Integration Documentation
</a>.
</p>';
}

function welcome_template( $args ) {
Expand Down Expand Up @@ -62,7 +74,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 '<input type="checkbox" id="sailthru_double_opt_in" name="sailthru_forms_options[sailthru_double_opt_in]" value="1"' . checked( 1, esc_attr( $sailthru_double_opt_in ), false ) . '/>';
echo '<small><strong>Important:</strong> Ensure the template uses <code><a href="http://docs.sailthru.com/developers/zephyr-syntax/zephyr-functions/signupconfirm">signup_confirm</a></code> or the user will not be added to a list </small><p><small>The welcome email will only be sent when a user does not belong to a list selected in the subscribe widget. </small></p>';
}

function sailthru_forms_callback( $args ) {
Expand Down
2 changes: 1 addition & 1 deletion views/widget.subscribe.admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
// if no lists are checked, show a warning.
if ( empty( $instance['sailthru_list'] ) ) {
echo '<p>&nbsp;</p>';
echo '<div style="border-left: 4px solid #dd3d36;padding-left:4px;"><p>If you do not select as least one list to subscribe to, this widget will not display to the user.</p></div>';
echo '<div style="border-left: 4px solid #dd3d36;padding-left:4px;"><p>If you do not select at least one list to subscribe to, this widget will not display to the user.</p></div>';
}

?>
Expand Down
136 changes: 61 additions & 75 deletions widget.subscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,34 +359,6 @@ 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

}

// clean up vars
unset( $vars['email'] );
unset( $vars['sailthru_nonce'] );
Expand Down Expand Up @@ -437,66 +409,28 @@ 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',
'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': '';
if ($should_update_optout) {
$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 = $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_template( $client, $customfields['sailthru_welcome_template'], $email, $vars );
}

// Handle the Event If it's been set to fire.
Expand Down Expand Up @@ -548,8 +482,60 @@ 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_user_api_list_update( $sailthru_email_list );
}

private function create_user_api_list_update( $email_list ): array {
$lists = explode(',', $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_template( $client, string $template, string $email, array $vars ): void {
try {
$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 {
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'] );

return array_keys( $new_list_diff );
}

} // end class

Expand Down

0 comments on commit 8dcbe63

Please sign in to comment.