Make WordPress Core


Ignore:
Timestamp:
11/17/2023 05:09:09 PM (7 months ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Rewrite a few capability checks for clarity and readability.

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-comments-list-table.php

    r57120 r57123  
    639639        global $post, $comment;
    640640
     641        // Restores the more descriptive, specific name for use within this method.
    641642        $comment = $item;
     643
     644        if ( $comment->comment_post_ID > 0 ) {
     645            $post = get_post( $comment->comment_post_ID );
     646        }
     647
     648        $edit_post_cap = $post ? 'edit_post' : 'edit_posts';
     649
     650        if ( ! current_user_can( $edit_post_cap, $comment->comment_post_ID )
     651            && ( ! empty( $post->post_password )
     652                || ! current_user_can( 'read_post', $comment->comment_post_ID ) )
     653        ) {
     654            // The user has no access to the post and thus cannot see the comments.
     655            return false;
     656        }
    642657
    643658        $the_comment_class = wp_get_comment_status( $comment );
     
    649664        $the_comment_class = implode( ' ', get_comment_class( $the_comment_class, $comment, $comment->comment_post_ID ) );
    650665
    651         if ( $comment->comment_post_ID > 0 ) {
    652             $post = get_post( $comment->comment_post_ID );
    653         }
    654 
    655666        $this->user_can = current_user_can( 'edit_comment', $comment->comment_ID );
    656 
    657         $edit_post_cap = $post ? 'edit_post' : 'edit_posts';
    658         if (
    659             current_user_can( $edit_post_cap, $comment->comment_post_ID ) ||
    660             (
    661                 empty( $post->post_password ) &&
    662                 current_user_can( 'read_post', $comment->comment_post_ID )
    663             )
    664         ) {
    665             // The user has access to the post and thus can see comments.
    666         } else {
    667             return false;
    668         }
    669667
    670668        echo "<tr id='comment-$comment->comment_ID' class='$the_comment_class'>";
Note: See TracChangeset for help on using the changeset viewer.