WPCOM_JSON_API_List_Media_v1_1_Endpoint

The Jetpack by WordPress.com WPCOM JSON API List Media v1 1 Endpoint class.

Defined (1)

The class is defined in the following location(s).

/json-endpoints/class.wpcom-json-api-list-media-v1-1-endpoint.php  
  1. class WPCOM_JSON_API_List_Media_v1_1_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2.  
  3. public $date_range = array(); 
  4. public $page_handle = array(); 
  5.  
  6. function callback( $path = '', $blog_id = 0 ) { 
  7. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  8. if ( is_wp_error( $blog_id ) ) { 
  9. return $blog_id; 
  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. $args = $this->query_args(); 
  16. $is_eligible_for_page_handle = true; 
  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. if ( isset( $args['before'] ) ) { 
  24. $this->date_range['before'] = $args['before']; 
  25. if ( isset( $args['after'] ) ) { 
  26. $this->date_range['after'] = $args['after']; 
  27.  
  28. $query = array( 
  29. 'post_type' => 'attachment',  
  30. 'post_status' => 'inherit',  
  31. 'post_parent' => isset( $args['post_ID'] ) ? $args['post_ID'] : null,  
  32. 'offset' => isset( $args['offset'] ) ? $args['offset'] : null,  
  33. 'posts_per_page' => $args['number'],  
  34. 'post_mime_type' => isset( $args['mime_type'] ) ? $args['mime_type'] : null,  
  35. 'order' => isset( $args['order'] ) ? $args['order'] : 'DESC',  
  36. 'orderby' => isset( $args['order_by'] ) ? $args['order_by'] : 'date',  
  37. 's' => isset( $args['search'] ) ? $args['search'] : null,  
  38. ); 
  39.  
  40. if ( isset( $args['page'] ) ) { 
  41. if ( $args['page'] < 1 ) { 
  42. $args['page'] = 1; 
  43.  
  44. $query['paged'] = $args['page']; 
  45. if ( $query['paged'] !== 1 ) { 
  46. $is_eligible_for_page_handle = false; 
  47. } else { 
  48. if ( $args['offset'] < 0 ) { 
  49. $args['offset'] = 0; 
  50.  
  51. $query['offset'] = $args['offset']; 
  52. if ( $query['offset'] !== 0 ) { 
  53. $is_eligible_for_page_handle = false; 
  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. if ( $this->date_range ) { 
  63. add_filter( 'posts_where', array( $this, 'handle_date_range' ) ); 
  64.  
  65. $this->performed_query = $query; 
  66. add_filter( 'posts_orderby', array( $this, 'handle_orderby_for_page_handle' ) ); 
  67.  
  68. $media = new WP_Query( $query ); 
  69.  
  70. remove_filter( 'posts_orderby', array( $this, 'handle_orderby_for_page_handle' ) ); 
  71.  
  72. if ( $this->date_range ) { 
  73. remove_filter( 'posts_where', array( $this, 'handle_date_range' ) ); 
  74. $this->date_range = array(); 
  75.  
  76. if ( $this->page_handle ) { 
  77. remove_filter( 'posts_where', array( $this, 'handle_where_for_page_handle' ) ); 
  78.  
  79. $response = array(); 
  80. foreach ( $media->posts as $item ) { 
  81. $response[] = $this->get_media_item_v1_1( $item->ID ); 
  82.  
  83. $return = array( 
  84. 'found' => (int) $media->found_posts,  
  85. 'media' => $response 
  86. ); 
  87.  
  88. if ( $is_eligible_for_page_handle && $return['media'] ) { 
  89. $last_post = end( $return['media'] ); 
  90. reset( $return['media'] ); 
  91.  
  92. if ( ( $return['found'] > count( $return['media'] ) ) && $last_post ) { 
  93. $return['meta'] = array(); 
  94. $return['meta']['next_page'] = $this->build_page_handle( $last_post, $query ); 
  95.  
  96. return $return; 
  97.  
  98. function build_page_handle( $post, $query ) { 
  99. $column = $query['orderby']; 
  100. if ( ! $column ) { 
  101. $column = 'date'; 
  102. return build_query( array( 'value' => urlencode( $post->$column ), 'id' => $post->ID ) ); 
  103.  
  104. function handle_where_for_page_handle( $where ) { 
  105. global $wpdb; 
  106.  
  107. $column = $this->performed_query['orderby']; 
  108. if ( ! $column ) { 
  109. $column = 'date'; 
  110. $order = $this->performed_query['order']; 
  111. if ( ! $order ) { 
  112. $order = 'DESC'; 
  113.  
  114. if ( ! in_array( $column, array( 'ID', 'title', 'date', 'modified', 'comment_count' ) ) ) { 
  115. return $where; 
  116.  
  117. if ( ! in_array( $order, array( 'DESC', 'ASC' ) ) ) { 
  118. return $where; 
  119.  
  120. $db_column = ''; 
  121. $db_value = ''; 
  122. switch( $column ) { 
  123. case 'ID': 
  124. $db_column = 'ID'; 
  125. $db_value = '%d'; 
  126. break; 
  127. case 'title': 
  128. $db_column = 'post_title'; 
  129. $db_value = '%s'; 
  130. break; 
  131. case 'date': 
  132. $db_column = 'post_date'; 
  133. $db_value = 'CAST( %s as DATETIME )'; 
  134. break; 
  135. case 'modified': 
  136. $db_column = 'post_modified'; 
  137. $db_value = 'CAST( %s as DATETIME )'; 
  138. break; 
  139. case 'comment_count': 
  140. $db_column = 'comment_count'; 
  141. $db_value = '%d'; 
  142. break; 
  143.  
  144. if ( 'DESC'=== $order ) { 
  145. $db_order = '<'; 
  146. } else { 
  147. $db_order = '>'; 
  148.  
  149. // Add a clause that limits the results to items beyond the passed item, or equivalent to the passed item 
  150. // but with an ID beyond the passed item. When we're ordering by the ID already, we only ask for items 
  151. // beyond the passed item. 
  152. $where .= $wpdb->prepare( " AND ( ( `$wpdb->posts`.`$db_column` $db_order $db_value ) ", $this->page_handle['value'] ); 
  153. if ( $db_column !== 'ID' ) { 
  154. $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'] ); 
  155. $where .= ' )'; 
  156.  
  157. return $where; 
  158.  
  159. function handle_date_range( $where ) { 
  160. global $wpdb; 
  161.  
  162. switch ( count( $this->date_range ) ) { 
  163. case 2 : 
  164. $where .= $wpdb->prepare( 
  165. " AND `$wpdb->posts`.post_date BETWEEN CAST( %s AS DATETIME ) AND CAST( %s AS DATETIME ) ",  
  166. $this->date_range['after'],  
  167. $this->date_range['before'] 
  168. ); 
  169. break; 
  170. case 1 : 
  171. if ( isset( $this->date_range['before'] ) ) { 
  172. $where .= $wpdb->prepare( 
  173. " AND `$wpdb->posts`.post_date <= CAST( %s AS DATETIME ) ",  
  174. $this->date_range['before'] 
  175. ); 
  176. } else { 
  177. $where .= $wpdb->prepare( 
  178. " AND `$wpdb->posts`.post_date >= CAST( %s AS DATETIME ) ",  
  179. $this->date_range['after'] 
  180. ); 
  181. break; 
  182.  
  183. return $where; 
  184.  
  185. function handle_orderby_for_page_handle( $orderby ) { 
  186. global $wpdb; 
  187. if ( $this->performed_query['orderby'] === 'ID' ) { 
  188. // bail if we're already ordering by ID 
  189. return $orderby; 
  190.  
  191. if ( $orderby ) { 
  192. $orderby .= ' , '; 
  193. $order = $this->performed_query['order']; 
  194. if ( ! $order ) { 
  195. $order = 'DESC'; 
  196. $orderby .= " `$wpdb->posts`.ID $order"; 
  197. return $orderby; 
  198.