Нет описания

ScheduledTaskSubscriber.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace MailPoet\Models;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Util\Helpers;
  5. use MailPoet\WP\Functions as WPFunctions;
  6. /**
  7. * @property int $taskId
  8. * @property int $subscriberId
  9. * @property int $processed
  10. * @property int $failed
  11. * @property string $error
  12. */
  13. class ScheduledTaskSubscriber extends Model {
  14. const STATUS_UNPROCESSED = 0;
  15. const STATUS_PROCESSED = 1;
  16. const FAIL_STATUS_OK = 0;
  17. const FAIL_STATUS_FAILED = 1;
  18. const SENDING_STATUS_SENT = 'sent';
  19. const SENDING_STATUS_FAILED = 'failed';
  20. const SENDING_STATUS_UNPROCESSED = 'unprocessed';
  21. public static $_table = MP_SCHEDULED_TASK_SUBSCRIBERS_TABLE; // phpcs:ignore PSR2.Classes.PropertyDeclaration
  22. public static $_id_column = ['task_id', 'subscriber_id']; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps,PSR2.Classes.PropertyDeclaration
  23. public function task() {
  24. return $this->hasOne(__NAMESPACE__ . '\ScheduledTask', 'id', 'task_id');
  25. }
  26. public static function createOrUpdate($data = []) {
  27. if (!is_array($data) || empty($data['task_id']) || empty($data['subscriber_id'])) {
  28. return;
  29. }
  30. $data['processed'] = !empty($data['processed']) ? self::STATUS_PROCESSED : self::STATUS_UNPROCESSED;
  31. $data['failed'] = !empty($data['failed']) ? self::FAIL_STATUS_FAILED : self::FAIL_STATUS_OK;
  32. return parent::_createOrUpdate($data, [
  33. 'subscriber_id' => $data['subscriber_id'],
  34. 'task_id' => $data['task_id'],
  35. ]);
  36. }
  37. public static function setSubscribers($taskId, array $subscriberIds) {
  38. static::clearSubscribers($taskId);
  39. return static::addSubscribers($taskId, $subscriberIds);
  40. }
  41. /**
  42. * For large batches use MailPoet\Segments\SubscribersFinder::addSubscribersToTaskFromSegments()
  43. */
  44. public static function addSubscribers($taskId, array $subscriberIds) {
  45. foreach ($subscriberIds as $subscriberId) {
  46. self::createOrUpdate([
  47. 'task_id' => $taskId,
  48. 'subscriber_id' => $subscriberId,
  49. ]);
  50. }
  51. }
  52. public static function clearSubscribers($taskId) {
  53. return self::where('task_id', $taskId)->deleteMany();
  54. }
  55. public static function getUnprocessedCount($taskId) {
  56. return self::getCount($taskId, self::STATUS_UNPROCESSED);
  57. }
  58. public static function getProcessedCount($taskId) {
  59. return self::getCount($taskId, self::STATUS_PROCESSED);
  60. }
  61. public static function getTotalCount($taskId) {
  62. return self::getCount($taskId);
  63. }
  64. public static function listingQuery($data) {
  65. $group = isset($data['group']) ? $data['group'] : 'all';
  66. $query = self::join(Subscriber::$_table, ["subscriber_id", "=", "subscribers.id"], "subscribers")
  67. ->filter($group, $data['params'])
  68. ->select('error', 'error')
  69. ->select('failed', 'failed')
  70. ->select('task_id', 'taskId')
  71. ->select('processed', 'processed')
  72. ->select('subscribers.email', 'email')
  73. ->select('subscribers.id', 'subscriberId')
  74. ->select('subscribers.last_name', 'lastName')
  75. ->select('subscribers.first_name', 'firstName');
  76. if (isset($data['search'])) {
  77. $search = trim($data['search']);
  78. $search = Helpers::escapeSearch($search);
  79. if (strlen($search) === 0) {
  80. return $query;
  81. }
  82. $search = '%' . $search . '%';
  83. return $query->whereRaw(
  84. '(`subscribers`.`email` LIKE ? OR `subscribers`.`first_name` LIKE ? OR `subscribers`.`last_name` LIKE ?)',
  85. [$search, $search, $search]
  86. );
  87. }
  88. return $query;
  89. }
  90. public static function groups($data) {
  91. $params = $data['params'];
  92. return [
  93. [
  94. 'name' => 'all',
  95. 'label' => WPFunctions::get()->__('All', 'mailpoet'),
  96. 'count' => self::filter('all', $params)->count(),
  97. ],
  98. [
  99. 'name' => self::SENDING_STATUS_SENT,
  100. 'label' => WPFunctions::get()->_x('Sent', 'status when a newsletter has been sent', 'mailpoet'),
  101. 'count' => self::filter(self::SENDING_STATUS_SENT, $params)->count(),
  102. ],
  103. [
  104. 'name' => self::SENDING_STATUS_FAILED,
  105. 'label' => WPFunctions::get()->_x('Failed', 'status when the sending of a newsletter has failed', 'mailpoet'),
  106. 'count' => self::filter(self::SENDING_STATUS_FAILED, $params)->count(),
  107. ],
  108. [
  109. 'name' => self::SENDING_STATUS_UNPROCESSED,
  110. 'label' => WPFunctions::get()->_x('Unprocessed', 'status when the sending of a newsletter has not been processed', 'mailpoet'),
  111. 'count' => self::filter(self::SENDING_STATUS_UNPROCESSED, $params)->count(),
  112. ],
  113. ];
  114. }
  115. public static function all($orm, $params) {
  116. return $orm->whereIn('task_id', $params['task_ids']);
  117. }
  118. public static function sent($orm, $params) {
  119. return $orm->filter('all', $params)
  120. ->where('processed', self::STATUS_PROCESSED)
  121. ->where('failed', self::FAIL_STATUS_OK);
  122. }
  123. public static function failed($orm, $params) {
  124. return $orm->filter('all', $params)
  125. ->where('processed', self::STATUS_PROCESSED)
  126. ->where('failed', self::FAIL_STATUS_FAILED);
  127. }
  128. public static function unprocessed($orm, $params) {
  129. return $orm->filter('all', $params)
  130. ->where('processed', self::STATUS_UNPROCESSED);
  131. }
  132. private static function getCount($taskId, $processed = null) {
  133. $orm = self::where('task_id', $taskId);
  134. if (!is_null($processed)) {
  135. $orm->where('processed', $processed);
  136. }
  137. return $orm->count();
  138. }
  139. }