Make WordPress Core


Ignore:
Timestamp:
04/25/2018 01:05:48 PM (8 years ago)
Author:
pento
Message:

REST API: Add who=authors as a query parameter for GET wp/v2/users.

Any WordPress user who can edit_posts of a post type with show_in_rest=true can query for authors. This maps to current WordPress behavior where a WordPress user who can view the Manage Posts view for a post type can see any WordPress user assigned to a post (whether published or draft).

This implementation, over restricting who=authors to users with list_users, gives us future flexibility in displaying lists of posts. It still respects more restrictive permissions for context=edit.

Props danielbachhuber.
Fixes #42202.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r42761 r43001  
    193193                }
    194194
     195                if ( 'authors' === $request['who'] ) {
     196                        $can_view = false;
     197                        $types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
     198                        foreach ( $types as $type ) {
     199                                if ( current_user_can( $type->cap->edit_posts ) ) {
     200                                        $can_view = true;
     201                                }
     202                        }
     203                        if ( ! $can_view ) {
     204                                return new WP_Error( 'rest_forbidden_who', __( 'Sorry, you are not allowed to query users by this parameter.' ), array( 'status' => rest_authorization_required_code() ) );
     205                        }
     206                }
     207
    195208                return true;
    196209        }
     
    257270                }
    258271
    259                 if ( ! current_user_can( 'list_users' ) ) {
     272                if ( isset( $registered['who'] ) && ! empty( $request['who'] ) && 'authors' === $request['who'] ) {
     273                        $prepared_args['who'] = 'authors';
     274                } elseif ( ! current_user_can( 'list_users' ) ) {
    260275                        $prepared_args['has_published_posts'] = get_post_types( array( 'show_in_rest' => true ), 'names' );
    261276                }
     
    13731388                );
    13741389
     1390                $query_params['who'] = array(
     1391                        'description' => __( 'Limit result set to users who are considered authors.' ),
     1392                        'type'        => 'string',
     1393                        'enum'        => array(
     1394                                'authors',
     1395                        ),
     1396                );
     1397
    13751398                /**
    13761399                 * Filter collection parameters for the users controller.
Note: See TracChangeset for help on using the changeset viewer.