hasOne(__NAMESPACE__ . '\ScheduledTask', 'id', 'task_id'); } public static function createOrUpdate($data = []) { if (!is_array($data) || empty($data['task_id']) || empty($data['subscriber_id'])) { return; } $data['processed'] = !empty($data['processed']) ? self::STATUS_PROCESSED : self::STATUS_UNPROCESSED; $data['failed'] = !empty($data['failed']) ? self::FAIL_STATUS_FAILED : self::FAIL_STATUS_OK; return parent::_createOrUpdate($data, [ 'subscriber_id' => $data['subscriber_id'], 'task_id' => $data['task_id'], ]); } /** * @deprecated This method can be removed after 2024-01-04. */ public static function setSubscribers($taskId, array $subscriberIds) { self::deprecationError(__METHOD__); static::clearSubscribers($taskId); return static::addSubscribers($taskId, $subscriberIds); } /** * For large batches use MailPoet\Segments\SubscribersFinder::addSubscribersToTaskFromSegments() */ public static function addSubscribers($taskId, array $subscriberIds) { foreach ($subscriberIds as $subscriberId) { self::createOrUpdate([ 'task_id' => $taskId, 'subscriber_id' => $subscriberId, ]); } } public static function clearSubscribers($taskId) { return self::where('task_id', $taskId)->deleteMany(); } public static function getUnprocessedCount($taskId) { return self::getCount($taskId, self::STATUS_UNPROCESSED); } public static function getProcessedCount($taskId) { return self::getCount($taskId, self::STATUS_PROCESSED); } private static function getCount($taskId, $processed = null) { $orm = self::where('task_id', $taskId); if (!is_null($processed)) { $orm->where('processed', $processed); } return $orm->count(); } private static function deprecationError($methodName) { trigger_error( 'Calling ' . esc_html($methodName) . ' is deprecated and will be removed. Use \MailPoet\Newsletter\Sending\ScheduledTaskSubscribersRepository and \MailPoet\Entities\ScheduledTaskSubscriberEntity instead.', E_USER_DEPRECATED ); } }