PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/hunt9310/ras
PHP | 448 lines | 360 code | 66 blank | 22 comment | 88 complexity | c3fae431e9b418bf62c83c5852190986 MD5 | raw file
  1. <?php
  2. class WPCOM_JSON_API_List_Posts_v1_1_Endpoint extends WPCOM_JSON_API_Post_v1_1_Endpoint {
  3. public $date_range = array();
  4. public $modified_range = array();
  5. public $page_handle = array();
  6. public $performed_query = null;
  7. public $response_format = array(
  8. 'found' => '(int) The total number of posts found that match the request (ignoring limits, offsets, and pagination).',
  9. 'posts' => '(array:post) An array of post objects.',
  10. 'meta' => '(object) Meta data',
  11. );
  12. // /sites/%s/posts/ -> $blog_id
  13. function callback( $path = '', $blog_id = 0 ) {
  14. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  15. if ( is_wp_error( $blog_id ) ) {
  16. return $blog_id;
  17. }
  18. $args = $this->query_args();
  19. $is_eligible_for_page_handle = true;
  20. if ( $args['number'] < 1 ) {
  21. $args['number'] = 20;
  22. } elseif ( 100 < $args['number'] ) {
  23. return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 100.', 400 );
  24. }
  25. if ( isset( $args['type'] ) && ! $this->is_post_type_allowed( $args['type'] ) ) {
  26. return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 );
  27. }
  28. // Normalize post_type
  29. if ( isset( $args['type'] ) && 'any' == $args['type'] ) {
  30. if ( version_compare( $this->api->version, '1.1', '<' ) ) {
  31. $args['type'] = array( 'post', 'page' );
  32. } else { // 1.1+
  33. $args['type'] = $this->_get_whitelisted_post_types();
  34. }
  35. }
  36. // determine statuses
  37. $status = ( ! empty( $args['status'] ) ) ? explode( ',', $args['status'] ) : array( 'publish' );
  38. if ( is_user_logged_in() ) {
  39. $statuses_whitelist = array(
  40. 'publish',
  41. 'pending',
  42. 'draft',
  43. 'future',
  44. 'private',
  45. 'trash',
  46. 'any',
  47. );
  48. $status = array_intersect( $status, $statuses_whitelist );
  49. } else {
  50. // logged-out users can see only published posts
  51. $statuses_whitelist = array( 'publish', 'any' );
  52. $status = array_intersect( $status, $statuses_whitelist );
  53. if ( empty( $status ) ) {
  54. // requested only protected statuses? nothing for you here
  55. return array( 'found' => 0, 'posts' => array() );
  56. }
  57. // clear it (AKA published only) because "any" includes protected
  58. $status = array();
  59. }
  60. if ( isset( $args['type'] ) &&
  61. ! in_array( $args['type'], array( 'post', 'page', 'revision', 'any' ) ) &&
  62. defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  63. $this->load_theme_functions();
  64. }
  65. // let's be explicit about defaulting to 'post'
  66. $args['type'] = isset( $args['type'] ) ? $args['type'] : 'post';
  67. // make sure the user can read or edit the requested post type(s)
  68. if ( is_array( $args['type'] ) ) {
  69. $allowed_types = array();
  70. foreach ( $args['type'] as $post_type ) {
  71. if ( $this->current_user_can_access_post_type( $post_type, $args['context'] ) ) {
  72. $allowed_types[] = $post_type;
  73. }
  74. }
  75. if ( empty( $allowed_types ) ) {
  76. return array( 'found' => 0, 'posts' => array() );
  77. }
  78. $args['type'] = $allowed_types;
  79. }
  80. else {
  81. if ( ! $this->current_user_can_access_post_type( $args['type'], $args['context'] ) ) {
  82. return array( 'found' => 0, 'posts' => array() );
  83. }
  84. }
  85. $query = array(
  86. 'posts_per_page' => $args['number'],
  87. 'order' => $args['order'],
  88. 'orderby' => $args['order_by'],
  89. 'post_type' => $args['type'],
  90. 'post_status' => $status,
  91. 'post_parent' => isset( $args['parent_id'] ) ? $args['parent_id'] : null,
  92. 'author' => isset( $args['author'] ) && 0 < $args['author'] ? $args['author'] : null,
  93. 's' => isset( $args['search'] ) ? $args['search'] : null,
  94. 'fields' => 'ids',
  95. );
  96. if ( ! is_user_logged_in () ) {
  97. $query['has_password'] = false;
  98. }
  99. if ( isset( $args['meta_key'] ) ) {
  100. $show = false;
  101. if ( WPCOM_JSON_API_Metadata::is_public( $args['meta_key'] ) )
  102. $show = true;
  103. if ( current_user_can( 'edit_post_meta', $query['post_type'], $args['meta_key'] ) )
  104. $show = true;
  105. if ( is_protected_meta( $args['meta_key'], 'post' ) && ! $show )
  106. return new WP_Error( 'invalid_meta_key', 'Invalid meta key', 404 );
  107. $meta = array( 'key' => $args['meta_key'] );
  108. if ( isset( $args['meta_value'] ) )
  109. $meta['value'] = $args['meta_value'];
  110. $query['meta_query'] = array( $meta );
  111. }
  112. if ( $args['sticky'] === 'include' ) {
  113. $query['ignore_sticky_posts'] = 1;
  114. } else if ( $args['sticky'] === 'exclude' ) {
  115. $sticky = get_option( 'sticky_posts' );
  116. if ( is_array( $sticky ) ) {
  117. $query['post__not_in'] = $sticky;
  118. }
  119. } else if ( $args['sticky'] === 'require' ) {
  120. $sticky = get_option( 'sticky_posts' );
  121. if ( is_array( $sticky ) && ! empty( $sticky ) ) {
  122. $query['post__in'] = $sticky;
  123. } else {
  124. // no sticky posts exist
  125. return array( 'found' => 0, 'posts' => array() );
  126. }
  127. }
  128. if ( isset( $args['exclude'] ) ) {
  129. $excluded_ids = (array) $args['exclude'];
  130. $query['post__not_in'] = isset( $query['post__not_in'] ) ? array_merge( $query['post__not_in'], $excluded_ids ) : $excluded_ids;
  131. }
  132. if ( isset( $args['exclude_tree'] ) && is_post_type_hierarchical( $args['type'] ) ) {
  133. // get_page_children is a misnomer; it supports all hierarchical post types
  134. $page_args = array(
  135. 'child_of' => $args['exclude_tree'],
  136. 'post_type' => $args['type'],
  137. // since we're looking for things to exclude, be aggressive
  138. 'post_status' => 'publish,draft,pending,private,future,trash',
  139. );
  140. $post_descendants = get_pages( $page_args );
  141. $exclude_tree = array( $args['exclude_tree'] );
  142. foreach ( $post_descendants as $child ) {
  143. $exclude_tree[] = $child->ID;
  144. }
  145. $query['post__not_in'] = isset( $query['post__not_in'] ) ? array_merge( $query['post__not_in'], $exclude_tree ) : $exclude_tree;
  146. }
  147. if ( isset( $args['category'] ) ) {
  148. $category = get_term_by( 'slug', $args['category'], 'category' );
  149. if ( $category === false) {
  150. $query['category_name'] = $args['category'];
  151. } else {
  152. $query['cat'] = $category->term_id;
  153. }
  154. }
  155. if ( isset( $args['tag'] ) ) {
  156. $query['tag'] = $args['tag'];
  157. }
  158. if ( isset( $args['page'] ) ) {
  159. if ( $args['page'] < 1 ) {
  160. $args['page'] = 1;
  161. }
  162. $query['paged'] = $args['page'];
  163. if ( $query['paged'] !== 1 ) {
  164. $is_eligible_for_page_handle = false;
  165. }
  166. } else {
  167. if ( $args['offset'] < 0 ) {
  168. $args['offset'] = 0;
  169. }
  170. $query['offset'] = $args['offset'];
  171. if ( $query['offset'] !== 0 ) {
  172. $is_eligible_for_page_handle = false;
  173. }
  174. }
  175. if ( isset( $args['before'] ) ) {
  176. $this->date_range['before'] = $args['before'];
  177. }
  178. if ( isset( $args['after'] ) ) {
  179. $this->date_range['after'] = $args['after'];
  180. }
  181. if ( isset( $args['modified_before_gmt'] ) ) {
  182. $this->modified_range['before'] = $args['modified_before_gmt'];
  183. }
  184. if ( isset( $args['modified_after_gmt'] ) ) {
  185. $this->modified_range['after'] = $args['modified_after_gmt'];
  186. }
  187. if ( $this->date_range ) {
  188. add_filter( 'posts_where', array( $this, 'handle_date_range' ) );
  189. }
  190. if ( $this->modified_range ) {
  191. add_filter( 'posts_where', array( $this, 'handle_modified_range' ) );
  192. }
  193. if ( isset( $args['page_handle'] ) ) {
  194. $page_handle = wp_parse_args( $args['page_handle'] );
  195. if ( isset( $page_handle['value'] ) && isset( $page_handle['id'] ) ) {
  196. // we have a valid looking page handle
  197. $this->page_handle = $page_handle;
  198. add_filter( 'posts_where', array( $this, 'handle_where_for_page_handle' ) );
  199. }
  200. }
  201. /**
  202. * 'column' necessary for the me/posts endpoint (which extends sites/$site/posts).
  203. * Would need to be added to the sites/$site/posts definition if we ever want to
  204. * use it there.
  205. */
  206. $column_whitelist = array( 'post_modified_gmt' );
  207. if ( isset( $args['column'] ) && in_array( $args['column'], $column_whitelist ) ) {
  208. $query['column'] = $args['column'];
  209. }
  210. $this->performed_query = $query;
  211. add_filter( 'posts_orderby', array( $this, 'handle_orderby_for_page_handle' ) );
  212. $wp_query = new WP_Query( $query );
  213. remove_filter( 'posts_orderby', array( $this, 'handle_orderby_for_page_handle' ) );
  214. if ( $this->date_range ) {
  215. remove_filter( 'posts_where', array( $this, 'handle_date_range' ) );
  216. $this->date_range = array();
  217. }
  218. if ( $this->modified_range ) {
  219. remove_filter( 'posts_where', array( $this, 'handle_modified_range' ) );
  220. $this->modified_range = array();
  221. }
  222. if ( $this->page_handle ) {
  223. remove_filter( 'posts_where', array( $this, 'handle_where_for_page_handle' ) );
  224. }
  225. $return = array();
  226. $excluded_count = 0;
  227. foreach ( array_keys( $this->response_format ) as $key ) {
  228. switch ( $key ) {
  229. case 'found' :
  230. $return[$key] = (int) $wp_query->found_posts;
  231. break;
  232. case 'posts' :
  233. $posts = array();
  234. foreach ( $wp_query->posts as $post_ID ) {
  235. $the_post = $this->get_post_by( 'ID', $post_ID, $args['context'] );
  236. if ( $the_post && ! is_wp_error( $the_post ) ) {
  237. $posts[] = $the_post;
  238. } else {
  239. $excluded_count++;
  240. }
  241. }
  242. if ( $posts ) {
  243. /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
  244. do_action( 'wpcom_json_api_objects', 'posts', count( $posts ) );
  245. }
  246. $return[$key] = $posts;
  247. break;
  248. case 'meta' :
  249. if ( ! is_array( $args['type'] ) ) {
  250. $return[$key] = (object) array(
  251. 'links' => (object) array(
  252. 'counts' => (string) $this->links->get_site_link( $blog_id, 'post-counts/' . $args['type'] ),
  253. )
  254. );
  255. }
  256. if ( $is_eligible_for_page_handle && $return['posts'] ) {
  257. $last_post = end( $return['posts'] );
  258. reset( $return['posts'] );
  259. if ( ( $return['found'] > count( $return['posts'] ) ) && $last_post ) {
  260. if ( ! isset( $return[$key] ) ) {
  261. $return[$key] = (object) array();
  262. }
  263. $return[$key]->next_page = $this->build_page_handle( $last_post, $query );
  264. }
  265. }
  266. break;
  267. }
  268. }
  269. $return['found'] -= $excluded_count;
  270. return $return;
  271. }
  272. function build_page_handle( $post, $query ) {
  273. $column = $query['orderby'];
  274. if ( ! $column ) {
  275. $column = 'date';
  276. }
  277. return build_query( array( 'value' => urlencode($post[$column]), 'id' => $post['ID'] ) );
  278. }
  279. function _build_date_range_query( $column, $range, $where ) {
  280. global $wpdb;
  281. switch ( count( $range ) ) {
  282. case 2 :
  283. $where .= $wpdb->prepare(
  284. " AND `$wpdb->posts`.$column >= CAST( %s AS DATETIME ) AND `$wpdb->posts`.$column < CAST( %s AS DATETIME ) ",
  285. $range['after'],
  286. $range['before']
  287. );
  288. break;
  289. case 1 :
  290. if ( isset( $range['before'] ) ) {
  291. $where .= $wpdb->prepare(
  292. " AND `$wpdb->posts`.$column < CAST( %s AS DATETIME ) ",
  293. $range['before']
  294. );
  295. } else {
  296. $where .= $wpdb->prepare(
  297. " AND `$wpdb->posts`.$column > CAST( %s AS DATETIME ) ",
  298. $range['after']
  299. );
  300. }
  301. break;
  302. }
  303. return $where;
  304. }
  305. function handle_date_range( $where ) {
  306. return $this->_build_date_range_query( 'post_date', $this->date_range, $where );
  307. }
  308. function handle_modified_range( $where ) {
  309. return $this->_build_date_range_query( 'post_modified_gmt', $this->modified_range, $where );
  310. }
  311. function handle_where_for_page_handle( $where ) {
  312. global $wpdb;
  313. $column = $this->performed_query['orderby'];
  314. if ( ! $column ) {
  315. $column = 'date';
  316. }
  317. $order = $this->performed_query['order'];
  318. if ( ! $order ) {
  319. $order = 'DESC';
  320. }
  321. if ( ! in_array( $column, array( 'ID', 'title', 'date', 'modified', 'comment_count' ) ) ) {
  322. return $where;
  323. }
  324. if ( ! in_array( $order, array( 'DESC', 'ASC' ) ) ) {
  325. return $where;
  326. }
  327. $db_column = '';
  328. $db_value = '';
  329. switch( $column ) {
  330. case 'ID':
  331. $db_column = 'ID';
  332. $db_value = '%d';
  333. break;
  334. case 'title':
  335. $db_column = 'post_title';
  336. $db_value = '%s';
  337. break;
  338. case 'date':
  339. $db_column = 'post_date';
  340. $db_value = 'CAST( %s as DATETIME )';
  341. break;
  342. case 'modified':
  343. $db_column = 'post_modified';
  344. $db_value = 'CAST( %s as DATETIME )';
  345. break;
  346. case 'comment_count':
  347. $db_column = 'comment_count';
  348. $db_value = '%d';
  349. break;
  350. }
  351. if ( 'DESC'=== $order ) {
  352. $db_order = '<';
  353. } else {
  354. $db_order = '>';
  355. }
  356. // Add a clause that limits the results to items beyond the passed item, or equivalent to the passed item
  357. // but with an ID beyond the passed item. When we're ordering by the ID already, we only ask for items
  358. // beyond the passed item.
  359. $where .= $wpdb->prepare( " AND ( ( `$wpdb->posts`.`$db_column` $db_order $db_value ) ", $this->page_handle['value'] );
  360. if ( $db_column !== 'ID' ) {
  361. $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'] );
  362. }
  363. $where .= ' )';
  364. return $where;
  365. }
  366. function handle_orderby_for_page_handle( $orderby ) {
  367. global $wpdb;
  368. if ( $this->performed_query['orderby'] === 'ID' ) {
  369. // bail if we're already ordering by ID
  370. return $orderby;
  371. }
  372. if ( $orderby ) {
  373. $orderby .= ' ,';
  374. }
  375. $order = $this->performed_query['order'];
  376. if ( ! $order ) {
  377. $order = 'DESC';
  378. }
  379. $orderby .= " `$wpdb->posts`.ID $order";
  380. return $orderby;
  381. }
  382. }