FormsBulkActionEndpoint.php
2 months ago
FormsEndpoint.php
2 months ago
FormsListingEndpoint.php
1 month ago
index.php
2 months ago
FormsBulkActionEndpoint.php
90 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Form\RestApi\Endpoints; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\REST\ApiException; |
| 9 | use MailPoet\API\REST\Request; |
| 10 | use MailPoet\API\REST\Response; |
| 11 | use MailPoet\Form\FormsRepository; |
| 12 | use MailPoet\Validator\Builder; |
| 13 | |
| 14 | class FormsBulkActionEndpoint extends FormsEndpoint { |
| 15 | private const ACTION_TRASH = 'trash'; |
| 16 | private const ACTION_RESTORE = 'restore'; |
| 17 | private const ACTION_DELETE = 'delete'; |
| 18 | |
| 19 | private const SUPPORTED_ACTIONS = [ |
| 20 | self::ACTION_TRASH, |
| 21 | self::ACTION_RESTORE, |
| 22 | self::ACTION_DELETE, |
| 23 | ]; |
| 24 | |
| 25 | /** @var FormsRepository */ |
| 26 | private $formsRepository; |
| 27 | |
| 28 | public function __construct( |
| 29 | FormsRepository $formsRepository |
| 30 | ) { |
| 31 | $this->formsRepository = $formsRepository; |
| 32 | } |
| 33 | |
| 34 | public function handle(Request $request): Response { |
| 35 | $action = is_string($request->getParam('action')) ? (string)$request->getParam('action') : ''; |
| 36 | if (!in_array($action, self::SUPPORTED_ACTIONS, true)) { |
| 37 | throw new ApiException( |
| 38 | // translators: %s is the list of supported bulk actions. |
| 39 | sprintf(__('Unsupported bulk action. Allowed values are: %s.', 'mailpoet'), implode(', ', self::SUPPORTED_ACTIONS)), |
| 40 | 400, |
| 41 | 'mailpoet_forms_invalid_bulk_action' |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | $rawIds = $request->getParam('ids'); |
| 46 | if (!is_array($rawIds) || $rawIds === []) { |
| 47 | throw new ApiException( |
| 48 | __('At least one form id is required.', 'mailpoet'), |
| 49 | 400, |
| 50 | 'mailpoet_forms_ids_required' |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | $ids = array_values(array_filter( |
| 55 | array_map(static fn($id): int => is_scalar($id) ? (int)$id : 0, $rawIds), |
| 56 | static function (int $id): bool { |
| 57 | return $id > 0; |
| 58 | } |
| 59 | )); |
| 60 | if ($ids === []) { |
| 61 | throw new ApiException( |
| 62 | __('At least one form id is required.', 'mailpoet'), |
| 63 | 400, |
| 64 | 'mailpoet_forms_ids_required' |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | $count = 0; |
| 69 | if ($action === self::ACTION_TRASH) { |
| 70 | $count = $this->formsRepository->bulkTrash($ids); |
| 71 | } elseif ($action === self::ACTION_RESTORE) { |
| 72 | $count = $this->formsRepository->bulkRestore($ids); |
| 73 | } elseif ($action === self::ACTION_DELETE) { |
| 74 | $count = $this->formsRepository->bulkDelete($ids); |
| 75 | } |
| 76 | |
| 77 | return new Response([ |
| 78 | 'action' => $action, |
| 79 | 'count' => $count, |
| 80 | ]); |
| 81 | } |
| 82 | |
| 83 | public static function getRequestSchema(): array { |
| 84 | return [ |
| 85 | 'action' => Builder::string()->required(), |
| 86 | 'ids' => Builder::array(Builder::integer())->required(), |
| 87 | ]; |
| 88 | } |
| 89 | } |
| 90 |