summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYury German <blueknight@gentoo.org>2019-05-22 01:01:36 -0400
committerYury German <blueknight@gentoo.org>2019-05-22 01:01:36 -0400
commit0914c92da22824025992c368c745546e41fbeb84 (patch)
tree965f6adf3b725e56d559fe4a93eff02281499dcc /plugins/jetpack/json-endpoints/class.wpcom-json-api-get-post-counts-v1-1-endpoint.php
parentDeleting plugins for update (diff)
downloadblogs-gentoo-0914c92da22824025992c368c745546e41fbeb84.tar.gz
blogs-gentoo-0914c92da22824025992c368c745546e41fbeb84.tar.bz2
blogs-gentoo-0914c92da22824025992c368c745546e41fbeb84.zip
Adding Plugins
Updating the following akismet.4.1.2, google-authenticator.0.52, jetpack.7.3.1 Signed-off-by: Yury German <blueknight@gentoo.org>
Diffstat (limited to 'plugins/jetpack/json-endpoints/class.wpcom-json-api-get-post-counts-v1-1-endpoint.php')
-rw-r--r--plugins/jetpack/json-endpoints/class.wpcom-json-api-get-post-counts-v1-1-endpoint.php137
1 files changed, 137 insertions, 0 deletions
diff --git a/plugins/jetpack/json-endpoints/class.wpcom-json-api-get-post-counts-v1-1-endpoint.php b/plugins/jetpack/json-endpoints/class.wpcom-json-api-get-post-counts-v1-1-endpoint.php
new file mode 100644
index 00000000..5e83b41d
--- /dev/null
+++ b/plugins/jetpack/json-endpoints/class.wpcom-json-api-get-post-counts-v1-1-endpoint.php
@@ -0,0 +1,137 @@
+<?php
+
+new WPCOM_JSON_API_GET_Post_Counts_V1_1_Endpoint( array(
+ 'description' => 'Get number of posts in the post type groups by post status',
+ 'group' => 'sites',
+ 'stat' => 'sites:X:post-counts:X',
+ 'force' => 'wpcom',
+ 'method' => 'GET',
+ 'min_version' => '1.1',
+ 'max_version' => '1.2',
+ 'path' => '/sites/%s/post-counts/%s',
+ 'path_labels' => array(
+ '$site' => '(int|string) Site ID or domain',
+ '$post_type' => '(string) Post Type',
+ ),
+
+ 'query_parameters' => array(
+ 'context' => false,
+ 'author' => '(int) author ID',
+ ),
+
+ 'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/en.blog.wordpress.com/post-counts/page',
+
+ 'response_format' => array(
+ 'counts' => array(
+ 'all' => '(array) Number of posts by any author in the post type grouped by post status',
+ 'mine' => '(array) Number of posts by the current user in the post type grouped by post status'
+ )
+ )
+) );
+
+class WPCOM_JSON_API_GET_Post_Counts_V1_1_Endpoint extends WPCOM_JSON_API_Endpoint {
+
+ private $whitelist = array( 'publish' );
+
+ /**
+ * Build SQL query
+ *
+ * @param {String} type - post type
+ * @param {Number} [author]
+ * @return {String} SQL query
+ */
+ private function buildCountsQuery( $post_type = 'post', $user_id = null ) {
+ global $wpdb;
+
+ $query = "SELECT post_status as status, count(*) as count ";
+ $query .= "FROM {$wpdb->posts} ";
+ $query .= "WHERE post_type = %s ";
+ if ( isset( $user_id ) ) {
+ $query .= "AND post_author = %d ";
+ }
+
+ $query .= "GROUP BY status";
+
+ return $wpdb->prepare( $query, $post_type, $user_id );
+ }
+
+ /**
+ * Retrive counts using wp_cache
+ *
+ * @param {String} $post_type
+ * @param {Number} [$id]
+ */
+ private function retrieveCounts( $post_type, $id = null) {
+ if ( ! isset( $id ) ) {
+ $counts = array();
+ foreach( (array) wp_count_posts( $post_type ) as $status => $count ) {
+ if ( in_array( $status, $this->whitelist ) && $count > 0 ) {
+ $counts[ $status ] = (int) $count;
+ }
+ };
+
+ return $counts;
+ }
+
+ global $wpdb;
+ $key = 'rest-api-' . $id . '-' . _count_posts_cache_key( $post_type );
+ $counts = wp_cache_get( $key, 'counts' );
+
+ if ( false === $counts ) {
+ $results = $wpdb->get_results( $this->buildCountsQuery( $post_type, $id ) );
+ $counts = $this->filterStatusesByWhiteslist( $results );
+ wp_cache_set( $key, $counts, 'counts' );
+ }
+
+ return $counts;
+ }
+
+ private function filterStatusesByWhiteslist( $in ) {
+ $return = array();
+ foreach( $in as $result) {
+ if ( in_array( $result->status, $this->whitelist ) ) {
+ $return[ $result->status ] = (int) $result->count;
+ }
+ };
+ return $return;
+ }
+
+ // /sites/%s/post-counts/%s
+ public function callback( $path = '', $blog_id = 0, $post_type = 'post' ) {
+ if ( ! get_current_user_id() ) {
+ return new WP_Error( 'authorization_required', __( 'An active access token must be used to retrieve post counts.', 'jetpack' ), 403 );
+ }
+
+ $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ), false );
+
+ if ( is_wp_error( $blog_id ) ) {
+ return $blog_id;
+ }
+
+ if ( ! post_type_exists( $post_type ) ) {
+ return new WP_Error( 'unknown_post_type', __( 'Unknown post type requested.', 'jetpack' ), 404 );
+ }
+
+ $args = $this->query_args();
+ $mine_ID = get_current_user_id();
+
+ if ( current_user_can( 'edit_posts' ) ) {
+ array_push( $this->whitelist, 'draft', 'future', 'pending', 'private', 'trash' );
+ }
+
+ $return = array(
+ 'counts' => (array) array(
+ 'all' => (object) $this->retrieveCounts( $post_type ),
+ 'mine' => (object) $this->retrieveCounts( $post_type, $mine_ID ),
+ )
+ );
+
+ // AUTHOR
+ if ( isset( $args['author'] ) ) {
+ $author_ID = $args['author'];
+ $return['counts']['author'] = (object) $this->retrieveCounts( $post_type, $author_ID );
+ }
+
+ return (object) $return;
+ }
+}