Нет описания

Segment.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. namespace MailPoet\Models;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Entities\SegmentEntity;
  5. use MailPoet\WooCommerce\Helper as WCHelper;
  6. use MailPoet\WP\Functions as WPFunctions;
  7. /**
  8. * @property array $subscribersCount
  9. * @property array $automatedEmailsSubjects
  10. * @property string $name
  11. * @property string $type
  12. * @property string $description
  13. * @property string $countConfirmations
  14. */
  15. class Segment extends Model {
  16. public static $_table = MP_SEGMENTS_TABLE; // phpcs:ignore PSR2.Classes.PropertyDeclaration
  17. const TYPE_WP_USERS = SegmentEntity::TYPE_WP_USERS;
  18. const TYPE_WC_USERS = SegmentEntity::TYPE_WC_USERS;
  19. const TYPE_DEFAULT = SegmentEntity::TYPE_DEFAULT;
  20. public function __construct() {
  21. parent::__construct();
  22. $this->addValidations('name', [
  23. 'required' => WPFunctions::get()->__('Please specify a name.', 'mailpoet'),
  24. ]);
  25. }
  26. public function delete() {
  27. // delete all relations to subscribers
  28. SubscriberSegment::where('segment_id', $this->id)->deleteMany();
  29. return parent::delete();
  30. }
  31. public function newsletters() {
  32. return $this->has_many_through(
  33. __NAMESPACE__ . '\Newsletter',
  34. __NAMESPACE__ . '\NewsletterSegment',
  35. 'segment_id',
  36. 'newsletter_id'
  37. );
  38. }
  39. public function subscribers() {
  40. return $this->has_many_through(
  41. __NAMESPACE__ . '\Subscriber',
  42. __NAMESPACE__ . '\SubscriberSegment',
  43. 'segment_id',
  44. 'subscriber_id'
  45. );
  46. }
  47. public function duplicate($data = []) {
  48. $duplicate = parent::duplicate($data);
  49. if ($duplicate !== false) {
  50. foreach ($this->subscribers()->findResultSet() as $relation) {
  51. $newRelation = SubscriberSegment::create();
  52. $newRelation->set('subscriber_id', $relation->id);
  53. $newRelation->set('segment_id', $duplicate->id);
  54. $newRelation->save();
  55. }
  56. return $duplicate;
  57. }
  58. return false;
  59. }
  60. public function addSubscriber($subscriberId) {
  61. $relation = SubscriberSegment::create();
  62. $relation->set('subscriber_id', $subscriberId);
  63. $relation->set('segment_id', $this->id);
  64. return $relation->save();
  65. }
  66. public function removeSubscriber($subscriberId) {
  67. return SubscriberSegment::where('subscriber_id', $subscriberId)
  68. ->where('segment_id', $this->id)
  69. ->delete();
  70. }
  71. /**
  72. * @deprecated Use the version in \MailPoet\Segments\SegmentSubscribersRepository::getSubscribersStatisticsCount
  73. * @return $this
  74. */
  75. public function withSubscribersCount() {
  76. trigger_error('Calling Segment::withSubscribersCount() is deprecated and will be removed. Use MailPoet\Segments\SegmentSubscribersRepository::getSubscribersStatisticsCount. ', E_USER_DEPRECATED);
  77. $query = SubscriberSegment::tableAlias('relation')
  78. ->where('relation.segment_id', $this->id)
  79. ->join(
  80. MP_SUBSCRIBERS_TABLE,
  81. 'subscribers.id = relation.subscriber_id',
  82. 'subscribers'
  83. )
  84. ->select_expr(
  85. 'SUM(CASE WHEN subscribers.status = "' . Subscriber::STATUS_SUBSCRIBED . '"
  86. AND relation.status = "' . Subscriber::STATUS_SUBSCRIBED . '" THEN 1 ELSE 0 END)',
  87. Subscriber::STATUS_SUBSCRIBED
  88. )
  89. ->select_expr(
  90. 'SUM(CASE WHEN subscribers.status = "' . Subscriber::STATUS_UNSUBSCRIBED . '"
  91. OR relation.status = "' . Subscriber::STATUS_UNSUBSCRIBED . '" THEN 1 ELSE 0 END)',
  92. Subscriber::STATUS_UNSUBSCRIBED
  93. )
  94. ->select_expr(
  95. 'SUM(CASE WHEN subscribers.status = "' . Subscriber::STATUS_INACTIVE . '"
  96. AND relation.status != "' . Subscriber::STATUS_UNSUBSCRIBED . '" THEN 1 ELSE 0 END)',
  97. Subscriber::STATUS_INACTIVE
  98. )
  99. ->select_expr(
  100. 'SUM(CASE WHEN subscribers.status = "' . Subscriber::STATUS_UNCONFIRMED . '"
  101. AND relation.status != "' . Subscriber::STATUS_UNSUBSCRIBED . '" THEN 1 ELSE 0 END)',
  102. Subscriber::STATUS_UNCONFIRMED
  103. )
  104. ->select_expr(
  105. 'SUM(CASE WHEN subscribers.status = "' . Subscriber::STATUS_BOUNCED . '"
  106. AND relation.status != "' . Subscriber::STATUS_UNSUBSCRIBED . '" THEN 1 ELSE 0 END)',
  107. Subscriber::STATUS_BOUNCED
  108. )
  109. ->whereNull('subscribers.deleted_at')
  110. ->findOne();
  111. if ($query instanceof SubscriberSegment) {
  112. $this->subscribersCount = $query->asArray();
  113. }
  114. return $this;
  115. }
  116. public static function getWPSegment() {
  117. $wpSegment = self::where('type', self::TYPE_WP_USERS)->findOne();
  118. if ($wpSegment === false) {
  119. // create the wp users segment
  120. $wpSegment = Segment::create();
  121. $wpSegment->hydrate([
  122. 'name' => WPFunctions::get()->__('WordPress Users', 'mailpoet'),
  123. 'description' =>
  124. WPFunctions::get()->__('This list contains all of your WordPress users.', 'mailpoet'),
  125. 'type' => self::TYPE_WP_USERS,
  126. ]);
  127. $wpSegment->save();
  128. }
  129. return $wpSegment;
  130. }
  131. public static function getWooCommerceSegment() {
  132. $wcSegment = self::where('type', self::TYPE_WC_USERS)->findOne();
  133. if ($wcSegment === false) {
  134. // create the WooCommerce customers segment
  135. $wcSegment = Segment::create();
  136. $wcSegment->hydrate([
  137. 'name' => WPFunctions::get()->__('WooCommerce Customers', 'mailpoet'),
  138. 'description' =>
  139. WPFunctions::get()->__('This list contains all of your WooCommerce customers.', 'mailpoet'),
  140. 'type' => self::TYPE_WC_USERS,
  141. ]);
  142. $wcSegment->save();
  143. }
  144. return $wcSegment;
  145. }
  146. /**
  147. * @deprecated Use the non static implementation in \MailPoet\Segments\WooCommerce::shouldShowWooCommerceSegment instead
  148. */
  149. public static function shouldShowWooCommerceSegment() {
  150. $woocommerceHelper = new WCHelper();
  151. $isWoocommerceActive = $woocommerceHelper->isWooCommerceActive();
  152. $woocommerceUserExists = Segment::tableAlias('segment')
  153. ->where('segment.type', Segment::TYPE_WC_USERS)
  154. ->join(
  155. MP_SUBSCRIBER_SEGMENT_TABLE,
  156. 'segment_subscribers.segment_id = segment.id',
  157. 'segment_subscribers'
  158. )
  159. ->limit(1)
  160. ->findOne();
  161. if (!$isWoocommerceActive && !$woocommerceUserExists) {
  162. return false;
  163. }
  164. return true;
  165. }
  166. public static function getSegmentTypes() {
  167. $types = [Segment::TYPE_DEFAULT, Segment::TYPE_WP_USERS];
  168. if (Segment::shouldShowWooCommerceSegment()) {
  169. $types[] = Segment::TYPE_WC_USERS;
  170. }
  171. return $types;
  172. }
  173. public static function groupBy($orm, $group = null) {
  174. if ($group === 'trash') {
  175. $orm->whereNotNull('deleted_at');
  176. } else {
  177. $orm->whereNull('deleted_at');
  178. }
  179. return $orm;
  180. }
  181. /**
  182. * @deprecated Will be removed after 2021/07/30. Use MailPoet\Segments\SegmentsSimpleListRepository
  183. */
  184. public static function getSegmentsWithSubscriberCount($type = self::TYPE_DEFAULT) {
  185. trigger_error('Calling Segment::getSegmentsWithSubscriberCount() is deprecated and will be removed. Use MailPoet\Segments\SegmentsSimpleListRepository. ', E_USER_DEPRECATED);
  186. $query = self::selectMany([self::$_table . '.id', self::$_table . '.name'])
  187. ->whereIn('type', Segment::getSegmentTypes())
  188. ->selectExpr(
  189. self::$_table . '.type, ' .
  190. 'COUNT(IF(' .
  191. MP_SUBSCRIBER_SEGMENT_TABLE . '.status="' . Subscriber::STATUS_SUBSCRIBED . '"'
  192. . ' AND ' .
  193. MP_SUBSCRIBERS_TABLE . '.deleted_at IS NULL'
  194. . ' AND ' .
  195. MP_SUBSCRIBERS_TABLE . '.status="' . Subscriber::STATUS_SUBSCRIBED . '"'
  196. . ', 1, NULL)) `subscribers`'
  197. )
  198. ->leftOuterJoin(
  199. MP_SUBSCRIBER_SEGMENT_TABLE,
  200. [self::$_table . '.id', '=', MP_SUBSCRIBER_SEGMENT_TABLE . '.segment_id'])
  201. ->leftOuterJoin(
  202. MP_SUBSCRIBERS_TABLE,
  203. [MP_SUBSCRIBER_SEGMENT_TABLE . '.subscriber_id', '=', MP_SUBSCRIBERS_TABLE . '.id'])
  204. ->groupBy(self::$_table . '.id')
  205. ->groupBy(self::$_table . '.name')
  206. ->groupBy(self::$_table . '.type')
  207. ->orderByAsc(self::$_table . '.name')
  208. ->whereNull(self::$_table . '.deleted_at');
  209. if (!empty($type)) {
  210. $query->where(self::$_table . '.type', $type);
  211. }
  212. return $query->findArray();
  213. }
  214. /**
  215. * @deprecated Will be removed after 2021/07/30. Use MailPoet\Segments\SegmentsSimpleListRepository
  216. */
  217. public static function getSegmentsForImport() {
  218. trigger_error('Calling Segment::getSegmentsForImport() is deprecated and will be removed. Use MailPoet\Segments\SegmentsSimpleListRepository. ', E_USER_DEPRECATED);
  219. $segments = self::getSegmentsWithSubscriberCount($type = false);
  220. return array_values(array_filter($segments, function($segment) {
  221. return $segment['type'] !== Segment::TYPE_WC_USERS;
  222. }));
  223. }
  224. /**
  225. * @deprecated Will be removed after 2021/07/30. Use MailPoet\Segments\SegmentsSimpleListRepository
  226. */
  227. public static function getSegmentsForExport() {
  228. trigger_error('Calling Segment::getSegmentsForExport() is deprecated and will be removed. Use MailPoet\Segments\SegmentsSimpleListRepository. ', E_USER_DEPRECATED);
  229. return self::rawQuery(
  230. '(SELECT segments.id, segments.name, COUNT(relation.subscriber_id) as subscribers ' .
  231. 'FROM ' . MP_SUBSCRIBER_SEGMENT_TABLE . ' relation ' .
  232. 'LEFT JOIN ' . self::$_table . ' segments ON segments.id = relation.segment_id ' .
  233. 'INNER JOIN ' . MP_SUBSCRIBERS_TABLE . ' subscribers ON subscribers.id = relation.subscriber_id ' .
  234. 'WHERE relation.segment_id IS NOT NULL ' .
  235. 'AND subscribers.deleted_at IS NULL ' .
  236. 'GROUP BY segments.id) ' .
  237. 'UNION ALL ' .
  238. '(SELECT 0 as id, "' . WPFunctions::get()->__('Subscribers without a list', 'mailpoet') . '" as name, COUNT(*) as subscribers ' .
  239. 'FROM ' . MP_SUBSCRIBERS_TABLE . ' subscribers ' .
  240. 'LEFT JOIN ' . MP_SUBSCRIBER_SEGMENT_TABLE . ' relation on relation.subscriber_id = subscribers.id ' .
  241. 'WHERE relation.subscriber_id is NULL ' .
  242. 'AND subscribers.deleted_at IS NULL ' .
  243. 'HAVING subscribers) ' .
  244. 'ORDER BY name'
  245. )->findArray();
  246. }
  247. public static function getPublic() {
  248. return self::getPublished()->where('type', self::TYPE_DEFAULT)->orderByAsc('name');
  249. }
  250. public static function bulkTrash($orm) {
  251. $count = parent::bulkAction($orm, function($ids) {
  252. Segment::rawExecute(join(' ', [
  253. 'UPDATE `' . Segment::$_table . '`',
  254. 'SET `deleted_at` = NOW()',
  255. 'WHERE `id` IN (' . rtrim(str_repeat('?,', count($ids)), ',') . ')',
  256. 'AND `type` = "' . Segment::TYPE_DEFAULT . '"',
  257. ]), $ids);
  258. });
  259. return ['count' => $count];
  260. }
  261. public static function bulkDelete($orm) {
  262. $count = parent::bulkAction($orm, function($ids) {
  263. // delete segments (only default)
  264. $segments = Segment::whereIn('id', $ids)
  265. ->where('type', Segment::TYPE_DEFAULT)
  266. ->findMany();
  267. $ids = array_map(function($segment) {
  268. return $segment->id;
  269. }, $segments);
  270. if (!$ids) {
  271. return;
  272. }
  273. SubscriberSegment::whereIn('segment_id', $ids)
  274. ->deleteMany();
  275. Segment::whereIn('id', $ids)->deleteMany();
  276. });
  277. return ['count' => $count];
  278. }
  279. }