8000 Merge pull request #2770 from WP-API/exclude-unsupported-params · WP-API/WP-API@a7a9c39 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Commit a7a9c39

Browse files
authored
Merge pull request #2770 from WP-API/exclude-unsupported-params
Exclude unsupported query parameters in posts controller
2 parents 28f5d48 + 64f6753 commit a7a9c39

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

lib/endpoints/class-wp-rest-posts-controller.php

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,42 +96,63 @@ public function get_items( $request ) {
9696
return new WP_Error( 'rest_no_search_term_defined', __( 'You need to define a search term to order by relevance.' ), array( 'status' => 400 ) );
9797
}
9898

99-
$args = array();
100-
$args['author__in'] = $request['author'];
101-
$args['author__not_in'] = $request['author_exclude'];
102-
$args['menu_order'] = $request['menu_order'];
103-
$args['offset'] = $request['offset'];
104-
$args['order'] = $request['order'];
105-
$args['orderby'] = $request['orderby'];
106-
$args['paged'] = $request['page'];
107-
$args['post__in'] = $request['include'];
108-
$args['post__not_in'] = $request['exclude'];
109-
$args['name'] = $request['slug'];
110-
$args['post_parent__in'] = $request['parent'];
111-
$args['post_parent__not_in'] = $request['parent_exclude'];
112-
$args['post_status'] = $request['status'];
113-
$args['s'] = $request['search'];
99+
// Retrieve the list of registered collection query parameters.
100+
$registered = $this->get_collection_params();
101+
$args = array();
102+
103+
// This array defines mappings between public API query parameters whose
104+
// values are accepted as-passed, and their internal WP_Query parameter
105+
// name equivalents (some are the same). Only values which are also
106+
// present in $registered will be set.
107+
$parameter_mappings = array(
108+
'author' => 'author__in',
109+
'author_exclude' => 'author__not_in',
110+
'exclude' => 'post__not_in',
111+
'include' => 'post__in',
112+
'menu_order' => 'menu_order',
113+
'offset' => 'offset',
114+
'order' => 'order',
115+
'orderby' => 'orderby',
116+
'page' => 'paged',
117+
'parent' => 'post_parent__in',
118+
'parent_exclude' => 'post_parent__not_in',
119+
'search' => 's',
120+
'slug' => 'name',
121+
'status' => 'post_status',
122+
);
123+
124+
// For each known parameter which is both registered and present in the request,
125+
// set the parameter's value on the query $args.
126+
foreach ( $parameter_mappings as $api_param => $wp_param ) {
127+
if ( isset( $registered[ $api_param ] ) && isset( $request[ $api_param ] ) ) {
128+
$args[ $wp_param ] = $request[ $api_param ];
129+
}
130+
}
131+
132+
// Check for & assign any parameters which require special handling or setting.
114133

115134
$args['date_query'] = array();
116135
// Set before into date query. Date query must be specified as an array of an array.
117-
if ( isset( $request['before'] ) ) {
136+
if ( isset( $registered['before'] ) && isset( $request['before'] ) ) {
118137
$args['date_query'][0]['before'] = $request['before'];
119138
}
120139

121140
// Set after into date query. Date query must be specified as an array of an array.
122-
if ( isset( $request['after'] ) ) {
141+
if ( isset( $registered['after'] ) && isset( $request['after'] ) ) {
123142
$args['date_query'][0]['after'] = $request['after'];
124143
}
125144

126-
if ( is_array( $request['filter'] ) ) {
145+
if ( isset( $registered['filter'] ) && is_array( $request['filter'] ) ) {
127146
$args = array_merge( $args, $request['filter'] );
128147
unset( $args['filter'] );
129148
}
130149

131-
// Ensure our per_page parameter overrides filter.
132-
$args['posts_per_page'] = $request['per_page'];
150+
// Ensure our per_page parameter overrides any provided posts_per_page filter.
151+
if ( isset( $registered['per_page'] ) ) {
152+
$args['posts_per_page'] = $request['per_page'];
153+
}
133154

134-
if ( isset( $request['sticky'] ) ) {
155+
if ( isset( $registered['sticky'] ) && isset( $request['sticky'] ) ) {
135156
$sticky_posts = get_option( 'sticky_posts', array() );
136157
if ( $sticky_posts && $request['sticky'] ) {
137158
// As post__in will be used to only get sticky posts,

0 commit comments

Comments
 (0)
0