Sync post and newsletter

This commit is contained in:
Mike Jolley
2024-04-03 15:19:45 +01:00
committed by Aschepikov
parent 43752a7e39
commit f6a793d2e8
3 changed files with 41 additions and 0 deletions

View File

@@ -54,6 +54,17 @@ class EmailApiController {
$this->newsletterRepository->flush(); $this->newsletterRepository->flush();
} }
public function trashEmail(\WP_Post $wpPost) {
$newsletter = $this->newsletterRepository->findOneBy(['wpPost' => $wpPost->ID]);
if (!$newsletter) {
throw new NotFoundException('Newsletter was not found');
}
if ($newsletter->getWpPostId() !== $wpPost->ID) {
throw new UnexpectedValueException('Newsletter ID does not match the post ID');
}
$this->newsletterRepository->bulkTrash([$newsletter->getId()]);
}
public function getEmailDataSchema(): array { public function getEmailDataSchema(): array {
return Builder::object([ return Builder::object([
'id' => Builder::integer()->nullable(), 'id' => Builder::integer()->nullable(),

View File

@@ -34,6 +34,7 @@ class EmailEditor {
} }
$this->cli->initialize(); $this->cli->initialize();
$this->wp->addFilter('mailpoet_email_editor_post_types', [$this, 'addEmailPostType']); $this->wp->addFilter('mailpoet_email_editor_post_types', [$this, 'addEmailPostType']);
$this->wp->addAction('rest_delete_mailpoet_email', [$this->emailApiController, 'trashEmail'], 10, 1);
$this->extendEmailPostApi(); $this->extendEmailPostApi();
} }

View File

@@ -312,6 +312,13 @@ class NewslettersRepository extends Repository {
WHERE q.`newsletter_id` IN (:ids) WHERE q.`newsletter_id` IN (:ids)
", ['ids' => $ids], ['ids' => Connection::PARAM_INT_ARRAY]); ", ['ids' => $ids], ['ids' => Connection::PARAM_INT_ARRAY]);
// Trash CPT.
$wpPostIds = $this->getWpPostIds($ids);
foreach ($wpPostIds as $wpPostId) {
wp_trash_post($wpPostId);
}
return count($ids); return count($ids);
} }
@@ -352,6 +359,12 @@ class NewslettersRepository extends Repository {
WHERE q.`newsletter_id` IN (:ids) WHERE q.`newsletter_id` IN (:ids)
", ['ids' => $ids], ['ids' => Connection::PARAM_INT_ARRAY]); ", ['ids' => $ids], ['ids' => Connection::PARAM_INT_ARRAY]);
// Untrash CPT.
$wpPostIds = $this->getWpPostIds($ids);
foreach ($wpPostIds as $wpPostId) {
wp_untrash_post($wpPostId);
}
return count($ids); return count($ids);
} }
@@ -556,4 +569,20 @@ class NewslettersRepository extends Repository {
->getSingleColumnResult(); ->getSingleColumnResult();
return array_map('intval', $ids); return array_map('intval', $ids);
} }
public function getWpPostIds(array $ids): array {
/** @var string[] $wpPostIds */
$wpPostIds = $this->entityManager->createQueryBuilder()
->select('IDENTITY(n.wpPost) AS id')
->from(NewsletterEntity::class, 'n')
->where('n.id IN (:ids)')
->andWhere('n.wpPost IS NOT NULL')
->setParameter('ids', $ids)
->getQuery()
->getSingleColumnResult();
$wpPostIds = array_map('intval', $wpPostIds);
return $wpPostIds;
}
} }