Prevents overwriting global subscription status
This commit is contained in:
@ -794,7 +794,8 @@ class Subscriber extends Model {
|
|||||||
$ignore_columns_on_update = array(
|
$ignore_columns_on_update = array(
|
||||||
'wp_user_id',
|
'wp_user_id',
|
||||||
'email',
|
'email',
|
||||||
'created_at'
|
'created_at',
|
||||||
|
'status'
|
||||||
);
|
);
|
||||||
// check if there is anything to update after excluding ignored columns
|
// check if there is anything to update after excluding ignored columns
|
||||||
if(!array_diff($columns, $ignore_columns_on_update)) return;
|
if(!array_diff($columns, $ignore_columns_on_update)) return;
|
||||||
|
@ -106,8 +106,6 @@ class Import {
|
|||||||
if($new_subscribers['data']) {
|
if($new_subscribers['data']) {
|
||||||
// add, if required, missing required fields to new subscribers
|
// add, if required, missing required fields to new subscribers
|
||||||
$new_subscribers = $this->addMissingRequiredFields($new_subscribers);
|
$new_subscribers = $this->addMissingRequiredFields($new_subscribers);
|
||||||
// filter contents of the "status" field
|
|
||||||
$new_subscribers = $this->filterSubscribersStatus($new_subscribers);
|
|
||||||
$created_subscribers =
|
$created_subscribers =
|
||||||
$this->createOrUpdateSubscribers(
|
$this->createOrUpdateSubscribers(
|
||||||
'create',
|
'create',
|
||||||
@ -116,8 +114,6 @@ class Import {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
if($existing_subscribers['data'] && $this->update_subscribers) {
|
if($existing_subscribers['data'] && $this->update_subscribers) {
|
||||||
// filter contents of the "status" field
|
|
||||||
$existing_subscribers = $this->filterSubscribersStatus($existing_subscribers);
|
|
||||||
$updated_subscribers =
|
$updated_subscribers =
|
||||||
$this->createOrUpdateSubscribers(
|
$this->createOrUpdateSubscribers(
|
||||||
'update',
|
'update',
|
||||||
@ -299,49 +295,6 @@ class Import {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function filterSubscribersStatus($subscribers_data) {
|
|
||||||
if(!in_array('status', $subscribers_data['fields'])) return $subscribers_data;
|
|
||||||
$statuses = array(
|
|
||||||
Subscriber::STATUS_SUBSCRIBED => array(
|
|
||||||
'subscribed',
|
|
||||||
'confirmed',
|
|
||||||
1,
|
|
||||||
'1',
|
|
||||||
'true'
|
|
||||||
),
|
|
||||||
Subscriber::STATUS_UNCONFIRMED => array(
|
|
||||||
'unconfirmed',
|
|
||||||
0,
|
|
||||||
"0"
|
|
||||||
),
|
|
||||||
Subscriber::STATUS_UNSUBSCRIBED => array(
|
|
||||||
'unsubscribed',
|
|
||||||
-1,
|
|
||||||
'-1',
|
|
||||||
'false'
|
|
||||||
),
|
|
||||||
Subscriber::STATUS_BOUNCED => array(
|
|
||||||
'bounced'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$subscribers_data['data']['status'] = array_map(function($state) use ($statuses) {
|
|
||||||
if(in_array(strtolower($state), $statuses[Subscriber::STATUS_SUBSCRIBED], true)) {
|
|
||||||
return Subscriber::STATUS_SUBSCRIBED;
|
|
||||||
}
|
|
||||||
if(in_array(strtolower($state), $statuses[Subscriber::STATUS_UNSUBSCRIBED], true)) {
|
|
||||||
return Subscriber::STATUS_UNSUBSCRIBED;
|
|
||||||
}
|
|
||||||
if(in_array(strtolower($state), $statuses[Subscriber::STATUS_UNCONFIRMED], true)) {
|
|
||||||
return Subscriber::STATUS_UNCONFIRMED;
|
|
||||||
}
|
|
||||||
if(in_array(strtolower($state), $statuses[Subscriber::STATUS_BOUNCED], true)) {
|
|
||||||
return Subscriber::STATUS_BOUNCED;
|
|
||||||
}
|
|
||||||
return Subscriber::STATUS_SUBSCRIBED;
|
|
||||||
}, $subscribers_data['data']['status']);
|
|
||||||
return $subscribers_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
function createOrUpdateSubscribers(
|
function createOrUpdateSubscribers(
|
||||||
$action,
|
$action,
|
||||||
$subscribers_data,
|
$subscribers_data,
|
||||||
|
@ -383,29 +383,35 @@ class SubscriberTest extends \MailPoetTest {
|
|||||||
$columns = array(
|
$columns = array(
|
||||||
'first_name',
|
'first_name',
|
||||||
'last_name',
|
'last_name',
|
||||||
'email'
|
'email',
|
||||||
|
'status'
|
||||||
);
|
);
|
||||||
$values = array(
|
$values = array(
|
||||||
array(
|
array(
|
||||||
'first_name' => 'Adam',
|
'first_name' => 'Adam',
|
||||||
'last_name' => 'Smith',
|
'last_name' => 'Smith',
|
||||||
'email' => 'adam@smith.com'
|
'email' => 'adam@smith.com',
|
||||||
|
'status' => 'unsubscribed'
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'first_name' => 'Mary',
|
'first_name' => 'Mary',
|
||||||
'last_name' => 'Jane',
|
'last_name' => 'Jane',
|
||||||
'email' => 'mary@jane.com'
|
'email' => 'mary@jane.com',
|
||||||
|
'status' => 'unsubscribed'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
Subscriber::createMultiple($columns, $values);
|
Subscriber::createMultiple($columns, $values);
|
||||||
$subscribers = Subscriber::findArray();
|
$subscribers = Subscriber::findArray();
|
||||||
expect(count($subscribers))->equals(2);
|
expect(count($subscribers))->equals(2);
|
||||||
expect($subscribers[1]['email'])->equals($values[1]['email']);
|
expect($subscribers[1]['email'])->equals($values[1]['email']);
|
||||||
|
expect($subscribers[1]['status'])->equals($values[1]['status']);
|
||||||
|
|
||||||
$values[0]['first_name'] = 'John';
|
$values[0]['first_name'] = 'John';
|
||||||
|
$values[0]['status'] = 'subscribed';
|
||||||
Subscriber::updateMultiple($columns, $values);
|
Subscriber::updateMultiple($columns, $values);
|
||||||
$subscribers = Subscriber::findArray();
|
$subscribers = Subscriber::findArray();
|
||||||
expect($subscribers[0]['first_name'])->equals($values[0]['first_name']);
|
expect($subscribers[0]['first_name'])->equals($values[0]['first_name']);
|
||||||
|
expect($subscribers[0]['status'])->equals('unsubscribed');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanSubscribe() {
|
function testItCanSubscribe() {
|
||||||
|
@ -251,58 +251,6 @@ class ImportTest extends \MailPoetTest {
|
|||||||
expect($fields)->equals(array(39));
|
expect($fields)->equals(array(39));
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItFiltersSubscribersStatus() {
|
|
||||||
$subscribers_data = array(
|
|
||||||
'fields' => array('status'),
|
|
||||||
'data' => array(
|
|
||||||
'status' => array(
|
|
||||||
#subscribed
|
|
||||||
'subscribed',
|
|
||||||
'confirmed',
|
|
||||||
1,
|
|
||||||
'1',
|
|
||||||
'true',
|
|
||||||
#unconfirmed
|
|
||||||
'unconfirmed',
|
|
||||||
0,
|
|
||||||
"0",
|
|
||||||
#unsubscribed
|
|
||||||
'unsubscribed',
|
|
||||||
-1,
|
|
||||||
'-1',
|
|
||||||
'false',
|
|
||||||
#bounced
|
|
||||||
'bounced',
|
|
||||||
#unexpected
|
|
||||||
'qwerty',
|
|
||||||
null
|
|
||||||
),
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$result = $this->import->filterSubscribersStatus($subscribers_data);
|
|
||||||
expect($result['data'])->equals(
|
|
||||||
array(
|
|
||||||
'status' => array(
|
|
||||||
'subscribed',
|
|
||||||
'subscribed',
|
|
||||||
'subscribed',
|
|
||||||
'subscribed',
|
|
||||||
'subscribed',
|
|
||||||
'unconfirmed',
|
|
||||||
'unconfirmed',
|
|
||||||
'unconfirmed',
|
|
||||||
'unsubscribed',
|
|
||||||
'unsubscribed',
|
|
||||||
'unsubscribed',
|
|
||||||
'unsubscribed',
|
|
||||||
'bounced',
|
|
||||||
'subscribed',
|
|
||||||
'subscribed'
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function testItAddsOrUpdatesSubscribers() {
|
function testItAddsOrUpdatesSubscribers() {
|
||||||
$subscribers_data = array(
|
$subscribers_data = array(
|
||||||
'data' => $this->subscribers_data,
|
'data' => $this->subscribers_data,
|
||||||
@ -469,10 +417,10 @@ class ImportTest extends \MailPoetTest {
|
|||||||
expect($updated_subscriber->status)->equals('unsubscribed');
|
expect($updated_subscriber->status)->equals('unsubscribed');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItUpdatesExistingSubscribersStatusWhenStatusColumnIsPresent() {
|
function testItDoesNotUpdateExistingSubscribersStatusWhenStatusColumnIsPresent() {
|
||||||
$data = $this->data;
|
$data = $this->data;
|
||||||
$data['columns']['status'] = array('index' => 4);
|
$data['columns']['status'] = array('index' => 4);
|
||||||
$data['subscribers'][0][] = 'unsubscribed';
|
$data['subscribers'][0][] = 'subscribed';
|
||||||
$data['subscribers'][1][] = 'subscribed';
|
$data['subscribers'][1][] = 'subscribed';
|
||||||
$import = new Import($data);
|
$import = new Import($data);
|
||||||
$existing_subscriber = Subscriber::create();
|
$existing_subscriber = Subscriber::create();
|
||||||
@ -481,7 +429,7 @@ class ImportTest extends \MailPoetTest {
|
|||||||
'first_name' => 'Adam',
|
'first_name' => 'Adam',
|
||||||
'last_name' => 'Smith',
|
'last_name' => 'Smith',
|
||||||
'email' => 'Adam@Smith.com',
|
'email' => 'Adam@Smith.com',
|
||||||
'status' => 'subscribed'
|
'status' => 'unsubscribed'
|
||||||
));
|
));
|
||||||
$existing_subscriber->save();
|
$existing_subscriber->save();
|
||||||
$result = $import->process();
|
$result = $import->process();
|
||||||
|
@ -268,9 +268,9 @@ class ImportExportFactoryTest extends \MailPoetTest {
|
|||||||
$importMenu = $import->bootstrap();
|
$importMenu = $import->bootstrap();
|
||||||
expect(count(json_decode($importMenu['segments'], true)))
|
expect(count(json_decode($importMenu['segments'], true)))
|
||||||
->equals(2);
|
->equals(2);
|
||||||
// email, first_name, last_name, status + 1 custom field
|
// email, first_name, last_name + 1 custom field
|
||||||
expect(count(json_decode($importMenu['subscriberFields'], true)))
|
expect(count(json_decode($importMenu['subscriberFields'], true)))
|
||||||
->equals(5);
|
->equals(4);
|
||||||
// action, system fields, user fields
|
// action, system fields, user fields
|
||||||
expect(count(json_decode($importMenu['subscriberFieldsSelect2'], true)))
|
expect(count(json_decode($importMenu['subscriberFieldsSelect2'], true)))
|
||||||
->equals(3);
|
->equals(3);
|
||||||
|
Reference in New Issue
Block a user