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_List_Posts_v1_2_Endpoint( array( |
||
| 4 | 'description' => 'Get a list of matching posts.', |
||
| 5 | 'min_version' => '1.2', |
||
| 6 | 'max_version' => '1.2', |
||
| 7 | |||
| 8 | 'group' => 'posts', |
||
| 9 | 'stat' => 'posts', |
||
| 10 | |||
| 11 | 'method' => 'GET', |
||
| 12 | 'path' => '/sites/%s/posts/', |
||
| 13 | 'path_labels' => array( |
||
| 14 | '$site' => '(int|string) Site ID or domain', |
||
| 15 | ), |
||
| 16 | |||
| 17 | 'query_parameters' => array( |
||
| 18 | 'number' => '(int=20) The number of posts to return. Limit: 100.', |
||
| 19 | 'offset' => '(int=0) 0-indexed offset.', |
||
| 20 | 'page' => '(int) Return the Nth 1-indexed page of posts. Takes precedence over the <code>offset</code> parameter.', |
||
| 21 | 'page_handle' => '(string) A page handle, returned from a previous API call as a <code>meta.next_page</code> property. This is the most efficient way to fetch the next page of results.', |
||
| 22 | 'order' => array( |
||
| 23 | 'DESC' => 'Return posts in descending order. For dates, that means newest to oldest.', |
||
| 24 | 'ASC' => 'Return posts in ascending order. For dates, that means oldest to newest.', |
||
| 25 | ), |
||
| 26 | 'order_by' => array( |
||
| 27 | 'date' => 'Order by the created time of each post.', |
||
| 28 | 'modified' => 'Order by the modified time of each post.', |
||
| 29 | 'title' => "Order lexicographically by the posts' titles.", |
||
| 30 | 'comment_count' => 'Order by the number of comments for each post.', |
||
| 31 | 'ID' => 'Order by post ID.', |
||
| 32 | ), |
||
| 33 | 'after' => '(ISO 8601 datetime) Return posts dated after the specified datetime.', |
||
| 34 | 'before' => '(ISO 8601 datetime) Return posts dated before the specified datetime.', |
||
| 35 | 'modified_after' => '(ISO 8601 datetime) Return posts modified after the specified datetime.', |
||
| 36 | 'modified_before' => '(ISO 8601 datetime) Return posts modified before the specified datetime.', |
||
| 37 | 'tag' => '(string) Specify the tag name or slug.', |
||
| 38 | 'category' => '(string) Specify the category name or slug.', |
||
| 39 | 'term' => '(object:string) Specify comma-separated term slugs to search within, indexed by taxonomy slug.', |
||
| 40 | 'type' => "(string) Specify the post type. Defaults to 'post', use 'any' to query for both posts and pages. Post types besides post and page need to be whitelisted using the <code>rest_api_allowed_post_types</code> filter.", |
||
| 41 | 'exclude_private_types' => '(bool=false) Use this flag together with `type=any` to get only publicly accessible posts.', |
||
| 42 | 'parent_id' => '(int) Returns only posts which are children of the specified post. Applies only to hierarchical post types.', |
||
| 43 | 'exclude' => '(array:int|int) Excludes the specified post ID(s) from the response', |
||
| 44 | 'exclude_tree' => '(int) Excludes the specified post and all of its descendants from the response. Applies only to hierarchical post types.', |
||
| 45 | 'status' => '(string) Comma-separated list of statuses for which to query, including any of: "publish", "private", "draft", "pending", "future", and "trash", or simply "any". Defaults to "publish"', |
||
| 46 | 'sticky' => array( |
||
| 47 | 'include' => 'Sticky posts are not excluded from the list.', |
||
| 48 | 'exclude' => 'Sticky posts are excluded from the list.', |
||
| 49 | 'require' => 'Only include sticky posts', |
||
| 50 | ), |
||
| 51 | 'author' => "(int) Author's user ID", |
||
| 52 | 'search' => '(string) Search query', |
||
| 53 | 'meta_key' => '(string) Metadata key that the post should contain', |
||
| 54 | 'meta_value' => '(string) Metadata value that the post should contain. Will only be applied if a `meta_key` is also given', |
||
| 55 | ), |
||
| 56 | |||
| 57 | 'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/en.blog.wordpress.com/posts/?number=2' |
||
| 58 | ) ); |
||
| 59 | |||
| 60 | class WPCOM_JSON_API_List_Posts_v1_2_Endpoint extends WPCOM_JSON_API_List_Posts_v1_1_Endpoint { |
||
| 61 | // /sites/%s/posts/ -> $blog_id |
||
| 62 | function callback( $path = '', $blog_id = 0 ) { |
||
| 63 | $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
||
| 64 | if ( is_wp_error( $blog_id ) ) { |
||
| 65 | return $blog_id; |
||
| 66 | } |
||
| 67 | |||
| 68 | $args = $this->query_args(); |
||
| 69 | $is_eligible_for_page_handle = true; |
||
| 70 | $site = $this->get_platform()->get_site( $blog_id ); |
||
| 71 | |||
| 72 | if ( $args['number'] < 1 ) { |
||
| 73 | $args['number'] = 20; |
||
| 74 | } elseif ( 100 < $args['number'] ) { |
||
| 75 | return new WP_Error( 'invalid_number', 'The NUMBER parameter must be less than or equal to 100.', 400 ); |
||
| 76 | } |
||
| 77 | |||
| 78 | if ( isset( $args['type'] ) ) { |
||
| 79 | // load all types on WPCOM, unless only built-in ones are requested |
||
| 80 | View Code Duplication | if ( defined( 'IS_WPCOM' ) && IS_WPCOM && ! in_array( $args['type'], array( 'post', 'revision', 'page' ) ) ) { |
|
| 81 | $this->load_theme_functions(); |
||
| 82 | } |
||
| 83 | |||
| 84 | if ( ! $site->is_post_type_allowed( $args['type'] ) ) { |
||
| 85 | return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 ); |
||
| 86 | } |
||
| 87 | |||
| 88 | // Normalize post_type |
||
| 89 | if ( 'any' == $args['type'] ) { |
||
| 90 | $whitelisted_post_types = $site->get_whitelisted_post_types(); |
||
| 91 | |||
| 92 | if ( isset( $args['exclude_private_types'] ) && $args['exclude_private_types'] == true ) { |
||
| 93 | $public_post_types = get_post_types( array( 'public' => true ) ); |
||
| 94 | $args['type'] = array_intersect( $public_post_types, $whitelisted_post_types ); |
||
| 95 | } else { |
||
| 96 | $args['type'] = $whitelisted_post_types; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } else { |
||
| 100 | // let's be explicit about defaulting to 'post' |
||
| 101 | $args['type'] = 'post'; |
||
| 102 | } |
||
| 103 | |||
| 104 | // make sure the user can read or edit the requested post type(s) |
||
| 105 | View Code Duplication | if ( is_array( $args['type'] ) ) { |
|
| 106 | $allowed_types = array(); |
||
| 107 | foreach ( $args['type'] as $post_type ) { |
||
| 108 | if ( $site->current_user_can_access_post_type( $post_type, $args['context'] ) ) { |
||
| 109 | $allowed_types[] = $post_type; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | if ( empty( $allowed_types ) ) { |
||
| 114 | return array( 'found' => 0, 'posts' => array() ); |
||
| 115 | } |
||
| 116 | $args['type'] = $allowed_types; |
||
| 117 | } |
||
| 118 | else { |
||
| 119 | if ( ! $site->current_user_can_access_post_type( $args['type'], $args['context'] ) ) { |
||
| 120 | return array( 'found' => 0, 'posts' => array() ); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | // determine statuses |
||
| 125 | $status = ( ! empty( $args['status'] ) ) ? explode( ',', $args['status'] ) : array( 'publish' ); |
||
| 126 | View Code Duplication | if ( is_user_logged_in() ) { |
|
| 127 | $statuses_whitelist = array( |
||
| 128 | 'publish', |
||
| 129 | 'pending', |
||
| 130 | 'draft', |
||
| 131 | 'future', |
||
| 132 | 'private', |
||
| 133 | 'trash', |
||
| 134 | 'any', |
||
| 135 | ); |
||
| 136 | $status = array_intersect( $status, $statuses_whitelist ); |
||
| 137 | } else { |
||
| 138 | // logged-out users can see only published posts |
||
| 139 | $statuses_whitelist = array( 'publish', 'any' ); |
||
| 140 | $status = array_intersect( $status, $statuses_whitelist ); |
||
| 141 | |||
| 142 | if ( empty( $status ) ) { |
||
| 143 | // requested only protected statuses? nothing for you here |
||
| 144 | return array( 'found' => 0, 'posts' => array() ); |
||
| 145 | } |
||
| 146 | // clear it (AKA published only) because "any" includes protected |
||
| 147 | $status = array(); |
||
| 148 | } |
||
| 149 | |||
| 150 | $query = array( |
||
| 151 | 'posts_per_page' => $args['number'], |
||
| 152 | 'order' => $args['order'], |
||
| 153 | 'orderby' => $args['order_by'], |
||
| 154 | 'post_type' => $args['type'], |
||
| 155 | 'post_status' => $status, |
||
| 156 | 'post_parent' => isset( $args['parent_id'] ) ? $args['parent_id'] : null, |
||
| 157 | 'author' => isset( $args['author'] ) && 0 < $args['author'] ? $args['author'] : null, |
||
| 158 | 's' => isset( $args['search'] ) && '' !== $args['search'] ? $args['search'] : null, |
||
| 159 | 'fields' => 'ids', |
||
| 160 | ); |
||
| 161 | |||
| 162 | if ( ! is_user_logged_in () ) { |
||
| 163 | $query['has_password'] = false; |
||
| 164 | } |
||
| 165 | |||
| 166 | View Code Duplication | if ( isset( $args['meta_key'] ) ) { |
|
| 167 | $show = false; |
||
| 168 | if ( WPCOM_JSON_API_Metadata::is_public( $args['meta_key'] ) ) |
||
| 169 | $show = true; |
||
| 170 | if ( current_user_can( 'edit_post_meta', $query['post_type'], $args['meta_key'] ) ) |
||
| 171 | $show = true; |
||
| 172 | |||
| 173 | if ( is_protected_meta( $args['meta_key'], 'post' ) && ! $show ) |
||
| 174 | return new WP_Error( 'invalid_meta_key', 'Invalid meta key', 404 ); |
||
| 175 | |||
| 176 | $meta = array( 'key' => $args['meta_key'] ); |
||
| 177 | if ( isset( $args['meta_value'] ) ) |
||
| 178 | $meta['value'] = $args['meta_value']; |
||
| 179 | |||
| 180 | $query['meta_query'] = array( $meta ); |
||
| 181 | } |
||
| 182 | |||
| 183 | View Code Duplication | if ( $args['sticky'] === 'include' ) { |
|
| 184 | $query['ignore_sticky_posts'] = 1; |
||
| 185 | } else if ( $args['sticky'] === 'exclude' ) { |
||
| 186 | $sticky = get_option( 'sticky_posts' ); |
||
| 187 | if ( is_array( $sticky ) ) { |
||
| 188 | $query['post__not_in'] = $sticky; |
||
| 189 | } |
||
| 190 | } else if ( $args['sticky'] === 'require' ) { |
||
| 191 | $sticky = get_option( 'sticky_posts' ); |
||
| 192 | if ( is_array( $sticky ) && ! empty( $sticky ) ) { |
||
| 193 | $query['post__in'] = $sticky; |
||
| 194 | } else { |
||
| 195 | // no sticky posts exist |
||
| 196 | return array( 'found' => 0, 'posts' => array() ); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | View Code Duplication | if ( isset( $args['exclude'] ) ) { |
|
| 201 | $excluded_ids = (array) $args['exclude']; |
||
| 202 | $query['post__not_in'] = isset( $query['post__not_in'] ) ? array_merge( $query['post__not_in'], $excluded_ids ) : $excluded_ids; |
||
| 203 | } |
||
| 204 | |||
| 205 | View Code Duplication | if ( isset( $args['exclude_tree'] ) && is_post_type_hierarchical( $args['type'] ) ) { |
|
| 206 | // get_page_children is a misnomer; it supports all hierarchical post types |
||
| 207 | $page_args = array( |
||
| 208 | 'child_of' => $args['exclude_tree'], |
||
| 209 | 'post_type' => $args['type'], |
||
| 210 | // since we're looking for things to exclude, be aggressive |
||
| 211 | 'post_status' => 'publish,draft,pending,private,future,trash', |
||
| 212 | ); |
||
| 213 | $post_descendants = get_pages( $page_args ); |
||
| 214 | |||
| 215 | $exclude_tree = array( $args['exclude_tree'] ); |
||
| 216 | foreach ( $post_descendants as $child ) { |
||
| 217 | $exclude_tree[] = $child->ID; |
||
| 218 | } |
||
| 219 | |||
| 220 | $query['post__not_in'] = isset( $query['post__not_in'] ) ? array_merge( $query['post__not_in'], $exclude_tree ) : $exclude_tree; |
||
| 221 | } |
||
| 222 | |||
| 223 | View Code Duplication | if ( isset( $args['category'] ) ) { |
|
| 224 | $category = get_term_by( 'slug', $args['category'], 'category' ); |
||
| 225 | if ( $category === false) { |
||
| 226 | $query['category_name'] = $args['category']; |
||
| 227 | } else { |
||
| 228 | $query['cat'] = $category->term_id; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | if ( isset( $args['tag'] ) ) { |
||
| 233 | $query['tag'] = $args['tag']; |
||
| 234 | } |
||
| 235 | |||
| 236 | View Code Duplication | if ( ! empty( $args['term'] ) ) { |
|
| 237 | $query['tax_query'] = array(); |
||
| 238 | foreach ( $args['term'] as $taxonomy => $slug ) { |
||
| 239 | $taxonomy_object = get_taxonomy( $taxonomy ); |
||
| 240 | if ( false === $taxonomy_object || ( ! $taxonomy_object->public && |
||
| 241 | ! current_user_can( $taxonomy_object->cap->assign_terms ) ) ) { |
||
| 242 | continue; |
||
| 243 | } |
||
| 244 | |||
| 245 | $query['tax_query'][] = array( |
||
| 246 | 'taxonomy' => $taxonomy, |
||
| 247 | 'field' => 'slug', |
||
| 248 | 'terms' => explode( ',', $slug ) |
||
| 249 | ); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | View Code Duplication | if ( isset( $args['page'] ) ) { |
|
| 254 | if ( $args['page'] < 1 ) { |
||
| 255 | $args['page'] = 1; |
||
| 256 | } |
||
| 257 | |||
| 258 | $query['paged'] = $args['page']; |
||
| 259 | if ( $query['paged'] !== 1 ) { |
||
| 260 | $is_eligible_for_page_handle = false; |
||
| 261 | } |
||
| 262 | } else { |
||
| 263 | if ( $args['offset'] < 0 ) { |
||
| 264 | $args['offset'] = 0; |
||
| 265 | } |
||
| 266 | |||
| 267 | $query['offset'] = $args['offset']; |
||
| 268 | if ( $query['offset'] !== 0 ) { |
||
| 269 | $is_eligible_for_page_handle = false; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | if ( isset( $args['before'] ) ) { |
||
| 274 | $this->date_range['before'] = $args['before']; |
||
| 275 | } |
||
| 276 | if ( isset( $args['after'] ) ) { |
||
| 277 | $this->date_range['after'] = $args['after']; |
||
| 278 | } |
||
| 279 | |||
| 280 | if ( isset( $args['modified_before_gmt'] ) ) { |
||
| 281 | $this->modified_range['before'] = $args['modified_before_gmt']; |
||
| 282 | } |
||
| 283 | if ( isset( $args['modified_after_gmt'] ) ) { |
||
| 284 | $this->modified_range['after'] = $args['modified_after_gmt']; |
||
| 285 | } |
||
| 286 | |||
| 287 | if ( $this->date_range ) { |
||
| 288 | add_filter( 'posts_where', array( $this, 'handle_date_range' ) ); |
||
| 289 | } |
||
| 290 | |||
| 291 | if ( $this->modified_range ) { |
||
| 292 | add_filter( 'posts_where', array( $this, 'handle_modified_range' ) ); |
||
| 293 | } |
||
| 294 | |||
| 295 | View Code Duplication | if ( isset( $args['page_handle'] ) ) { |
|
| 296 | $page_handle = wp_parse_args( $args['page_handle'] ); |
||
| 297 | if ( isset( $page_handle['value'] ) && isset( $page_handle['id'] ) ) { |
||
| 298 | // we have a valid looking page handle |
||
| 299 | $this->page_handle = $page_handle; |
||
| 300 | add_filter( 'posts_where', array( $this, 'handle_where_for_page_handle' ) ); |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * 'column' necessary for the me/posts endpoint (which extends sites/$site/posts). |
||
| 306 | * Would need to be added to the sites/$site/posts definition if we ever want to |
||
| 307 | * use it there. |
||
| 308 | */ |
||
| 309 | $column_whitelist = array( 'post_modified_gmt' ); |
||
| 310 | View Code Duplication | if ( isset( $args['column'] ) && in_array( $args['column'], $column_whitelist ) ) { |
|
| 311 | $query['column'] = $args['column']; |
||
| 312 | } |
||
| 313 | |||
| 314 | $this->performed_query = $query; |
||
| 315 | add_filter( 'posts_orderby', array( $this, 'handle_orderby_for_page_handle' ) ); |
||
| 316 | |||
| 317 | $wp_query = new WP_Query( $query ); |
||
| 318 | |||
| 319 | remove_filter( 'posts_orderby', array( $this, 'handle_orderby_for_page_handle' ) ); |
||
| 320 | |||
| 321 | if ( $this->date_range ) { |
||
| 322 | remove_filter( 'posts_where', array( $this, 'handle_date_range' ) ); |
||
| 323 | $this->date_range = array(); |
||
| 324 | } |
||
| 325 | |||
| 326 | if ( $this->modified_range ) { |
||
| 327 | remove_filter( 'posts_where', array( $this, 'handle_modified_range' ) ); |
||
| 328 | $this->modified_range = array(); |
||
| 329 | } |
||
| 330 | |||
| 331 | if ( $this->page_handle ) { |
||
|
0 ignored issues
–
show
|
|||
| 332 | remove_filter( 'posts_where', array( $this, 'handle_where_for_page_handle' ) ); |
||
| 333 | |||
| 334 | } |
||
| 335 | |||
| 336 | $return = array(); |
||
| 337 | $excluded_count = 0; |
||
| 338 | View Code Duplication | foreach ( array_keys( $this->response_format ) as $key ) { |
|
| 339 | switch ( $key ) { |
||
| 340 | case 'found' : |
||
| 341 | $return[$key] = (int) $wp_query->found_posts; |
||
| 342 | break; |
||
| 343 | case 'posts' : |
||
| 344 | $posts = array(); |
||
| 345 | foreach ( $wp_query->posts as $post_ID ) { |
||
| 346 | $the_post = $this->get_post_by( 'ID', $post_ID, $args['context'] ); |
||
| 347 | if ( $the_post && ! is_wp_error( $the_post ) ) { |
||
| 348 | $posts[] = $the_post; |
||
| 349 | } else { |
||
| 350 | $excluded_count++; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | if ( $posts ) { |
||
| 355 | /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ |
||
| 356 | do_action( 'wpcom_json_api_objects', 'posts', count( $posts ) ); |
||
| 357 | } |
||
| 358 | |||
| 359 | $return[$key] = $posts; |
||
| 360 | break; |
||
| 361 | |||
| 362 | case 'meta' : |
||
| 363 | if ( ! is_array( $args['type'] ) ) { |
||
| 364 | $return[$key] = (object) array( |
||
| 365 | 'links' => (object) array( |
||
| 366 | 'counts' => (string) $this->links->get_site_link( $blog_id, 'post-counts/' . $args['type'] ), |
||
| 367 | ) |
||
| 368 | ); |
||
| 369 | } |
||
| 370 | |||
| 371 | if ( $is_eligible_for_page_handle && $return['posts'] ) { |
||
| 372 | $last_post = end( $return['posts'] ); |
||
| 373 | reset( $return['posts'] ); |
||
| 374 | if ( ( $return['found'] > count( $return['posts'] ) ) && $last_post ) { |
||
| 375 | if ( ! isset( $return[$key] ) ) { |
||
| 376 | $return[$key] = (object) array(); |
||
| 377 | } |
||
| 378 | $return[$key]->next_page = $this->build_page_handle( $last_post, $query ); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
||
| 383 | if ( !isset( $return[$key] ) ) |
||
| 384 | $return[$key] = new stdClass; |
||
| 385 | $return[$key]->wpcom = true; |
||
| 386 | } |
||
| 387 | |||
| 388 | break; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | $return['found'] -= $excluded_count; |
||
| 393 | |||
| 394 | return $return; |
||
| 395 | } |
||
| 396 | |||
| 397 | View Code Duplication | function build_page_handle( $post, $query ) { |
|
| 398 | $column = $query['orderby']; |
||
| 399 | if ( ! $column ) { |
||
| 400 | $column = 'date'; |
||
| 401 | } |
||
| 402 | return build_query( array( 'value' => urlencode($post[$column]), 'id' => $post['ID'] ) ); |
||
| 403 | } |
||
| 404 | |||
| 405 | View Code Duplication | function _build_date_range_query( $column, $range, $where ) { |
|
| 406 | global $wpdb; |
||
| 407 | |||
| 408 | switch ( count( $range ) ) { |
||
| 409 | case 2 : |
||
| 410 | $where .= $wpdb->prepare( |
||
| 411 | " AND `$wpdb->posts`.$column >= CAST( %s AS DATETIME ) AND `$wpdb->posts`.$column < CAST( %s AS DATETIME ) ", |
||
| 412 | $range['after'], |
||
| 413 | $range['before'] |
||
| 414 | ); |
||
| 415 | break; |
||
| 416 | case 1 : |
||
| 417 | if ( isset( $range['before'] ) ) { |
||
| 418 | $where .= $wpdb->prepare( |
||
| 419 | " AND `$wpdb->posts`.$column < CAST( %s AS DATETIME ) ", |
||
| 420 | $range['before'] |
||
| 421 | ); |
||
| 422 | } else { |
||
| 423 | $where .= $wpdb->prepare( |
||
| 424 | " AND `$wpdb->posts`.$column > CAST( %s AS DATETIME ) ", |
||
| 425 | $range['after'] |
||
| 426 | ); |
||
| 427 | } |
||
| 428 | break; |
||
| 429 | } |
||
| 430 | |||
| 431 | return $where; |
||
| 432 | } |
||
| 433 | } |
||
| 434 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.