AbstractSegmentsListingEndpoint.php
1 month ago
DynamicSegmentsBulkActionEndpoint.php
2 months ago
DynamicSegmentsListingEndpoint.php
1 month ago
SegmentRequestValidationTrait.php
2 months ago
SegmentsBulkActionEndpoint.php
2 months ago
SegmentsEndpoint.php
2 months ago
SegmentsListingEndpoint.php
1 month ago
index.php
2 months ago
SegmentsListingEndpoint.php
70 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Segments\RestApi\Endpoints; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\JSON\ResponseBuilders\SegmentsResponseBuilder; |
| 9 | use MailPoet\Config\AccessControl; |
| 10 | use MailPoet\Listing\Handler as ListingHandler; |
| 11 | use MailPoet\Listing\ListingDefinition; |
| 12 | use MailPoet\Listing\ListingRepository; |
| 13 | use MailPoet\Segments\SegmentListingRepository; |
| 14 | use MailPoet\WP\Functions as WPFunctions; |
| 15 | |
| 16 | class SegmentsListingEndpoint extends AbstractSegmentsListingEndpoint { |
| 17 | /** @var SegmentListingRepository */ |
| 18 | private $segmentListingRepository; |
| 19 | |
| 20 | /** @var SegmentsResponseBuilder */ |
| 21 | private $segmentsResponseBuilder; |
| 22 | |
| 23 | public function __construct( |
| 24 | ListingHandler $listingHandler, |
| 25 | SegmentListingRepository $segmentListingRepository, |
| 26 | SegmentsResponseBuilder $segmentsResponseBuilder |
| 27 | ) { |
| 28 | parent::__construct($listingHandler, [ |
| 29 | 'name', |
| 30 | 'created_at', |
| 31 | 'updated_at', |
| 32 | 'average_engagement_score', |
| 33 | ], 20); |
| 34 | $this->segmentListingRepository = $segmentListingRepository; |
| 35 | $this->segmentsResponseBuilder = $segmentsResponseBuilder; |
| 36 | } |
| 37 | |
| 38 | public function checkPermissions(): bool { |
| 39 | return WPFunctions::get()->currentUserCan(AccessControl::PERMISSION_MANAGE_SEGMENTS); |
| 40 | } |
| 41 | |
| 42 | protected function getListingRepository(): ListingRepository { |
| 43 | return $this->segmentListingRepository; |
| 44 | } |
| 45 | |
| 46 | protected function buildItems(array $rows, ListingDefinition $definition): array { |
| 47 | return $this->segmentsResponseBuilder->buildForListing($rows); |
| 48 | } |
| 49 | |
| 50 | protected function getDefaultSortBy(): string { |
| 51 | return 'name'; |
| 52 | } |
| 53 | |
| 54 | protected function getDefaultSortOrder(): string { |
| 55 | return 'asc'; |
| 56 | } |
| 57 | |
| 58 | protected function allowsSearch(): bool { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | protected function getAllowedFilters(): array { |
| 63 | return ['created_from', 'created_to', 'score_min', 'score_max']; |
| 64 | } |
| 65 | |
| 66 | protected function getDefaultParameters(): array { |
| 67 | return ['lists']; |
| 68 | } |
| 69 | } |
| 70 |