<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing namespace MailPoet\API\JSON\v1; if (!defined('ABSPATH')) exit; use MailPoet\API\JSON\Endpoint as APIEndpoint; use MailPoet\API\JSON\Error as APIError; use MailPoet\API\JSON\ResponseBuilders\NewsletterTemplatesResponseBuilder; use MailPoet\Config\AccessControl; use MailPoet\Newsletter\ApiDataSanitizer; use MailPoet\Newsletter\NewsletterCoupon; use MailPoet\Newsletter\NewslettersRepository; use MailPoet\NewsletterTemplates\NewsletterTemplatesRepository; use MailPoet\NewsletterTemplates\ThumbnailSaver; class NewsletterTemplates extends APIEndpoint { public $permissions = [ 'global' => AccessControl::PERMISSION_MANAGE_EMAILS, ]; protected static $getMethods = [ 'getAll', ]; /** @var NewsletterTemplatesRepository */ private $newsletterTemplatesRepository; /** @var NewsletterTemplatesResponseBuilder */ private $newsletterTemplatesResponseBuilder; /** @var ThumbnailSaver */ private $thumbnailImageSaver; /** @var ApiDataSanitizer */ private $apiDataSanitizer; /** @var NewslettersRepository */ private $newsletterRepository; /*** @var NewsletterCoupon */ private $newsletterCoupon; public function __construct( NewsletterTemplatesRepository $newsletterTemplatesRepository, NewsletterTemplatesResponseBuilder $newsletterTemplatesResponseBuilder, ThumbnailSaver $thumbnailImageSaver, ApiDataSanitizer $apiDataSanitizer, NewslettersRepository $newsletterRepository, NewsletterCoupon $newsletterCoupon ) { $this->newsletterTemplatesRepository = $newsletterTemplatesRepository; $this->newsletterTemplatesResponseBuilder = $newsletterTemplatesResponseBuilder; $this->thumbnailImageSaver = $thumbnailImageSaver; $this->apiDataSanitizer = $apiDataSanitizer; $this->newsletterRepository = $newsletterRepository; $this->newsletterCoupon = $newsletterCoupon; } public function get($data = []) { $template = isset($data['id']) ? $this->newsletterTemplatesRepository->findOneById((int)$data['id']) : null; if (!$template) { return $this->errorResponse([ APIError::NOT_FOUND => __('This template does not exist.', 'mailpoet'), ]); } $data = $this->newsletterTemplatesResponseBuilder->build($template); return $this->successResponse($data); } public function getAll() { $templates = $this->newsletterTemplatesRepository->findAllForListing(); $data = $this->newsletterTemplatesResponseBuilder->buildForListing($templates); return $this->successResponse($data); } public function save($data = []) { ignore_user_abort(true); // Do not save templates for emails created via Gutenberg editor $newsletterId = isset($data['newsletter_id']) ? (int)$data['newsletter_id'] : null; if ($newsletterId) { $newsletter = $this->newsletterRepository->findOneById($newsletterId); if ($newsletter && $newsletter->getWpPostId() !== null) { return $this->successResponse($data); } } if (!empty($data['body'])) { $body = $this->apiDataSanitizer->sanitizeBody(json_decode($data['body'], true)); $body = $this->newsletterCoupon->cleanupBodySensitiveData($body); $data['body'] = json_encode($body); } try { $template = $this->newsletterTemplatesRepository->createOrUpdate($data); $template = $this->thumbnailImageSaver->ensureTemplateThumbnailFile($template); if (!empty($data['categories']) && $data['categories'] === NewsletterTemplatesRepository::RECENTLY_SENT_CATEGORIES) { $this->newsletterTemplatesRepository->cleanRecentlySent(); } $data = $this->newsletterTemplatesResponseBuilder->build($template); return $this->successResponse($data); } catch (\Throwable $e) { return $this->errorResponse(); } } public function delete($data = []) { $template = isset($data['id']) ? $this->newsletterTemplatesRepository->findOneById((int)$data['id']) : null; if (!$template) { return $this->errorResponse([ APIError::NOT_FOUND => __('This template does not exist.', 'mailpoet'), ]); } $this->newsletterTemplatesRepository->remove($template); $this->newsletterTemplatesRepository->flush(); return $this->successResponse(null, ['count' => 1]); } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
Analytics.php | File | 668 B | 0644 |
|
AutomatedLatestContent.php | File | 3.98 KB | 0644 |
|
AutomaticEmails.php | File | 2.83 KB | 0644 |
|
Captcha.php | File | 951 B | 0644 |
|
Coupons.php | File | 1.8 KB | 0644 |
|
CustomFields.php | File | 2.98 KB | 0644 |
|
DynamicSegments.php | File | 11.93 KB | 0644 |
|
FeatureFlags.php | File | 1.42 KB | 0644 |
|
Forms.php | File | 11.3 KB | 0644 |
|
Help.php | File | 2.8 KB | 0644 |
|
ImportExport.php | File | 5.76 KB | 0644 |
|
Mailer.php | File | 3.1 KB | 0644 |
|
NewsletterLinks.php | File | 1.01 KB | 0644 |
|
NewsletterTemplates.php | File | 4.29 KB | 0644 |
|
Newsletters.php | File | 16.01 KB | 0644 |
|
Premium.php | File | 3.03 KB | 0644 |
|
RedirectResponse.php | File | 347 B | 0644 |
|
Segments.php | File | 12.19 KB | 0644 |
|
SendingQueue.php | File | 9.93 KB | 0644 |
|
SendingTaskSubscribers.php | File | 5.42 KB | 0644 |
|
Services.php | File | 12.35 KB | 0644 |
|
Settings.php | File | 16.49 KB | 0644 |
|
Setup.php | File | 1.19 KB | 0644 |
|
SubscriberStats.php | File | 4.08 KB | 0644 |
|
Subscribers.php | File | 12.31 KB | 0644 |
|
Tags.php | File | 1.34 KB | 0644 |
|
UserFlags.php | File | 1.01 KB | 0644 |
|
WoocommerceSettings.php | File | 870 B | 0644 |
|
index.php | File | 6 B | 0644 |
|