Making WordPress.org


Ignore:
Timestamp:
07/29/2016 11:29:18 PM (8 years ago)
Author:
coffee2code
Message:

developer.wordpress.org: Prevent user from voting on their own user contributed note.

Props keesiemeijer.
See #1755.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/user-content-voting.php

    r3665 r3753  
    195195        if ( $comment_id ) {
    196196            $can = ( '1' == get_comment( $comment_id )->comment_approved );
     197            // Users can't vote on their own comments.
     198            if ( $can && self::is_current_user_note( $comment_id ) ) {
     199                $can = false;
     200            }
    197201        }
    198202
    199203        return apply_filters( 'devhub_user_can_vote', $can, $user_id, $comment_id );
     204    }
     205
     206    /**
     207     * Determines if a note was submitted by the current user.
     208     *
     209     * @param int   $comment_id The comment ID, or empty to use current comment.
     210     * @return bool True if the note was submitted by the current user.
     211     */
     212    public static function is_current_user_note( $comment_id = '' ) {
     213        if ( ! $comment_id ) {
     214            global $comment;
     215            $comment_id = $comment->comment_ID;
     216        }
     217
     218        $note    = get_comment( $comment_id );
     219        $user_id = get_current_user_id();
     220
     221        if ( ! $note || ! $user_id ) {
     222            return false;
     223        }
     224
     225        if ( (int) $note->user_id === $user_id ) {
     226            return true;
     227        }
     228
     229        return false;
    200230    }
    201231
     
    264294
    265295        $can_vote     = self::user_can_vote( get_current_user_id(), $comment_id );
     296        $user_note    = self::is_current_user_note( $comment_id );
    266297        $logged_in    = is_user_logged_in();
    267298        $comment_link = get_comment_link( $comment_id );
     
    270301        $log_in_str   = __( 'You must log in to vote on the helpfulness of this note', 'wporg' );
    271302        $log_in_url   = add_query_arg( 'redirect_to', urlencode( $comment_link ), 'https://login.wordpress.org' );
     303
     304        if ( ! $can_vote && $user_note ) {
     305            $disabled_str = __( 'Voting for your own note is disabled', 'wporg' );
     306        }
    272307
    273308        echo '<div class="user-note-voting" data-nonce="' . esc_attr( $nonce ) . '">';
Note: See TracChangeset for help on using the changeset viewer.