Making WordPress.org

Changeset 7797


Ignore:
Timestamp:
10/27/2018 08:58:39 PM (6 years ago)
Author:
drewapicture
Message:

Developer: Allow moderators to selectively reset the vote counts on contributed notes.

Props keesiemeijer.
Fixes #3886.

Location:
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc
Files:
2 edited

Legend:

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

    r7776 r7797  
    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
     
    4654    }
    4755
     56    /**
     57     * Adds a checkbox for resetting the contributor note votes in the comment submit meta box.
     58     *
     59     * Only displays the checkbox if the vote score is not zero.
     60     *
     61     * @param string $html    Html in the submit meta box.
     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>';
     72            $html .= '</div>';
     73        }
     74
     75        return $html;
     76    }
     77
     78    /**
     79     * Reset votes before the user is redirected from the wp-admin (after editing a comment).
     80     *
     81     * @param string $location   The URI the user will be redirected to.
     82     * @param int    $comment_id The ID of the comment being edited.
     83     * @return string The redirect URI.
     84     */
     85    public static function comment_edit_redirect( $location, $comment_id ) {
     86        if ( isset( $_REQUEST['reset_votes'] ) && $_REQUEST['reset_votes'] ) {
     87            DevHub_User_Contributed_Notes_Voting::reset_votes( $comment_id );
     88        }
     89
     90        return $location;
     91    }
     92
    4893} // DevHub_Admin
    4994
    5095DevHub_Admin::init();
    51 
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/user-content-voting.php

    r7276 r7797  
    455455
    456456    /**
     457     * Resets the votes for a note..
     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     *
Note: See TracChangeset for help on using the changeset viewer.