<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[9802] sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums: Support Forums: Add the ability to flag topics as NSFW.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/9802">9802</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/9802","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dd32</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-05-01 04:47:37 +0000 (Fri, 01 May 2020)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Support Forums: Add the ability to flag topics as NSFW.

This supports both a checkbox, and for matching on a phrase within the title/url/tags.

Props Clorith, denisco.
Fixes <a href="http://meta.trac.wordpress.org/ticket/2018">#2018</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginssupportforumsincclasshooksphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-hooks.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginssupportforumsincclasspluginphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-plugin.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginssupportforumssupportforumsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/support-forums.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginssupportforumsincclassnsfwhandlerphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-nsfw-handler.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginssupportforumsincclasshooksphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-hooks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-hooks.php       2020-05-01 03:31:13 UTC (rev 9801)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-hooks.php 2020-05-01 04:47:37 UTC (rev 9802)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -488,6 +488,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $robots = true;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // If topic is marked as NSFW.
+                       $is_nsfw = get_post_meta( bbp_get_topic_id(), '_topic_is_nsfw', true );
+
+                       if ( $is_nsfw ) {
+                               $robots = true;
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $robots ) {
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginssupportforumsincclassnsfwhandlerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-nsfw-handler.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-nsfw-handler.php                                (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-nsfw-handler.php  2020-05-01 04:47:37 UTC (rev 9802)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,201 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+namespace WordPressdotorg\Forums;
+
+class NSFW_Handler {
+
+       public function __construct() {
+               add_action( 'init', array( $this, 'register_taxonomy' ) );
+
+               add_action( 'bbp_new_topic_post_extras', array( $this, 'manually_assign_nsfw_flag' ), 10 );
+               add_action( 'bbp_edit_topic_post_extras', array( $this, 'manually_assign_nsfw_flag' ), 10 );
+
+               add_action( 'bbp_new_topic_post_extras', array( $this, 'maybe_flag_nsfw_automatically' ), 11 );
+               add_action( 'bbp_edit_topic_post_extras', array( $this, 'maybe_flag_nsfw_automatically' ), 11 );
+
+               add_action( 'nsfw_terms_add_form_fields', array( $this, 'add_term_meta' ) );
+
+               add_action( 'bbp_theme_before_topic_form_subscriptions', array( $this, 'mark_topic_nsfw_form_field' ) );
+
+               add_filter( 'bbp_get_topic_title', array( $this, 'prepend_topic_title' ), 10, 2 );
+
+               add_action( 'admin_head', array( $this, 'taxonomy_styles' ) );
+       }
+
+       /**
+        * Provide additional term meta fields.
+        */
+       public function add_term_meta() {
+               // Output a helping text indicating what to use the description field for.
+               echo '<p>' . __( 'Please provide a reason for adding this term in the description field', 'wporg-forums' ) . '</p>';
+       }
+
+       /**
+        * Output some CSS to keep the NSFW term interface as simple as possible.
+        */
+       public function taxonomy_styles() {
+               if ( ! isset( $_GET['taxonomy'] ) || 'nsfw_terms' !== $_GET['taxonomy'] ) {
+                       return;
+               }
+
+               ?>
+
+               <style type="text/css">
+                       body.taxonomy-nsfw_terms .term-slug-wrap {
+                               display: none;
+                       }
+                       body.taxonomy-nsfw_terms .column-posts,
+                       body.taxonomy-nsfw_terms .column-slug {
+                               display: none;
+                       }
+               </style>
+
+               <?php
+       }
+
+       public function register_taxonomy() {
+               register_taxonomy(
+                       'nsfw_terms',
+                       'forum',
+                       array(
+                               'labels' => array(
+                                       'name' => __( 'NSFW Terms', 'wporg-forums' ),
+                               ),
+                               'description'   => __( 'Declare terms for mature content that the forums will automatically mark not safe for work (NSFW)', 'wporg-forums' ),
+                               'public'        => false,
+                               'show_ui'       => true,
+                               'show_in_menu'  => true,
+                               'show_in_rest'  => false,
+                               'show_tagcloud' => false,
+                               'show_in_quick_edit' => false,
+                               'capabilities' => array(
+                                       'keep_gate',
+                               ),
+                       )
+               );
+       }
+
+       /**
+        * Helper to check if a topic is in a forum that should get the added features of this file.
+        *
+        * @param $topic_id
+        *
+        * @return bool
+        */
+       private function topic_in_affected_forum( $topic_id ) {
+               return ( Plugin::REVIEWS_FORUM_ID != bbp_get_topic_forum_id( $topic_id ) && ( ! bbp_is_single_view() || 'reviews' !== bbp_get_view_id() ) );
+       }
+
+       /**
+        * Output an additional form field when creating or editing a topic to mark it as containing mature content.
+        */
+       public function mark_topic_nsfw_form_field() {
+               $topic_id = bbp_get_topic_id();
+
+               if ( $this->topic_in_affected_forum( $topic_id ) ) {
+                       $checked = false;
+
+                       if ( bbp_is_topic_edit() ) {
+                               $checked = get_post_meta( $topic_id, '_topic_is_nsfw', true );
+                       }
+
+                       ?>
+
+                       <p>
+                               <label for="topic-is-nsfw">
+                                       <input type="checkbox" name="topic_is_nsfw" id="topic-is-nsfw" <?php checked( true, $checked ); ?>>
+                                       <?php esc_html_e( 'This topic relates to mature content and may be considered Not Safe For Work (NSFW)', 'wporg-forums' ); ?>
+                               </label>
+                       </p>
+
+                       <?php
+               }
+       }
+
+       /**
+        * Capture manually assigned mature content from form submissions.
+        *
+        * @param $topic_id
+        */
+       public function manually_assign_nsfw_flag( $topic_id ) {
+               if ( ! $this->topic_in_affected_forum( $topic_id ) ) {
+                       return;
+               }
+
+               if ( isset( $_POST['topic_is_nsfw'] ) ) {
+                       update_post_meta( $topic_id, '_topic_is_nsfw', true );
+               } else {
+                       delete_post_meta( $topic_id, '_topic_is_nsfw' );
+               }
+       }
+
+       /**
+        * Automatically test if a topic might contain mature content when being published.
+        *
+        * @param $topic_id
+        */
+       public function maybe_flag_nsfw_automatically( $topic_id ) {
+               // If the topic was manually flagged, save some processing by not seeing if it needs automatic flagging.
+               if ( isset( $_POST['topic_is_nsfw'] ) ) {
+                       return;
+               }
+
+               /* Skip checking for moderators, this is so that the flagging happens even on edits
+                * by users, but allows mods to force-remove the tag if needed.
+                */
+               if ( current_user_can( 'moderate' ) ) {
+                       return;
+               }
+
+               $topic_title = bbp_get_topic_title( $topic_id );
+               $topic_url   = $_POST['site_url'] ?? '';
+               $topic_tags  = $_POST['bbp_topic_tags'] ?? '';
+
+               $terms = get_terms(
+                       array(
+                               'taxonomy'   => 'nsfw_terms',
+                               'hide_empty' => false,
+                       )
+               );
+
+               foreach ( $terms as $term ) {
+                       // If the term is found in the title, tags, or URL field, mark it as NSFW and stop processing.
+                       if (
+                               stristr( $topic_title, $term->name ) ||
+                               stristr( $topic_url, $term->name ) ||
+                               stristr( $topic_tags, $term->name )
+                       ) {
+                               update_post_meta( $topic_id, '_topic_is_nsfw', true );
+                               return;
+                       }
+               }
+       }
+
+       /**
+        * Add a NSFW tag to any title marked as such.
+        *
+        * @param $title
+        * @param $topic_id
+        *
+        * @return string
+        */
+       public function prepend_topic_title( $title, $topic_id ) {
+               $is_nsfw = get_post_meta( $topic_id, '_topic_is_nsfw', true );
+
+               if ( ! $is_nsfw ) {
+                       return $title;
+               }
+
+               $prefix = sprintf(
+                       '<abbr title="%s">%s</abbr>',
+                       // translators: Explanation of what the abbreviation NSFW means.
+                       esc_attr__( 'Not Safe For Work / Mature content', 'wporg-forums' ),
+                       // translators: Prepended titles for topics with mature content.
+                       __( '[NSFW]', 'wporg-forums' )
+               );
+
+               return "$prefix $title";
+       }
+
+}
+
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-nsfw-handler.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginssupportforumsincclasspluginphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-plugin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-plugin.php      2020-05-01 03:31:13 UTC (rev 9801)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-plugin.php        2020-05-01 04:47:37 UTC (rev 9802)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -42,6 +42,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->moderators   = new Moderators;
</span><span class="cx" style="display: block; padding: 0 10px">                $this->hooks        = new Hooks;
</span><span class="cx" style="display: block; padding: 0 10px">                $this->report_topic = new Report_Topic;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->nsfw_handler = new NSFW_Handler;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // These modifications are specific to https://wordpress.org/support/
</span><span class="cx" style="display: block; padding: 0 10px">                $blog_id = get_current_blog_id();
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginssupportforumssupportforumsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/support-forums.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/support-forums.php        2020-05-01 03:31:13 UTC (rev 9801)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/support-forums.php  2020-05-01 04:47:37 UTC (rev 9802)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -25,6 +25,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> include( dirname( __FILE__ ) . '/inc/class-moderators.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> include( dirname( __FILE__ ) . '/inc/class-hooks.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> include( dirname( __FILE__ ) . '/inc/class-report-topic.php' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+include( dirname( __FILE__ ) . '/inc/class-nsfw-handler.php' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> // Compat-only includes.
</span><span class="cx" style="display: block; padding: 0 10px"> include( dirname( __FILE__ ) . '/inc/class-dropin.php' );
</span></span></pre>
</div>
</div>

</body>
</html>