Changeset 9594 for sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-report-topic.php
- Timestamp:
- 03/17/2020 05:27:12 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-report-topic.php
r8167 r9594 6 6 7 7 public function __construct() { 8 add_action( 'wporg_support_after_topic_info', array( $this, 'add_sidebar_link' ) ); 8 add_action( 'wporg_support_after_topic_info', array( $this, 'add_sidebar_form' ) ); 9 10 add_action( 'set_object_terms', array( $this, 'detect_manual_modlook' ), 10, 6 ); 9 11 10 12 add_action( 'init', array( $this, 'capture_topic_report' ) ); 11 13 } 12 14 13 public function capture_topic_report() { 14 // Do not process anything if the user is not logged in. 15 if ( ! is_user_logged_in() ) { 16 return; 17 } 18 19 if ( isset( $_GET['wporg-support-report-topic'] ) ) { 20 $action = sprintf( 21 'report-topic-%d', 22 $_GET['wporg-support-report-topic'] 23 ); 24 25 // Verify that the nonce is valid. 26 if ( ! wp_verify_nonce( $_GET['_wpnonce'], $action ) ) { 27 return; 28 } 29 30 wp_add_object_terms( $_GET['wporg-support-report-topic'], 'modlook', 'topic-tag' ); 31 32 $reporter = $this->get_previous_reports( $_GET['wporg-support-report-topic'] ); 33 if ( empty( $reporter ) ) { 34 $reporter = array(); 35 } 36 37 $current_user = get_current_user_id(); 38 39 // In those odd cases where the same user reports a topic multiple times, let's increment them, so we can track each report time. 40 $report_id = $current_user; 41 if ( isset( $reporter[ $report_id ] ) ) { 42 $increment = 1; 15 public function detect_manual_modlook( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) { 16 $modlook = null; 17 18 /* 19 * Loop over the submitted terms to get the modlook taxonomy ID. 20 * it's slightly less ideal than a `in_array()` check, but is needed as users 21 * may have put spaces around the term somehow for example, and we need the ID 22 * later for processing. 23 */ 24 foreach ( $terms as $term_id => $term_slug ) { 25 if ( 'modlook' === trim( $term_slug ) ) { 26 $modlook = $tt_ids[ $term_id ]; 27 break; 28 } 29 } 30 31 // If no modlook tag was found, or if the tag already existed from a previous reply, don't record it again. 32 if ( null === $modlook || in_array( $modlook, $old_tt_ids ) ) { 33 return; 34 } 35 36 // Translators: Default string to show when a topic is reported outside the report form feature. 37 $this->add_modlook_history( $object_id, __( '[Manually added when replying]', 'wporg-forums' ), true ); 38 } 39 40 public function add_modlook_history( $topic, $reason = null ) { 41 $reporter = $this->get_previous_reports( $topic ); 42 if ( empty( $reporter ) ) { 43 $reporter = array(); 44 } 45 46 $current_user = get_current_user_id(); 47 48 // In those odd cases where the same user reports a topic multiple times, let's increment them, so we can track each report time. 49 $report_id = $current_user; 50 if ( isset( $reporter[ $report_id ] ) ) { 51 $increment = 1; 52 53 $report_id = sprintf( 54 '%d-%d', 55 $current_user, 56 $increment 57 ); 58 59 while ( isset( $reporter[ $report_id ] ) ) { 60 $increment++; 61 62 /* 63 * If someone reports the same topic repeatedly, let's just stop logging it to avoid 64 * a never ending incremental loop, our moderators are smart enough to pick up on such behavior. 65 */ 66 if ( $increment > 10 ) { 67 return; 68 } 43 69 44 70 $report_id = sprintf( … … 47 73 $increment 48 74 ); 49 50 while ( isset( $reporter[ $report_id ] ) ) { 51 $increment++; 52 53 /* 54 * If someone reports the same topic repeatedly, let's just stop logging it to avoid 55 * a never ending incremental loop, our moderators are smart enough to pick up on such behavior. 56 */ 57 if ( $increment > 10 ) { 58 return; 59 } 60 61 $report_id = sprintf( 62 '%d-%d', 63 $current_user, 64 $increment 65 ); 66 } 67 } 68 69 $reporter[ $report_id ] = array( 70 'time' => current_time( 'mysql' ), 71 'user' => $current_user, 72 ); 73 74 update_post_meta( $_GET['wporg-support-report-topic'], '_wporg_topic_reported_by', $reporter ); 75 76 wp_safe_redirect( get_the_permalink( $_GET['wporg-support-report-topic'] ) ); 75 } 76 } 77 78 $reporter[ $report_id ] = array( 79 'time' => current_time( 'mysql' ), 80 'user' => $current_user, 81 'reason' => $reason, 82 ); 83 84 update_post_meta( $topic, '_wporg_topic_reported_by', $reporter ); 85 86 } 87 88 public function capture_topic_report() { 89 // Do not process anything if the user is not logged in. 90 if ( ! is_user_logged_in() ) { 91 return; 92 } 93 94 if ( isset( $_POST['wporg-support-report-topic'] ) ) { 95 $action = sprintf( 96 'report-topic-%d', 97 $_POST['wporg-support-report-topic'] 98 ); 99 100 // Verify that the nonce is valid. 101 if ( ! wp_verify_nonce( $_POST['_wpnonce'], $action ) ) { 102 return; 103 } 104 105 remove_action( 'set_object_terms', array( $this, 'detect_manual_modlook' ), 10 ); 106 wp_add_object_terms( $_POST['wporg-support-report-topic'], 'modlook', 'topic-tag' ); 107 108 $reason = $_POST['topic-report-reason']; 109 110 if ( 'other-input' === $reason ) { 111 $reason = $_POST['topic-report-reason-other']; 112 } 113 114 $this->add_modlook_history( $_POST['wporg-support-report-topic'], $reason ); 115 116 wp_safe_redirect( get_the_permalink( $_POST['wporg-support-report-topic'] ) ); 77 117 78 118 exit(); … … 98 138 } 99 139 100 public function add_sidebar_ link() {140 public function add_sidebar_form() { 101 141 // We don't want to allow anonymous users to report topics, we want to track who reports them. 102 142 if ( ! is_user_logged_in() ) { … … 113 153 } 114 154 115 $current_user = get_current_user_id();116 155 $previous_reports = $this->get_previous_reports(); 117 156 $is_reported = has_term( 'modlook', 'topic-tag', $topic_id ); 118 157 119 $report_text = '';120 121 158 if ( $is_reported ) { 122 159 $report_text = __( 'This topic has been reported', 'wporg-forums' ); 123 160 } 124 161 else { 125 if ( isset( $previous_reports[ $current_user ] ) ) { 126 $report_text = sprintf( 127 '<a href="%s">%s</a>', 128 esc_url( $this->get_report_topic_url() ), 129 __( 'Report this topic again', 'wporg-forums' ) 130 ); 131 } 132 else { 133 $report_text = sprintf( 134 '<a href="%s">%s</a>', 135 esc_url( $this->get_report_topic_url() ), 136 __( 'Report this topic', 'wporg-forums' ) 137 ); 138 } 162 $action = sprintf( 163 'report-topic-%d', 164 bbp_get_topic_id() 165 ); 166 167 ob_start(); 168 ?> 169 170 <form action="" method="post"> 171 <?php wp_nonce_field( $action ); ?> 172 <input type="hidden" name="wporg-support-report-topic" value="<?php echo esc_attr( bbp_get_topic_id() ); ?>"> 173 174 <label for="topic-report-reason"><?php _e( 'Report this topic for:', 'wporg-forums' ); ?></label> 175 <select name="topic-report-reason" id="topic-report-reason" required="required" onchange="wporg_report_topic_change()"> 176 <option value=""><?php _ex( '— Choose one —', 'Report a topic reason', 'wporg-forums' ); ?></option> 177 <option><?php _ex( 'Guideline violation', 'Report a topic reason', 'wporg-forums' ); ?></option> 178 <option><?php _ex( 'Security related', 'Report a topic reason', 'wporg-forums' ); ?></option> 179 <option><?php _ex( 'Spam', 'Report a topic reason', 'wporg-forums' ); ?></option> 180 <option><?php _ex( 'NSFW (Not Safe For Work) link', 'Report a topic reason', 'wporg-forums' ); ?></option> 181 <option value="other-input"><?php _ex( 'Other', 'Report a topic reason', 'wporg-forums' ); ?></option> 182 </select> 183 <aside id="report-topic-other" style="display: none;"> 184 <label for="topic-report-reason-other"><?php _e( 'Your own reason:', 'wporg-forums' ); ?></label> 185 <input type="text" name="topic-report-reason-other" id="topic-report-reason-other"> 186 </aside> 187 <input type="submit" name="submit" value="<?php esc_attr_e( 'Report', 'wporg-forums' ); ?>"> 188 </form> 189 190 <script type="text/javascript"> 191 function wporg_report_topic_change() { 192 if ( 'other-input' === document.getElementById('topic-report-reason').value ) { 193 document.getElementById( 'report-topic-other' ).style.display = 'block'; 194 } else { 195 document.getElementById( 'report-topic-other' ).style.display = 'none'; 196 } 197 } 198 </script> 199 <?php 200 $report_text = ob_get_clean(); 139 201 } 140 202 … … 157 219 $lines = array(); 158 220 221 // List the latest report first. 222 $previous_reports = array_reverse( $previous_reports ); 223 159 224 foreach( $previous_reports as $report ) { 160 225 $lines[] = sprintf( 161 '<li><a href="%s">%s</a></li>', 162 esc_url( bbp_get_user_profile_url( $report['user']) ), 226 '<li>%s</li>', 163 227 sprintf( 164 /* translators: %1$s: Reporters display name, %2$s: date, %3$s: time */ 165 '%1$s on %2$s at %3$s', 166 get_the_author_meta( 'display_name', $report['user'] ), 228 /* translators: 1: Reporters display name, 2: date, 3: time, 4: reason (when provided) */ 229 '%1$s on %2$s at %3$s %4$s', 230 sprintf( 231 '<a href="%s">%s</a>', 232 esc_url( bbp_get_user_profile_url( $report['user']) ), 233 get_the_author_meta( 'display_name', $report['user'] ) 234 ), 167 235 /* translators: localized date format, see https://secure.php.net/date */ 168 236 mysql2date( __( 'F j, Y', 'wporg-forums' ), $report['time'] ), 169 237 /* translators: localized time format, see https://secure.php.net/date */ 170 mysql2date( __( 'g:i a', 'wporg-forums' ), $report['time'] ) 238 mysql2date( __( 'g:i a', 'wporg-forums' ), $report['time'] ), 239 ( ! isset( $report['reason'] ) || empty( $report['reason'] ) ? '' : sprintf( 240 /* translators: %s: The reason this topic was reported. */ 241 'reason: %s', 242 esc_html( $report['reason'] ) 243 ) ) 171 244 ) 172 245 ); … … 195 268 } 196 269 197 public function get_report_topic_url() {198 $url = add_query_arg( array(199 'wporg-support-report-topic' => bbp_get_topic_id(),200 ), get_the_permalink() );201 202 $action = sprintf(203 'report-topic-%d',204 bbp_get_topic_id()205 );206 207 return wp_nonce_url( $url, $action );208 }209 210 270 public function remove_topic_modlook_url() { 211 271 $url = add_query_arg( array(
Note: See TracChangeset
for help on using the changeset viewer.