Collect subscriber IPs for 3rd party API subscriptions [MAILPOET-2575]
This commit is contained in:
@ -13,6 +13,7 @@ use MailPoet\Subscribers\NewSubscriberNotificationMailer;
|
||||
use MailPoet\Subscribers\RequiredCustomFieldValidator;
|
||||
use MailPoet\Subscribers\Source;
|
||||
use MailPoet\Tasks\Sending;
|
||||
use MailPoet\Util\Helpers;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class API {
|
||||
@ -262,11 +263,15 @@ class API {
|
||||
);
|
||||
}
|
||||
|
||||
if (empty($subscriber['subscribed_ip'])) {
|
||||
$subscriber['subscribed_ip'] = Helpers::getIP();
|
||||
}
|
||||
|
||||
// separate data into default and custom fields
|
||||
list($default_fields, $custom_fields) = Subscriber::extractCustomFieldsFromFromObject($subscriber);
|
||||
|
||||
// filter out all incoming data that we don't want to change, like status, ip address, ...
|
||||
$default_fields = array_intersect_key($default_fields, array_flip(['email', 'first_name', 'last_name']));
|
||||
// filter out all incoming data that we don't want to change, like status ...
|
||||
$default_fields = array_intersect_key($default_fields, array_flip(['email', 'first_name', 'last_name', 'subscribed_ip']));
|
||||
|
||||
// if some required default fields are missing, set their values
|
||||
$default_fields = Subscriber::setRequiredFieldsDefaultValues($default_fields);
|
||||
|
@ -471,14 +471,26 @@ class APITest extends \MailPoetTest {
|
||||
'cf_' . $custom_field->id => 'test',
|
||||
];
|
||||
|
||||
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
||||
$result = $this->getApi()->addSubscriber($subscriber);
|
||||
expect($result['id'])->greaterThan(0);
|
||||
expect($result['email'])->equals($subscriber['email']);
|
||||
expect($result['cf_' . $custom_field->id])->equals('test');
|
||||
expect($result['source'])->equals('api');
|
||||
expect($result['subscribed_ip'])->equals($_SERVER['REMOTE_ADDR']);
|
||||
expect(strlen($result['unsubscribe_token']))->equals(15);
|
||||
}
|
||||
|
||||
function testItAllowsToOverrideSubscriberIPAddress() {
|
||||
$subscriber = [
|
||||
'email' => 'test-ip-2@example.com',
|
||||
'subscribed_ip' => '1.2.3.4',
|
||||
];
|
||||
|
||||
$result = $this->getApi()->addSubscriber($subscriber);
|
||||
expect($result['subscribed_ip'])->equals($subscriber['subscribed_ip']);
|
||||
}
|
||||
|
||||
function testItChecksForMandatoryCustomFields() {
|
||||
CustomField::createOrUpdate([
|
||||
'name' => 'custom field',
|
||||
|
Reference in New Issue
Block a user