Prevent WP users from being trashed/deleted
- return actual rowCount of affected rows for bulk actions (based on PDO last statement) - prevent removal of WP Users segment relationship with subscribers.
This commit is contained in:
@ -124,7 +124,8 @@ class Model extends \Sudzy\ValidModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $total;
|
$last_statement = $orm->get_last_statement();
|
||||||
|
return $last_statement->rowCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
function duplicate($data = array()) {
|
function duplicate($data = array()) {
|
||||||
|
@ -42,10 +42,24 @@ class Subscriber extends Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function delete() {
|
function delete() {
|
||||||
// delete all relations to segments
|
// WP Users cannot be deleted
|
||||||
SubscriberSegment::where('subscriber_id', $this->id)->deleteMany();
|
if($this->wp_user_id !== null) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
// delete all relations to segments
|
||||||
|
SubscriberSegment::where('subscriber_id', $this->id)->deleteMany();
|
||||||
|
|
||||||
return parent::delete();
|
return parent::delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function trash() {
|
||||||
|
// WP Users cannot be trashed
|
||||||
|
if($this->wp_user_id !== null) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return parent::trash();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendConfirmationEmail() {
|
function sendConfirmationEmail() {
|
||||||
@ -203,7 +217,7 @@ class Subscriber extends Model {
|
|||||||
);
|
);
|
||||||
|
|
||||||
$segment_list[] = array(
|
$segment_list[] = array(
|
||||||
'label' => str_replace(' (0)', '', $subscribers_without_segment_label),
|
'label' => $subscribers_without_segment_label,
|
||||||
'value' => 'none'
|
'value' => 'none'
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -212,9 +226,14 @@ class Subscriber extends Model {
|
|||||||
->filter('groupBy', $group)
|
->filter('groupBy', $group)
|
||||||
->count();
|
->count();
|
||||||
|
|
||||||
$label = sprintf('%s (%s)', $segment->name, number_format($subscribers_count));
|
$label = sprintf(
|
||||||
|
'%s (%s)',
|
||||||
|
$segment->name,
|
||||||
|
number_format($subscribers_count)
|
||||||
|
);
|
||||||
|
|
||||||
$segment_list[] = array(
|
$segment_list[] = array(
|
||||||
'label' => str_replace(' (0)', '', $label),
|
'label' => $label,
|
||||||
'value' => $segment->id()
|
'value' => $segment->id()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -520,23 +539,18 @@ class Subscriber extends Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static function bulkRemoveFromAllLists($orm) {
|
static function bulkRemoveFromAllLists($orm) {
|
||||||
$segments = Segment::findMany();
|
$subscribers = $orm->findResultSet();
|
||||||
$segment_ids = array_map(function($segment) {
|
$subscribers_count = 0;
|
||||||
return $segment->id();
|
foreach($subscribers as $subscriber) {
|
||||||
}, $segments);
|
try {
|
||||||
|
SubscriberSegment::removeSubscriptions($subscriber);
|
||||||
if(!empty($segment_ids)) {
|
$subscribers_count++;
|
||||||
// delete relations with segment
|
} catch(Exception $e) {
|
||||||
$subscribers = $orm->findResultSet();
|
continue;
|
||||||
foreach($subscribers as $subscriber) {
|
|
||||||
SubscriberSegment::where('subscriber_id', $subscriber->id)
|
|
||||||
->whereIn('segment_id', $segment_ids)
|
|
||||||
->deleteMany();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $subscribers->count();
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
|
return $subscribers_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function bulkSendConfirmationEmail($orm) {
|
static function bulkSendConfirmationEmail($orm) {
|
||||||
@ -564,12 +578,11 @@ class Subscriber extends Model {
|
|||||||
$subscribers_count = 0;
|
$subscribers_count = 0;
|
||||||
$subscribers = $orm->findMany();
|
$subscribers = $orm->findMany();
|
||||||
foreach($subscribers as $subscriber) {
|
foreach($subscribers as $subscriber) {
|
||||||
// create relation with segment
|
try {
|
||||||
$association = \MailPoet\Models\SubscriberSegment::create();
|
SubscriberSegment::addSubscriptions($subscriber, array($segment->id));
|
||||||
$association->subscriber_id = $subscriber->id;
|
|
||||||
$association->segment_id = $segment->id;
|
|
||||||
if($association->save()) {
|
|
||||||
$subscribers_count++;
|
$subscribers_count++;
|
||||||
|
} catch(Exception $e) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return array(
|
return array(
|
||||||
@ -580,12 +593,32 @@ class Subscriber extends Model {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function bulkDelete($orm) {
|
static function bulkTrash($orm) {
|
||||||
return parent::bulkAction($orm, function($ids) {
|
return parent::bulkAction($orm, function($ids) {
|
||||||
// delete subscribers
|
parent::rawExecute(join(' ', array(
|
||||||
Subscriber::whereIn('id', $ids)->deleteMany();
|
'UPDATE `'.self::$_table.'`',
|
||||||
// delete subscribers' relations to segments
|
'SET `deleted_at`=NOW()',
|
||||||
SubscriberSegment::whereIn('subscriber_id', $ids)->deleteMany();
|
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')',
|
||||||
|
'AND `wp_user_id` IS NULL'
|
||||||
|
)),
|
||||||
|
$ids
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static function bulkDelete($orm) {
|
||||||
|
$wp_users_segment = Segment::getWPUsers();
|
||||||
|
|
||||||
|
return parent::bulkAction($orm, function($ids) use ($wp_users_segment) {
|
||||||
|
// delete subscribers' relations to segments (except WP Users' segment)
|
||||||
|
SubscriberSegment::whereIn('subscriber_id', $ids)
|
||||||
|
->whereNotEqual('segment_id', $wp_users_segment->id)
|
||||||
|
->deleteMany();
|
||||||
|
|
||||||
|
// delete subscribers (except WP Users)
|
||||||
|
Subscriber::whereIn('id', $ids)
|
||||||
|
->whereNull('wp_user_id')
|
||||||
|
->deleteMany();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,10 +17,18 @@ class SubscriberSegment extends Model {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static function removeSubscriptions($subscriber, $segment_ids = array()) {
|
static function removeSubscriptions($subscriber, $segment_ids = array()) {
|
||||||
|
$wp_users_segment = Segment::getWPUsers();
|
||||||
|
|
||||||
if($subscriber->id > 0) {
|
if($subscriber->id > 0) {
|
||||||
if(!empty($segment_ids)) {
|
if(!empty($segment_ids)) {
|
||||||
// subscribe to segments
|
// unsubscribe from segments
|
||||||
foreach($segment_ids as $segment_id) {
|
foreach($segment_ids as $segment_id) {
|
||||||
|
|
||||||
|
// do not remove subscriptions to the WP Users segment
|
||||||
|
if($wp_users_segment->id === (int)$segment_id) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if((int)$segment_id > 0) {
|
if((int)$segment_id > 0) {
|
||||||
self::createOrUpdate(array(
|
self::createOrUpdate(array(
|
||||||
'subscriber_id' => $subscriber->id,
|
'subscriber_id' => $subscriber->id,
|
||||||
@ -29,9 +37,11 @@ class SubscriberSegment extends Model {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
// unsubscribe from all segments
|
// unsubscribe from all segments (except the WP users segment)
|
||||||
SubscriberSegment::where('subscriber_id', $subscriber->id)
|
return SubscriberSegment::where('subscriber_id', $subscriber->id)
|
||||||
|
->whereNotEqual('segment_id', $wp_users_segment->id)
|
||||||
->findResultSet()
|
->findResultSet()
|
||||||
->set('status', Subscriber::STATUS_UNSUBSCRIBED)
|
->set('status', Subscriber::STATUS_UNSUBSCRIBED)
|
||||||
->save();
|
->save();
|
||||||
@ -52,9 +62,11 @@ class SubscriberSegment extends Model {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
// subscribe to all segments
|
// subscribe to all segments
|
||||||
SubscriberSegment::where('subscriber_id', $subscriber->id)
|
return SubscriberSegment::where('subscriber_id', $subscriber->id)
|
||||||
->findResultSet()
|
->findResultSet()
|
||||||
->set('status', Subscriber::STATUS_SUBSCRIBED)
|
->set('status', Subscriber::STATUS_SUBSCRIBED)
|
||||||
->save();
|
->save();
|
||||||
|
Reference in New Issue
Block a user