Editor
2 months ago
Embed
1 month ago
Links
2 months ago
Listing
9 months ago
Options
2 years ago
Preview
1 month ago
Renderer
5 days ago
RestApi
1 week ago
Scheduler
5 days ago
Segment
1 year ago
Sending
1 week ago
Sharing
1 month ago
Shortcodes
4 days ago
Statistics
1 week ago
ViewInBrowser
1 month ago
ApiDataSanitizer.php
3 years ago
AutomatedLatestContent.php
2 months ago
AutomaticEmailsRepository.php
3 years ago
BlockPostQuery.php
2 months ago
BulkActionController.php
2 months ago
BulkActionException.php
2 months ago
DynamicProducts.php
11 months ago
NewsletterCoupon.php
2 months ago
NewsletterDeleteController.php
2 years ago
NewsletterHtmlSanitizer.php
2 years ago
NewsletterPostsRepository.php
2 years ago
NewsletterResendController.php
5 days ago
NewsletterSaveController.php
1 month ago
NewsletterValidator.php
1 year ago
NewslettersRepository.php
2 weeks ago
StatusController.php
2 months ago
Url.php
2 months ago
index.php
3 years ago
BulkActionController.php
90 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Newsletter; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Listing\ListingDefinition; |
| 9 | use MailPoet\Newsletter\Listing\NewsletterListingRepository; |
| 10 | use MailPoet\Newsletter\NewslettersRepository; |
| 11 | use MailPoet\WP\Functions as WPFunctions; |
| 12 | |
| 13 | /** |
| 14 | * Centralized newsletter bulk-action dispatcher used by the REST endpoint at |
| 15 | * `mailpoet/v1/newsletters/bulk-action`. The `export_stats` action is handled |
| 16 | * by the REST endpoint directly because it is asynchronous and premium-gated; |
| 17 | * this controller covers the synchronous destructive actions. |
| 18 | */ |
| 19 | class BulkActionController { |
| 20 | public const ACTION_TRASH = 'trash'; |
| 21 | public const ACTION_RESTORE = 'restore'; |
| 22 | public const ACTION_DELETE = 'delete'; |
| 23 | |
| 24 | public const SUPPORTED_ACTIONS = [ |
| 25 | self::ACTION_TRASH, |
| 26 | self::ACTION_RESTORE, |
| 27 | self::ACTION_DELETE, |
| 28 | ]; |
| 29 | |
| 30 | /** @var NewsletterListingRepository */ |
| 31 | private $newsletterListingRepository; |
| 32 | |
| 33 | /** @var NewslettersRepository */ |
| 34 | private $newslettersRepository; |
| 35 | |
| 36 | /** @var NewsletterDeleteController */ |
| 37 | private $newsletterDeleteController; |
| 38 | |
| 39 | /** @var WPFunctions */ |
| 40 | private $wp; |
| 41 | |
| 42 | public function __construct( |
| 43 | NewsletterListingRepository $newsletterListingRepository, |
| 44 | NewslettersRepository $newslettersRepository, |
| 45 | NewsletterDeleteController $newsletterDeleteController, |
| 46 | WPFunctions $wp |
| 47 | ) { |
| 48 | $this->newsletterListingRepository = $newsletterListingRepository; |
| 49 | $this->newslettersRepository = $newslettersRepository; |
| 50 | $this->newsletterDeleteController = $newsletterDeleteController; |
| 51 | $this->wp = $wp; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @return array{action: string, count: int, ids: int[]} |
| 56 | * @throws BulkActionException |
| 57 | */ |
| 58 | public function execute(string $action, ListingDefinition $definition): array { |
| 59 | if (!in_array($action, self::SUPPORTED_ACTIONS, true)) { |
| 60 | throw new BulkActionException( |
| 61 | // translators: %s is the offending bulk-action name. |
| 62 | sprintf(__("Invalid bulk action '%s' provided.", 'mailpoet'), $action), |
| 63 | 'mailpoet_newsletters_invalid_bulk_action', |
| 64 | 400 |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | $ids = array_values(array_map('intval', $this->newsletterListingRepository->getActionableIds($definition))); |
| 69 | |
| 70 | if ($action === self::ACTION_TRASH) { |
| 71 | $this->newslettersRepository->bulkTrash($ids); |
| 72 | } elseif ($action === self::ACTION_RESTORE) { |
| 73 | $this->newslettersRepository->bulkRestore($ids); |
| 74 | } elseif ($action === self::ACTION_DELETE) { |
| 75 | // Hooks fire around the cascading delete so premium add-ons (and any |
| 76 | // third-party listeners) keep observing the same lifecycle they did |
| 77 | // under the legacy JSON endpoint. |
| 78 | $this->wp->doAction('mailpoet_api_newsletters_delete_before', $ids); |
| 79 | $this->newsletterDeleteController->bulkDelete($ids); |
| 80 | $this->wp->doAction('mailpoet_api_newsletters_delete_after', $ids); |
| 81 | } |
| 82 | |
| 83 | return [ |
| 84 | 'action' => $action, |
| 85 | 'count' => count($ids), |
| 86 | 'ids' => $ids, |
| 87 | ]; |
| 88 | } |
| 89 | } |
| 90 |