← All changes
|
_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-app-media.php
+15
-7
13.1.5
→
16.3-beta
View file →
| @@ -5,8 +5,12 @@ | ||
| 5 | 5 | * @package automattic/jetpack |
| 6 | 6 | * @since 13.1 |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 10 | + exit( 0 ); | |
| 11 | +} | |
| 12 | + | |
| 9 | 13 | /** |
| 10 | 14 | * Media uploaded by the Jetpack app helper API. |
| 11 | 15 | * |
| 12 | 16 | * @since 13.1 |
| @@ -44,8 +48,9 @@ | ||
| 44 | 48 | |
| 45 | 49 | ), |
| 46 | 50 | 'page_handle' => array( |
| 47 | 51 | 'type' => 'number', |
| 52 | + 'default' => 1, | |
| 48 | 53 | 'required' => false, |
| 49 | 54 | 'sanitize_callback' => 'absint', |
| 50 | 55 | |
| 51 | 56 | ), |
| @@ -121,14 +126,13 @@ | ||
| 121 | 126 | * @return array|\WP_Error|mixed |
| 122 | 127 | */ |
| 123 | 128 | public function get_media( \WP_REST_Request $request ) { |
| 124 | 129 | $params = $request->get_params(); |
| 125 | - $number = $params['number']; | |
| 126 | 130 | |
| 127 | - $query_args = array( | |
| 131 | + $query_args = array( | |
| 128 | 132 | 'post_type' => 'attachment', |
| 129 | 133 | 'post_status' => 'inherit', |
| 130 | - 'number' => $number, | |
| 134 | + 'number' => $params['number'], | |
| 131 | 135 | 'date_query' => array( |
| 132 | 136 | 'after' => gmdate( DATE_RSS, intval( $params['after'] ) ), |
| 133 | 137 | ), |
| 134 | 138 | 'paged' => $params['page_handle'], |
| @@ -134,8 +138,9 @@ | ||
| 134 | 138 | 'paged' => $params['page_handle'], |
| 135 | 139 | 'author' => get_current_user_id(), |
| 136 | 140 | 'orderby' => 'date', |
| 137 | 141 | ); |
| 142 | + | |
| 138 | 143 | $media_query = new WP_Query( $query_args ); |
| 139 | 144 | $response = $this->format_response( $media_query ); |
| 140 | 145 | |
| 141 | 146 | wp_reset_postdata(); |
| @@ -150,9 +155,12 @@ | ||
| 150 | 155 | $response = array(); |
| 151 | 156 | $response['media'] = array(); |
| 152 | 157 | while ( $media_query->have_posts() ) { |
| 153 | 158 | $media_query->the_post(); |
| 154 | - $response['media'][] = $this->format_item( $media_query->post ); | |
| 159 | + // only include images. | |
| 160 | + if ( wp_attachment_is_image( $media_query->post->ID ) ) { | |
| 161 | + $response['media'][] = $this->format_item( $media_query->post ); | |
| 162 | + } | |
| 155 | 163 | } |
| 156 | 164 | $response['found'] = $media_query->found_posts; |
| 157 | 165 | $response['meta'] = array( 'next_page' => $media_query->paged + 1 ); |
| 158 | 166 | return $response; |
| @@ -164,12 +172,12 @@ | ||
| 164 | 172 | */ |
| 165 | 173 | private function format_item( $item ) { |
| 166 | 174 | return array( |
| 167 | 175 | 'ID' => $item->ID, |
| 168 | - 'url' => get_permalink( $item ), | |
| 176 | + 'url' => wp_get_attachment_image_url( $item->ID, 'full', true ), | |
| 169 | 177 | 'date' => get_date_from_gmt( $item->post_date_gmt ), |
| 170 | 178 | 'name' => get_the_title( $item ), |
| 171 | - 'file' => basename( wp_get_attachment_image_url( $item->ID, 'full' ) ), | |
| 179 | + 'file' => basename( wp_get_attachment_url( $item->ID ) ), | |
| 172 | 180 | 'title' => get_the_title( $item ), |
| 173 | 181 | 'guid' => get_the_guid( $item ), |
| 174 | 182 | 'type' => get_post_mime_type( $item ), |
| 175 | 183 | 'caption' => '', |
| @@ -174,9 +182,9 @@ | ||
| 174 | 182 | 'type' => get_post_mime_type( $item ), |
| 175 | 183 | 'caption' => '', |
| 176 | 184 | 'thumbnails' => array( |
| 177 | 185 | 'thumbnail' => wp_get_attachment_image_url( $item->ID, 'thumbnail' ), |
| 178 | - 'large' => wp_get_attachment_image_url( $item->ID, 'full' ), | |
| 186 | + 'large' => wp_get_attachment_image_url( $item->ID, 'large' ), | |
| 179 | 187 | ), |
| 180 | 188 | ); |
| 181 | 189 | } |
| 182 | 190 | } |