PluginProbe
Assistant – Every Day Productivity Apps / 1.4.3
Assistant – Every Day Productivity Apps v1.4.3
1.5.5 trunk 0.1.0 0.2.0 0.2.1 0.2.2 0.3.0 0.3.1 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.6.0 0.7.0 0.7.0.2 0.7.0.3 0.7.0.4 0.7.0.5 0.7.0.6 0.7.0.7 0.7.0.8 0.7.0.9 0.7.1 1.0 All 71 releases
assistant / backend / src / Controllers / AttachmentsController.php

AttachmentsController.php in Assistant – Every Day Productivity Apps 1.4.3, at backend/src/Controllers/AttachmentsController.php

369 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FL\Assistant\Controllers;
4
5 use FL\Assistant\Data\Repository\AttachmentsRepository;
6 use FL\Assistant\Data\Repository\LabelsRepository;
7 use FL\Assistant\Data\Transformers\AttachmentTransformer;
8 use FL\Assistant\System\Contracts\ControllerAbstract;
9 use WP_REST_Request;
10 use WP_REST_Response;
11 use WP_REST_Server;
12
13
14 /**
15 * REST API logic for attachments.
16 */
17 class AttachmentsController extends ControllerAbstract {
18
19
20 /**
21 * @var AttachmentsRepository
22 */
23 protected $attachments;
24
25 /**
26 * @var AttachmentTransformer|callable
27 */
28 protected $transformer;
29
30 /**
31 * @var LabelsRepository
32 */
33 protected $labels;
34
35 /**
36 * AttachmentsController constructor.
37 *
38 * @param AttachmentsRepository $attachments
39 * @param AttachmentTransformer $transformer
40 */
41 public function __construct(
42 AttachmentsRepository $attachments,
43 AttachmentTransformer $transformer,
44 LabelsRepository $labels
45 ) {
46 $this->attachments = $attachments;
47 $this->transformer = $transformer;
48 $this->labels = $labels;
49 }
50
51 /**
52 * Register routes.
53 */
54 public function register_routes() {
55 $this->route(
56 '/attachments',
57 [
58 [
59 'methods' => WP_REST_Server::READABLE,
60 'callback' => [ $this, 'index' ],
61 'permission_callback' => function () {
62 return current_user_can( 'edit_others_posts' );
63 },
64 ],
65
66 ]
67 );
68
69 $this->route(
70 '/attachments/upload',
71 [
72 [
73 'methods' => WP_REST_Server::CREATABLE,
74 'callback' => [ $this, 'upload_media' ],
75 'permission_callback' => function () {
76 return current_user_can( 'edit_others_posts' );
77 },
78 ],
79 ]
80 );
81
82 $this->route(
83 '/attachments/count',
84 [
85 [
86 'methods' => WP_REST_Server::READABLE,
87 'callback' => [ $this, 'count' ],
88 'permission_callback' => function () {
89 return current_user_can( 'edit_others_posts' );
90 },
91 ],
92 ]
93 );
94
95 $this->route(
96 '/attachments/(?P<id>\d+)',
97 [
98 [
99 'methods' => WP_REST_Server::READABLE,
100 'callback' => [ $this, 'find' ],
101 'args' => [
102 'id' => [
103 'required' => true,
104 'type' => 'number',
105 ],
106 ],
107 'permission_callback' => function () {
108 return current_user_can( 'edit_others_posts' );
109 },
110 ],
111 [
112 'methods' => WP_REST_Server::CREATABLE,
113 'callback' => [ $this, 'update' ],
114 'args' => [
115 'id' => [
116 'required' => true,
117 'type' => 'number',
118 ],
119 'action' => [
120 'required' => true,
121 'type' => 'string',
122 ],
123 ],
124 'permission_callback' => function () {
125 return current_user_can( 'edit_others_posts' );
126 },
127 ],
128 [
129 'methods' => WP_REST_Server::DELETABLE,
130 'callback' => [ $this, 'delete' ],
131 'args' => [
132 'id' => [
133 'required' => true,
134 'type' => 'number',
135 ],
136 ],
137 'permission_callback' => function () {
138 return current_user_can( 'edit_others_posts' );
139 },
140 ],
141 ]
142 );
143
144 $this->route(
145 '/attachments/restore/(?P<id>\d+)',
146 [
147 [
148 'methods' => WP_REST_Server::EDITABLE,
149 'callback' => [ $this, 'restore' ],
150 'args' => [
151 'id' => [
152 'required' => true,
153 'type' => 'number',
154 ],
155 ],
156 'permission_callback' => function () {
157 return current_user_can( 'edit_others_posts' );
158 },
159 ],
160 ]
161 );
162 }
163
164 /**
165 * Returns an array of counts by attachment type.
166 *
167 * @param WP_REST_Request $request
168 *
169 * @return mixed|WP_REST_Response
170 */
171 public function count( WP_REST_Request $request ) {
172 $counts = wp_count_attachments();
173 $response = [];
174
175 foreach ( $counts as $type => $count ) {
176 $parts = explode( '/', $type );
177 $type = array_shift( $parts );
178
179 if ( isset( $response[ $type ] ) ) {
180 $response[ $type ] += $count;
181 } else {
182 $response[ $type ] = $count;
183 }
184 }
185
186 return rest_ensure_response( $response );
187 }
188
189 /**
190 * Returns an array of attachments and related data.
191 *
192 * @param WP_REST_Request $request
193 *
194 * @return mixed|WP_REST_Response
195 */
196 public function index( WP_REST_Request $request ) {
197
198 $args = $request->get_params();
199 $type = isset( $args['post_mime_type'] ) ? $args['post_mime_type'] : 'all';
200
201 switch ( $type ) {
202 case 'document':
203 $all_mimes = get_allowed_mime_types();
204 $doc_mimes = [];
205 $exclude = [ 'image', 'video', 'audio' ];
206
207 foreach ( $all_mimes as $key => $mime ) {
208 $parts = explode( '/', $mime );
209 if ( in_array( $parts[0], $exclude, true ) ) {
210 continue;
211 }
212 $doc_mimes[ $key ] = $mime;
213 }
214
215 $args['post_mime_type'] = $doc_mimes;
216 break;
217 case 'spreadsheets':
218 $args['post_mime_type'] = 'application/vnd.apple.numbers,application/vnd.oasis.opendocument.spreadsheet,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel.sheet.macroEnabled.12,application/vnd.ms-excel.sheet.binary.macroEnabled.12';
219 break;
220 case 'archives':
221 $args['post_mime_type'] = 'application/x-gzip,application/rar,application/x-tar,application/zip,application/x-7z-compressed';
222 break;
223 case 'doc':
224 $args['post_mime_type'] = 'application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-word.document.macroEnabled.12,application/vnd.ms-word.template.macroEnabled.12,application/vnd.oasis.opendocument.text,application/vnd.apple.pages,application/pdf,application/vnd.ms-xpsdocument,application/oxps,application/rtf,application/wordperfect,application/octet-stream';
225 break;
226 case 'mine':
227 $args['post_author'] = 1;
228 $args['post_mime_type'] = '';
229 break;
230 case 'all':
231 $args['post_mime_type'] = '';
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 }
240 }
241
242 return $this->attachments->paginate( $args )
243 ->apply_transform( $this->transformer )
244 ->to_rest_response();
245 }
246
247
248 /**
249 * Returns data for a single attachment.
250 *
251 * @param WP_REST_Request $request
252 *
253 * @return array|mixed
254 */
255 public function find( WP_REST_Request $request ) {
256 $id = $request->get_param( 'id' );
257
258 if ( current_user_can( 'edit_post', $id ) ) {
259 return $this->attachments->find( $id, $this->transformer );
260 }
261
262 // @todo: implement this on the client side.
263 // return new \WP_REST_Response(["message" => "Resource not found"], 404);
264
265 return [];
266 }
267
268
269 /**
270 * Updates a single attachment based on the specified action.
271 *
272 * @param WP_REST_Request $request
273 *
274 * @return mixed|WP_REST_Response
275 */
276 public function update( WP_REST_Request $request ) {
277 $id = $request->get_param( 'id' );
278 $action = $request->get_param( 'action' );
279
280 if ( ! current_user_can( 'edit_post', $id ) ) {
281 return rest_ensure_response( [ 'error' => true ] );
282 }
283
284 $deprecated = 'This route is deprecated. ';
285 switch ( $action ) {
286 case 'data':
287 $data = (array) $request->get_param( 'data' );
288 if ( isset( $data['meta'] ) ) {
289 if ( isset( $data['meta']['alt'] ) ) {
290 update_post_meta( $id, '_wp_attachment_image_alt', $data['meta']['alt'] );
291 } else {
292 wp_update_post(
293 array_merge(
294 $data,
295 [
296 'ID' => $id,
297 ]
298 )
299 );
300 }
301
302 unset( $data['meta'] );
303 }
304 wp_update_post(
305 array_merge(
306 $data,
307 [
308 'ID' => $id,
309 ]
310 )
311 );
312 break;
313 case 'trash':
314 wp_delete_attachment( $id );
315 $deprecated .= "Please use 'DELETE /attachments/delete/{id}";
316 break;
317 case 'untrash':
318 $deprecated .= "Please use 'POST /attachments/restore/{id}";
319 wp_untrash_post( $id );
320 break;
321 }
322
323 $response = new WP_REST_Response( [ 'success' => true ] );
324
325 $response->header( 'Warn', "299 {$deprecated}" );
326
327 return $response;
328 }
329
330 /**
331 * @param WP_REST_Request $request
332 *
333 * @return WP_REST_Response
334 */
335 public function delete( WP_REST_Request $request ) {
336 $id = $request->get_param( 'id' );
337
338 if ( ! current_user_can( 'edit_post', $id ) ) {
339 return rest_ensure_response( [ 'error' => true ] );
340 }
341
342 $this->attachments->delete( $id );
343
344 return rest_ensure_response( [ 'success' => true ] );
345 }
346
347 /**
348 * @param WP_REST_Request $request
349 *
350 * @return WP_REST_Response
351 */
352 public function restore( WP_REST_Request $request ) {
353 $id = $request->get_param( 'id' );
354
355 if ( ! current_user_can( 'edit_post', $id ) ) {
356 return rest_ensure_response( [ 'error' => true ] );
357 }
358
359 $attachment = $this->attachments->restore( $id, $this->transformer );
360
361 return rest_ensure_response(
362 [
363 'success' => true,
364 'data' => $attachment,
365 ]
366 );
367 }
368 }
369