WPCOM_JSON_API_GET_Post_Counts_V1_1_Endpoint

The Jetpack by WordPress.com WPCOM JSON API GET Post Counts V1 1 Endpoint class.

Defined (1)

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

/json-endpoints/class.wpcom-json-api-get-post-counts-v1-1-endpoint.php  
  1. class WPCOM_JSON_API_GET_Post_Counts_V1_1_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2.  
  3. private $whitelist = array( 'publish' ); 
  4.  
  5. /** 
  6. * Build SQL query 
  7. * @param {String} type - post type 
  8. * @param {Number} [author] 
  9. * @return {String} SQL query 
  10. */ 
  11. private function buildCountsQuery( $post_type = 'post', $user_id = null ) { 
  12. global $wpdb; 
  13.  
  14. $query = "SELECT post_status as status, count(*) as count "; 
  15. $query .= "FROM {$wpdb->posts} "; 
  16. $query .= "WHERE post_type = %s "; 
  17. if ( isset( $user_id ) ) { 
  18. $query .= "AND post_author = %d "; 
  19.  
  20. $query .= "GROUP BY status"; 
  21.  
  22. return $wpdb->prepare( $query, $post_type, $user_id ); 
  23.  
  24. /** 
  25. * Retrive counts using wp_cache 
  26. * @param {String} $post_type 
  27. * @param {Number} [$id] 
  28. */ 
  29. private function retrieveCounts( $post_type, $id = null) { 
  30. if ( ! isset( $id ) ) { 
  31. $counts = array(); 
  32. foreach( (array) wp_count_posts( $post_type ) as $status => $count ) { 
  33. if ( in_array( $status, $this->whitelist ) && $count > 0 ) { 
  34. $counts[ $status ] = (int) $count; 
  35. }; 
  36.  
  37. return $counts; 
  38.  
  39. global $wpdb; 
  40. $key = 'rest-api-' . $id . '-' . _count_posts_cache_key( $post_type ); 
  41. $counts = wp_cache_get( $key, 'counts' ); 
  42.  
  43. if ( false === $counts ) { 
  44. $results = $wpdb->get_results( $this->buildCountsQuery( $post_type, $id ) ); 
  45. $counts = $this->filterStatusesByWhiteslist( $results ); 
  46. wp_cache_set( $key, $counts, 'counts' ); 
  47.  
  48. return $counts; 
  49.  
  50. private function filterStatusesByWhiteslist( $in ) { 
  51. $return = array(); 
  52. foreach( $in as $result) { 
  53. if ( in_array( $result->status, $this->whitelist ) ) { 
  54. $return[ $result->status ] = (int) $result->count; 
  55. }; 
  56. return $return; 
  57.  
  58. public function callback( $path = '', $blog_id = 0, $post_type = 'post' ) { 
  59. if ( ! get_current_user_id() ) { 
  60. return new WP_Error( 'authorization_required', __( 'An active access token must be used to retrieve post counts.', 'jetpack' ), 403 ); 
  61.  
  62. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ), false ); 
  63.  
  64. if ( is_wp_error( $blog_id ) ) { 
  65. return $blog_id; 
  66.  
  67. if ( ! post_type_exists( $post_type ) ) { 
  68. return new WP_Error( 'unknown_post_type', __( 'Unknown post type requested.', 'jetpack' ), 404 ); 
  69.  
  70. $args = $this->query_args(); 
  71. $mine_ID = get_current_user_id(); 
  72.  
  73. if ( current_user_can( 'edit_posts' ) ) { 
  74. array_push( $this->whitelist, 'draft', 'future', 'pending', 'private', 'trash' ); 
  75.  
  76. $return = array( 
  77. 'counts' => (array) array( 
  78. 'all' => (object) $this->retrieveCounts( $post_type ),  
  79. 'mine' => (object) $this->retrieveCounts( $post_type, $mine_ID ),  
  80. ); 
  81.  
  82. // AUTHOR 
  83. if ( isset( $args['author'] ) ) { 
  84. $author_ID = $args['author']; 
  85. $return['counts']['author'] = (object) $this->retrieveCounts( $post_type, $author_ID ); 
  86.  
  87. return (object) $return;