2022-06-16 14:01:47 +02:00
< ? php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* List posts endpoint .
*/
new WPCOM_JSON_API_List_Posts_Endpoint (
array (
'description' => 'Get a list of matching posts.' ,
'new_version' => '1.1' ,
'max_version' => '1' ,
'group' => 'posts' ,
'stat' => 'posts' ,
'method' => 'GET' ,
'path' => '/sites/%s/posts/' ,
'path_labels' => array (
'$site' => '(int|string) Site ID or domain' ,
2019-11-15 23:26:29 +01:00
),
2022-06-16 14:01:47 +02:00
'allow_fallback_to_jetpack_blog_token' => true ,
'query_parameters' => array (
'number' => '(int=20) The number of posts to return. Limit: 100.' ,
'offset' => '(int=0) 0-indexed offset.' ,
'page' => '(int) Return the Nth 1-indexed page of posts. Takes precedence over the <code>offset</code> parameter.' ,
'order' => array (
'DESC' => 'Return posts in descending order. For dates, that means newest to oldest.' ,
'ASC' => 'Return posts in ascending order. For dates, that means oldest to newest.' ,
),
'order_by' => array (
'date' => 'Order by the created time of each post.' ,
'modified' => 'Order by the modified time of each post.' ,
'title' => " Order lexicographically by the posts' titles. " ,
'comment_count' => 'Order by the number of comments for each post.' ,
'ID' => 'Order by post ID.' ,
),
'after' => '(ISO 8601 datetime) Return posts dated on or after the specified datetime.' ,
'before' => '(ISO 8601 datetime) Return posts dated on or before the specified datetime.' ,
'tag' => '(string) Specify the tag name or slug.' ,
'category' => '(string) Specify the category name or slug.' ,
'term' => '(object:string) Specify comma-separated term slugs to search within, indexed by taxonomy slug.' ,
'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. " ,
'parent_id' => '(int) Returns only posts which are children of the specified post. Applies only to hierarchical post types.' ,
2023-04-26 17:39:43 +02:00
'include' => '(array:int|int) Includes the specified post ID(s) in the response' ,
2022-06-16 14:01:47 +02:00
'exclude' => '(array:int|int) Excludes the specified post ID(s) from the response' ,
'exclude_tree' => '(int) Excludes the specified post and all of its descendants from the response. Applies only to hierarchical post types.' ,
'status' => array (
'publish' => 'Return only published posts.' ,
'private' => 'Return only private posts.' ,
'draft' => 'Return only draft posts.' ,
'pending' => 'Return only posts pending editorial approval.' ,
'future' => 'Return only posts scheduled for future publishing.' ,
'trash' => 'Return only posts in the trash.' ,
'any' => 'Return all posts regardless of status.' ,
),
'sticky' => array (
'false' => 'Post is not marked as sticky.' ,
'true' => 'Stick the post to the front page.' ,
),
'author' => " (int) Author's user ID " ,
'search' => '(string) Search query' ,
'meta_key' => '(string) Metadata key that the post should contain' ,
'meta_value' => '(string) Metadata value that the post should contain. Will only be applied if a `meta_key` is also given' ,
2019-11-15 23:26:29 +01:00
),
2022-06-16 14:01:47 +02:00
'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/posts/?number=5' ,
)
);
2019-11-15 23:26:29 +01:00
2022-06-16 14:01:47 +02:00
/**
* List posts endpoint class .
*
* / sites /% s / posts / -> $blog_id
*/
2019-11-15 23:26:29 +01:00
class WPCOM_JSON_API_List_Posts_Endpoint extends WPCOM_JSON_API_Post_Endpoint {
2022-06-16 14:01:47 +02:00
/**
* The date range .
*
* @ var array .
*/
2019-11-15 23:26:29 +01:00
public $date_range = array ();
2022-06-16 14:01:47 +02:00
/**
* The response format .
*
* @ var array
*/
2019-11-15 23:26:29 +01:00
public $response_format = array (
2022-06-16 14:01:47 +02:00
'found' => '(int) The total number of posts found that match the request (ignoring limits, offsets, and pagination).' ,
'posts' => '(array:post) An array of post objects.' ,
2019-11-15 23:26:29 +01:00
);
2022-06-16 14:01:47 +02:00
/**
* API callback .
*
* @ param string $path - the path .
* @ param string $blog_id - the blog ID .
*/
public function callback ( $path = '' , $blog_id = 0 ) {
2019-11-15 23:26:29 +01:00
$blog_id = $this -> api -> switch_to_blog_and_validate_user ( $this -> api -> get_blog_id ( $blog_id ) );
if ( is_wp_error ( $blog_id ) ) {
return $blog_id ;
}
$args = $this -> query_args ();
if ( $args [ 'number' ] < 1 ) {
$args [ 'number' ] = 20 ;
} elseif ( 100 < $args [ 'number' ] ) {
2022-06-16 14:01:47 +02:00
return new WP_Error ( 'invalid_number' , 'The NUMBER parameter must be less than or equal to 100.' , 400 );
2019-11-15 23:26:29 +01:00
}
if ( isset ( $args [ 'type' ] ) && ! $this -> is_post_type_allowed ( $args [ 'type' ] ) ) {
return new WP_Error ( 'unknown_post_type' , 'Unknown post type' , 404 );
}
2022-06-16 14:01:47 +02:00
// Normalize post_type.
if ( isset ( $args [ 'type' ] ) && 'any' === $args [ 'type' ] ) {
2019-11-15 23:26:29 +01:00
if ( version_compare ( $this -> api -> version , '1.1' , '<' ) ) {
$args [ 'type' ] = array ( 'post' , 'page' );
} else { // 1.1+
$args [ 'type' ] = $this -> _get_whitelisted_post_types ();
}
}
2022-06-16 14:01:47 +02:00
// determine statuses.
2019-11-15 23:26:29 +01:00
$status = $args [ 'status' ];
$status = ( $status ) ? explode ( ',' , $status ) : array ( 'publish' );
if ( is_user_logged_in () ) {
$statuses_whitelist = array (
'publish' ,
'pending' ,
'draft' ,
'future' ,
'private' ,
'trash' ,
'any' ,
);
2022-06-16 14:01:47 +02:00
$status = array_intersect ( $status , $statuses_whitelist );
2019-11-15 23:26:29 +01:00
} else {
2022-06-16 14:01:47 +02:00
// logged-out users can see only published posts.
2019-11-15 23:26:29 +01:00
$statuses_whitelist = array ( 'publish' , 'any' );
2022-06-16 14:01:47 +02:00
$status = array_intersect ( $status , $statuses_whitelist );
2019-11-15 23:26:29 +01:00
if ( empty ( $status ) ) {
2022-06-16 14:01:47 +02:00
// requested only protected statuses? nothing for you here.
return array (
'found' => 0 ,
'posts' => array (),
);
2019-11-15 23:26:29 +01:00
}
2022-06-16 14:01:47 +02:00
// clear it (AKA published only) because "any" includes protected.
2019-11-15 23:26:29 +01:00
$status = array ();
}
2022-06-16 14:01:47 +02:00
// let's be explicit about defaulting to 'post'.
2019-11-15 23:26:29 +01:00
$args [ 'type' ] = isset ( $args [ 'type' ] ) ? $args [ 'type' ] : 'post' ;
2022-06-16 14:01:47 +02:00
// make sure the user can read or edit the requested post type(s).
2019-11-15 23:26:29 +01:00
if ( is_array ( $args [ 'type' ] ) ) {
$allowed_types = array ();
foreach ( $args [ 'type' ] as $post_type ) {
if ( $this -> current_user_can_access_post_type ( $post_type , $args [ 'context' ] ) ) {
2022-06-16 14:01:47 +02:00
$allowed_types [] = $post_type ;
2019-11-15 23:26:29 +01:00
}
}
if ( empty ( $allowed_types ) ) {
2022-06-16 14:01:47 +02:00
return array (
'found' => 0 ,
'posts' => array (),
);
2019-11-15 23:26:29 +01:00
}
$args [ 'type' ] = $allowed_types ;
2023-04-26 17:39:43 +02:00
} elseif ( ! $this -> current_user_can_access_post_type ( $args [ 'type' ], $args [ 'context' ] ) ) {
return array (
'found' => 0 ,
'posts' => array (),
);
2019-11-15 23:26:29 +01:00
}
$query = array (
'posts_per_page' => $args [ 'number' ],
'order' => $args [ 'order' ],
'orderby' => $args [ 'order_by' ],
'post_type' => $args [ 'type' ],
'post_status' => $status ,
'post_parent' => isset ( $args [ 'parent_id' ] ) ? $args [ 'parent_id' ] : null ,
'author' => isset ( $args [ 'author' ] ) && 0 < $args [ 'author' ] ? $args [ 'author' ] : null ,
's' => isset ( $args [ 'search' ] ) && '' !== $args [ 'search' ] ? $args [ 'search' ] : null ,
'fields' => 'ids' ,
);
2022-06-16 14:01:47 +02:00
if ( ! is_user_logged_in () ) {
2019-11-15 23:26:29 +01:00
$query [ 'has_password' ] = false ;
}
2023-04-26 17:39:43 +02:00
if ( isset ( $args [ 'include' ] ) ) {
$query [ 'post__in' ] = is_array ( $args [ 'include' ] ) ? $args [ 'include' ] : array ( ( int ) $args [ 'include' ] );
}
2019-11-15 23:26:29 +01:00
if ( isset ( $args [ 'meta_key' ] ) ) {
$show = false ;
2022-06-16 14:01:47 +02:00
if ( WPCOM_JSON_API_Metadata :: is_public ( $args [ 'meta_key' ] ) ) {
2019-11-15 23:26:29 +01:00
$show = true ;
2022-06-16 14:01:47 +02:00
}
if ( current_user_can ( 'edit_post_meta' , $query [ 'post_type' ], $args [ 'meta_key' ] ) ) {
2019-11-15 23:26:29 +01:00
$show = true ;
2022-06-16 14:01:47 +02:00
}
2019-11-15 23:26:29 +01:00
2022-06-16 14:01:47 +02:00
if ( is_protected_meta ( $args [ 'meta_key' ], 'post' ) && ! $show ) {
2019-11-15 23:26:29 +01:00
return new WP_Error ( 'invalid_meta_key' , 'Invalid meta key' , 404 );
2022-06-16 14:01:47 +02:00
}
2019-11-15 23:26:29 +01:00
$meta = array ( 'key' => $args [ 'meta_key' ] );
2022-06-16 14:01:47 +02:00
if ( isset ( $args [ 'meta_value' ] ) ) {
2019-11-15 23:26:29 +01:00
$meta [ 'value' ] = $args [ 'meta_value' ];
2022-06-16 14:01:47 +02:00
}
2019-11-15 23:26:29 +01:00
$query [ 'meta_query' ] = array ( $meta );
}
2022-06-16 14:01:47 +02:00
$sticky = get_option ( 'sticky_posts' );
2019-11-15 23:26:29 +01:00
if (
isset ( $args [ 'sticky' ] )
&&
2022-06-16 14:01:47 +02:00
$sticky
2019-11-15 23:26:29 +01:00
&&
is_array ( $sticky )
) {
if ( $args [ 'sticky' ] ) {
2023-04-26 17:39:43 +02:00
$query [ 'post__in' ] = isset ( $args [ 'include' ] ) ? array_merge ( $query [ 'post__in' ], $sticky ) : $sticky ;
2019-11-15 23:26:29 +01:00
} else {
2022-06-16 14:01:47 +02:00
$query [ 'post__not_in' ] = $sticky ;
2019-11-15 23:26:29 +01:00
$query [ 'ignore_sticky_posts' ] = 1 ;
}
} else {
2022-06-16 14:01:47 +02:00
$query [ 'post__not_in' ] = $sticky ;
2019-11-15 23:26:29 +01:00
$query [ 'ignore_sticky_posts' ] = 1 ;
}
if ( isset ( $args [ 'exclude' ] ) ) {
$query [ 'post__not_in' ] = array_merge ( $query [ 'post__not_in' ], ( array ) $args [ 'exclude' ] );
}
if ( isset ( $args [ 'exclude_tree' ] ) && is_post_type_hierarchical ( $args [ 'type' ] ) ) {
2022-06-16 14:01:47 +02:00
// get_page_children is a misnomer; it supports all hierarchical post types.
$page_args = array (
'child_of' => $args [ 'exclude_tree' ],
'post_type' => $args [ 'type' ],
// since we're looking for things to exclude, be aggressive.
'post_status' => 'publish,draft,pending,private,future,trash' ,
);
2019-11-15 23:26:29 +01:00
$post_descendants = get_pages ( $page_args );
$exclude_tree = array ( $args [ 'exclude_tree' ] );
foreach ( $post_descendants as $child ) {
$exclude_tree [] = $child -> ID ;
}
$query [ 'post__not_in' ] = isset ( $query [ 'post__not_in' ] ) ? array_merge ( $query [ 'post__not_in' ], $exclude_tree ) : $exclude_tree ;
}
if ( isset ( $args [ 'category' ] ) ) {
$category = get_term_by ( 'slug' , $args [ 'category' ], 'category' );
2022-06-16 14:01:47 +02:00
if ( false === $category ) {
2019-11-15 23:26:29 +01:00
$query [ 'category_name' ] = $args [ 'category' ];
} else {
$query [ 'cat' ] = $category -> term_id ;
}
}
if ( isset ( $args [ 'tag' ] ) ) {
$query [ 'tag' ] = $args [ 'tag' ];
}
if ( ! empty ( $args [ 'term' ] ) ) {
$query [ 'tax_query' ] = array ();
foreach ( $args [ 'term' ] as $taxonomy => $slug ) {
$taxonomy_object = get_taxonomy ( $taxonomy );
2020-11-18 09:10:44 +01:00
if ( false === $taxonomy_object || ( ! $taxonomy_object -> public &&
2019-11-15 23:26:29 +01:00
! current_user_can ( $taxonomy_object -> cap -> assign_terms ) ) ) {
continue ;
}
$query [ 'tax_query' ][] = array (
'taxonomy' => $taxonomy ,
2022-06-16 14:01:47 +02:00
'field' => 'slug' ,
'terms' => explode ( ',' , $slug ),
2020-11-18 09:10:44 +01:00
);
2019-11-15 23:26:29 +01:00
}
}
if ( isset ( $args [ 'page' ] ) ) {
if ( $args [ 'page' ] < 1 ) {
$args [ 'page' ] = 1 ;
}
$query [ 'paged' ] = $args [ 'page' ];
} else {
if ( $args [ 'offset' ] < 0 ) {
$args [ 'offset' ] = 0 ;
}
$query [ 'offset' ] = $args [ 'offset' ];
}
if ( isset ( $args [ 'before' ] ) ) {
$this -> date_range [ 'before' ] = $args [ 'before' ];
}
if ( isset ( $args [ 'after' ] ) ) {
$this -> date_range [ 'after' ] = $args [ 'after' ];
}
if ( $this -> date_range ) {
add_filter ( 'posts_where' , array ( $this , 'handle_date_range' ) );
}
/**
* 'column' necessary for the me / posts endpoint ( which extends sites / $site / posts ) .
* Would need to be added to the sites / $site / posts definition if we ever want to
* use it there .
*/
$column_whitelist = array ( 'post_modified_gmt' );
2022-06-16 14:01:47 +02:00
if ( isset ( $args [ 'column' ] ) && in_array ( $args [ 'column' ], $column_whitelist , true ) ) {
2019-11-15 23:26:29 +01:00
$query [ 'column' ] = $args [ 'column' ];
}
$wp_query = new WP_Query ( $query );
if ( $this -> date_range ) {
remove_filter ( 'posts_where' , array ( $this , 'handle_date_range' ) );
$this -> date_range = array ();
}
2022-06-16 14:01:47 +02:00
$return = array ();
2019-11-15 23:26:29 +01:00
$excluded_count = 0 ;
foreach ( array_keys ( $this -> response_format ) as $key ) {
switch ( $key ) {
2022-06-16 14:01:47 +02:00
case 'found' :
$return [ $key ] = ( int ) $wp_query -> found_posts ;
break ;
case 'posts' :
$posts = array ();
foreach ( $wp_query -> posts as $post_ID ) {
$the_post = $this -> get_post_by ( 'ID' , $post_ID , $args [ 'context' ] );
if ( $the_post && ! is_wp_error ( $the_post ) ) {
$posts [] = $the_post ;
} else {
2023-04-26 17:39:43 +02:00
++ $excluded_count ;
2022-06-16 14:01:47 +02:00
}
2019-11-15 23:26:29 +01:00
}
2022-06-16 14:01:47 +02:00
if ( $posts ) {
/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
do_action ( 'wpcom_json_api_objects' , 'posts' , count ( $posts ) );
}
2019-11-15 23:26:29 +01:00
2022-06-16 14:01:47 +02:00
$return [ $key ] = $posts ;
break ;
2019-11-15 23:26:29 +01:00
}
}
$return [ 'found' ] -= $excluded_count ;
return $return ;
}
2022-06-16 14:01:47 +02:00
/**
* Handle the date range .
*
* @ param string $where - SQL where clause .
*/
public function handle_date_range ( $where ) {
2019-11-15 23:26:29 +01:00
global $wpdb ;
switch ( count ( $this -> date_range ) ) {
2022-06-16 14:01:47 +02:00
case 2 :
2019-11-15 23:26:29 +01:00
$where .= $wpdb -> prepare (
2022-06-16 14:01:47 +02:00
" AND ` $wpdb->posts `.post_date BETWEEN CAST( %s AS DATETIME ) AND CAST( %s AS DATETIME ) " ,
$this -> date_range [ 'after' ],
2019-11-15 23:26:29 +01:00
$this -> date_range [ 'before' ]
);
2022-06-16 14:01:47 +02:00
break ;
case 1 :
if ( isset ( $this -> date_range [ 'before' ] ) ) {
$where .= $wpdb -> prepare (
" AND ` $wpdb->posts `.post_date <= CAST( %s AS DATETIME ) " ,
$this -> date_range [ 'before' ]
);
} else {
$where .= $wpdb -> prepare (
" AND ` $wpdb->posts `.post_date >= CAST( %s AS DATETIME ) " ,
$this -> date_range [ 'after' ]
);
}
break ;
2019-11-15 23:26:29 +01:00
}
return $where ;
}
}