Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove post type capabilities from the user object #6801

Merged
merged 1 commit into from
May 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Remove post type capabilities from the user object
It's no longer necessary to expose these through the REST API
  • Loading branch information
danielbachhuber committed May 17, 2018
commit 21fdb9322d177276926764de4a1a10ed2b403d44
60 changes: 0 additions & 60 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,66 +21,6 @@ function gutenberg_register_rest_routes() {
}
add_action( 'rest_api_init', 'gutenberg_register_rest_routes' );

/**
* Includes the value for the custom field `post_type_capabities` inside the REST API response of user.
*
* TODO: This is a temporary solution. Next step would be to edit the WP_REST_Users_Controller,
* once merged into Core.
*
* @since ?
*
* @param array $user An array containing user properties.
* @param string $name The name of the custom field.
* @param WP_REST_Request $request Full details about the REST API request.
* @return object The Post Type capabilities.
*/
function gutenberg_get_post_type_capabilities( $user, $name, $request ) {
$post_type = $request->get_param( 'post_type' );
$value = new stdClass;

if ( ! empty( $user['id'] ) && $post_type && post_type_exists( $post_type ) ) {
// The Post Type object contains the Post Type's specific caps.
$post_type_object = get_post_type_object( $post_type );

// Loop in the Post Type's caps to validate the User's caps for it.
foreach ( $post_type_object->cap as $post_cap => $post_type_cap ) {
// Ignore caps requiring a post ID.
if ( in_array( $post_cap, array( 'edit_post', 'read_post', 'delete_post' ) ) ) {
continue;
}

// Set the User's post type capability.
$value->{$post_cap} = user_can( $user['id'], $post_type_cap );
}
}

return $value;
}

/**
* Adds the custom field `post_type_capabities` to the REST API response of user.
*
* TODO: This is a temporary solution. Next step would be to edit the WP_REST_Users_Controller,
* once merged into Core.
*
* @since ?
*/
function gutenberg_register_rest_api_post_type_capabilities() {
register_rest_field( 'user',
'post_type_capabilities',
array(
'get_callback' => 'gutenberg_get_post_type_capabilities',
'schema' => array(
'description' => __( 'Post Type capabilities for the user.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'edit' ),
'readonly' => true,
),
)
);
}
add_action( 'rest_api_init', 'gutenberg_register_rest_api_post_type_capabilities' );

/**
* Make sure oEmbed REST Requests apply the WP Embed security mechanism for WordPress embeds.
*
Expand Down