Making WordPress.org

Ticket #1755: 1755.2.patch

File 1755.2.patch, 7.1 KB (added by keesiemeijer, 9 years ago)

Add Reddit-style voting with the opposite voting button disabled

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

     
    292292                        $comment_id = $comment->comment_ID;
    293293                }
    294294
    295                 $can_vote     = self::user_can_vote( get_current_user_id(), $comment_id );
    296                 $user_note    = self::is_current_user_note( $comment_id );
    297                 $logged_in    = is_user_logged_in();
    298                 $comment_link = get_comment_link( $comment_id );
    299                 $nonce        = wp_create_nonce( 'user-note-vote-' . $comment_id );
    300                 $disabled_str = __( 'Voting for this note is disabled', 'wporg' );
    301                 $log_in_str   = __( 'You must log in to vote on the helpfulness of this note', 'wporg' );
    302                 $log_in_url   = add_query_arg( 'redirect_to', urlencode( $comment_link ), 'https://login.wordpress.org' );
     295                $can_vote       = self::user_can_vote( get_current_user_id(), $comment_id );
     296                $user_upvoted   = self::has_user_upvoted_comment( $comment_id );
     297                $user_downvoted = ( $user_upvoted ? false : self::has_user_downvoted_comment( $comment_id ) );
     298                $logged_in      = is_user_logged_in();
     299                $comment_link   = get_comment_link( $comment_id );
     300                $nonce          = wp_create_nonce( 'user-note-vote-' . $comment_id );
     301                $disabled_str   = __( 'Voting for this note is disabled', 'wporg' );
     302                $cancel_str     = __( 'Click to cancel your vote.', 'wporg' );
     303                $log_in_str     = __( 'You must log in to vote on the helpfulness of this note', 'wporg' );
     304                $log_in_url     = add_query_arg( 'redirect_to', urlencode( $comment_link ), 'https://login.wordpress.org' );
     305                $class          = '';
    303306
    304                 if ( ! $can_vote && $user_note ) {
     307                if ( ! $can_vote && self::is_current_user_note( $comment_id ) ) {
    305308                        $disabled_str = __( 'Voting for your own note is disabled', 'wporg' );
    306309                }
    307310
    308311                echo '<div class="user-note-voting" data-nonce="' . esc_attr( $nonce ) . '">';
    309312
    310                 // Up vote link
    311                 $user_upvoted = self::has_user_upvoted_comment( $comment_id );
    312                 if ( $can_vote ) {
    313                         $title = $user_upvoted ?
    314                                 __( 'You have voted to indicate this note was helpful', 'wporg' ) :
    315                                 __( 'Vote up if this note was helpful', 'wporg' );
    316                         $tag = $user_upvoted ? 'span' : 'a';
     313                // Up vote
     314                if ( $can_vote && !$user_downvoted ) {
     315                        // This user can vote. (user is logged in and it's not a note from the current user)
     316                        // And the user has not already downvoted this note.
     317                        $tag = 'a';
     318                       
     319                        if( $user_upvoted ) {
     320                                $title = __( 'You have voted to indicate this note was helpful.', 'wporg' );
     321                                $title .= ' ' . $cancel_str;
     322                        } else {
     323                                $title = __( 'Vote up if this note was helpful', 'wporg' );
     324                        }
    317325                } else {
    318                         $title = ! $logged_in ? $log_in_str : $disabled_str;
    319                         $tag = $logged_in ? 'span' : 'a';
     326                        $tag   = 'span';
     327                        $class = ' vote-disabled';
     328                        $title = $disabled_str;
     329
     330                        // User has downvoted this note
     331                        if( $user_downvoted ) {
     332                                $title = __( "Voting up is disabled because you've already downvoted this note" , 'wporg' );           
     333                        }
     334
     335                        // User is not logged in. Display log in link
     336                        if( ! $logged_in ) {
     337                                $tag   = 'a';
     338                                $class = '';
     339                                $title = $log_in_str;
     340                        }               
    320341                }
     342
     343                $class .= $user_upvoted ? ' user-voted' : '';
     344
    321345                echo "<{$tag} "
    322                         . 'class="user-note-voting-up' . ( $user_upvoted ? ' user-voted' : '' )
     346                        . 'class="user-note-voting-up' . $class
    323347                        . '" title="' . esc_attr( $title )
    324348                        . '" data-id="' . esc_attr( $comment_id )
    325349                        . '" data-vote="up';
     
    347371                        . self::count_votes( $comment_id, 'difference' )
    348372                        . '</span>';
    349373
    350                 // Down vote link
    351                 $user_downvoted = ( $user_upvoted ? false : self::has_user_downvoted_comment( $comment_id ) );
    352                 if ( $can_vote ) {
    353                         $title = $user_downvoted ?
    354                                 __( 'You have voted to indicate this note was not helpful', 'wporg' ) :
    355                                 __( 'Vote down if this note was not helpful', 'wporg' );
    356                         $tag = $user_downvoted ? 'span' : 'a';
     374                // Down vote
     375                $class = '';
     376
     377                if ( $can_vote && !$user_upvoted) {
     378                        // This user can vote. (user is logged in and it's not a note from the current user)
     379                        // And the user has not already upvoted this note.
     380                        $tag = 'a';
     381                       
     382                        if( $user_downvoted ) {
     383                                $title = __( 'You have voted to indicate this note was not helpful.', 'wporg' );
     384                                $title .= ' ' . $cancel_str;
     385                        } else {
     386                                $title = __( 'Vote down if this note was not helpful', 'wporg' );
     387                        }
    357388                } else {
    358                         $title = ! $logged_in ? $log_in_str : $disabled_str;
    359                         $tag = $logged_in ? 'span' : 'a';
     389                        $tag        = 'span';
     390                        $class      = ' vote-disabled';
     391                        $title      = $disabled_str;
     392
     393                        // User has upvoted this note
     394                        if( $user_upvoted ) {
     395                                $title = __( "Voting down is disabled because you've already upvoted this note" , 'wporg' );
     396                        }
     397
     398                        // User is not logged in. Display log in link
     399                        if( ! $logged_in ) {
     400                                $tag   = 'a';
     401                                $class = '';
     402                                $title = $log_in_str;
     403                        }
    360404                }
     405
     406                $class .= $user_downvoted ? ' user-voted' : '';
     407
    361408                echo "<{$tag} "
    362                         . 'class="user-note-voting-down' . ( $user_downvoted ? ' user-voted' : '' )
     409                        . 'class="user-note-voting-down' . $class
    363410                        . '" title="' . esc_attr( $title )
    364411                        . '" data-id="' . esc_attr( $comment_id )
    365412                        . '" data-vote="down';
     
    485532                // Get list of people who cast the same vote.
    486533                $add_to_list = get_comment_meta( $comment_id, $add_to, true );
    487534
    488                 // Don't do anything if user is recasting the same vote as before.
     535                // Remove user from list if recasting the same vote as before.
    489536                if ( in_array( $user_id, (array) $add_to_list ) ) {
    490                         return false;
     537                        unset( $add_to_list[ array_search( $user_id, $add_to_list ) ] );
     538                        update_comment_meta( $comment_id, $add_to, $add_to_list );
     539                        return true;
    491540                }
    492541
    493542                // If the user had previously cast the opposite vote, undo that older vote.
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/scss/main.scss

     
    14821482        .user-voted.user-note-voting-down .dashicons {
    14831483                color: red;
    14841484        }
    1485         .user-submitted-note .dashicons {
     1485        .user-submitted-note .dashicons,
     1486        .vote-disabled .dashicons {
    14861487                color: grey;
    14871488        }
    14881489
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/stylesheets/main.css

     
    18171817  color: red;
    18181818}
    18191819
    1820 .devhub-wrap .user-submitted-note .dashicons {
     1820.devhub-wrap .user-submitted-note .dashicons,
     1821.devhub-wrap .vote-disabled .dashicons {
    18211822  color: grey;
    18221823}
    18231824