REST API: Support ordering response collection by listed slugs.

Adds an "include_slug" orderby value for REST API collections to permit returning a collection filtered by slugs in the same order in which those slugs are specified.
Previously, the order of slugs provided with the ?slug query parameter had no effect on the order of the returned records.

Props wonderboymusic, ocean90, boonebgorges.
Fixes #40826.


Built from https://develop.svn.wordpress.org/trunk@41760


git-svn-id: http://core.svn.wordpress.org/trunk@41594 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
K. Adam White 2017-10-05 00:37:47 +00:00
parent 5fe4acb1f5
commit 30827e4a4f
5 changed files with 25 additions and 5 deletions

View File

@ -87,6 +87,7 @@ class WP_Term_Query {
* @since 4.6.0
* @since 4.6.0 Introduced 'term_taxonomy_id' parameter.
* @since 4.7.0 Introduced 'object_ids' parameter.
* @since 4.9.0 Added 'slug__in' support for 'orderby'.
*
* @param string|array $query {
* Optional. Array or query string of term query parameters. Default empty.
@ -98,7 +99,8 @@ class WP_Term_Query {
* @type string $orderby Field(s) to order terms by. Accepts term fields ('name',
* 'slug', 'term_group', 'term_id', 'id', 'description', 'parent'),
* 'count' for term taxonomy count, 'include' to match the
* 'order' of the $include param, 'meta_value', 'meta_value_num',
* 'order' of the $include param, 'slug__in' to match the
* 'order' of the $slug param, 'meta_value', 'meta_value_num',
* the value of `$meta_key`, the array keys of `$meta_query`, or
* 'none' to omit the ORDER BY clause. Defaults to 'name'.
* @type string $order Whether to order terms in ascending or descending order.
@ -841,6 +843,9 @@ class WP_Term_Query {
} elseif ( 'include' == $_orderby && ! empty( $this->query_vars['include'] ) ) {
$include = implode( ',', wp_parse_id_list( $this->query_vars['include'] ) );
$orderby = "FIELD( t.term_id, $include )";
} elseif ( 'slug__in' == $_orderby && ! empty( $this->query_vars['slug'] ) && is_array( $this->query_vars['slug'] ) ) {
$slugs = implode( "', '", array_map( 'sanitize_title_for_query', $this->query_vars['slug__in'] ) );
$orderby = "FIELD( t.slug, '" . $slugs . "')";
} elseif ( 'none' == $_orderby ) {
$orderby = '';
} elseif ( empty( $_orderby ) || 'id' == $_orderby || 'term_id' === $_orderby ) {

View File

@ -877,6 +877,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
'id' => 'ID',
'include' => 'post__in',
'slug' => 'post_name',
'include_slugs' => 'post_name__in',
);
if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
@ -2109,6 +2110,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
'parent',
'relevance',
'slug',
'include_slugs',
'title',
),
);

View File

@ -188,6 +188,16 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
}
}
if ( isset( $prepared_args['orderby'] ) && isset( $request['orderby'] ) ) {
$orderby_mappings = array(
'include_slugs' => 'slug__in',
);
if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
$prepared_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
}
}
if ( isset( $registered['offset'] ) && ! empty( $request['offset'] ) ) {
$prepared_args['offset'] = $request['offset'];
} else {
@ -932,6 +942,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
'include',
'name',
'slug',
'include_slugs',
'term_group',
'description',
'count',

View File

@ -243,6 +243,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
'name' => 'display_name',
'registered_date' => 'registered',
'slug' => 'user_nicename',
'include_slugs' => 'nicename__in',
'email' => 'user_email',
'url' => 'user_url',
);
@ -1338,6 +1339,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller {
'name',
'registered_date',
'slug',
'include_slugs',
'email',
'url',
),

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.9-alpha-41759';
$wp_version = '4.9-alpha-41760';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.