@@ -6,9 +6,9 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
6
6
* Determine the allowed query_vars for a get_items() response and
7
7
* prepare for WP_Query.
8
8
*
9
- * @param array $prepared_args
10
- * @param WP_REST_Request $request
11
- * @return array $query_args
9
+ * @param array $prepared_args Optional. Array of prepared arguments.
10
+ * @param WP_REST_Request $request Optional. Request to prepare items for.
11
+ * @return array Array of query arguments.
12
12
*/
13
13
protected function prepare_items_query ( $ prepared_args = array (), $ request = null ) {
14
14
$ query_args = parent ::prepare_items_query ( $ prepared_args , $ request );
@@ -32,7 +32,7 @@ protected function prepare_items_query( $prepared_args = array(), $request = nul
32
32
* Check if a given request has access to create an attachment.
33
33
*
34
34
* @param WP_REST_Request $request Full details about the request.
35
- * @return WP_Error|boolean
35
+ * @return WP_Error|true Boolean true if the attachment may be created, or a WP_Error if not.
36
36
*/
37
37
public function create_item_permissions_check ( $ request ) {
38
38
$ ret = parent ::create_item_permissions_check ( $ request );
@@ -44,7 +44,7 @@ public function create_item_permissions_check( $request ) {
44
44
return new WP_Error ( 'rest_cannot_create ' , __ ( 'Sorry, you are not allowed to upload media on this site. ' ), array ( 'status ' => 400 ) );
45
45
}
46
46
47
- // Attaching media to a post requires ability to edit said post
47
+ // Attaching media to a post requires ability to edit said post.
48
48
if ( ! empty ( $ request ['post ' ] ) ) {
49
49
$ parent = $ this ->get_post ( (int ) $ request ['post ' ] );
50
50
$ post_parent_type = get_post_type_object ( $ parent ->post_type );
@@ -57,10 +57,10 @@ public function create_item_permissions_check( $request ) {
57
57
}
58
58
59
59
/**
60
- * Create a single attachment
60
+ * Create a single attachment.
61
61
*
62
- * @param WP_REST_Request $request Full details about the request
63
- * @return WP_Error|WP_REST_Response
62
+ * @param WP_REST_Request $request Full details about the request.
63
+ * @return WP_Error|WP_REST_Response Response object on success, WP_Error object on failure.
64
64
*/
65
65
public function create_item ( $ request ) {
66
66
@@ -123,7 +123,7 @@ public function create_item( $request ) {
123
123
}
124
124
$ attachment = $ this ->get_post ( $ id );
125
125
126
- /** Include admin functions to get access to wp_generate_attachment_metadata() */
126
+ // Include admin functions to get access to wp_generate_attachment_metadata().
127
127
require_once ABSPATH . 'wp-admin/includes/admin.php ' ;
128
128
129
129
wp_update_attachment_metadata ( $ id , wp_generate_attachment_metadata ( $ id , $ file ) );
@@ -157,10 +157,10 @@ public function create_item( $request ) {
157
157
}
158
158
159
159
/**
160
- * Update a single post
160
+ * Update a single post.
161
161
*
162
- * @param WP_REST_Request $request Full details about the request
163
- * @return WP_Error|WP_REST_Response
162
+ * @param WP_REST_Request $request Full details about the request.
163
+ * @return WP_Error|WP_REST_Response Response object on success, WP_Error object on failure.
164
164
*/
165
165
public function update_item ( $ request ) {
166
166
if ( ! empty ( $ request ['post ' ] ) && in_array ( get_post_type ( $ request ['post ' ] ), array ( 'revision ' , 'attachment ' ), true ) ) {
@@ -196,10 +196,10 @@ public function update_item( $request ) {
196
196
}
197
197
198
198
/**
199
- * Prepare a single attachment for create or update
199
+ * Prepare a single attachment for create or update.
200
200
*
201
- * @param WP_REST_Request $request Request object
202
- * @return WP_Error|stdClass $prepared_attachment Post object
201
+ * @param WP_REST_Request $request Request object.
202
+ * @return WP_Error|stdClass $prepared_attachment Post object.
203
203
*/
204
204
protected function prepare_item_for_database ( $ request ) {
205
205
$ prepared_attachment = parent ::prepare_item_for_database ( $ request );
@@ -220,11 +220,11 @@ protected function prepare_item_for_database( $request ) {
220
220
}
221
221
222
222
/**
223
- * Prepare a single attachment output for response
223
+ * Prepare a single attachment output for response.
224
224
*
225
- * @param WP_Post $post Post object
226
- * @param WP_REST_Request $request Request object
227
- * @return WP_REST_Response $response
225
+ * @param WP_Post $post Post object.
226
+ * @param WP_REST_Request $request Request object.
227
+ * @return WP_REST_Response Response object.
228
228
*/
229
229
public function prepare_item_for_response ( $ post , $ request ) {
230
230
$ response = parent ::prepare_item_for_response ( $ post , $ request );
@@ -239,7 +239,7 @@ public function prepare_item_for_response( $post, $request ) {
239
239
$ data ['post ' ] = ! empty ( $ post ->post_parent ) ? (int ) $ post ->post_parent : null ;
240
240
$ data ['source_url ' ] = wp_get_attachment_url ( $ post ->ID );
241
241
242
- // Ensure empty details is an empty object
242
+ // Ensure empty details is an empty object.
243
243
if ( empty ( $ data ['media_details ' ] ) ) {
244
244
$ data ['media_details ' ] = new stdClass ;
245
245
} elseif ( ! empty ( $ data ['media_details ' ]['sizes ' ] ) ) {
@@ -251,7 +251,7 @@ public function prepare_item_for_response( $post, $request ) {
251
251
unset( $ size_data ['mime-type ' ] );
252
252
}
253
253
254
- // Use the same method image_downsize() does
254
+ // Use the same method image_downsize() does.
255
255
$ image_src = wp_get_attachment_image_src ( $ post ->ID , $ size );
256
256
if ( ! $ image_src ) {
257
257
continue ;
@@ -278,7 +278,7 @@ public function prepare_item_for_response( $post, $request ) {
278
278
279
279
$ data = $ this ->filter_response_by_context ( $ data , $ context );
280
280
281
- // Wrap the data in a response object
281
+ // Wrap the data in a response object.
282
282
$ response = rest_ensure_response ( $ data );
283
283
284
284
$ response ->add_links ( $ this ->prepare_links ( $ post ) );
@@ -296,9 +296,9 @@ public function prepare_item_for_response( $post, $request ) {
296
296
}
297
297
298
298
/**
299
- * Get the Attachment's schema, conforming to JSON Schema
299
+ * Get the Attachment's schema, conforming to JSON Schema.
300
300
*
301
- * @return array
301
+ * @return array Item schema as an array.
302
302
*/
303
303
public function get_item_schema () {
304
304
@@ -363,11 +363,11 @@ public function get_item_schema() {
363
363
}
364
364
365
365
/**
366
- * Handle an upload via raw POST data
366
+ * Handle an upload via raw POST data.
367
367
*
368
- * @param array $data Supplied file data
369
- * @param array $headers HTTP headers from the request
370
- * @return array|WP_Error Data from {@see wp_handle_sideload()}
368
+ * @param array $data Supplied file data.
369
+ * @param array $headers HTTP headers from the request.
370
+ * @return array|WP_Error Data from {@see wp_handle_sideload()}.
371
371
*/
372
372
protected function upload_from_data ( $ data , $ headers ) {
373
373
if ( empty ( $ data ) ) {
@@ -398,13 +398,13 @@ protected function upload_from_data( $data, $headers ) {
398
398
}
399
399
}
400
400
401
- // Get the content-type
401
+ // Get the content-type.
402
402
$ type = array_shift ( $ headers ['content_type ' ] );
403
403
404
404
/** Include admin functions to get access to wp_tempnam() and wp_handle_sideload() */
405
405
require_once ABSPATH . 'wp-admin/includes/admin.php ' ;
406
406
407
- // Save the file
407
+ // Save the file.
408
408
$ tmpfname = wp_tempnam ( $ filename );
409
409
410
410
$ fp = fopen ( $ tmpfname , 'w+ ' );
@@ -416,7 +416,7 @@ protected function upload_from_data( $data, $headers ) {
416
416
fwrite ( $ fp , $ data );
417
417
fclose ( $ fp );
418
418
419
- // Now, sideload it in
419
+ // Now, sideload it in.
420
420
$ file_data = array (
421
421
'error ' => null ,
422
422
'tmp_name ' => $ tmpfname ,
@@ -466,7 +466,7 @@ protected function upload_from_data( $data, $headers ) {
466
466
* @return string|null Filename if available, or null if not found.
467
467
*/
468
468
public static function get_filename_from_disposition ( $ disposition_header ) {
469
- // Get the filename
469
+ // Get the filename.
470
470
$ filename = null ;
471
471
472
472
foreach ( $ disposition_header as $ value ) {
@@ -506,7 +506,7 @@ public static function get_filename_from_disposition( $disposition_header ) {
506
506
/**
507
507
* Get the query params for collections of attachments.
508
508
*
509
- * @return array
509
+ * @return array Query parameters for the attachment collection as an array.
510
510
*/
511
511
public function get_collection_params () {
512
512
$ params = parent ::get_collection_params ();
@@ -531,10 +531,10 @@ public function get_collection_params() {
531
531
/**
532
532
* Validate whether the user can query private statuses
533
533
*
534
- * @param mixed $value
535
- * @param WP_REST_Request $request
536
- * @param string $parameter
537
- * @return WP_Error|boolean
534
+ * @param mixed $value Status value.
535
+ * @param WP_REST_Request $request Request object.
536
+ * @param string $parameter Additional parameter to pass to validation.
537
+ * @return WP_Error|boolean Boolean true if the user may query, WP_Error if not.
538
538
*/
539
539
public function validate_user_can_query_private_statuses ( $ value , $ request , $ parameter ) {
540
540
if ( 'inherit ' === $ value ) {
@@ -544,18 +544,18 @@ public function validate_user_can_query_private_statuses( $value, $request, $par
544
544
}
545
545
546
546
/**
547
- * Handle an upload via multipart/form-data ($_FILES)
547
+ * Handle an upload via multipart/form-data ($_FILES).
548
548
*
549
- * @param array $files Data from $_FILES
550
- * @param array $headers HTTP headers from the request
551
- * @return array|WP_Error Data from {@see wp_handle_upload()}
549
+ * @param array $files Data from $_FILES.
550
+ * @param array $headers HTTP headers from the request.
551
+ * @return array|WP_Error Data from {@see wp_handle_upload()}.
552
552
*/
553
553
protected function upload_from_file ( $ files , $ headers ) {
554
554
if ( empty ( $ files ) ) {
555
555
return new WP_Error ( 'rest_upload_no_data ' , __ ( 'No data supplied. ' ), array ( 'status ' => 400 ) );
556
556
}
557
557
558
- // Verify hash, if given
558
+ // Verify hash, if given.
559
559
if ( ! empty ( $ headers ['content_md5 ' ] ) ) {
560
560
$ content_md5 = array_shift ( $ headers ['content_md5 ' ] );
561
561
$ expected = trim ( $ content_md5 );
@@ -565,16 +565,16 @@ protected function upload_from_file( $files, $headers ) {
565
565
}
566
566
}
567
567
568
- // Pass off to WP to handle the actual upload
568
+ // Pass off to WP to handle the actual upload.
569
569
$ overrides = array (
570
570
'test_form ' => false ,
571
571
);
572
- // Bypasses is_uploaded_file() when running unit tests
572
+ // Bypasses is_uploaded_file() when running unit tests.
573
573
if ( defined ( 'DIR_TESTDATA ' ) && DIR_TESTDATA ) {
574
574
$ overrides ['action ' ] = 'wp_handle_mock_upload ' ;
575
575
}
576
576
577
- /** Include admin functions to get access to wp_handle_upload() */
577
+ // Include admin functions to get access to wp_handle_upload().
578
578
require_once ABSPATH . 'wp-admin/includes/admin.php ' ;
579
579
$ file = wp_handle_upload ( $ files ['file ' ], $ overrides );
580
580
@@ -587,7 +587,8 @@ protected function upload_from_file( $files, $headers ) {
587
587
588
588
/**
589
589
* Get the supported media types.
590
- * Media types are considered the MIME type category
590
+ *
591
+ * Media types are considered the MIME type category.
591
592
*
592
593
* @return array
593
594
*/
0 commit comments