Skip to content

Commit

Permalink
Merge branch 'release/4.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Apr 11, 2017
2 parents 96ac4c4 + ef46575 commit e21b35b
Show file tree
Hide file tree
Showing 21 changed files with 224 additions and 56 deletions.
17 changes: 17 additions & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
------------------------------------------------------------------------------------------------------------------
Version 4.2
- Added additional logging when checking if a user's subscription status.
- Added existing member object as sixth parameter of "gform_mailchimp_subscription" filter.
- Fixed PHP warning when attempting to check for existing interests when member did not belong to any interest categories.
- Fixed Fatal error that happened in certain situations.
- Fixed existing interest categories not being properly associated to subscription object.
- Fixed an issue with subscriptions not being created due to empty merge fields.
- Fixed an issue which could prevent the feed groups being converted to interest categories when upgrading from older versions of the add-on.
- Fixed inability to use double opt-in with existing memebers.
- Fixed API response error handling; API key was incorrectly shown as valid when a 403 Forbidden error was returned.
- Fixed an issue with the field value used for interest category logic evaluation.
- Restored GFMailChimp::get_group_setting_key() for use when upgrading from older versions of MailChimp Add-On.
- Restored double opt-in support for members who are being resubscribed to a list they are unsubscribed from.
- Disabled double opt-in support for members who are being resubscribed to a list they are unsubscribed from. (Member will be automatically subscribed to the list. This is a temporary workaround for an issue with the MailChimp API.)
- Re-introduced support for "gform_mailchimp_keep_existing_groups" filter.

