Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | class WPCOM_JSON_API_List_Media_Endpoint extends WPCOM_JSON_API_Endpoint { |
||
| 4 | |||
| 5 | function callback( $path = '', $blog_id = 0 ) { |
||
| 6 | $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 7 | if ( is_wp_error( $blog_id ) ) { |
||
| 8 | return $blog_id; |
||
| 9 | } |
||
| 10 | |||
| 11 | //upload_files can probably be used for other endpoints but we want contributors to be able to use media too |
||
| 12 | if ( !current_user_can( 'edit_posts' ) ) { |
||
| 13 | return new WP_Error( 'unauthorized', 'User cannot view media', 403 ); |
||
| 14 | } |
||
| 15 | |||
| 16 | $args = $this->query_args(); |
||
| 17 | |||
| 18 | if ( $args['number'] < 1 ) { |
||
| 19 | $args['number'] = 20; |
||
| 20 | } elseif ( 100 < $args['number'] ) { |
||
| 21 | return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 100.', 400 ); |
||
| 22 | } |
||
| 23 | |||
| 24 | $media = get_posts( array( |
||
| 25 | 'post_type' => 'attachment', |
||
| 26 | 'post_parent' => $args['parent_id'], |
||
| 27 | 'offset' => $args['offset'], |
||
| 28 | 'numberposts' => $args['number'], |
||
| 29 | 'post_mime_type' => $args['mime_type'] |
||
| 30 | ) ); |
||
| 31 | |||
| 32 | $response = array(); |
||
| 33 | foreach ( $media as $item ) { |
||
| 34 | $response[] = $this->get_media_item( $item->ID ); |
||
| 35 | } |
||
| 36 | |||
| 37 | $_num = (array) wp_count_attachments(); |
||
| 38 | $_total_media = array_sum( $_num ) - $_num['trash']; |
||
| 39 | |||
| 40 | $return = array( |
||
| 41 | 'found' => $_total_media, |
||
| 42 | 'media' => $response |
||
| 43 | ); |
||
| 44 | |||
| 45 | return $return; |
||
| 46 | } |
||
| 47 | |||
| 48 | } |
||
| 49 |