Нет описания

SubscriberSegment.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace MailPoet\Models;
  3. if (!defined('ABSPATH')) exit;
  4. /**
  5. * @property int $id
  6. * @property int $subscriberId
  7. * @property int $segmentId
  8. * @property string $status
  9. */
  10. class SubscriberSegment extends Model {
  11. public static $_table = MP_SUBSCRIBER_SEGMENT_TABLE; // phpcs:ignore PSR2.Classes.PropertyDeclaration
  12. public function subscriber() {
  13. return $this->has_one(__NAMESPACE__ . '\Subscriber', 'id', 'subscriber_id');
  14. }
  15. public static function unsubscribeFromSegments($subscriber, $segmentIds = []) {
  16. if (!$subscriber) return false;
  17. // Reset confirmation emails count, so user can resubscribe
  18. $subscriber->countConfirmations = 0;
  19. $subscriber->save();
  20. $wpSegment = Segment::getWPSegment();
  21. if (!empty($segmentIds)) {
  22. // unsubscribe from segments
  23. foreach ($segmentIds as $segmentId) {
  24. // do not remove subscriptions to the WP Users segment
  25. if ($wpSegment !== false && (int)$wpSegment->id === (int)$segmentId) {
  26. continue;
  27. }
  28. if ((int)$segmentId > 0) {
  29. self::createOrUpdate([
  30. 'subscriber_id' => $subscriber->id,
  31. 'segment_id' => $segmentId,
  32. 'status' => Subscriber::STATUS_UNSUBSCRIBED,
  33. ]);
  34. }
  35. }
  36. } else {
  37. // unsubscribe from all segments (except the WP users and WooCommerce customers segments)
  38. $subscriptions = self::where('subscriber_id', $subscriber->id);
  39. if ($wpSegment !== false) {
  40. $subscriptions = $subscriptions->whereNotEqual(
  41. 'segment_id', $wpSegment->id
  42. );
  43. }
  44. $subscriptions->findResultSet()
  45. ->set('status', Subscriber::STATUS_UNSUBSCRIBED)
  46. ->save();
  47. }
  48. return true;
  49. }
  50. public static function resubscribeToAllSegments($subscriber) {
  51. if ($subscriber === false) return false;
  52. // (re)subscribe to all segments linked to the subscriber
  53. return self::where('subscriber_id', $subscriber->id)
  54. ->findResultSet()
  55. ->set('status', Subscriber::STATUS_SUBSCRIBED)
  56. ->save();
  57. }
  58. public static function subscribeToSegments($subscriber, $segmentIds = []) {
  59. if ($subscriber === false) return false;
  60. if (!empty($segmentIds)) {
  61. // subscribe to specified segments
  62. foreach ($segmentIds as $segmentId) {
  63. if ((int)$segmentId > 0) {
  64. self::createOrUpdate([
  65. 'subscriber_id' => $subscriber->id,
  66. 'segment_id' => $segmentId,
  67. 'status' => Subscriber::STATUS_SUBSCRIBED,
  68. ]);
  69. }
  70. }
  71. return true;
  72. }
  73. }
  74. public static function resetSubscriptions($subscriber, $segmentIds = []) {
  75. self::unsubscribeFromSegments($subscriber);
  76. return self::subscribeToSegments($subscriber, $segmentIds);
  77. }
  78. public static function subscribeManyToSegments(
  79. $subscriberIds = [],
  80. $segmentIds = []
  81. ) {
  82. if (empty($subscriberIds) || empty($segmentIds)) {
  83. return false;
  84. }
  85. // create many subscriptions to each segment
  86. $values = [];
  87. $rowCount = 0;
  88. foreach ($segmentIds as &$segmentId) {
  89. foreach ($subscriberIds as &$subscriberId) {
  90. $values[] = (int)$subscriberId;
  91. $values[] = (int)$segmentId;
  92. $rowCount++;
  93. }
  94. }
  95. $query = [
  96. 'INSERT IGNORE INTO `' . self::$_table . '`',
  97. '(`subscriber_id`, `segment_id`, `created_at`)',
  98. 'VALUES ' . rtrim(str_repeat('(?, ?, NOW()),', $rowCount), ','),
  99. ];
  100. self::rawExecute(join(' ', $query), $values);
  101. return true;
  102. }
  103. public static function deleteManySubscriptions($subscriberIds = [], $segmentIds = []) {
  104. if (empty($subscriberIds)) return false;
  105. // delete subscribers' relations to segments (except WP and WooCommerce segments)
  106. $subscriptions = self::whereIn(
  107. 'subscriber_id', $subscriberIds
  108. );
  109. $wpSegment = Segment::getWPSegment();
  110. $wcSegment = Segment::getWooCommerceSegment();
  111. if ($wpSegment !== false) {
  112. $subscriptions = $subscriptions->whereNotEqual(
  113. 'segment_id', $wpSegment->id
  114. );
  115. }
  116. if ($wcSegment !== false) {
  117. $subscriptions = $subscriptions->whereNotEqual(
  118. 'segment_id', $wcSegment->id
  119. );
  120. }
  121. if (!empty($segmentIds)) {
  122. $subscriptions = $subscriptions->whereIn('segment_id', $segmentIds);
  123. }
  124. return $subscriptions->deleteMany();
  125. }
  126. public static function deleteSubscriptions($subscriber, $segmentIds = []) {
  127. if ($subscriber === false) return false;
  128. $wpSegment = Segment::getWPSegment();
  129. $wcSegment = Segment::getWooCommerceSegment();
  130. $subscriptions = self::where('subscriber_id', $subscriber->id)
  131. ->whereNotIn('segment_id', [$wpSegment->id, $wcSegment->id]);
  132. if (!empty($segmentIds)) {
  133. $subscriptions = $subscriptions->whereIn('segment_id', $segmentIds);
  134. }
  135. return $subscriptions->deleteMany();
  136. }
  137. public static function subscribed($orm) {
  138. return $orm->where('status', Subscriber::STATUS_SUBSCRIBED);
  139. }
  140. public static function createOrUpdate($data = []) {
  141. $keys = false;
  142. if (isset($data['subscriber_id']) && isset($data['segment_id'])) {
  143. $keys = [
  144. 'subscriber_id' => (int)$data['subscriber_id'],
  145. 'segment_id' => (int)$data['segment_id'],
  146. ];
  147. }
  148. return parent::_createOrUpdate($data, $keys);
  149. }
  150. }