Completed
Push — fix/normalize-www-in-site-url-... ( e67e76 )
by
unknown
13:13 queued 02:59
created

...pcom-json-api-get-post-counts-v1-1-endpoint.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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