Skip to content

Commit

Permalink
fix(mailchimp): allow contacts to resubscribe after unsubscribing (#1654
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dkoo authored Sep 13, 2024
1 parent b5e4812 commit b76dbc2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1373,26 +1373,32 @@ function( $a, $b ) {
/**
* Gets the status and/or status_if_new keys based on the contact data.
*
* @param array $contact {
* Contact data.
* @param array $contact {
* Contact data.
*
* @type string $email Contact email address.
* @type string $name Contact name. Optional.
* @type string[] $metadata Contact additional metadata. Optional.
* }
* @param string $list_id List (Audience) to add the contact to, if any.
*
* @return array The status and/or status_if_new keys to be added to the payload
*/
private function get_status_for_payload( $contact ) {
private function get_status_for_payload( $contact, $list_id = null ) {
$return = [];
if ( isset( $contact['metadata'] ) && ! empty( $contact['metadata']['status_if_new'] ) ) {
if ( ! empty( $contact['metadata']['status_if_new'] ) ) {
$return['status_if_new'] = $contact['metadata']['status_if_new'];
}

if ( isset( $contact['metadata'] ) && ! empty( $contact['metadata']['status'] ) ) {
if ( ! empty( $contact['metadata']['status'] ) ) {
$return['status'] = $contact['metadata']['status'];
}

// Check if the contact has unsubscribed before. Mailchimp requires a double opt-in to resubscribe, so we set the status to 'pending'.
if ( $list_id && ! empty( $contact['existing_contact_data']['lists'][ $list_id ]['status'] ) && 'unsubscribed' === $contact['existing_contact_data']['lists'][ $list_id ]['status'] ) {
$return['status'] = 'pending';
}

// If we're subscribing the contact to a newsletter, they should have some status
// because 'non-subscriber' status can't receive newsletters.
if ( empty( $return['status'] ) && empty( $return['status_if_new'] ) ) {
Expand Down Expand Up @@ -1434,7 +1440,7 @@ public function add_contact( $contact, $list_id = false, $tags = [], $interests

$update_payload = array_merge(
$update_payload,
$this->get_status_for_payload( $contact )
$this->get_status_for_payload( $contact, $list_id )
);

// Parse full name into first + last.
Expand Down
40 changes: 30 additions & 10 deletions tests/test-mailchimp-contact-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,70 +138,90 @@ protected static function get_private_method( $name ) {
*/
public function get_status_payload_data() {
return [
'empty' => [
'empty' => [
[],
null,
[ 'status' => 'subscribed' ],
],
'empty_metadata' => [
'empty_metadata' => [
[
'metadata' => [],
],
null,
[ 'status' => 'subscribed' ],
],
'empty_status' => [
'empty_status' => [
[
'metadata' => [
'status' => '',
],
],
null,
[ 'status' => 'subscribed' ],
],
'only_status' => [
'only_status' => [
[
'metadata' => [
'status' => 'transactional',
],
],
null,
[
'status' => 'transactional',
],
],
'only status if new' => [
'only status if new' => [
[
'metadata' => [
'status_if_new' => 'transactional',
],
],
null,
[
'status_if_new' => 'transactional',
],
],
'both' => [
'both' => [
[
'metadata' => [
'status_if_new' => 'transactional',
'status' => 'subscribed',
],
],
null,
[
'status_if_new' => 'transactional',
'status' => 'subscribed',
],
],
'status_if_unsubscribed' => [
[
'existing_contact_data' => [
'lists' => [
'list1' => [
'status' => 'unsubscribed',
],
],
],
],
'list1',
[ 'status' => 'pending' ],
],
];
}

/**
* Test get_status_for_payload
*
* @param array $input Input data.
* @param array $expected Expected output.
* @param array $arg1 Input data.
* @param string|null $arg2 Input data.
* @param array $expected Expected output.
* @dataProvider get_status_payload_data
*/
public function test_get_status_for_payload( $input, $expected ) {
public function test_get_status_for_payload( $arg1, $arg2, $expected ) {
$method = self::get_private_method( 'get_status_for_payload' );
$service = Newspack_Newsletters_Mailchimp::instance();
$this->assertSame( $expected, $method->invoke( $service, $input ) );
$this->assertSame( $expected, $method->invoke( $service, $arg1, $arg2 ) );
}

/**
Expand Down

0 comments on commit b76dbc2

Please sign in to comment.