| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace FL\Assistant\Controllers; |
| 4 | 4 | |
| 5 | 5 | use FL\Assistant\Data\Repository\AttachmentsRepository; |
| 6 | +use FL\Assistant\Data\Repository\LabelsRepository; | |
| 6 | 7 | use FL\Assistant\Data\Transformers\AttachmentTransformer; |
| 7 | 8 | use FL\Assistant\System\Contracts\ControllerAbstract; |
| 8 | 9 | use WP_REST_Request; |
| 9 | 10 | use WP_REST_Response; |
| @@ -26,16 +27,26 @@ | ||
| 26 | 27 | */ |
| 27 | 28 | protected $transformer; |
| 28 | 29 | |
| 29 | 30 | /** |
| 31 | + * @var LabelsRepository | |
| 32 | + */ | |
| 33 | + protected $labels; | |
| 34 | + | |
| 35 | + /** | |
| 30 | 36 | * AttachmentsController constructor. |
| 31 | 37 | * |
| 32 | 38 | * @param AttachmentsRepository $attachments |
| 33 | 39 | * @param AttachmentTransformer $transformer |
| 34 | 40 | */ |
| 35 | - public function __construct( AttachmentsRepository $attachments, AttachmentTransformer $transformer ) { | |
| 41 | + public function __construct( | |
| 42 | + AttachmentsRepository $attachments, | |
| 43 | + AttachmentTransformer $transformer, | |
| 44 | + LabelsRepository $labels | |
| 45 | + ) { | |
| 36 | 46 | $this->attachments = $attachments; |
| 37 | 47 | $this->transformer = $transformer; |
| 48 | + $this->labels = $labels; | |
| 38 | 49 | } |
| 39 | 50 | |
| 40 | 51 | /** |
| 41 | 52 | * Register routes. |
| @@ -218,8 +229,15 @@ | ||
| 218 | 229 | break; |
| 219 | 230 | case 'all': |
| 220 | 231 | $args['post_mime_type'] = ''; |
| 221 | 232 | break; |
| 233 | + } | |
| 234 | + | |
| 235 | + if ( isset( $args['label'] ) && $args['label'] ) { | |
| 236 | + $args['post__in'] = $this->labels->get_object_ids( 'attachment', $args['label'] ); | |
| 237 | + if ( ! count( $args['post__in'] ) ) { | |
| 238 | + $args['post__in'][] = -1; // post__in returns all posts if empty. | |
| 239 | + } | |
| 222 | 240 | } |
| 223 | 241 | |
| 224 | 242 | return $this->attachments->paginate( $args ) |
| 225 | 243 | ->apply_transform( $this->transformer ) |