From 1a41e57f6cbfec5dbe1e55d8155d9f27a5055a13 Mon Sep 17 00:00:00 2001 From: Gravity Forms Date: Mon, 24 Dec 2018 09:04:56 +0100 Subject: [PATCH] 4.5 --- change_log.txt | 4 ++ class-gf-mailchimp.php | 45 ++++++++++++-- includes/class-gf-mailchimp-api.php | 55 ++++++++++++++---- languages/gravityformsmailchimp-ar.mo | Bin 498 -> 498 bytes languages/gravityformsmailchimp-ca.mo | Bin 1540 -> 1540 bytes languages/gravityformsmailchimp-da_DK.mo | Bin 5708 -> 5708 bytes languages/gravityformsmailchimp-de_DE.mo | Bin 5879 -> 5879 bytes .../gravityformsmailchimp-de_DE_formal.mo | Bin 1075 -> 1075 bytes languages/gravityformsmailchimp-en_AU.mo | Bin 5564 -> 5564 bytes languages/gravityformsmailchimp-en_GB.mo | Bin 5570 -> 5570 bytes languages/gravityformsmailchimp-es_ES.mo | Bin 5894 -> 5894 bytes languages/gravityformsmailchimp-fi.mo | Bin 5726 -> 5726 bytes languages/gravityformsmailchimp-fr_CA.mo | Bin 5971 -> 5971 bytes languages/gravityformsmailchimp-fr_FR.mo | Bin 5967 -> 5967 bytes languages/gravityformsmailchimp-hu_HU.mo | Bin 442 -> 442 bytes languages/gravityformsmailchimp-it_IT.mo | Bin 5891 -> 5891 bytes languages/gravityformsmailchimp-ja.mo | Bin 421 -> 421 bytes languages/gravityformsmailchimp-nb_NO.mo | Bin 5552 -> 5552 bytes languages/gravityformsmailchimp-nl_BE.mo | Bin 438 -> 438 bytes languages/gravityformsmailchimp-nl_NL.mo | Bin 5843 -> 5843 bytes languages/gravityformsmailchimp-pt_BR.mo | Bin 5934 -> 6213 bytes languages/gravityformsmailchimp-pt_PT.mo | Bin 5906 -> 5906 bytes languages/gravityformsmailchimp-ru_RU.mo | Bin 7285 -> 7285 bytes languages/gravityformsmailchimp-sv_SE.mo | Bin 439 -> 439 bytes languages/gravityformsmailchimp-zh_CN.mo | Bin 1324 -> 1324 bytes languages/gravityformsmailchimp.pot | 17 ++++-- mailchimp.php | 4 +- 27 files changed, 100 insertions(+), 25 deletions(-) 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 e80bbee7808e8c590ac6d840aa35721de4ff6175..2ccd3b16fae16399d71943f51b0604b65808083c 100644 GIT binary patch delta 21 ccmeyw{E2zOCN4__LrW_o3oB!@jeE=(0aP0Xv^j)n12X_dfd$?G diff --git a/languages/gravityformsmailchimp-da_DK.mo b/languages/gravityformsmailchimp-da_DK.mo index 0dd166d2582c9c2163bc7728317353f0e0445772..b9ab29d23b84a7b6f74e865495807a5c5ee56a89 100644 GIT binary patch delta 23 ecmX@3b4F)F3^$jhf}y3Ak%g79+2%CvXIubVa|bX0 delta 23 ecmX@3b4F)F3^$jFf}xp}sgaej(dIPnXIubU)CVB| diff --git a/languages/gravityformsmailchimp-de_DE.mo b/languages/gravityformsmailchimp-de_DE.mo index 3dcb490b4edf279122e3cdf26e9babe1176af0d3..e42ef5de021e725224b10a439f636a6d36486644 100644 GIT binary patch delta 23 ecmeya`(1ZK3^$jhf}y3Ak%g79+2%CviQE8USO+u! delta 23 ecmeya`(1ZK3^$jFf}xp}sgaej(dIPniQE8Txd$Zx diff --git a/languages/gravityformsmailchimp-de_DE_formal.mo b/languages/gravityformsmailchimp-de_DE_formal.mo index e83197e2b8ff56157e14e9dee40c32a4d4d59b20..a963d678ab94990a2030ea029db1ffe6f66e1d08 100644 GIT binary patch delta 23 ecmdnYv6*9oAtRTif}y3Ak%g79*=B3TA4~vD;RaR! delta 23 ecmdnYv6*9oAtRTGf}xp}sgaej(PnGLA4~vDLIy_w diff --git a/languages/gravityformsmailchimp-en_AU.mo b/languages/gravityformsmailchimp-en_AU.mo index dbe1b86a7a253c106c47aabcfb532b776160d52b..c081761efa7335e444aad1b3b34bc9e0ae143aeb 100644 GIT binary patch delta 23 ecmdm^y+?aP3^$jhf}y3Ak%g79+2%CvFfIUBX$F1( delta 23 ecmdm^y+?aP3^$jFf}xp}sgaej(dIPnFfIUA$_8%$ diff --git a/languages/gravityformsmailchimp-en_GB.mo b/languages/gravityformsmailchimp-en_GB.mo index 90edf9336f618f5f558dc14928231707cc7981d8..b1a4c4641e43cecf60234354eb6886487ef7716c 100644 GIT binary patch delta 23 ecmX@4eMoyl3^$jhf}y3Ak%g79+2%Cv7%l)U}$M&WMO4&wmFS^0XG0oGzMb; delta 23 ecmZqEYt!2h!_8%)U}$D#YGh?>v^kA?0XG0nl?GG* diff --git a/languages/gravityformsmailchimp-fi.mo b/languages/gravityformsmailchimp-fi.mo index 1e934620e14204a8efbaf52c97fc586d9d6d8851..c5e16db6fa9eb9ac45b921bb1ff44699874e3c0b 100644 GIT binary patch delta 23 ecmcbob5CbO3^$jhf}y3Ak%g79+2%CvA6x)luLouT delta 23 ecmcbob5CbO3^$jFf}xp}sgaej(dIPnA6x)l5C>NP diff --git a/languages/gravityformsmailchimp-fr_CA.mo b/languages/gravityformsmailchimp-fr_CA.mo index 0999949ea5e026e6fb1eeba02010ac12e035f5fc..3bd01e6fde4b39cdeb92dc36412cdf5a61bc528c 100644 GIT binary patch delta 23 ecmcbtcUf;k3^$jhf}y3Ak%g79+2%Cvx7+|-3I|F6 delta 23 ecmcbtcUf;k3^$jFf}xp}sgaej(dIPnx7+|+YX>_3 diff --git a/languages/gravityformsmailchimp-fr_FR.mo b/languages/gravityformsmailchimp-fr_FR.mo index f647049fe9d0a442e80e451f1a6e327fe171f8f9..affc5f7ef407894d81fad357efca22d7fec8178c 100644 GIT binary patch delta 23 ecmX@FcV2Hp3^$jhf}y3Ak%g79+2%Cvm)rncQU^W& delta 23 ecmX@FcV2Hp3^$jFf}xp}sgaej(dIPnm)rnbvj;B# diff --git a/languages/gravityformsmailchimp-hu_HU.mo b/languages/gravityformsmailchimp-hu_HU.mo index ea706db1a7f329b659987ac7361cacc993a7004c..d603663865a94b66a7fc62f5660eb64f3c368f8f 100644 GIT binary patch delta 20 ccmdnRyo-6lCN4__LrW_o3oB!@iF^J507Xs*8~^|S delta 20 ccmdnRyo-6lCN2{NLo+K=BP(O0iF^J507T6P4FCWD diff --git a/languages/gravityformsmailchimp-it_IT.mo b/languages/gravityformsmailchimp-it_IT.mo index 5cd2e12ce1ada5dce50575a6051ea8e3d194df55..c27811e742e1dd3c93e003a4e31d8bfb6267409f 100644 GIT binary patch delta 23 ecmZqHYu4Kk!_8%>U}$M&WMO4&wmFS^E;j&9ss>sB delta 23 ecmZqHYu4Kk!_8%)U}$D#YGh?>v^kA?E;j&93kFL7 diff --git a/languages/gravityformsmailchimp-ja.mo b/languages/gravityformsmailchimp-ja.mo index 8744f823927b959a9f529979c40f00206fcabf48..ea4454dff7d5fe89b56846d5be3853180006737c 100644 GIT binary patch delta 20 ccmZ3=yp(yuCN4__LrW_o3oB!@iF;lG074rE-2eap delta 20 ccmZ3=yp(yuCN2{NLo+K=BP(O0iF;lG0704t&Hw-a diff --git a/languages/gravityformsmailchimp-nb_NO.mo b/languages/gravityformsmailchimp-nb_NO.mo index f3f42b5f03f50b26eb63465e03f8a9d33d6a4137..7fa21bd425d1b89320c4dc65b05b2cba96f01394 100644 GIT binary patch delta 23 ecmdm>y+M0}5I2{lf}y3Ak%g79*=8B;DO>y+M0}5I2{Jf}xp}sgaej(PkO$DO>|oiY68!@y4U3&^LWvW}v8aU(`wZChB{7 zDDL-bqoa;(8|_&s;~L+J=|tS~L%P}u^@ViO8qx{N#A@{p*9^w}Vcq4pwpXi%tRC$- zX*X)sWZwh|gTZd5&rTMe3>3BLhPFs!Yow_q(_4I`qoJX$u}#;vM8d5b>KR45jpx^O zyC&%+jPJM+ZNe@luPopM|~IDXgdODBv+X%bsUEIXBOYIV{XbpBssFKbwu e?}q|bPfhmD+!y&WGh62^%seigl|5g2J@5yFKf#y) delta 944 zcmX}q&r1|x7{Ku-XR^&*(`;)iw|3T6Lvmzy++AqdLXAu?!n$Rb){F==MGV>swjh=| zbeaPDVcp6OMKK0;+6qFtb;)A|9i%@YQtsig3MEq z`pqJ`RwG`;X4HwQ7{giIhHvo*$WRq zWGxwI_1$;_wPFvjvWL#2V8BB*D~q@fKj9Gm#6dj7Dp{PtJifwn_zO>9F)H#M@8DM) z*(vfFQ=F4lY9-L*JhWoT7=<7~X{TxE?ld@JKlD#}#u}QrCTG#~JlT}A(R9bGw-K%M zbaz?-@|G}dFOAdns|5aW>m`iqOeEqczDv`k}SZI_Z7Z!gB%qf~MRF z60=Cga|U#Oz~}Qx?pJzyJUM diff --git a/languages/gravityformsmailchimp-pt_PT.mo b/languages/gravityformsmailchimp-pt_PT.mo index 800efe713ff72dcb583181616fd2a1b554baf35a..b9cfecd852496f130ebed4b972b37da3ed00d7ac 100644 GIT binary patch delta 23 ecmbQFH%V_p3^$jhf}y3Ak%g79+2%CvHQWGETn2{# delta 23 ecmbQFH%V_p3^$jFf}xp}sgaej(dIPnHQWGDy#{yy diff --git a/languages/gravityformsmailchimp-ru_RU.mo b/languages/gravityformsmailchimp-ru_RU.mo index 3eb08ae64752991b406fb5f263514e698f4edc2d..8d1c3cea02ecf751fa25856f88b2a8af16b873cc 100644 GIT binary patch delta 23 ecmexr@zr8O3^$jhf}y3Ak%g79+2%BEen9|Y&<4r? delta 23 ecmexr@zr8O3^$jFf}xp}sgaej(dIO6en9|YF$TK; diff --git a/languages/gravityformsmailchimp-sv_SE.mo b/languages/gravityformsmailchimp-sv_SE.mo index fc5f0cb24e7d32e9e23a87f21bda75a8a73de2d6..fe09307576903b686b4df463a1b2dc14b46a3c96 100644 GIT binary patch delta 20 ccmdnayq$T%CN4__LrW_o3oB!@iF;~)r delta 23 ecmZ3(wT5fMbw(}|1w%6{QzI*5qs