AppsService.php
3 weeks ago
CollaborationCommentService.php
3 weeks ago
CollaborationService.php
3 weeks ago
CollectionItemService.php
3 weeks ago
CollectionService.php
3 weeks ago
EditorService.php
3 weeks ago
FontService.php
3 weeks ago
MediaService.php
3 weeks ago
PageService.php
3 weeks ago
PageSettingsService.php
3 weeks ago
UtilityPageService.php
3 weeks ago
MediaService.php
53 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Services; |
| 4 | |
| 5 | use Kirki\App\Constants\PostStatus; |
| 6 | use Kirki\App\Constants\PostTypes; |
| 7 | use function Kirki\Framework\config; |
| 8 | |
| 9 | defined('ABSPATH') || exit; |
| 10 | |
| 11 | use Kirki\App\DTO\Media\MediaListFilterDTO; |
| 12 | use Kirki\App\Models\Media; |
| 13 | |
| 14 | class MediaService |
| 15 | { |
| 16 | /** |
| 17 | * Paginate media items. |
| 18 | * |
| 19 | * @param MediaListFilterDTO $dto |
| 20 | * @return \Kirki\Framework\Database\Query\Paginator |
| 21 | */ |
| 22 | public function paginated(MediaListFilterDTO $dto) |
| 23 | { |
| 24 | $supported_media_types = config('media.supports'); |
| 25 | |
| 26 | $query = Media::query(); |
| 27 | |
| 28 | if (!empty($dto->category)) { |
| 29 | $categories = explode(',', $dto->category); |
| 30 | $mime_types = []; |
| 31 | |
| 32 | foreach ($categories as $category) { |
| 33 | $category = trim($category); |
| 34 | |
| 35 | if (isset($supported_media_types[$category])) { |
| 36 | $mime_types = array_merge($mime_types, $supported_media_types[$category]); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | if (!empty($mime_types)) { |
| 41 | $query->where_in('post_mime_type', $mime_types); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | if (!empty($dto->search)) { |
| 46 | $query->search($dto->search); |
| 47 | } |
| 48 | |
| 49 | $query->order_by($dto->sort_by, $dto->sort_order); |
| 50 | |
| 51 | return $query->with('meta')->paginate($dto->limit, $dto->page); |
| 52 | } |
| 53 | } |