WordPress.org

Making WordPress.org

Ticket #3886: 3886.patch

File 3886.patch, 3.6 KB (added by keesiemeijer, 19 months ago)

Reset votes in the comment edit screen

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

     
    2222         */
    2323        public static function do_init() {
    2424                add_action( 'comment_author', [ __CLASS__, 'append_user_nicename' ], 10, 2 );
     25
     26                if ( class_exists( 'DevHub_User_Contributed_Notes_Voting' ) ) {
     27                        // Add a reset votes checkbox to the comment submit metabox.
     28                        add_filter( 'edit_comment_misc_actions', [ __CLASS__, 'add_reset_votes_form_field' ], 10, 2 );
     29
     30                        // Reset votes after editing a comment in the wp-admin.
     31                        add_filter( 'comment_edit_redirect',  [ __CLASS__, 'comment_edit_redirect'], 10, 2 );
     32                }
    2533        }
    2634
    2735        /**
     
    3846
    3947                if ( $comment->user_id ) {
    4048                        $username = get_user_by( 'id', $comment->user_id )->user_nicename;
    41        
     49
    4250                        $author_name .= '</strong><div class="comment-author-nicename">@' . $username . '</div><strong>';
    4351                }
    4452
     
    4553                return $author_name;
    4654        }
    4755
     56        /**
     57         * Adds a checkbox for resetting the comment votes in the comment submit metabox.
     58         *
     59         * Only displays the checkbox if the vote score is not zero.
     60         *
     61         * @param string $html    Html in the submit metabox.
     62         * @param object $comment Current comment object.
     63         * @return string Output html.
     64         */
     65        public static function add_reset_votes_form_field( $html, $comment ) {
     66                $count = (int) DevHub_User_Contributed_Notes_Voting::count_votes( $comment->comment_ID, 'difference' );
     67
     68                if ( 0 !== $count ) {
     69                        $html .= '<div class="misc-pub-section misc-pub-reset_votes">';
     70                        $html .= '<input id="reset_votes" type="checkbox" name="reset_votes" value="on" />';
     71                        $html .= '<label for="reset_votes">' . sprintf( __( 'Reset votes (%d)', 'wporg' ), $count )  . '</label></div>';
     72                }
     73
     74                return $html;
     75        }
     76
     77        /**
     78         * Reset votes before the user is redirected from the wp-admin (after editing a comment).
     79         *
     80         * @param string $location   The URI the user will be redirected to.
     81         * @param int    $comment_id The ID of the comment being edited.
     82         * @return string The redirect URI.
     83         */
     84        public static function comment_edit_redirect( $location, $comment_id ) {
     85                if ( isset( $_POST['reset_votes'] ) && $_POST['reset_votes'] ) {
     86                        DevHub_User_Contributed_Notes_Voting::reset_votes( $comment_id );
     87                }
     88
     89                return $location;
     90        }
     91
    4892} // DevHub_Admin
    4993
    5094DevHub_Admin::init();
    51 
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/user-content-voting.php

     
    454454        }
    455455
    456456        /**
     457         * Resets the votes for a comment.
     458         *
     459         * The current user needs to have the edit_comment capability for the votes to be deleted.
     460         *
     461         * @access public
     462         *
     463         * @param int $comment_id The comment ID to reset votes for
     464         */
     465        public static function reset_votes( $comment_id ) {
     466                $comment_id = absint( $comment_id );
     467                if ( ! $comment_id || ! current_user_can( 'edit_comment', $comment_id ) ) {
     468                        return;
     469                }
     470
     471                delete_comment_meta( $comment_id, self::$meta_upvotes );
     472                delete_comment_meta( $comment_id, self::$meta_downvotes );
     473        }
     474
     475        /**
    457476         * Handles abstraction between an up or down vote.
    458477         *
    459478         * @access protected