Analytics.php
3 years ago
AutomatedLatestContent.php
2 months ago
AutomaticEmails.php
3 years ago
Captcha.php
1 year ago
Coupons.php
2 years ago
CustomFields.php
2 months ago
DynamicProducts.php
1 year ago
DynamicSegments.php
2 months ago
FeatureFlags.php
3 years ago
Forms.php
2 months ago
Help.php
1 year ago
ImportExport.php
2 months ago
Mailer.php
1 year ago
NewsletterLinks.php
2 months ago
NewsletterTemplates.php
2 months ago
Newsletters.php
2 months ago
Premium.php
10 months ago
RedirectResponse.php
1 year ago
Segments.php
2 months ago
SendingQueue.php
2 months ago
Services.php
6 months ago
Settings.php
2 months ago
Setup.php
1 year ago
StatisticsExport.php
3 months ago
SubscriberStats.php
1 month ago
Subscribers.php
2 months ago
Tags.php
3 years ago
UserFlags.php
2 years ago
WoocommerceProductVariations.php
2 months ago
WoocommerceSettings.php
3 years ago
index.php
3 years ago
Help.php
89 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\API\JSON\v1; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\JSON\Endpoint as APIEndpoint; |
| 9 | use MailPoet\API\JSON\Error as APIError; |
| 10 | use MailPoet\API\JSON\Response; |
| 11 | use MailPoet\Config\AccessControl; |
| 12 | use MailPoet\Entities\ScheduledTaskEntity; |
| 13 | use MailPoet\Newsletter\Sending\ScheduledTasksRepository; |
| 14 | use MailPoet\Util\DataInconsistency\DataInconsistencyController; |
| 15 | |
| 16 | class Help extends APIEndpoint { |
| 17 | |
| 18 | public $permissions = [ |
| 19 | 'global' => AccessControl::PERMISSION_MANAGE_HELP, |
| 20 | ]; |
| 21 | |
| 22 | private ScheduledTasksRepository $scheduledTasksRepository; |
| 23 | private DataInconsistencyController $dataInconsistencyController; |
| 24 | |
| 25 | public function __construct( |
| 26 | ScheduledTasksRepository $scheduledTasksRepository, |
| 27 | DataInconsistencyController $dataInconsistencyController |
| 28 | ) { |
| 29 | $this->scheduledTasksRepository = $scheduledTasksRepository; |
| 30 | $this->dataInconsistencyController = $dataInconsistencyController; |
| 31 | } |
| 32 | |
| 33 | public function cancelTask($data): Response { |
| 34 | try { |
| 35 | $this->validateTaskId($data); |
| 36 | |
| 37 | $task = $this->scheduledTasksRepository->findOneById($data['id']); |
| 38 | if (!$task instanceof ScheduledTaskEntity) { |
| 39 | return $this->errorResponse([ |
| 40 | APIError::NOT_FOUND => __('Task not found.', 'mailpoet'), |
| 41 | ]); |
| 42 | } |
| 43 | |
| 44 | $this->scheduledTasksRepository->cancelTask($task); |
| 45 | return $this->successResponse(); |
| 46 | } catch (\Exception $e) { |
| 47 | return $this->badRequest([ApiError::BAD_REQUEST => $e->getMessage()]); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public function rescheduleTask($data): Response { |
| 52 | try { |
| 53 | $this->validateTaskId($data); |
| 54 | |
| 55 | $task = $this->scheduledTasksRepository->findOneById($data['id']); |
| 56 | if (!$task instanceof ScheduledTaskEntity) { |
| 57 | return $this->errorResponse([ |
| 58 | APIError::NOT_FOUND => __('Task not found.', 'mailpoet'), |
| 59 | ]); |
| 60 | } |
| 61 | |
| 62 | $this->scheduledTasksRepository->rescheduleTask($task); |
| 63 | return $this->successResponse(); |
| 64 | } catch (\Exception $e) { |
| 65 | return $this->badRequest([ApiError::BAD_REQUEST => $e->getMessage()]); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public function getInconsistentDataStatus(): Response { |
| 70 | return $this->successResponse($this->dataInconsistencyController->getInconsistentDataStatus()); |
| 71 | } |
| 72 | |
| 73 | public function fixInconsistentData($data): Response { |
| 74 | try { |
| 75 | $this->dataInconsistencyController->fixInconsistentData($data['inconsistency'] ?? ''); |
| 76 | } catch (\Exception $e) { |
| 77 | return $this->badRequest([ApiError::BAD_REQUEST => $e->getMessage()]); |
| 78 | } |
| 79 | return $this->successResponse($this->dataInconsistencyController->getInconsistentDataStatus()); |
| 80 | } |
| 81 | |
| 82 | private function validateTaskId($data): void { |
| 83 | $isValid = isset($data['id']) && is_numeric($data['id']); |
| 84 | if (!$isValid) { |
| 85 | throw new \Exception(__('Invalid or missing parameter `id`.', 'mailpoet')); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 |