class.wpcom-json-api-list-media-v1-1-endpoint.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. new WPCOM_JSON_API_List_Media_v1_1_Endpoint( array(
  3. 'description' => 'Get a list of items in the media library.',
  4. 'group' => 'media',
  5. 'stat' => 'media',
  6. 'min_version' => '1.1',
  7. 'max_version' => '1.1',
  8. 'method' => 'GET',
  9. 'path' => '/sites/%s/media/',
  10. 'path_labels' => array(
  11. '$site' => '(int|string) Site ID or domain',
  12. ),
  13. 'query_parameters' => array(
  14. 'number' => '(int=20) The number of media items to return. Limit: 100.',
  15. 'offset' => '(int=0) 0-indexed offset.',
  16. 'page' => '(int) Return the Nth 1-indexed page of posts. Takes precedence over the <code>offset</code> parameter.',
  17. 'page_handle' => '(string) A page handle, returned from a previous API call as a <code>meta.next_page</code> property. This is the most efficient way to fetch the next page of results.',
  18. 'order' => array(
  19. 'DESC' => 'Return files in descending order. For dates, that means newest to oldest.',
  20. 'ASC' => 'Return files in ascending order. For dates, that means oldest to newest.',
  21. ),
  22. 'order_by' => array(
  23. 'date' => 'Order by the uploaded time of each file.',
  24. 'title' => "Order lexicographically by file titles.",
  25. 'ID' => 'Order by media ID.',
  26. ),
  27. 'search' => '(string) Search query.',
  28. 'post_ID' => '(int) Default is showing all items. The post where the media item is attached. 0 shows unattached media items.',
  29. 'mime_type' => "(string) Default is empty. Filter by mime type (e.g., 'image/jpeg', 'application/pdf'). Partial searches also work (e.g. passing 'image' will search for all image files).",
  30. 'after' => '(ISO 8601 datetime) Return media items uploaded after the specified datetime.',
  31. 'before' => '(ISO 8601 datetime) Return media items uploaded before the specified datetime.',
  32. ),
  33. 'response_format' => array(
  34. 'media' => '(array) Array of media objects',
  35. 'found' => '(int) The number of total results found',
  36. 'meta' => '(object) Meta data',
  37. ),
  38. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/media',
  39. 'example_request_data' => array(
  40. 'headers' => array(
  41. 'authorization' => 'Bearer YOUR_API_TOKEN'
  42. )
  43. )
  44. ) );
  45. class WPCOM_JSON_API_List_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint {
  46. public $date_range = array();
  47. public $page_handle = array();
  48. function callback( $path = '', $blog_id = 0 ) {
  49. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  50. if ( is_wp_error( $blog_id ) ) {
  51. return $blog_id;
  52. }
  53. //upload_files can probably be used for other endpoints but we want contributors to be able to use media too
  54. if ( ! current_user_can( 'edit_posts' ) ) {
  55. return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
  56. }
  57. $args = $this->query_args();
  58. $is_eligible_for_page_handle = true;
  59. if ( $args['number'] < 1 ) {
  60. $args['number'] = 20;
  61. } elseif ( 100 < $args['number'] ) {
  62. return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 100.', 400 );
  63. }
  64. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  65. $this->load_theme_functions();
  66. }
  67. if ( isset( $args['before'] ) ) {
  68. $this->date_range['before'] = $args['before'];
  69. }
  70. if ( isset( $args['after'] ) ) {
  71. $this->date_range['after'] = $args['after'];
  72. }
  73. $query = array(
  74. 'post_type' => 'attachment',
  75. 'post_status' => 'inherit',
  76. 'post_parent' => isset( $args['post_ID'] ) ? $args['post_ID'] : null,
  77. 'posts_per_page' => $args['number'],
  78. 'post_mime_type' => isset( $args['mime_type'] ) ? $args['mime_type'] : null,
  79. 'order' => isset( $args['order'] ) ? $args['order'] : 'DESC',
  80. 'orderby' => isset( $args['order_by'] ) ? $args['order_by'] : 'date',
  81. 's' => isset( $args['search'] ) ? $args['search'] : null,
  82. );
  83. if ( isset( $args['page'] ) ) {
  84. if ( $args['page'] < 1 ) {
  85. $args['page'] = 1;
  86. }
  87. $query['paged'] = $args['page'];
  88. if ( $query['paged'] !== 1 ) {
  89. $is_eligible_for_page_handle = false;
  90. }
  91. } else {
  92. if ( $args['offset'] < 0 ) {
  93. $args['offset'] = 0;
  94. }
  95. $query['offset'] = $args['offset'];
  96. if ( $query['offset'] !== 0 ) {
  97. $is_eligible_for_page_handle = false;
  98. }
  99. }
  100. if ( isset( $args['page_handle'] ) ) {
  101. $page_handle = wp_parse_args( $args['page_handle'] );
  102. if ( isset( $page_handle['value'] ) && isset( $page_handle['id'] ) ) {
  103. // we have a valid looking page handle
  104. $this->page_handle = $page_handle;
  105. add_filter( 'posts_where', array( $this, 'handle_where_for_page_handle' ) );
  106. }
  107. }
  108. if ( $this->date_range ) {
  109. add_filter( 'posts_where', array( $this, 'handle_date_range' ) );
  110. }
  111. $this->performed_query = $query;
  112. add_filter( 'posts_orderby', array( $this, 'handle_orderby_for_page_handle' ) );
  113. $media = new WP_Query( $query );
  114. remove_filter( 'posts_orderby', array( $this, 'handle_orderby_for_page_handle' ) );
  115. if ( $this->date_range ) {
  116. remove_filter( 'posts_where', array( $this, 'handle_date_range' ) );
  117. $this->date_range = array();
  118. }
  119. if ( $this->page_handle ) {
  120. remove_filter( 'posts_where', array( $this, 'handle_where_for_page_handle' ) );
  121. }
  122. $response = array();
  123. foreach ( $media->posts as $item ) {
  124. $response[] = $this->get_media_item_v1_1( $item->ID );
  125. }
  126. $return = array(
  127. 'found' => (int) $media->found_posts,
  128. 'media' => $response
  129. );
  130. if ( $is_eligible_for_page_handle && $return['media'] ) {
  131. $last_post = end( $return['media'] );
  132. reset( $return['media'] );
  133. if ( ( $return['found'] > count( $return['media'] ) ) && $last_post ) {
  134. $return['meta'] = array();
  135. $return['meta']['next_page'] = $this->build_page_handle( $last_post, $query );
  136. }
  137. }
  138. return $return;
  139. }
  140. function build_page_handle( $post, $query ) {
  141. $column = $query['orderby'];
  142. if ( ! $column ) {
  143. $column = 'date';
  144. }
  145. return build_query( array( 'value' => urlencode( $post->$column ), 'id' => $post->ID ) );
  146. }
  147. function handle_where_for_page_handle( $where ) {
  148. global $wpdb;
  149. $column = $this->performed_query['orderby'];
  150. if ( ! $column ) {
  151. $column = 'date';
  152. }
  153. $order = $this->performed_query['order'];
  154. if ( ! $order ) {
  155. $order = 'DESC';
  156. }
  157. if ( ! in_array( $column, array( 'ID', 'title', 'date', 'modified', 'comment_count' ) ) ) {
  158. return $where;
  159. }
  160. if ( ! in_array( $order, array( 'DESC', 'ASC' ) ) ) {
  161. return $where;
  162. }
  163. $db_column = '';
  164. $db_value = '';
  165. switch( $column ) {
  166. case 'ID':
  167. $db_column = 'ID';
  168. $db_value = '%d';
  169. break;
  170. case 'title':
  171. $db_column = 'post_title';
  172. $db_value = '%s';
  173. break;
  174. case 'date':
  175. $db_column = 'post_date';
  176. $db_value = 'CAST( %s as DATETIME )';
  177. break;
  178. case 'modified':
  179. $db_column = 'post_modified';
  180. $db_value = 'CAST( %s as DATETIME )';
  181. break;
  182. case 'comment_count':
  183. $db_column = 'comment_count';
  184. $db_value = '%d';
  185. break;
  186. }
  187. if ( 'DESC'=== $order ) {
  188. $db_order = '<';
  189. } else {
  190. $db_order = '>';
  191. }
  192. // Add a clause that limits the results to items beyond the passed item, or equivalent to the passed item
  193. // but with an ID beyond the passed item. When we're ordering by the ID already, we only ask for items
  194. // beyond the passed item.
  195. $where .= $wpdb->prepare( " AND ( ( `$wpdb->posts`.`$db_column` $db_order $db_value ) ", $this->page_handle['value'] );
  196. if ( $db_column !== 'ID' ) {
  197. $where .= $wpdb->prepare( "OR ( `$wpdb->posts`.`$db_column` = $db_value AND `$wpdb->posts`.ID $db_order %d )", $this->page_handle['value'], $this->page_handle['id'] );
  198. }
  199. $where .= ' )';
  200. return $where;
  201. }
  202. function handle_date_range( $where ) {
  203. global $wpdb;
  204. switch ( count( $this->date_range ) ) {
  205. case 2 :
  206. $where .= $wpdb->prepare(
  207. " AND `$wpdb->posts`.post_date BETWEEN CAST( %s AS DATETIME ) AND CAST( %s AS DATETIME ) ",
  208. $this->date_range['after'],
  209. $this->date_range['before']
  210. );
  211. break;
  212. case 1 :
  213. if ( isset( $this->date_range['before'] ) ) {
  214. $where .= $wpdb->prepare(
  215. " AND `$wpdb->posts`.post_date <= CAST( %s AS DATETIME ) ",
  216. $this->date_range['before']
  217. );
  218. } else {
  219. $where .= $wpdb->prepare(
  220. " AND `$wpdb->posts`.post_date >= CAST( %s AS DATETIME ) ",
  221. $this->date_range['after']
  222. );
  223. }
  224. break;
  225. }
  226. return $where;
  227. }
  228. function handle_orderby_for_page_handle( $orderby ) {
  229. global $wpdb;
  230. if ( $this->performed_query['orderby'] === 'ID' ) {
  231. // bail if we're already ordering by ID
  232. return $orderby;
  233. }
  234. if ( $orderby ) {
  235. $orderby .= ' ,';
  236. }
  237. $order = $this->performed_query['order'];
  238. if ( ! $order ) {
  239. $order = 'DESC';
  240. }
  241. $orderby .= " `$wpdb->posts`.ID $order";
  242. return $orderby;
  243. }
  244. }