diff --git a/change_log.txt b/change_log.txt index 2601b05..0eba52b 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,3 +1,7 @@ +# 4.5 | 2018-12-19 + - Fixed new tags not being added when updating existing list member. + + # 4.4 | 2018-10-29 - Added support for MailChimp contact tags. diff --git a/class-gf-mailchimp.php b/class-gf-mailchimp.php index 0e225f4..103c5bd 100644 --- a/class-gf-mailchimp.php +++ b/class-gf-mailchimp.php @@ -166,7 +166,7 @@ class GFMailChimp extends GFFeedAddOn { * * @since 1.0 * @access protected - * @var object $api If available, contains an instance of the Mailchimp API library. + * @var GF_MailChimp_API $api If available, contains an instance of the Mailchimp API library. */ public $api = null; @@ -410,7 +410,7 @@ public function feed_settings_fields() { 'tooltip' => sprintf( '
%s
%s', esc_html__( 'Tags', 'gravityformsmailchimp' ), - esc_html__( 'Associate tags to your MailChimp contacts with a comma separated list. (e.g. new lead, Gravity Forms, web source)', 'gravityformsmailchimp' ) + esc_html__( 'Associate tags to your MailChimp contacts with a comma separated list (e.g. new lead, Gravity Forms, web source). Commas within a merge tag value will be created as a single tag.', 'gravityformsmailchimp' ) ), ), array( @@ -1194,11 +1194,19 @@ public function process_feed( $feed, $entry, $form ) { 'ip_signup' => rgar( $entry, 'ip' ), 'vip' => rgars( $feed, 'meta/markAsVIP' ) ? true : false, 'note' => rgars( $feed, 'meta/note' ), + 'tags' => array(), ); + // Get existing tags. + $existing_tags = $member ? wp_list_pluck( $member['tags'], 'name' ) : array(); + // Add tags to subscription. if ( ! empty( $tags ) ) { - $subscription['tags'] = $tags; + $subscription['tags'] = $member ? array_merge( $existing_tags, $tags ) : $tags; + $subscription['tags'] = array_unique( $subscription['tags'] ); + } else { + $subscription['tags'] = $member ? $existing_tags : $subscription['tags']; + $subscription['tags'] = array_unique( $subscription['tags'] ); } // Prepare transaction type for filter. @@ -1262,6 +1270,13 @@ public function process_feed( $feed, $entry, $form ) { unset( $subscription['vip'] ); } + // Remove tags from subscription object. + $tags = $subscription['tags']; + unset( $subscription['tags'] ); + foreach ( $tags as &$tag ) { + $tag = array( 'name' => $tag, 'status' => 'active' ); + } + // Remove note from the subscription object and process any merge tags. $note = GFCommon::replace_variables( $subscription['note'], $form, $entry, false, true, false, 'text' ); unset( $subscription['note'] ); @@ -1289,13 +1304,31 @@ public function process_feed( $feed, $entry, $form ) { $this->log_error( __METHOD__ . '(): Field errors when attempting subscription: ' . print_r( $e->getErrors(), true ) ); } - return; + return $entry; + + } + + try { + + // Log the subscriber tags to be added or updated. + $this->log_debug( __METHOD__ . "(): Subscriber tags to be {$action}: " . print_r( $tags, true ) ); + + // Update tags. + $this->api->update_member_tags( $list_id, $subscription['email_address'], $tags ); + + // Log that the subscriber tags was added or updated. + $this->log_debug( __METHOD__ . "(): Subscriber tags successfully {$action}." ); + + } catch ( Exception $e ) { + + // Log that subscription could not be added or updated. + $this->add_feed_error( sprintf( esc_html__( 'Unable to add/update subscriber tags: %s', 'gravityformsmailchimp' ), $e->getMessage() ), $feed, $entry, $form ); } if ( ! $note ) { // Abort as there is no note to process. - return; + return $entry; } try { @@ -1309,7 +1342,7 @@ public function process_feed( $feed, $entry, $form ) { // Log that the note could not be added. $this->add_feed_error( sprintf( esc_html__( 'Unable to add note to subscriber: %s', 'gravityformsmailchimp' ), $e->getMessage() ), $feed, $entry, $form ); - return; + return $entry; } diff --git a/includes/class-gf-mailchimp-api.php b/includes/class-gf-mailchimp-api.php index 51ec780..c2db7e8 100644 --- a/includes/class-gf-mailchimp-api.php +++ b/includes/class-gf-mailchimp-api.php @@ -54,9 +54,10 @@ public function __construct( $api_key = '' ) { * @since 4.0 * @access public * - * @uses GF_MailChimp_API::process_request() + * @uses GF_MailChimp_API::process_request() * * @return array + * @throws GF_MailChimp_Exception|Exception */ public function account_details() { @@ -73,9 +74,10 @@ public function account_details() { * @param string $list_id MailChimp list ID. * @param string $category_id Interest category ID. * - * @uses GF_MailChimp_API::process_request() + * @uses GF_MailChimp_API::process_request() * * @return array + * @throws GF_MailChimp_Exception|Exception */ public function get_interest_category_interests( $list_id, $category_id ) { @@ -91,9 +93,10 @@ public function get_interest_category_interests( $list_id, $category_id ) { * * @param string $list_id MailChimp list ID. * - * @uses GF_MailChimp_API::process_request() + * @uses GF_MailChimp_API::process_request() * * @return array + * @throws Exception */ public function get_list( $list_id ) { @@ -109,9 +112,10 @@ public function get_list( $list_id ) { * * @param array $params List request parameters. * - * @uses GF_MailChimp_API::process_request() + * @uses GF_MailChimp_API::process_request() * * @return array + * @throws GF_MailChimp_Exception|Exception */ public function get_lists( $params ) { @@ -127,9 +131,10 @@ public function get_lists( $params ) { * * @param string $list_id MailChimp list ID. * - * @uses GF_MailChimp_API::process_request() + * @uses GF_MailChimp_API::process_request() * * @return array + * @throws GF_MailChimp_Exception|Exception */ public function get_list_interest_categories( $list_id ) { @@ -146,9 +151,10 @@ public function get_list_interest_categories( $list_id ) { * @param string $list_id MailChimp list ID. * @param string $email_address Email address. * - * @uses GF_MailChimp_API::process_request() + * @uses GF_MailChimp_API::process_request() * * @return array + * @throws GF_MailChimp_Exception|Exception */ public function get_list_member( $list_id, $email_address ) { @@ -167,9 +173,10 @@ public function get_list_member( $list_id, $email_address ) { * * @param string $list_id MailChimp list ID. * - * @uses GF_MailChimp_API::process_request() + * @uses GF_MailChimp_API::process_request() * * @return array + * @throws GF_MailChimp_Exception|Exception */ public function get_list_merge_fields( $list_id ) { @@ -187,9 +194,10 @@ public function get_list_merge_fields( $list_id ) { * @param string $email_address Email address. * @param array $subscription Subscription details. * - * @uses GF_MailChimp_API::process_request() + * @uses GF_MailChimp_API::process_request() * * @return array + * @throws GF_MailChimp_Exception|Exception */ public function update_list_member( $list_id, $email_address, $subscription ) { @@ -200,6 +208,30 @@ public function update_list_member( $list_id, $email_address, $subscription ) { } + /** + * Update tags for a MailChimp list member. + * + * @since Unknown + * @access public + * + * @param string $list_id MailChimp list ID. + * @param string $email_address Email address. + * @param array $tags Member tags. + * + * @uses GF_MailChimp_API::process_request() + * + * @return array + * @throws GF_MailChimp_Exception|Exception + */ + public function update_member_tags( $list_id, $email_address, $tags ) { + + // Prepare subscriber hash. + $subscriber_hash = md5( strtolower( $email_address ) ); + + return $this->process_request( 'lists/' . $list_id . '/members/' . $subscriber_hash . '/tags', array( 'tags' => $tags ), 'POST' ); + + } + /** * Add a note to the MailChimp list member. * @@ -210,9 +242,10 @@ public function update_list_member( $list_id, $email_address, $subscription ) { * @param string $email_address Email address. * @param string $note The note to be added to the member. * - * @uses GF_MailChimp_API::process_request() + * @uses GF_MailChimp_API::process_request() * * @return array + * @throws GF_MailChimp_Exception|Exception */ public function add_member_note( $list_id, $email_address, $note ) { @@ -234,7 +267,7 @@ public function add_member_note( $list_id, $email_address, $note ) { * @param string $method Request method. Defaults to GET. * @param string $return_key Array key from response to return. Defaults to null (return full response). * - * @throws GF_MailChimp_Exception If API request returns an error, exception is thrown. + * @throws GF_MailChimp_Exception|Exception If API request returns an error, exception is thrown. * * @return array */ @@ -308,7 +341,7 @@ private function process_request( $path = '', $data = array(), $method = 'GET', // Get the response code. $response_code = wp_remote_retrieve_response_code( $response ); - if ( $response_code != 200 ) { + if ( ! in_array( $response_code, array( 200, 204 ) ) ) { // If status code is set, throw exception. if ( isset( $response['body']['status'] ) && isset( $response['body']['title'] ) ) { diff --git a/languages/gravityformsmailchimp-ar.mo b/languages/gravityformsmailchimp-ar.mo index e80bbee..2ccd3b1 100644 Binary files a/languages/gravityformsmailchimp-ar.mo and b/languages/gravityformsmailchimp-ar.mo differ diff --git a/languages/gravityformsmailchimp-ca.mo b/languages/gravityformsmailchimp-ca.mo index a96352f..8434a61 100644 Binary files a/languages/gravityformsmailchimp-ca.mo and b/languages/gravityformsmailchimp-ca.mo differ diff --git a/languages/gravityformsmailchimp-da_DK.mo b/languages/gravityformsmailchimp-da_DK.mo index 0dd166d..b9ab29d 100644 Binary files a/languages/gravityformsmailchimp-da_DK.mo and b/languages/gravityformsmailchimp-da_DK.mo differ diff --git a/languages/gravityformsmailchimp-de_DE.mo b/languages/gravityformsmailchimp-de_DE.mo index 3dcb490..e42ef5d 100644 Binary files a/languages/gravityformsmailchimp-de_DE.mo and b/languages/gravityformsmailchimp-de_DE.mo differ diff --git a/languages/gravityformsmailchimp-de_DE_formal.mo b/languages/gravityformsmailchimp-de_DE_formal.mo index e83197e..a963d67 100644 Binary files a/languages/gravityformsmailchimp-de_DE_formal.mo and b/languages/gravityformsmailchimp-de_DE_formal.mo differ diff --git a/languages/gravityformsmailchimp-en_AU.mo b/languages/gravityformsmailchimp-en_AU.mo index dbe1b86..c081761 100644 Binary files a/languages/gravityformsmailchimp-en_AU.mo and b/languages/gravityformsmailchimp-en_AU.mo differ diff --git a/languages/gravityformsmailchimp-en_GB.mo b/languages/gravityformsmailchimp-en_GB.mo index 90edf93..b1a4c46 100644 Binary files a/languages/gravityformsmailchimp-en_GB.mo and b/languages/gravityformsmailchimp-en_GB.mo differ diff --git a/languages/gravityformsmailchimp-es_ES.mo b/languages/gravityformsmailchimp-es_ES.mo index c28ead5..de6ea06 100644 Binary files a/languages/gravityformsmailchimp-es_ES.mo and b/languages/gravityformsmailchimp-es_ES.mo differ diff --git a/languages/gravityformsmailchimp-fi.mo b/languages/gravityformsmailchimp-fi.mo index 1e93462..c5e16db 100644 Binary files a/languages/gravityformsmailchimp-fi.mo and b/languages/gravityformsmailchimp-fi.mo differ diff --git a/languages/gravityformsmailchimp-fr_CA.mo b/languages/gravityformsmailchimp-fr_CA.mo index 0999949..3bd01e6 100644 Binary files a/languages/gravityformsmailchimp-fr_CA.mo and b/languages/gravityformsmailchimp-fr_CA.mo differ diff --git a/languages/gravityformsmailchimp-fr_FR.mo b/languages/gravityformsmailchimp-fr_FR.mo index f647049..affc5f7 100644 Binary files a/languages/gravityformsmailchimp-fr_FR.mo and b/languages/gravityformsmailchimp-fr_FR.mo differ diff --git a/languages/gravityformsmailchimp-hu_HU.mo b/languages/gravityformsmailchimp-hu_HU.mo index ea706db..d603663 100644 Binary files a/languages/gravityformsmailchimp-hu_HU.mo and b/languages/gravityformsmailchimp-hu_HU.mo differ diff --git a/languages/gravityformsmailchimp-it_IT.mo b/languages/gravityformsmailchimp-it_IT.mo index 5cd2e12..c27811e 100644 Binary files a/languages/gravityformsmailchimp-it_IT.mo and b/languages/gravityformsmailchimp-it_IT.mo differ diff --git a/languages/gravityformsmailchimp-ja.mo b/languages/gravityformsmailchimp-ja.mo index 8744f82..ea4454d 100644 Binary files a/languages/gravityformsmailchimp-ja.mo and b/languages/gravityformsmailchimp-ja.mo differ diff --git a/languages/gravityformsmailchimp-nb_NO.mo b/languages/gravityformsmailchimp-nb_NO.mo index f3f42b5..7fa21bd 100644 Binary files a/languages/gravityformsmailchimp-nb_NO.mo and b/languages/gravityformsmailchimp-nb_NO.mo differ diff --git a/languages/gravityformsmailchimp-nl_BE.mo b/languages/gravityformsmailchimp-nl_BE.mo index 36e7c93..63bd481 100644 Binary files a/languages/gravityformsmailchimp-nl_BE.mo and b/languages/gravityformsmailchimp-nl_BE.mo differ diff --git a/languages/gravityformsmailchimp-nl_NL.mo b/languages/gravityformsmailchimp-nl_NL.mo index 3664839..752c9fc 100644 Binary files a/languages/gravityformsmailchimp-nl_NL.mo and b/languages/gravityformsmailchimp-nl_NL.mo differ diff --git a/languages/gravityformsmailchimp-pt_BR.mo b/languages/gravityformsmailchimp-pt_BR.mo index 4df5a4c..853b52f 100644 Binary files a/languages/gravityformsmailchimp-pt_BR.mo and b/languages/gravityformsmailchimp-pt_BR.mo differ diff --git a/languages/gravityformsmailchimp-pt_PT.mo b/languages/gravityformsmailchimp-pt_PT.mo index 800efe7..b9cfecd 100644 Binary files a/languages/gravityformsmailchimp-pt_PT.mo and b/languages/gravityformsmailchimp-pt_PT.mo differ diff --git a/languages/gravityformsmailchimp-ru_RU.mo b/languages/gravityformsmailchimp-ru_RU.mo index 3eb08ae..8d1c3ce 100644 Binary files a/languages/gravityformsmailchimp-ru_RU.mo and b/languages/gravityformsmailchimp-ru_RU.mo differ diff --git a/languages/gravityformsmailchimp-sv_SE.mo b/languages/gravityformsmailchimp-sv_SE.mo index fc5f0cb..fe09307 100644 Binary files a/languages/gravityformsmailchimp-sv_SE.mo and b/languages/gravityformsmailchimp-sv_SE.mo differ diff --git a/languages/gravityformsmailchimp-zh_CN.mo b/languages/gravityformsmailchimp-zh_CN.mo index 9644b4d..2a9db2b 100644 Binary files a/languages/gravityformsmailchimp-zh_CN.mo and b/languages/gravityformsmailchimp-zh_CN.mo differ diff --git a/languages/gravityformsmailchimp.pot b/languages/gravityformsmailchimp.pot index bfe03c0..c12316d 100644 --- a/languages/gravityformsmailchimp.pot +++ b/languages/gravityformsmailchimp.pot @@ -1,9 +1,9 @@ # Copyright 2009-2018 Rocketgenius, Inc. msgid "" msgstr "" -"Project-Id-Version: Gravity Forms MailChimp Add-On 4.4\n" +"Project-Id-Version: Gravity Forms MailChimp Add-On 4.5\n" "Report-Msgid-Bugs-To: https://www.gravtiyforms.com\n" -"POT-Creation-Date: 2018-10-29 19:28:36+00:00\n" +"POT-Creation-Date: 2018-12-19 13:46:11+00:00\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" @@ -109,8 +109,9 @@ msgstr "" #: class-gf-mailchimp.php:413 msgid "" -"Associate tags to your MailChimp contacts with a comma separated list. " -"(e.g. new lead, Gravity Forms, web source)" +"Associate tags to your MailChimp contacts with a comma separated list (e.g. " +"new lead, Gravity Forms, web source). Commas within a merge tag value will " +"be created as a single tag." msgstr "" #: class-gf-mailchimp.php:420 @@ -172,11 +173,15 @@ msgstr "" msgid "Unable to check if email address is already used by a member: %s" msgstr "" -#: class-gf-mailchimp.php:1285 +#: class-gf-mailchimp.php:1300 msgid "Unable to add/update subscriber: %s" msgstr "" -#: class-gf-mailchimp.php:1310 +#: class-gf-mailchimp.php:1325 +msgid "Unable to add/update subscriber tags: %s" +msgstr "" + +#: class-gf-mailchimp.php:1343 msgid "Unable to add note to subscriber: %s" msgstr "" diff --git a/mailchimp.php b/mailchimp.php index 8265cea..570cd37 100644 --- a/mailchimp.php +++ b/mailchimp.php @@ -9,7 +9,7 @@ Plugin Name: Gravity Forms MailChimp Add-On Plugin URI: https://www.gravityforms.com Description: Integrates Gravity Forms with MailChimp, allowing form submissions to be automatically sent to your MailChimp account -Version: 4.4 +Version: 4.5 Author: rocketgenius Author URI: https://www.rocketgenius.com License: GPL-2.0+ @@ -34,7 +34,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA **/ -define( 'GF_MAILCHIMP_VERSION', '4.4' ); +define( 'GF_MAILCHIMP_VERSION', '4.5' ); // If Gravity Forms is loaded, bootstrap the Mailchimp Add-On. add_action( 'gform_loaded', array( 'GF_MailChimp_Bootstrap', 'load' ), 5 );