LogsDeleteEndpoint.php
83 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Logging\RestApi\Endpoints; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\REST\ApiException; |
| 9 | use MailPoet\API\REST\Endpoint; |
| 10 | use MailPoet\API\REST\ListingRequestValidationTrait; |
| 11 | use MailPoet\API\REST\Request; |
| 12 | use MailPoet\API\REST\Response; |
| 13 | use MailPoet\Config\AccessControl; |
| 14 | use MailPoet\Logging\LogRepository; |
| 15 | use MailPoet\Logging\RestApi\LogsFilterTrait; |
| 16 | use MailPoet\Validator\Builder; |
| 17 | use MailPoet\WP\Functions as WPFunctions; |
| 18 | |
| 19 | class LogsDeleteEndpoint extends Endpoint { |
| 20 | use ListingRequestValidationTrait; |
| 21 | use LogsFilterTrait; |
| 22 | |
| 23 | // Referenced by ListingRequestValidationTrait's pagination helpers, which this |
| 24 | // endpoint composes for filter/date validation but never calls (it does not |
| 25 | // paginate). Values mirror AbstractListingEndpoint. |
| 26 | private const MAX_PER_PAGE = 100; |
| 27 | private const MAX_PAGE = 100000; |
| 28 | |
| 29 | /** @var LogRepository */ |
| 30 | private $logRepository; |
| 31 | |
| 32 | public function __construct( |
| 33 | LogRepository $logRepository |
| 34 | ) { |
| 35 | $this->logRepository = $logRepository; |
| 36 | } |
| 37 | |
| 38 | public function handle(Request $request): Response { |
| 39 | $filter = $this->validateAndNormalizeLogsFilter($request->getParam('filter')); |
| 40 | $search = $this->validateSearch($request->getParam('search')); |
| 41 | |
| 42 | $isUnrestricted = $filter === [] && $search === null; |
| 43 | if ($isUnrestricted && $request->getParam('all') !== true) { |
| 44 | throw new ApiException( |
| 45 | __('Deleting all logs requires confirmation.', 'mailpoet'), |
| 46 | 400, |
| 47 | 'mailpoet_logs_delete_confirmation_required' |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | return new Response([ |
| 52 | 'deleted' => $this->logRepository->deleteLogs($filter, $search), |
| 53 | ]); |
| 54 | } |
| 55 | |
| 56 | public function checkPermissions(): bool { |
| 57 | return WPFunctions::get()->currentUserCan(AccessControl::PERMISSION_ACCESS_PLUGIN_ADMIN); |
| 58 | } |
| 59 | |
| 60 | public static function getRequestSchema(): array { |
| 61 | return [ |
| 62 | 'filter' => Builder::object(), |
| 63 | 'search' => Builder::string(), |
| 64 | 'all' => Builder::boolean(), |
| 65 | ]; |
| 66 | } |
| 67 | |
| 68 | protected function getListingValidationErrorPrefix(): string { |
| 69 | return 'logs'; |
| 70 | } |
| 71 | |
| 72 | /** @param mixed $search */ |
| 73 | private function validateSearch($search): ?string { |
| 74 | if ($search === null || (is_string($search) && trim($search) === '')) { |
| 75 | return null; |
| 76 | } |
| 77 | if (!is_string($search)) { |
| 78 | throw $this->listingValidationError(__('Search must be a string.', 'mailpoet'), 'search'); |
| 79 | } |
| 80 | return $search; |
| 81 | } |
| 82 | } |
| 83 |