Skip to content

Commit

Permalink
Coding Standards: Rewrite a few capability checks for clarity and rea…
Browse files Browse the repository at this point in the history
…dability.

This aims to:
* Perform the checks as early as possible to avoid redundant function calls.
* Remove an empty conditiaonal branch and make the exit conditions clearer.
* Bring the formatting in line with other multi-line conditionals in core.

Follow-up to [56836].

See #59650.

git-svn-id: https://develop.svn.wordpress.org/trunk@57123 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Nov 17, 2023
1 parent b558a7b commit 2b15e0f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
32 changes: 15 additions & 17 deletions src/wp-admin/includes/class-wp-comments-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,23 @@ public function display() {
public function single_row( $item ) {
global $post, $comment;

// Restores the more descriptive, specific name for use within this method.
$comment = $item;

if ( $comment->comment_post_ID > 0 ) {
$post = get_post( $comment->comment_post_ID );
}

$edit_post_cap = $post ? 'edit_post' : 'edit_posts';

if ( ! current_user_can( $edit_post_cap, $comment->comment_post_ID )
&& ( ! empty( $post->post_password )
|| ! current_user_can( 'read_post', $comment->comment_post_ID ) )
) {
// The user has no access to the post and thus cannot see the comments.
return false;
}

$the_comment_class = wp_get_comment_status( $comment );

if ( ! $the_comment_class ) {
Expand All @@ -648,25 +663,8 @@ public function single_row( $item ) {

$the_comment_class = implode( ' ', get_comment_class( $the_comment_class, $comment, $comment->comment_post_ID ) );

if ( $comment->comment_post_ID > 0 ) {
$post = get_post( $comment->comment_post_ID );
}

$this->user_can = current_user_can( 'edit_comment', $comment->comment_ID );

$edit_post_cap = $post ? 'edit_post' : 'edit_posts';
if (
current_user_can( $edit_post_cap, $comment->comment_post_ID ) ||
(
empty( $post->post_password ) &&
current_user_can( 'read_post', $comment->comment_post_ID )
)
) {
// The user has access to the post and thus can see comments.
} else {
return false;
}

echo "<tr id='comment-$comment->comment_ID' class='$the_comment_class'>";
$this->single_row_columns( $comment );
echo "</tr>\n";
Expand Down
25 changes: 11 additions & 14 deletions src/wp-admin/includes/class-wp-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,17 @@ protected function view_switcher( $current_mode ) {
* @param int $pending_comments Number of pending comments.
*/
protected function comments_bubble( $post_id, $pending_comments ) {
$post_object = get_post( $post_id );
$edit_post_cap = $post_object ? 'edit_post' : 'edit_posts';

if ( ! current_user_can( $edit_post_cap, $post_id )
&& ( ! empty( $post_object->post_password )
|| ! current_user_can( 'read_post', $post_id ) )
) {
// The user has no access to the post and thus cannot see the comments.
return false;
}

$approved_comments = get_comments_number();

$approved_comments_number = number_format_i18n( $approved_comments );
Expand All @@ -851,20 +862,6 @@ protected function comments_bubble( $post_id, $pending_comments ) {
$pending_comments_number
);

$post_object = get_post( $post_id );
$edit_post_cap = $post_object ? 'edit_post' : 'edit_posts';
if (
current_user_can( $edit_post_cap, $post_id ) ||
(
empty( $post_object->post_password ) &&
current_user_can( 'read_post', $post_id )
)
) {
// The user has access to the post and thus can see comments.
} else {
return false;
}

if ( ! $approved_comments && ! $pending_comments ) {
// No comments at all.
printf(
Expand Down
19 changes: 8 additions & 11 deletions src/wp-admin/includes/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,13 @@ function wp_dashboard_recent_comments( $total_items = 5 ) {
}

foreach ( $possible as $comment ) {
if ( ! current_user_can( 'read_post', $comment->comment_post_ID ) ) {
$comment_post = get_post( $comment->comment_post_ID );

if ( ! current_user_can( 'edit_post', $comment->comment_post_ID )
&& ( ! empty( $comment_post->post_password )
|| ! current_user_can( 'read_post', $comment->comment_post_ID ) )
) {
// The user has no access to the post and thus cannot see the comments.
continue;
}

Expand All @@ -1109,16 +1115,7 @@ function wp_dashboard_recent_comments( $total_items = 5 ) {

echo '<ul id="the-comment-list" data-wp-lists="list:comment">';
foreach ( $comments as $comment ) {
$comment_post = get_post( $comment->comment_post_ID );
if (
current_user_can( 'edit_post', $comment->comment_post_ID ) ||
(
empty( $comment_post->post_password ) &&
current_user_can( 'read_post', $comment->comment_post_ID )
)
) {
_wp_dashboard_recent_comments_row( $comment );
}
_wp_dashboard_recent_comments_row( $comment );
}
echo '</ul>';

Expand Down

0 comments on commit 2b15e0f

Please sign in to comment.