diff --git a/assets/js/src/subscribers/list.jsx b/assets/js/src/subscribers/list.jsx index d15b1fc3c2..c28da73970 100644 --- a/assets/js/src/subscribers/list.jsx +++ b/assets/js/src/subscribers/list.jsx @@ -121,7 +121,7 @@ const bulk_actions = [ onSuccess: function(response) { MailPoet.Notice.success( MailPoet.I18n.t('multipleSubscribersMovedToList') - .replace('%$1d', ~~response.subscribers) + .replace('%$1d', ~~(response.subscribers)) .replace('%$2s', response.segment) ); } diff --git a/lib/Models/Model.php b/lib/Models/Model.php index 91a857f52a..68a4945e64 100644 --- a/lib/Models/Model.php +++ b/lib/Models/Model.php @@ -5,7 +5,6 @@ if(!defined('ABSPATH')) exit; class Model extends \Sudzy\ValidModel { protected $_errors; - const MAX_BATCH_SIZE = 200; function __construct() { $this->_errors = array(); @@ -91,7 +90,7 @@ class Model extends \Sudzy\ValidModel { } function restore() { - return $this->set_expr('deleted_at', 'NULl')->save(); + return $this->set_expr('deleted_at', 'NULL')->save(); } static function bulkRestore($orm) { @@ -99,7 +98,7 @@ class Model extends \Sudzy\ValidModel { return self::bulkAction($orm, function($ids) use($model) { self::rawExecute(join(' ', array( 'UPDATE `'.$model::$_table.'`', - 'SET `deleted_at`=NULL', + 'SET `deleted_at` = NULL', 'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')' )), $ids @@ -112,48 +111,21 @@ class Model extends \Sudzy\ValidModel { if($total === 0) return false; - $affected_rows = 0; + $rows = $orm->select(static::$_table.'.id') + ->offset(null) + ->limit(null) + ->findArray(); - if($total > self::MAX_BATCH_SIZE) { - for( - $i = 0, $batches = ceil($total / self::MAX_BATCH_SIZE); - $i < $batches; - $i++ - ) { - $rows = $orm->select('id') - ->offset($i * self::MAX_BATCH_SIZE) - ->limit(self::MAX_BATCH_SIZE) - ->findArray(); + $ids = array_map(function($model) { + return (int)$model['id']; + }, $rows); - $ids = array_map(function($model) { - return (int)$model['id']; - }, $rows); - - if($callback !== false) { - $callback($ids); - } - - // increment number of affected rows - $affected_rows += $orm->get_last_statement()->rowCount(); - } - } else { - $rows = $orm->select(static::$_table.'.id') - ->offset(null) - ->limit(null) - ->findArray(); - - $ids = array_map(function($model) { - return (int)$model['id']; - }, $rows); - - if($callback !== false) { - $callback($ids); - } - - // get number of affected rows - $affected_rows = $orm->get_last_statement()->rowCount(); + if($callback !== false) { + $callback($ids); } - return $affected_rows; + + // get number of affected rows + return $orm->get_last_statement()->rowCount(); } function duplicate($data = array()) { diff --git a/lib/Models/Subscriber.php b/lib/Models/Subscriber.php index e530df4b3f..f757886259 100644 --- a/lib/Models/Subscriber.php +++ b/lib/Models/Subscriber.php @@ -544,13 +544,13 @@ class Subscriber extends Model { if($segment === false) return false; - $subscribers_count = parent::bulkAction($orm, - function($subscriber_ids) use($segment) { - SubscriberSegment::deleteManySubscriptions( - $subscriber_ids, array($segment->id) - ); - } - ); + $subscribers_count = $orm->count(); + + parent::bulkAction($orm, function($subscriber_ids) use($segment) { + SubscriberSegment::deleteManySubscriptions( + $subscriber_ids, array($segment->id) + ); + }); return array( 'subscribers' => $subscribers_count, @@ -590,7 +590,9 @@ class Subscriber extends Model { self::rawExecute(join(' ', array( 'UPDATE `'.self::$_table.'`', 'SET `deleted_at` = NOW()', - 'WHERE `id` IN ('.rtrim(str_repeat('?,', count($subscriber_ids)), ',').')', + 'WHERE `id` IN ('. + rtrim(str_repeat('?,', count($subscriber_ids)), ',') + .')', 'AND `wp_user_id` IS NULL' )), $subscriber_ids diff --git a/lib/Models/SubscriberSegment.php b/lib/Models/SubscriberSegment.php index a0c661fd8e..566b85c6da 100644 --- a/lib/Models/SubscriberSegment.php +++ b/lib/Models/SubscriberSegment.php @@ -95,18 +95,24 @@ class SubscriberSegment extends Model { } // create many subscriptions to each segment - foreach($segment_ids as $segment_id) { - $query = array( - 'INSERT IGNORE INTO `'.self::$_table.'`', - '(`subscriber_id`, `segment_id`, `status`)', - 'VALUES '.rtrim(str_repeat( - "(?, ".(int)$segment_id.", '".Subscriber::STATUS_SUBSCRIBED."'), ", - count($subscriber_ids) - ), ', ') - ); - self::rawExecute(join(' ', $query), $subscriber_ids); + $values = array(); + $row_count = 0; + foreach($segment_ids as &$segment_id) { + foreach($subscriber_ids as &$subscriber_id) { + $values[] = (int)$subscriber_id; + $values[] = (int)$segment_id; + $values[] = Subscriber::STATUS_SUBSCRIBED; + $row_count++; + } } + $query = array( + 'INSERT IGNORE INTO `'.self::$_table.'`', + '(`subscriber_id`, `segment_id`, `status`)', + 'VALUES '.rtrim(str_repeat('(?, ?, ?), ', $row_count), ', ') + ); + self::rawExecute(join(' ', $query), $values); + return true; }