------------------------------------------------------------------------------------------------------------------
Version 4.1
- Added additional error logging when creating a subscription.
Expand Down
190 changes: 152 additions & 38 deletions class-gf-mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,11 @@ public function process_feed( $feed, $entry, $form ) {
// Loop through field map.
foreach ( $field_map as $name => $field_id ) {

// If no field is mapped, skip it.
if ( rgblank( $field_id ) ) {
continue;
}

// If this is the email field, skip it.
if ( strtoupper( $name ) === 'EMAIL' ) {
continue;
Expand All @@ -984,27 +989,8 @@ public function process_feed( $feed, $entry, $form ) {

}

// Initialize interests array.
$interests = array();

// Get interest categories.
$categories = $this->get_feed_interest_categories( $feed );

// Loop through categories.
foreach ( $categories as $category_id => $category_meta ) {

// Log that we are evaluating the category conditions.
$this->log_debug( __METHOD__ . '(): Evaluating condition for interest category "' . $category_id . '": ' . print_r( $category_meta, true ) );

// Get condition evaluation.
$condition_evaluation = $this->is_category_condition_met( $category_meta, $form, $entry );

// Set interest category based on evaluation.
$interests[ $category_id ] = $condition_evaluation;

}

// Define initial member found and member status variables.
// Define initial member, member found and member status variables.
$member = false;
$member_found = false;
$member_status = null;

Expand All @@ -1022,6 +1008,9 @@ public function process_feed( $feed, $entry, $form ) {
// Set member status.
$member_status = $member['status'];

// Log member status.
$this->log_debug( __METHOD__ . "(): $email was found on list. Status: $member_status" );

} catch ( Exception $e ) {

// If the exception code is not 404, abort feed processing.
Expand All @@ -1034,6 +1023,9 @@ public function process_feed( $feed, $entry, $form ) {

}

// Log member status.
$this->log_debug( __METHOD__ . "(): $email was not found on list." );

}

/**
Expand All @@ -1053,6 +1045,77 @@ public function process_feed( $feed, $entry, $form ) {
return;
}

// Initialize interests array.
$interests = $member_found ? $member['interests'] : array();

/**
* Modify whether a user that is already subscribed to your list has their groups replaced when submitting the form a second time.
*
* @since 1.9
*
* @param bool $keep_existing_interests Should user keep existing interest categories?
* @param array $form The form object.
* @param array $entry The entry object.
* @param array $feed The feed object.
*/
$keep_existing_interests = gf_apply_filters( array( 'gform_mailchimp_keep_existing_groups', $form['id'] ), true, $form, $entry, $feed );

// Initialize interests to keep array.
$interests_to_keep = array();

// Get existing interests.
$existing_interests = rgar( $member, 'interests' );

// If member was found, has existing interests and we are not keeping existing interest categories, remove them.
if ( $member_found && $existing_interests ) {

// Loop through existing interests.
foreach ( $existing_interests as $interest_id => $interest_enabled ) {

// If interest is not enabled, skip it.
if ( ! $interest_enabled ) {
continue;
}

// If we are keeping existing interests, add to array.
if ( $keep_existing_interests ) {

$interests_to_keep[] = $interest_id;
continue;

} else if ( ! $keep_existing_interests ) {

// Disable interest in new subscription.
$interests[ $interest_id ] = false;

}

}

}

// Get interest categories.
$categories = $this->get_feed_interest_categories( $feed );

// Loop through categories.
foreach ( $categories as $category_id => $category_meta ) {

// If category is not enabled or the category is one we are keeping, skip it.
if ( ! rgar( $category_meta, 'enabled' ) || in_array( $category_id, $interests_to_keep ) ) {
continue;
}

// Log that we are evaluating the category conditions.
$this->log_debug( __METHOD__ . '(): Evaluating condition for interest category "' . $category_id . '": ' . print_r( $category_meta, true ) );

// Get condition evaluation.
$condition_evaluation = $this->is_category_condition_met( $category_meta, $form, $entry );

// Set interest category based on evaluation.
$interests[ $category_id ] = $condition_evaluation;

}

// If member status is not defined, set to subscribed.
$member_status = isset( $member_status ) ? $member_status : 'subscribed';

Expand Down Expand Up @@ -1091,7 +1154,7 @@ public function process_feed( $feed, $entry, $form ) {
unset( $subscription['merge_vars'] );

// Convert double optin.
$subscription['status'] = $subscription['double_optin'] && ! $member_found ? 'pending' : 'subscribed';
$subscription['status'] = $subscription['double_optin'] ? 'pending' : $subscription['status'];
unset( $subscription['double_optin'] );

// Extract list ID.
Expand All @@ -1105,19 +1168,22 @@ public function process_feed( $feed, $entry, $form ) {
/**
* Modify the subscription object before it is executed.
*
* @param array $subscription Subscription arguments.
* @param string $list_id MailChimp list ID.
* @param array $form The form object.
* @param array $entry The entry object.
* @param array $feed The feed object.
* @since 4.1.9 Added existing member object as $member parameter.
*
* @param array $subscription Subscription arguments.
* @param string $list_id MailChimp list ID.
* @param array $form The form object.
* @param array $entry The entry object.
* @param array $feed The feed object.
* @param array|false $member The existing member object. (False if member does not currently exist in MailChimp.)
*/
$subscription = gf_apply_filters( array( 'gform_mailchimp_subscription', $form['id'] ), $subscription, $list_id, $form, $entry, $feed );
$subscription = gf_apply_filters( array( 'gform_mailchimp_subscription', $form['id'] ), $subscription, $list_id, $form, $entry, $feed, $member );

// Remove merge_fields if none are defined.
if ( empty( $subscription['merge_fields'] ) ) {
unset( $subscription['merge_fields'] );
}

// Remove interests if none are defined.
if ( empty( $subscription['interests'] ) ) {
unset( $subscription['interests'] );
Expand All @@ -1132,7 +1198,7 @@ public function process_feed( $feed, $entry, $form ) {
$note = GFCommon::replace_variables( $subscription['note'], $form, $entry, false, true, false, 'text' );
unset( $subscription['note'] );

$action = $member_found ? 'added' : 'updated';
$action = $member_found ? 'updated' : 'added';

try {

Expand All @@ -1151,7 +1217,7 @@ public function process_feed( $feed, $entry, $form ) {
$this->add_feed_error( sprintf( esc_html__( 'Unable to add/update subscriber: %s', 'gravityformsmailchimp' ), $e->getMessage() ), $feed, $entry, $form );

// Log field errors.
if ( $e->getErrors() ) {
if ( $e->hasErrors() ) {
$this->log_error( __METHOD__ . '(): Field errors when attempting subscription: ' . print_r( $e->getErrors(), true ) );
}

Expand Down Expand Up @@ -1532,11 +1598,13 @@ public function is_category_condition_met( $category, $form, $entry ) {
if ( ! $category['enabled'] ) {

$this->log_debug( __METHOD__ . '(): Interest category not enabled. Returning false.' );

return false;

} elseif ( $category['decision'] == 'always' ) {
} else if ( $category['decision'] == 'always' ) {

$this->log_debug( __METHOD__ . '(): Interest category decision is always. Returning true.' );

return true;

}
Expand All @@ -1546,15 +1614,18 @@ public function is_category_condition_met( $category, $form, $entry ) {
if ( ! is_object( $field ) ) {

$this->log_debug( __METHOD__ . "(): Field #{$category['field']} not found. Returning true." );

return true;

} else {

$field_value = $this->get_field_value( $form, $entry, $category['field'] );
$is_value_match = GFFormsModel::is_value_match( $field_value, $category['value'], $category['operator'], $field );
$field_value = GFFormsModel::get_lead_field_value( $entry, $field );
$is_value_match = GFFormsModel::is_value_match( $field_value, $category['value'], $category['operator'] );

$this->log_debug( __METHOD__ . "(): Add to interest category if field #{$category['field']} value {$category['operator']} '{$category['value']}'. Is value match? " . var_export( $is_value_match, 1 ) );

return $is_value_match;

}

}
Expand Down Expand Up @@ -1670,6 +1741,9 @@ public function convert_groups_to_categories() {
// Get MailChimp feeds.
$feeds = $this->get_feeds();

$list_interest_categories = array();
$interest_category_interests = array();

// Loop through MailChimp feeds.
foreach ( $feeds as $feed ) {

Expand All @@ -1683,8 +1757,14 @@ public function convert_groups_to_categories() {

try {

// Get interest categories for list.
$interest_categories = $this->api->get_list_interest_categories( $feed['meta']['mailchimpList'] );
$list_id = $feed['meta']['mailchimpList'];

if ( ! isset( $list_interest_categories[ $list_id ] ) ) {
// Get interest categories for list.
$list_interest_categories[ $list_id ] = $this->api->get_list_interest_categories( $list_id );
}

$interest_categories = rgar( $list_interest_categories, $list_id, array() );

} catch ( Exception $e ) {

Expand All @@ -1698,8 +1778,14 @@ public function convert_groups_to_categories() {
// Loop through interest categories.
foreach ( $interest_categories as $interest_category ) {

// Get interests for interest category.
$interests = $this->api->get_interest_category_interests( $feed['meta']['mailchimpList'], $interest_category['id'] );
$category_id = $interest_category['id'];

if ( ! isset( $interest_category_interests[ $category_id ] ) ) {
// Get interests for interest category.
$interest_category_interests[ $category_id ] = $this->api->get_interest_category_interests( $list_id, $category_id );
}

$interests = rgar( $list_interest_categories, $category_id, array() );

// Loop through interests.
foreach ( $interests as $interest ) {
Expand Down Expand Up @@ -2025,4 +2111,32 @@ public function get_old_feeds() {

}

/**
* Retrieve the group setting key.
*
* @param string $grouping_id The group ID.
* @param string $group_name The group name.
*
* @return string
*/
public function get_group_setting_key( $grouping_id, $group_name ) {

$plugin_settings = GFCache::get( 'mailchimp_plugin_settings' );
if ( empty( $plugin_settings ) ) {
$plugin_settings = $this->get_plugin_settings();
GFCache::set( 'mailchimp_plugin_settings', $plugin_settings );
}

$key = 'group_key_' . $grouping_id . '_' . str_replace( '%', '', sanitize_title_with_dashes( $group_name ) );

if ( ! isset( $plugin_settings[ $key ] ) ) {
$group_key = sanitize_key( uniqid( 'mc_group_', true ) );
$plugin_settings[ $key ] = $group_key;
$this->update_plugin_settings( $plugin_settings );
GFCache::set( 'mailchimp_plugin_settings', $plugin_settings );
}

return $plugin_settings[ $key ];
}

}
Loading

0 comments on commit e21b35b

Please sign in to comment.