From a5e7356a9271a550df09863163ed86d9fb0aa49f Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Tue, 15 Nov 2022 20:55:56 +0000 Subject: [PATCH 1/3] refactors attribute_post_type handling --- inc/namespace.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/inc/namespace.php b/inc/namespace.php index e3c4b18..e45c976 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -245,26 +245,33 @@ 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] ); break; }//end switch From 6bc18405ae00e74ac00c6704cf121b4445b5c252 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Tue, 15 Nov 2022 20:57:56 +0000 Subject: [PATCH 2/3] PHPCS fixes --- inc/namespace.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/inc/namespace.php b/inc/namespace.php index e45c976..1a6a486 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -245,32 +245,31 @@ function filter_user_has_cap( array $user_caps, array $required_caps, array $arg break; case 'attribute_post_type': - // if the user already has the cap then, defer to the existing value + // 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 + // 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 it's for an undefined post type, then they do not have `attribute_post_type`. if ( ! $post_type_object ) { $user_caps[ $cap ] = false; break; } - - // fallback to post type edit others post + // Fallback to post type edit others post. /** @var stdClass */ $post_type_caps = $post_type_object->cap; - // if we are not checking a specific post, then check if they can edit other posts of this type + // 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 + // 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] ); break; From 92c3e81a184700c8bac65e39873152508bd5ecf5 Mon Sep 17 00:00:00 2001 From: Tom J Nowell Date: Thu, 15 Dec 2022 18:05:57 +0000 Subject: [PATCH 3/3] address feedback on mistaken args usage --- inc/namespace.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/inc/namespace.php b/inc/namespace.php index 1a6a486..9afff72 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -264,13 +264,7 @@ function filter_user_has_cap( array $user_caps, array $required_caps, array $arg $post_type_caps = $post_type_object->cap; // 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] ); + $user_caps[ $cap ] = user_can( $user->ID, $post_type_caps->edit_others_posts ); break; }//end switch