DynamicSegments
1 month ago
RestApi
1 month ago
SegmentDependencyValidator.php
3 years ago
SegmentListingRepository.php
1 month ago
SegmentSaveController.php
2 weeks ago
SegmentSubscribersRepository.php
2 weeks ago
SegmentsFinder.php
3 years ago
SegmentsRepository.php
2 weeks ago
SegmentsSimpleListRepository.php
2 months ago
SubscribersFinder.php
1 month ago
WP.php
2 weeks ago
WooCommerce.php
2 weeks ago
index.php
3 years ago
SegmentSaveController.php
94 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Segments; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\ConflictException; |
| 9 | use MailPoet\Entities\SegmentEntity; |
| 10 | use MailPoet\Entities\SubscriberSegmentEntity; |
| 11 | use MailPoet\NotFoundException; |
| 12 | use MailPoet\Subscribers\SegmentsCountRecalculator; |
| 13 | use MailPoetVendor\Doctrine\ORM\EntityManager; |
| 14 | use MailPoetVendor\Doctrine\ORM\ORMException; |
| 15 | |
| 16 | class SegmentSaveController { |
| 17 | /** @var SegmentsRepository */ |
| 18 | private $segmentsRepository; |
| 19 | |
| 20 | /** @var EntityManager */ |
| 21 | private $entityManager; |
| 22 | |
| 23 | /** @var SegmentsCountRecalculator */ |
| 24 | private $segmentsCountRecalculator; |
| 25 | |
| 26 | public function __construct( |
| 27 | SegmentsRepository $segmentsRepository, |
| 28 | EntityManager $entityManager, |
| 29 | SegmentsCountRecalculator $segmentsCountRecalculator |
| 30 | ) { |
| 31 | $this->segmentsRepository = $segmentsRepository; |
| 32 | $this->entityManager = $entityManager; |
| 33 | $this->segmentsCountRecalculator = $segmentsCountRecalculator; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @throws ConflictException |
| 38 | * @throws NotFoundException |
| 39 | * @throws ORMException |
| 40 | */ |
| 41 | public function save(array $data = []): SegmentEntity { |
| 42 | $id = isset($data['id']) ? (int)$data['id'] : null; |
| 43 | $name = $data['name'] ?? ''; |
| 44 | $description = $data['description'] ?? ''; |
| 45 | $displayInManageSubPage = isset($data['show_in_manage_subscription_page']) ? (int)$data['show_in_manage_subscription_page'] : false; |
| 46 | $confirmationEmailId = isset($data['confirmation_email_id']) ? (int)$data['confirmation_email_id'] : null; |
| 47 | if ($confirmationEmailId === 0) { |
| 48 | $confirmationEmailId = null; |
| 49 | } |
| 50 | $confirmationPageId = isset($data['confirmation_page_id']) ? (int)$data['confirmation_page_id'] : null; |
| 51 | if ($confirmationPageId === 0) { |
| 52 | $confirmationPageId = null; |
| 53 | } |
| 54 | $publicDescription = array_key_exists('public_description', $data) ? (string)$data['public_description'] : null; |
| 55 | |
| 56 | return $this->segmentsRepository->createOrUpdate($name, $description, SegmentEntity::TYPE_DEFAULT, [], $id, (bool)$displayInManageSubPage, $confirmationEmailId, $confirmationPageId, $publicDescription); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @throws ConflictException |
| 61 | */ |
| 62 | public function duplicate(SegmentEntity $segmentEntity): SegmentEntity { |
| 63 | $duplicate = clone $segmentEntity; |
| 64 | // translators: %s is the name of the segment |
| 65 | $duplicate->setName(sprintf(__('Copy of %s', 'mailpoet'), $segmentEntity->getName())); |
| 66 | |
| 67 | $this->segmentsRepository->verifyNameIsUnique($duplicate->getName(), $duplicate->getId()); |
| 68 | |
| 69 | $this->entityManager->transactional(function (EntityManager $entityManager) use ($duplicate, $segmentEntity) { |
| 70 | $entityManager->persist($duplicate); |
| 71 | $entityManager->flush(); |
| 72 | |
| 73 | $subscriberSegmentTable = $entityManager->getClassMetadata(SubscriberSegmentEntity::class)->getTableName(); |
| 74 | $conn = $this->entityManager->getConnection(); |
| 75 | $stmt = $conn->prepare(" |
| 76 | INSERT INTO $subscriberSegmentTable (segment_id, subscriber_id, status, created_at) |
| 77 | SELECT :duplicateId, subscriber_id, status, NOW() |
| 78 | FROM $subscriberSegmentTable |
| 79 | WHERE segment_id = :segmentId |
| 80 | "); |
| 81 | $stmt->bindValue('duplicateId', $duplicate->getId()); |
| 82 | $stmt->bindValue('segmentId', $segmentEntity->getId()); |
| 83 | $stmt->executeQuery(); |
| 84 | }); |
| 85 | |
| 86 | // The bulk INSERT above copies subscribed memberships from the original |
| 87 | // segment. Only subscribed members gain a new counted membership, so only |
| 88 | // they need their segments_count refreshed. |
| 89 | $this->segmentsCountRecalculator->recalculateForSegment((int)$duplicate->getId()); |
| 90 | |
| 91 | return $duplicate; |
| 92 | } |
| 93 | } |
| 94 |