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