| 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 | } |
| 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 | |