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

Fix capability checks, update capability for post_restrictions #3243

Merged
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
14 changes: 11 additions & 3 deletions includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,10 @@ function pmpro_rest_api_get_permissions_check( $request ) {
'/pmpro/v1/me' => true,
'/pmpro/v1/recent_memberships' => 'pmpro_edit_members',
'/pmpro/v1/recent_orders' => 'pmpro_orders',
'/pmpro/v1/post_restrictions' => 'pmpro_edit_members',
'/pmpro/v1/post_restrictions' => array(
'capability' => 'edit_post',
'request_param' => 'post_id',
),
);
$route_caps = apply_filters( 'pmpro_rest_api_route_capabilities', $route_caps, $request );

Expand All @@ -1205,8 +1208,9 @@ function pmpro_rest_api_get_permissions_check( $request ) {
if ( is_array ( $route_caps[$route] ) && isset( $route_caps[$route][$method] ) ) {
// Different permission for this method, use it.
$permission_to_check = $route_caps[$route][$method];
} elseif ( is_array( $route_caps[$route] ) ) {
} elseif ( is_array( $route_caps[$route] ) && ! isset( $route_caps[$route]['capability'] ) ) {
// No permission for this method, default to false.
// Skipping if there is a capability set as this is likely an array with a specific post/user ID to check a capability for.
$permission_to_check = false;
} else {
// Same permission for all methods, use it.
Expand All @@ -1217,9 +1221,13 @@ function pmpro_rest_api_get_permissions_check( $request ) {
if ( $permission_to_check === true || $permission_to_check === false ) {
// For true or false, just pass it along.
$permission = $permission_to_check;
} elseif ( is_array( $permission_to_check ) && isset( $permission_to_check['capability'] ) && isset( $permission_to_check['request_param'] ) ) {
// Check if the current user has this capability.
// This is used in cases like edit_post where we need to check a specific post ID.
$permission = current_user_can( $permission_to_check['capability'], $request->get_param( $permission_to_check['request_param'] ) );
} else {
// Check if the current user has this capability.
$permission = current_user_can( $permission );
$permission = current_user_can( $permission_to_check );
}
}

Expand Down