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 attribute post type handling #113

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Changes from 3 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
20 changes: 13 additions & 7 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,26 +245,32 @@ function filter_user_has_cap( array $user_caps, array $required_caps, array $arg
break;

case 'attribute_post_type':
if ( empty( $args[2] ) ) {
$user_caps[ $cap ] = false;
// If the user already has the cap then, defer to the existing value.
if ( array_key_exists( $cap, $user_caps ) ) {
break;
}

// Fetch the post type.
$post_type_object = get_post_type_object( $args[2] );

// If it's for an undefined post type, then they do not have `attribute_post_type`.
if ( ! $post_type_object ) {
$user_caps[ $cap ] = false;
break;
}

if ( array_key_exists( $cap, $user_caps ) ) {
break;
}

// Fallback to post type edit others post.
/** @var stdClass */
$post_type_caps = $post_type_object->cap;

$user_caps[ $cap ] = user_can( $user->ID, $post_type_caps->edit_others_posts );
// If we are not checking a specific post, then check if they can edit other posts of this type.
if ( empty( $args[2] ) ) {
$user_caps[ $cap ] = user_can( $user->ID, $post_type_caps->edit_others_posts );
break;
}

// Otherwise we _are_ checking a specific post, so perform the check but with the post ID.
$user_caps[ $cap ] = user_can( $user->ID, $post_type_caps->edit_others_posts, $args[2] );
tomjn marked this conversation as resolved.
Show resolved Hide resolved
break;

}//end switch
Expand Down