PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/json-endpoints/class.wpcom-json-api-list-media-v1-1-endpoint.php

https://gitlab.com/juanito.abelo/nlmobile
PHP | 237 lines | 193 code | 38 blank | 6 comment | 35 complexity | 964d3d010f23840e39f9d9cbc770a9e6 MD5 | raw file
  1. <?php
  2. class WPCOM_JSON_API_List_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint {
  3. var $date_range = array();
  4. var $page_handle = array();
  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 ) );
  7. if ( is_wp_error( $blog_id ) ) {
  8. return $blog_id;
  9. }
  10. //upload_files can probably be used for other endpoints but we want contributors to be able to use media too
  11. if ( ! current_user_can( 'edit_posts' ) ) {
  12. return new WP_Error( 'unauthorized', 'User cannot view media', 403 );
  13. }
  14. $args = $this->query_args();
  15. $is_eligible_for_page_handle = true;
  16. if ( $args['number'] < 1 ) {
  17. $args['number'] = 20;
  18. } elseif ( 100 < $args['number'] ) {
  19. return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 100.', 400 );
  20. }
  21. if ( isset( $args['before'] ) ) {
  22. $this->date_range['before'] = $args['before'];
  23. }
  24. if ( isset( $args['after'] ) ) {
  25. $this->date_range['after'] = $args['after'];
  26. }
  27. $query = array(
  28. 'post_type' => 'attachment',
  29. 'post_status' => 'inherit',
  30. 'post_parent' => $args['parent_id'],
  31. 'offset' => $args['offset'],
  32. 'posts_per_page' => $args['number'],
  33. 'post_mime_type' => $args['mime_type'],
  34. 'order' => $args['order'],
  35. 'orderby' => $args['order_by'],
  36. 's' => isset( $args['search'] ) ? $args['search'] : null,
  37. );
  38. if ( isset( $args['page'] ) ) {
  39. if ( $args['page'] < 1 ) {
  40. $args['page'] = 1;
  41. }
  42. $query['paged'] = $args['page'];
  43. if ( $query['paged'] !== 1 ) {
  44. $is_eligible_for_page_handle = false;
  45. }
  46. } else {
  47. if ( $args['offset'] < 0 ) {
  48. $args['offset'] = 0;
  49. }
  50. $query['offset'] = $args['offset'];
  51. if ( $query['offset'] !== 0 ) {
  52. $is_eligible_for_page_handle = false;
  53. }
  54. }
  55. if ( isset( $args['page_handle'] ) ) {
  56. $page_handle = wp_parse_args( $args['page_handle'] );
  57. if ( isset( $page_handle['value'] ) && isset( $page_handle['id'] ) ) {
  58. // we have a valid looking page handle
  59. $this->page_handle = $page_handle;
  60. add_filter( 'posts_where', array( $this, 'handle_where_for_page_handle' ) );
  61. }
  62. }
  63. if ( $this->date_range ) {
  64. add_filter( 'posts_where', array( $this, 'handle_date_range' ) );
  65. }
  66. $this->performed_query = $query;
  67. add_filter( 'posts_orderby', array( $this, 'handle_orderby_for_page_handle' ) );
  68. $media = new WP_Query( $query );
  69. remove_filter( 'posts_orderby', array( $this, 'handle_orderby_for_page_handle' ) );
  70. if ( $this->date_range ) {
  71. remove_filter( 'posts_where', array( $this, 'handle_date_range' ) );
  72. $this->date_range = array();
  73. }
  74. if ( $this->page_handle ) {
  75. remove_filter( 'posts_where', array( $this, 'handle_where_for_page_handle' ) );
  76. }
  77. $response = array();
  78. foreach ( $media->posts as $item ) {
  79. $response[] = $this->get_media_item_v1_1( $item->ID );
  80. }
  81. $return = array(
  82. 'found' => (int) $media->found_posts,
  83. 'media' => $response
  84. );
  85. if ( $is_eligible_for_page_handle && $return['media'] ) {
  86. $last_post = end( $return['media'] );
  87. reset( $return['media'] );
  88. if ( ( $return['found'] > count( $return['media'] ) ) && $last_post ) {
  89. $return['meta'] = array();
  90. $return['meta']['next_page'] = $this->build_page_handle( $last_post, $query );
  91. }
  92. }
  93. return $return;
  94. }
  95. function build_page_handle( $post, $query ) {
  96. $column = $query['orderby'];
  97. if ( ! $column ) {
  98. $column = 'date';
  99. }
  100. return build_query( array( 'value' => urlencode( $post->$column ), 'id' => $post->ID ) );
  101. }
  102. function handle_where_for_page_handle( $where ) {
  103. global $wpdb;
  104. $column = $this->performed_query['orderby'];
  105. if ( ! $column ) {
  106. $column = 'date';
  107. }
  108. $order = $this->performed_query['order'];
  109. if ( ! $order ) {
  110. $order = 'DESC';
  111. }
  112. if ( ! in_array( $column, array( 'ID', 'title', 'date', 'modified', 'comment_count' ) ) ) {
  113. return $where;
  114. }
  115. if ( ! in_array( $order, array( 'DESC', 'ASC' ) ) ) {
  116. return $where;
  117. }
  118. $db_column = '';
  119. $db_value = '';
  120. switch( $column ) {
  121. case 'ID':
  122. $db_column = 'ID';
  123. $db_value = '%d';
  124. break;
  125. case 'title':
  126. $db_column = 'post_title';
  127. $db_value = '%s';
  128. break;
  129. case 'date':
  130. $db_column = 'post_date';
  131. $db_value = 'CAST( %s as DATETIME )';
  132. break;
  133. case 'modified':
  134. $db_column = 'post_modified';
  135. $db_value = 'CAST( %s as DATETIME )';
  136. break;
  137. case 'comment_count':
  138. $db_column = 'comment_count';
  139. $db_value = '%d';
  140. break;
  141. }
  142. if ( 'DESC'=== $order ) {
  143. $db_order = '<';
  144. } else {
  145. $db_order = '>';
  146. }
  147. // Add a clause that limits the results to items beyond the passed item, or equivalent to the passed item
  148. // but with an ID beyond the passed item. When we're ordering by the ID already, we only ask for items
  149. // beyond the passed item.
  150. $where .= $wpdb->prepare( " AND ( ( `$wpdb->posts`.`$db_column` $db_order $db_value ) ", $this->page_handle['value'] );
  151. if ( $db_column !== 'ID' ) {
  152. $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'] );
  153. }
  154. $where .= ' )';
  155. return $where;
  156. }
  157. function handle_date_range( $where ) {
  158. global $wpdb;
  159. switch ( count( $this->date_range ) ) {
  160. case 2 :
  161. $where .= $wpdb->prepare(
  162. " AND `$wpdb->posts`.post_date BETWEEN CAST( %s AS DATETIME ) AND CAST( %s AS DATETIME ) ",
  163. $this->date_range['after'],
  164. $this->date_range['before']
  165. );
  166. break;
  167. case 1 :
  168. if ( isset( $this->date_range['before'] ) ) {
  169. $where .= $wpdb->prepare(
  170. " AND `$wpdb->posts`.post_date <= CAST( %s AS DATETIME ) ",
  171. $this->date_range['before']
  172. );
  173. } else {
  174. $where .= $wpdb->prepare(
  175. " AND `$wpdb->posts`.post_date >= CAST( %s AS DATETIME ) ",
  176. $this->date_range['after']
  177. );
  178. }
  179. break;
  180. }
  181. return $where;
  182. }
  183. function handle_orderby_for_page_handle( $orderby ) {
  184. global $wpdb;
  185. if ( $this->performed_query['orderby'] === 'ID' ) {
  186. // bail if we're already ordering by ID
  187. return $orderby;
  188. }
  189. if ( $orderby ) {
  190. $orderby .= ' ,';
  191. }
  192. $order = $this->performed_query['order'];
  193. if ( ! $order ) {
  194. $order = 'DESC';
  195. }
  196. $orderby .= " `$wpdb->posts`.ID $order";
  197. return $orderby;
  198. }
  199. }