Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | new WPCOM_JSON_API_GET_Post_Counts_V1_1_Endpoint( array( |
||
| 4 | 'description' => 'Get number of posts in the post type groups by post status', |
||
| 5 | 'group' => 'sites', |
||
| 6 | 'stat' => 'sites:X:post-counts:X', |
||
| 7 | 'force' => 'wpcom', |
||
| 8 | 'method' => 'GET', |
||
| 9 | 'min_version' => '1.1', |
||
| 10 | 'max_version' => '1.2', |
||
| 11 | 'path' => '/sites/%s/post-counts/%s', |
||
| 12 | 'path_labels' => array( |
||
| 13 | '$site' => '(int|string) Site ID or domain', |
||
| 14 | '$post_type' => '(string) Post Type', |
||
| 15 | ), |
||
| 16 | |||
| 17 | 'query_parameters' => array( |
||
| 18 | 'context' => false, |
||
| 19 | 'author' => '(int) author ID', |
||
| 20 | ), |
||
| 21 | |||
| 22 | 'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/en.blog.wordpress.com/post-counts/page', |
||
| 23 | |||
| 24 | 'response_format' => array( |
||
| 25 | 'counts' => array( |
||
| 26 | 'all' => '(array) Number of posts by any author in the post type grouped by post status', |
||
| 27 | 'mine' => '(array) Number of posts by the current user in the post type grouped by post status' |
||
| 28 | ) |
||
| 29 | ) |
||
| 30 | ) ); |
||
| 31 | |||
| 32 | class WPCOM_JSON_API_GET_Post_Counts_V1_1_Endpoint extends WPCOM_JSON_API_Endpoint { |
||
| 33 | |||
| 34 | private $whitelist = array( 'publish' ); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Build SQL query |
||
| 38 | * |
||
| 39 | * @param {String} type - post type |
||
| 40 | * @param {Number} [author] |
||
| 41 | * @return {String} SQL query |
||
| 42 | */ |
||
| 43 | private function buildCountsQuery( $post_type = 'post', $user_id = null ) { |
||
| 44 | global $wpdb; |
||
| 45 | |||
| 46 | $query = "SELECT post_status as status, count(*) as count "; |
||
| 47 | $query .= "FROM {$wpdb->posts} "; |
||
| 48 | $query .= "WHERE post_type = %s "; |
||
| 49 | if ( isset( $user_id ) ) { |
||
| 50 | $query .= "AND post_author = %d "; |
||
| 51 | } |
||
| 52 | |||
| 53 | $query .= "GROUP BY status"; |
||
| 54 | |||
| 55 | return $wpdb->prepare( $query, $post_type, $user_id ); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Retrive counts using wp_cache |
||
| 60 | * |
||
| 61 | * @param {String} $post_type |
||
| 62 | * @param {Number} [$id] |
||
| 63 | */ |
||
| 64 | private function retrieveCounts( $post_type, $id = null) { |
||
| 65 | if ( ! isset( $id ) ) { |
||
| 66 | $counts = array(); |
||
| 67 | foreach( (array) wp_count_posts( $post_type ) as $status => $count ) { |
||
| 68 | if ( in_array( $status, $this->whitelist ) && $count > 0 ) { |
||
| 69 | $counts[ $status ] = (int) $count; |
||
| 70 | } |
||
| 71 | }; |
||
| 72 | |||
| 73 | return $counts; |
||
| 74 | } |
||
| 75 | |||
| 76 | global $wpdb; |
||
| 77 | $key = 'rest-api-' . $id . '-' . _count_posts_cache_key( $post_type ); |
||
| 78 | $counts = wp_cache_get( $key, 'counts' ); |
||
| 79 | |||
| 80 | if ( false === $counts ) { |
||
| 81 | $results = $wpdb->get_results( $this->buildCountsQuery( $post_type, $id ) ); |
||
| 82 | $counts = $this->filterStatusesByWhiteslist( $results ); |
||
| 83 | wp_cache_set( $key, $counts, 'counts' ); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $counts; |
||
| 87 | } |
||
| 88 | |||
| 89 | private function filterStatusesByWhiteslist( $in ) { |
||
| 90 | $return = array(); |
||
| 91 | foreach( $in as $result) { |
||
| 92 | if ( in_array( $result->status, $this->whitelist ) ) { |
||
| 93 | $return[ $result->status ] = (int) $result->count; |
||
| 94 | } |
||
| 95 | }; |
||
| 96 | return $return; |
||
| 97 | } |
||
| 98 | |||
| 99 | // /sites/%s/post-counts/%s |
||
| 100 | public function callback( $path = '', $blog_id = 0, $post_type = 'post' ) { |
||
| 101 | if ( ! get_current_user_id() ) { |
||
| 102 | return new WP_Error( 'authorization_required', __( 'An active access token must be used to retrieve post counts.', 'jetpack' ), 403 ); |
||
|
0 ignored issues
–
show
|
|||
| 103 | } |
||
| 104 | |||
| 105 | $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ), false ); |
||
| 106 | |||
| 107 | if ( is_wp_error( $blog_id ) ) { |
||
| 108 | return $blog_id; |
||
| 109 | } |
||
| 110 | |||
| 111 | View Code Duplication | if ( ! in_array( $post_type, array( 'post', 'revision', 'page', 'any' ) ) && defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
|
| 112 | $this->load_theme_functions(); |
||
| 113 | } |
||
| 114 | |||
| 115 | if ( ! post_type_exists( $post_type ) ) { |
||
| 116 | return new WP_Error( 'unknown_post_type', __( 'Unknown post type requested.', 'jetpack' ), 404 ); |
||
|
0 ignored issues
–
show
The call to
WP_Error::__construct() has too many arguments starting with 'unknown_post_type'.
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. In this case you can add the Loading history...
|
|||
| 117 | } |
||
| 118 | |||
| 119 | $args = $this->query_args(); |
||
| 120 | $mine_ID = get_current_user_id(); |
||
| 121 | |||
| 122 | if ( current_user_can( 'edit_posts' ) ) { |
||
| 123 | array_push( $this->whitelist, 'draft', 'future', 'pending', 'private', 'trash' ); |
||
| 124 | } |
||
| 125 | |||
| 126 | $return = array( |
||
| 127 | 'counts' => (array) array( |
||
| 128 | 'all' => (object) $this->retrieveCounts( $post_type ), |
||
| 129 | 'mine' => (object) $this->retrieveCounts( $post_type, $mine_ID ), |
||
| 130 | ) |
||
| 131 | ); |
||
| 132 | |||
| 133 | // AUTHOR |
||
| 134 | if ( isset( $args['author'] ) ) { |
||
| 135 | $author_ID = $args['author']; |
||
| 136 | $return['counts']['author'] = (object) $this->retrieveCounts( $post_type, $author_ID ); |
||
| 137 | } |
||
| 138 | |||
| 139 | return (object) $return; |
||
| 140 | } |
||
| 141 | } |
||
| 142 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.