Making WordPress.org


Ignore:
Timestamp:
03/17/2020 05:27:12 AM (6 years ago)
Author:
dd32
Message:

Support Forums: Upgrade the Report This Topic link from a link to a form with reasons.

Props Clorith.
Fixes #4821.

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  
    66
    77    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 );
    911
    1012        add_action( 'init', array( $this, 'capture_topic_report' ) );
    1113    }
    1214
    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                }
    4369
    4470                $report_id = sprintf(
     
    4773                    $increment
    4874                );
    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'] ) );
    77117
    78118            exit();
     
    98138    }
    99139
    100     public function add_sidebar_link() {
     140    public function add_sidebar_form() {
    101141        // We don't want to allow anonymous users to report topics, we want to track who reports them.
    102142        if ( ! is_user_logged_in() ) {
     
    113153        }
    114154
    115         $current_user     = get_current_user_id();
    116155        $previous_reports = $this->get_previous_reports();
    117156        $is_reported      = has_term( 'modlook', 'topic-tag', $topic_id );
    118157
    119         $report_text = '';
    120 
    121158        if ( $is_reported ) {
    122159            $report_text = __( 'This topic has been reported', 'wporg-forums' );
    123160        }
    124161        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( '&mdash; Choose one &mdash;', '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();
    139201        }
    140202
     
    157219            $lines = array();
    158220
     221            // List the latest report first.
     222            $previous_reports = array_reverse( $previous_reports );
     223
    159224            foreach( $previous_reports as $report ) {
    160225                $lines[] = sprintf(
    161                     '<li><a href="%s">%s</a></li>',
    162                     esc_url( bbp_get_user_profile_url( $report['user']) ),
     226                    '<li>%s</li>',
    163227                    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                        ),
    167235                        /* translators: localized date format, see https://secure.php.net/date */
    168236                        mysql2date( __( 'F j, Y', 'wporg-forums' ), $report['time'] ),
    169237                        /* 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                        ) )
    171244                    )
    172245                );
     
    195268    }
    196269
    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 
    210270    public function remove_topic_modlook_url() {
    211271        $url = add_query_arg( array(
Note: See TracChangeset for help on using the changeset viewer.