class.wpcom-json-api-get-post-counts-v1-1-endpoint.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. new WPCOM_JSON_API_GET_Post_Counts_V1_1_Endpoint( array(
  3. 'description' => 'Get number of posts in the post type groups by post status',
  4. 'group' => 'sites',
  5. 'stat' => 'sites:X:post-counts:X',
  6. 'force' => 'wpcom',
  7. 'method' => 'GET',
  8. 'min_version' => '1.1',
  9. 'max_version' => '1.2',
  10. 'path' => '/sites/%s/post-counts/%s',
  11. 'path_labels' => array(
  12. '$site' => '(int|string) Site ID or domain',
  13. '$post_type' => '(string) Post Type',
  14. ),
  15. 'query_parameters' => array(
  16. 'context' => false,
  17. 'author' => '(int) author ID',
  18. ),
  19. 'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/en.blog.wordpress.com/post-counts/page',
  20. 'response_format' => array(
  21. 'counts' => array(
  22. 'all' => '(array) Number of posts by any author in the post type grouped by post status',
  23. 'mine' => '(array) Number of posts by the current user in the post type grouped by post status'
  24. )
  25. )
  26. ) );
  27. class WPCOM_JSON_API_GET_Post_Counts_V1_1_Endpoint extends WPCOM_JSON_API_Endpoint {
  28. private $whitelist = array( 'publish' );
  29. /**
  30. * Build SQL query
  31. *
  32. * @param {String} type - post type
  33. * @param {Number} [author]
  34. * @return {String} SQL query
  35. */
  36. private function buildCountsQuery( $post_type = 'post', $user_id = null ) {
  37. global $wpdb;
  38. $query = "SELECT post_status as status, count(*) as count ";
  39. $query .= "FROM {$wpdb->posts} ";
  40. $query .= "WHERE post_type = %s ";
  41. if ( isset( $user_id ) ) {
  42. $query .= "AND post_author = %d ";
  43. }
  44. $query .= "GROUP BY status";
  45. return $wpdb->prepare( $query, $post_type, $user_id );
  46. }
  47. /**
  48. * Retrive counts using wp_cache
  49. *
  50. * @param {String} $post_type
  51. * @param {Number} [$id]
  52. */
  53. private function retrieveCounts( $post_type, $id = null) {
  54. if ( ! isset( $id ) ) {
  55. $counts = array();
  56. foreach( (array) wp_count_posts( $post_type ) as $status => $count ) {
  57. if ( in_array( $status, $this->whitelist ) && $count > 0 ) {
  58. $counts[ $status ] = (int) $count;
  59. }
  60. };
  61. return $counts;
  62. }
  63. global $wpdb;
  64. $key = 'rest-api-' . $id . '-' . _count_posts_cache_key( $post_type );
  65. $counts = wp_cache_get( $key, 'counts' );
  66. if ( false === $counts ) {
  67. $results = $wpdb->get_results( $this->buildCountsQuery( $post_type, $id ) );
  68. $counts = $this->filterStatusesByWhiteslist( $results );
  69. wp_cache_set( $key, $counts, 'counts' );
  70. }
  71. return $counts;
  72. }
  73. private function filterStatusesByWhiteslist( $in ) {
  74. $return = array();
  75. foreach( $in as $result) {
  76. if ( in_array( $result->status, $this->whitelist ) ) {
  77. $return[ $result->status ] = (int) $result->count;
  78. }
  79. };
  80. return $return;
  81. }
  82. // /sites/%s/post-counts/%s
  83. public function callback( $path = '', $blog_id = 0, $post_type = 'post' ) {
  84. if ( ! get_current_user_id() ) {
  85. return new WP_Error( 'authorization_required', __( 'An active access token must be used to retrieve post counts.', 'jetpack' ), 403 );
  86. }
  87. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ), false );
  88. if ( is_wp_error( $blog_id ) ) {
  89. return $blog_id;
  90. }
  91. if ( ! post_type_exists( $post_type ) ) {
  92. return new WP_Error( 'unknown_post_type', __( 'Unknown post type requested.', 'jetpack' ), 404 );
  93. }
  94. $args = $this->query_args();
  95. $mine_ID = get_current_user_id();
  96. if ( current_user_can( 'edit_posts' ) ) {
  97. array_push( $this->whitelist, 'draft', 'future', 'pending', 'private', 'trash' );
  98. }
  99. $return = array(
  100. 'counts' => (array) array(
  101. 'all' => (object) $this->retrieveCounts( $post_type ),
  102. 'mine' => (object) $this->retrieveCounts( $post_type, $mine_ID ),
  103. )
  104. );
  105. // AUTHOR
  106. if ( isset( $args['author'] ) ) {
  107. $author_ID = $args['author'];
  108. $return['counts']['author'] = (object) $this->retrieveCounts( $post_type, $author_ID );
  109. }
  110. return (object) $return;
  111. }
  112. }