<!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>[11580] sites/trunk/wordpress.org/public_html/wp-content/plugins: Support Forums: Add unsubscription links to plugin/theme/tag subscription emails.</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/11580">11580</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/11580","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>2022-02-18 03:02:37 +0000 (Fri, 18 Feb 2022)</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 unsubscription links to plugin/theme/tag subscription emails.

Note: This does not add unsubscription links to bbPress emails, only the ones this plugin adds.
Emails from bbPress for forum / topic subscription ('notify me of replies') will not have these links.

See <a href="http://meta.trac.wordpress.org/ticket/5505">#5505</a>, <a href="http://meta.trac.wordpress.org/ticket/3456">#3456</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginssupportforumsincclassdirectorycompatphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-directory-compat.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginssupportforumsincclassstatsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-stats.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporgbbptermsubscriptionincclasspluginphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-term-subscription/inc/class-plugin.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginssupportforumsincclassdirectorycompatphp"></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-directory-compat.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-directory-compat.php    2022-02-17 23:04:09 UTC (rev 11579)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-directory-compat.php      2022-02-18 03:02:37 UTC (rev 11580)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -248,10 +248,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public function always_load() {
</span><span class="cx" style="display: block; padding: 0 10px">                // Add filters necessary for determining which compat file to use.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_action( 'bbp_init',              array( $this, 'register_taxonomy' ) );
-               add_filter( 'query_vars',            array( $this, 'add_query_var' ) );
-               add_action( 'bbp_add_rewrite_rules', array( $this, 'add_rewrite_rules' ) );
-               add_filter( 'term_link',             array( $this, 'get_term_link' ), 10, 3 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         add_action( 'bbp_init',                 array( $this, 'register_taxonomy' ) );
+               add_filter( 'query_vars',               array( $this, 'add_query_var' ) );
+               add_action( 'bbp_add_rewrite_rules',    array( $this, 'add_rewrite_rules' ) );
+               add_filter( 'term_link',                array( $this, 'get_term_link' ), 10, 3 );
+               add_filter( 'get_' . $this->taxonomy(), array( $this, 'get_term' ) );
</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">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -742,8 +743,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                do_action( 'bbp_template_notices' );
-
</del><span class="cx" style="display: block; padding: 0 10px">                 $term_subscription = '';
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $this->term ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $subscribe = $unsubscribe = '';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -771,43 +770,47 @@
</span><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">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Term subscriptions use `get_term_link` for the redirect. This needs to be
-        * filtered to redirect to the appropriate theme/plugin support view.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Correct the link to the compat terms.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param string $termlink The term link
-        * @param object $term The term object
-        * @param string $taxonomy The taxonomy object
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param string $term_link The term link
+        * @param object $term      The term object
+        * @param string $taxonomy  The taxonomy object
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return string The term link, or the support view link
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function get_term_link( $termlink, $term, $taxonomy ) {
-               if ( ! class_exists( 'WordPressdotorg\Forums\Term_Subscription\Plugin' ) ) {
-                       return $termlink;
-               }
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function get_term_link( $term_link, $term, $taxonomy ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Only do this for the non-public compat taxonomies.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $this->taxonomy() != $taxonomy ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        return $termlink;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 return $term_link;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Are we on a view where this needs filtering?
-               if (
-                       ( bbp_is_single_view() && $this->compat() == bbp_get_view_id() )
-               ||
-                       bbp_is_subscriptions()
-               ||
-                       ( isset( $_GET['term_id'] ) && isset( $_GET['taxonomy'] ) )
-               ) {
-                       // Check that the subscription is to this compat.
-                       if ( $this->taxonomy() == $taxonomy ) {
-                               $paged = get_query_var( 'paged' ) > 1 ? 'page/' . absint( get_query_var( 'paged' ) ) . '/' : '';
-                               $termlink = sprintf( home_url( '/%s/%s/%s' ), $this->compat(), $term->slug, $paged );
-                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $paged = '';
+               if ( get_query_var( 'paged' ) > 1 ) {
+                       $paged = 'page/' . absint( get_query_var( 'paged' ) ) . '/';
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return $termlink;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return sprintf(
+                       home_url( '/%s/%s/%s' ),
+                       $this->compat(),
+                       $term->slug,
+                       $paged
+               );
</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">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Set the term name for the compat terms to that of the directory.
+        * 
+        * @param \WP_Term $term 
+        * @return \WP_Term
+        */
+       public function get_term( $term ) {
+               // Note: Not using $this->title() here so as to filter other terms of this taxonomy correctly.
+
+               $term->name = $this->get_object( $term->slug )->post_title ?? $term->name;
+
+               return $term;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Set the compat taxonomy on a topic if that data is provided on new post.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param int $topic_id The topic id
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -838,14 +841,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public function get_object( $slug ) {
</span><span class="cx" style="display: block; padding: 0 10px">                global $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( 'theme' == $this->compat() ) {
-                       if ( ! is_null( $this->theme ) ) {
-                               return $this->theme;
-                       }
-               } else {
-                       if ( ! is_null( $this->plugin ) ) {
-                               return $this->plugin;
-                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if (
+                       'theme' == $this->compat() &&
+                       ! is_null( $this->theme ) &&
+                       ( ! $slug || $slug === $this->theme->post_name )
+               ) {
+                       return $this->theme;
+
+               } elseif (
+                       'plugin' == $this->compat() &&
+                       ! is_null( $this->plugin ) &&
+                       ( ! $slug || $slug === $this->plugin->post_name )
+               ) {
+                       return $this->plugin;
</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">                return self::get_object_by_slug_and_type( $slug, $this->compat() );
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginssupportforumsincclassstatsphp"></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-stats.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-stats.php       2022-02-17 23:04:09 UTC (rev 11579)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-stats.php 2022-02-18 03:02:37 UTC (rev 11580)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -32,6 +32,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $type   = str_replace( 'topic-', '', get_term( $term_id )->taxonomy );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                bump_stats_extra( 'wporg-support', $action . '-' . $type );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               // Tokenised links are from email unsubscribe links, record these in duplicate.
+               if ( isset( $_GET['token'] ) ) {
+                       $type .= '_email';
+                       if ( isset( $_POST['List-Unsubscribe'] ) && 'One-Click' === $_POST['List-Unsubscribe'] ) {
+                               $type .= '_oneclick';
+                       }
+
+                       bump_stats_extra( 'wporg-support', $action . '-' . $type );
+               }
+               
</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">        /**
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporgbbptermsubscriptionincclasspluginphp"></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/wporg-bbp-term-subscription/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/wporg-bbp-term-subscription/inc/class-plugin.php 2022-02-17 23:04:09 UTC (rev 11579)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-term-subscription/inc/class-plugin.php   2022-02-18 03:02:37 UTC (rev 11580)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6,17 +6,32 @@
</span><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">         * @todo AJAXify subscription action.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @todo Add unsubscribe link to outgoing emails.
</del><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private $subscribers = array();
-
</del><span class="cx" style="display: block; padding: 0 10px">         public $taxonomy  = false;
</span><span class="cx" style="display: block; padding: 0 10px">        public $labels    = array();
</span><span class="cx" style="display: block; padding: 0 10px">        public $directory = false;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        protected $term        = false;
+       protected $subscribers = array();
+
</ins><span class="cx" style="display: block; padding: 0 10px">         const META_KEY = '_bbp_term_subscription';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Valid actions for this plugin.
+        */
+       const VALID_ACTIONS = array(
+               'wporg_bbp_subscribe_term',
+               'wporg_bbp_unsubscribe_term',
+       );
+
+       /**
+        * Length of time the unsubscription links are valid.
+        * 
+        * @var int
+        */
+       const UNSUBSCRIBE_LIFETIME = 604800; // WEEK_IN_SECONDS
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function __construct( $args = array() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $r = wp_parse_args( $args, array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'taxonomy'  => 'topic-tag',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -25,7 +40,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'subscribed_header'      => __( 'Subscribed Topic Tags', 'wporg-forums' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                'subscribed_user_notice' => __( 'You are not currently subscribed to any topic tags.', 'wporg-forums' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                'subscribed_anon_notice' => __( 'This user is not currently subscribed to any topic tags.', 'wporg-forums' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'receipt'                => __( 'You are receiving this email because you are subscribed to a topic tag.', 'wporg-forums'),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'receipt'                => __( "You are receiving this email because you are subscribed to the %s tag.", 'wporg-forums'),
</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"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -33,6 +48,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->labels    = $r['labels'];
</span><span class="cx" style="display: block; padding: 0 10px">                $this->directory = $r['directory'];
</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 no taxonomy was provided, there's nothing we can do.
+               if ( ! $this->taxonomy ) {
+                       return;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 add_action( 'bbp_init', array( $this, 'bbp_init' ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -40,17 +60,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Initialize the plugin.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function bbp_init() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // If the user isn't logged in, there will be no topics or replies added.
-               if ( ! is_user_logged_in() ) {
-                       return;
-               }
-
-               if ( ! $this->taxonomy ) {
-                       return;
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 // Add views and actions for users.
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'bbp_get_request', array( $this, 'term_subscribe_handler' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_action( 'bbp_post_request', array( $this, 'term_subscribe_handler' ) );
+               add_action( 'bbp_template_redirect', array( $this, 'fix_bbpress_post_actions' ), 9 ); // before bbp_get_request/bbp_post_request
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Notify subscribers when a topic or reply with a given term is added.
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'bbp_new_topic', array( $this, 'notify_term_subscribers_of_new_topic' ), 10, 4 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -61,6 +74,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'bbp_forum_subscription_mail_title', array( $this, 'replace_forum_subscription_mail_title' ), 10, 2 );
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'bbp_subscription_mail_title',       array( $this, 'replace_topic_subscription_mail_title' ), 10, 3 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Add a section to the user subscriptions list to allow management of subscriptions.
</ins><span class="cx" style="display: block; padding: 0 10px">                 add_action( 'bbp_template_after_user_subscriptions', array( $this, 'user_subscriptions' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Add a banner above the 'forum' about being subscribed to it.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -69,39 +83,45 @@
</span><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">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * bbPress has two action handlers, GET and POST, a POST action cannot work with ?action= from the URL.
+        * 
+        * This is used by the email-unsubscription handler.
+        */
+       public function fix_bbpress_post_actions() {
+               if (
+                       bbp_is_post_request() &&
+                       empty( $_POST['action'] ) &&
+                       ! empty( $_GET['action'] ) &&
+                       in_array( $_GET['action'], self::VALID_ACTIONS, true )
+               ) {
+                       $_POST['action'] = $_GET['action'];
+               }
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Add a notice that you're subscribed to the tag/plugin/theme, or have just unsubscribed.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function before_view() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $term = false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $term = $this->get_current_term();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( $this->directory && $this->directory->slug && $this->directory->term ) {
-                       $term      = $this->directory->term;
-                       $term_name = $this->directory->title();
-               } elseif ( bbp_is_topic_tag() ) {
-                       $term = get_queried_object();
-                       if ( empty( $term->taxonomy ) || $term->taxonomy !== $this->taxonomy ) {
-                               return;
-                       }
-
-                       $term_name = $term->name;
-               }
-
-               if ( empty( $term->term_id ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! $term || $term->taxonomy !== $this->taxonomy ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                do_action( 'bbp_template_notices' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $is_subscribed = self::is_user_subscribed_to_term( get_current_user_id(), $term->term_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $is_subscribed ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $message = sprintf(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                __( 'You are subscribed to this forum, and will receive emails for future topic activity. <a href="%1$s">Unsubscribe from %2$s</a>', 'wporg-forums' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         __( 'You are subscribed to this forum, and will receive emails for future topic activity. <a href="%1$s">Unsubscribe from %2$s.</a>', 'wporg-forums' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                                 self::get_subscription_url( get_current_user_id(), $term->term_id, $this->taxonomy ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                esc_html( $term_name )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         esc_html( $term->name )
</ins><span class="cx" style="display: block; padding: 0 10px">                         );
</span><span class="cx" style="display: block; padding: 0 10px">                } elseif ( ! empty( $_GET['success'] ) && 'subscription-removed' === $_GET['success'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $message = sprintf(
</span><span class="cx" style="display: block; padding: 0 10px">                                __( 'You have been unsubscribed from future emails for %1$s.', 'wporg-forums' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                esc_html( $term_name )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         esc_html( $term->name )
</ins><span class="cx" style="display: block; padding: 0 10px">                         );
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -121,53 +141,78 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function term_subscribe_handler( $action = '' ) {
</span><span class="cx" style="display: block; padding: 0 10px">                // Bail if the actions aren't meant for this function.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! in_array( $action, self::get_valid_actions() ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! in_array( $action, self::VALID_ACTIONS ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! $this->taxonomy ) {
-                       return;
-               }
-
-               // Taxonomy mismatch; a different instance should handle this.
-               if ( ! isset( $_GET['taxonomy'] ) || $this->taxonomy != $_GET['taxonomy'] ) {
-                       return;
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 if ( ! bbp_is_subscriptions_active() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return false;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Bail if the actions aren't meant for this function.
-               if ( ! in_array( $action, self::get_valid_actions() ) ) {
-                       return;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Determine the term the request is for, overwrite with ?term_id if specified.
+               $term = $this->get_current_term();
+               if ( ! empty( $_GET['term_id'] ) ) {
+                       $term = get_term( intval( $_GET['term_id'] ), $this->taxonomy );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-               // Bail if no term id is passed.
-               if ( ! isset( $_GET['term_id'] ) || empty( $_GET['term_id'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! $term ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Get required data.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $term_id = $term->term_id;
+               $auth    = 'nonce';
</ins><span class="cx" style="display: block; padding: 0 10px">                 $user_id = get_current_user_id();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $term_id = intval( $_GET['term_id'] );
-               $term = get_term( $term_id, $this->taxonomy );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // If a user_id + token is provided, verify the request and maybe use the provided user_id.
+               if ( isset( $_GET['token'] ) ) {
+                       $auth    = 'token';
+                       $user_id = $this->has_valid_unsubscription_token();
+
+                       if ( ! $user_id ) {
+                               bbp_add_error( 'wporg_bbp_subscribe_invalid_token', __( '<strong>ERROR</strong>: Link expired!', 'wporg-forums' ) );
+                               return false;
+                       }
+
+                       // Require a POST request for verification. Gmail will bypass this for one-click unsubscriptions by POST'ing to the URL.
+                       if ( empty( $_POST ) ) {
+                               wp_die(
+                                       sprintf(
+                                               '<h1>%1$s</h1>' .
+                                               '<p>%2$s</p>' .
+                                               '<form method="POST" action="%3$s">' .
+                                                       '<input type="submit" name="confirm" value="%4$s">' .
+                                                       '&nbsp<a href="%5$s">%6$s</a>' .
+                                               '</form>',
+                                               get_bloginfo('name'),
+                                               sprintf(
+                                                       /* translators: 1: Plugin, Theme, or Tag name. */
+                                                       esc_html__( 'Do you wish to unsubscribe from future emails for %s?', 'wporg-forums' ),
+                                                       $term->name
+                                               ),
+                                               esc_attr( $_SERVER['REQUEST_URI'] ),
+                                               esc_attr__( 'Yes, unsubscribe me', 'wporg-forums' ),
+                                               get_term_link( $term ),
+                                               sprintf(
+                                                       /* translators: 1: Plugin, Theme, or Tag name. */
+                                                       esc_attr__( 'No, take me to the %s forum.', 'wporg-forums' ),
+                                                       $term->name
+                                               )
+                                       )
+                               );
+                               exit;
+                       }
+
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Check for empty term id.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! $term ) {
-                       /* translators: Term: topic tag */
-                       bbp_add_error( 'wporg_bbp_subscribe_term_id', __( '<strong>ERROR</strong>: No term was found! Which term are you subscribing/unsubscribing to?', 'wporg-forums' ) );
-
-               // Check for current user.
-               } elseif ( empty( $user_id ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( empty( $user_id ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         bbp_add_error( 'wporg_bbp_subscribe_logged_id', __( '<strong>ERROR</strong>: You must be logged in to do this!', 'wporg-forums' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Check nonce.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                } elseif ( ! bbp_verify_nonce_request( 'toggle-term-subscription_' . $user_id . '_' . $term_id . '_' . $this->taxonomy ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         } elseif ( 'nonce' === $auth && ! bbp_verify_nonce_request( 'toggle-term-subscription_' . $user_id . '_' . $term_id . '_' . $this->taxonomy ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         bbp_add_error( 'wporg_bbp_subscribe_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'wporg-forums' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Check current user's ability to spectate.
-               } elseif ( ! current_user_can( 'spectate' ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Check user's ability to spectate.
+               } elseif ( ! user_can( $user_id, 'spectate' ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         bbp_add_error( 'wporg_bbp_subscribe_permissions', __( '<strong>ERROR</strong>: You don\'t have permission to do this!', 'wporg-forums' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -175,13 +220,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $success       = false;
</ins><span class="cx" style="display: block; padding: 0 10px">                 $is_subscribed = self::is_user_subscribed_to_term( $user_id, $term_id );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $success       = false;
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( true === $is_subscribed && 'wporg_bbp_unsubscribe_term' === $action ) {
-                       $success = self::remove_user_subscription( $user_id, $term_id );
-               } elseif ( false === $is_subscribed && 'wporg_bbp_subscribe_term' === $action ) {
-                       $success = self::add_user_subscription( $user_id, $term_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( 'wporg_bbp_unsubscribe_term' === $action ) {
+                       $success = ! $is_subscribed || self::remove_user_subscription( $user_id, $term_id );
+               } elseif ( 'wporg_bbp_subscribe_term' === $action ) {
+                       $success = $is_subscribed || self::add_user_subscription( $user_id, $term_id );
</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">                // Redirect
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -223,26 +268,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                foreach ( $terms as $term ) {
-                       $subscribers = $this->get_term_subscribers( $term->term_id );
-                       if ( $subscribers ) {
-                               $this->subscribers = array_unique( array_merge( $subscribers, $this->subscribers ) );
-                       }
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Users that will be notified another way, or have already been notified.
+               $notified_users = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Remove the author from being notified of their own topic.
+               $notified_users[] = bbp_get_topic_author_id( $topic_id );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Get users who were already notified and exclude them.
</span><span class="cx" style="display: block; padding: 0 10px">                $forum_subscribers = bbp_get_forum_subscribers( $forum_id, true );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $forum_subscribers ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->subscribers = array_diff( $this->subscribers, $forum_subscribers );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $notified_users = array_merge( $notified_users, $forum_subscribers );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Remove the author from being notified of their own topic.
-               $this->subscribers = array_diff( $this->subscribers, array( bbp_get_topic_author_id( $topic_id ) ) );
-
-               if ( empty( $this->subscribers ) ) {
-                       return;
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 // Replace forum-specific messaging with term subscription messaging.
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'bbp_forum_subscription_mail_message', array( $this, 'replace_forum_subscription_mail_message' ), 10, 4 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -249,13 +286,38 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Replace forum subscriber list with term subscribers, avoiding duplicates.
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'bbp_forum_subscription_user_ids', array( $this, 'add_term_subscribers' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Actually notify our term subscribers.
-               bbp_notify_forum_subscribers( $topic_id, $forum_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Personalize the emails.
+               add_filter( 'wporg_bbp_subscription_email', array( $this, 'personalize_subscription_email' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                foreach ( $terms as $term ) {
+                       $subscribers = $this->get_term_subscribers( $term->term_id );
+                       if ( ! $subscribers ) {
+                               continue;
+                       }
+
+                       $subscribers = array_diff( $subscribers, $notified_users );
+                       if ( ! $subscribers ) {
+                               continue;
+                       }
+
+                       $this->term        = $term;
+                       $this->subscribers = $subscribers;
+
+                       // Actually notify the term subscribers.
+                       bbp_notify_forum_subscribers( $topic_id, $forum_id );
+
+                       // Don't email them twice.
+                       $notified_users = array_merge( $notified_users, $subscribers );
+               }
+
+               // Reset
+               $this->term        = false;
+               $this->subscribers = array();
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Remove filters.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                remove_filter( 'bbp_forum_subscription_mail_message', array( $this, 'replace_forum_subscription_mail_message' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 remove_filter( 'bbp_forum_subscription_user_ids',     array( $this, 'add_term_subscribers' ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                remove_filter( 'bbp_forum_subscription_mail_message', array( $this, 'replace_forum_subscription_mail_message' ), 10 );
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         remove_filter( 'wporg_bbp_subscription_email',        array( $this, 'personalize_subscription_email' ) );
</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">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -283,22 +345,30 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $topic_content = strip_tags( bbp_get_topic_content( $topic_id ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $topic_url     = get_permalink( $topic_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $message = sprintf( __( '%1$s wrote:
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $message = sprintf(
+                       /* translators: 1: Author, 2: Forum message, 3: Link to topic, 4: Descriptive text of why they're getting the email */
+                       __( '%1$s wrote:
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> %2$s
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-Topic Link: %3$s
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+Link: %3$s
</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"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+To reply, visit the above link and log in.
+Note that replying to this email has no effect.
+
</ins><span class="cx" style="display: block; padding: 0 10px"> %4$s
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-Log in and visit the topic to reply to the topic or unsubscribe from these emails. Note that replying to this email has no effect.', 'wporg-forums' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+To unsubscribe from future emails click here:
+####UNSUB_LINK####', 'wporg-forums' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         $topic_author_name,
</span><span class="cx" style="display: block; padding: 0 10px">                        $topic_content,
</span><span class="cx" style="display: block; padding: 0 10px">                        $topic_url,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // String may not have placeholders, ie. in the case of tags.
-                       sprintf( $this->labels['receipt'], $this->directory ? $this->directory->title() : '' )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 sprintf(
+                               $this->labels['receipt'],
+                               $this->get_current_term()->name
+                       )
</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">                return $message;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -344,26 +414,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                foreach ( $terms as $term ) {
-                       $subscribers = $this->get_term_subscribers( $term->term_id );
-                       if ( $subscribers ) {
-                               $this->subscribers = array_unique( array_merge( $subscribers, $this->subscribers ) );
-                       }
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Users that will be notified another way, or have already been notified.
+               $notified_users = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Remove the author from being notified of their own topic.
+               $notified_users[] = bbp_get_reply_author_id( $reply_id );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Get users who were already notified and exclude them.
</span><span class="cx" style="display: block; padding: 0 10px">                $topic_subscribers = bbp_get_topic_subscribers( $topic_id, true );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $topic_subscribers ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->subscribers = array_diff( $this->subscribers, $topic_subscribers );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $notified_users = array_merge( $notified_users, $topic_subscribers );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Remove the author from being notified of their own reply.
-               $this->subscribers = array_diff( $this->subscribers, array( bbp_get_reply_author_id( $reply_id ) ) );
-
-               if ( empty( $this->subscribers ) ) {
-                       return;
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 // Replace topic-specific messaging with term subscription messaging.
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'bbp_subscription_mail_message', array( $this, 'replace_topic_subscription_mail_message' ), 10, 3 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -370,12 +432,38 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Replace forum subscriber list with term subscribers, avoiding duplicates.
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'bbp_topic_subscription_user_ids', array( $this, 'add_term_subscribers' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Actually notify our term subscribers.
-               bbp_notify_topic_subscribers( $reply_id, $topic_id, $forum_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Personalize the emails.
+               add_filter( 'wporg_bbp_subscription_email', array( $this, 'personalize_subscription_email' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                foreach ( $terms as $term ) {
+                       $subscribers = $this->get_term_subscribers( $term->term_id );
+                       if ( ! $subscribers ) {
+                               continue;
+                       }
+
+                       $subscribers = array_diff( $subscribers, $notified_users );
+                       if ( ! $subscribers ) {
+                               continue;
+                       }
+
+                       $this->term        = $term;
+                       $this->subscribers = $subscribers;
+
+                       // Actually notify our term subscribers.
+                       bbp_notify_topic_subscribers( $reply_id, $topic_id, $forum_id );
+
+                       // Don't email them twice.
+                       $notified_users = array_merge( $notified_users, $subscribers );
+               }
+
+               // Reset
+               $this->term        = false;
+               $this->subscribers = array();
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Remove filters.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                remove_filter( 'bbp_subscription_mail_message',   array( $this, 'replace_topic_subscription_mail_message' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 remove_filter( 'bbp_topic_subscription_user_ids', array( $this, 'add_term_subscribers' ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                remove_filter( 'bbp_subscription_mail_message',   array( $this, 'replace_topic_subscription_mail_message' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         remove_filter( 'wporg_bbp_subscription_email',    array( $this, 'personalize_subscription_email' ) );
</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">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -395,22 +483,30 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $reply_content = strip_tags( bbp_get_reply_content( $reply_id ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $reply_url = bbp_get_reply_url( $reply_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $message = sprintf( __( '%1$s wrote:
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $message = sprintf(
+                       /* translators: 1: Author, 2: Forum message, 3: Link to topic, 4: Descriptive text of why they're getting the email */
+                       __( '%1$s wrote:
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> %2$s
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-Reply Link: %3$s
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+Link: %3$s
</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"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+To reply, visit the above link and log in.
+Note that replying to this email has no effect.
+
</ins><span class="cx" style="display: block; padding: 0 10px"> %4$s
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-Log in and visit the topic to reply to the topic or unsubscribe from these emails. Note that replying to this email has no effect.', 'wporg-forums' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+To unsubscribe from future emails click here:
+####UNSUB_LINK####', 'wporg-forums' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         $reply_author_name,
</span><span class="cx" style="display: block; padding: 0 10px">                        $reply_content,
</span><span class="cx" style="display: block; padding: 0 10px">                        $reply_url,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // String may not have placeholders, ie. in the case of tags.
-                       sprintf( $this->labels['receipt'], $this->directory ? $this->directory->title() : '' )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 sprintf(
+                               $this->labels['receipt'],
+                               $this->get_current_term()->name
+                       )
</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">                return $message;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -441,6 +537,62 @@
</span><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">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Personalize subscription emails by adding an unsubscription link.
+        * 
+        * @param array $email_parts The email parts.
+        * @return The email parts.
+        */
+       public function personalize_subscription_email( $email_parts ) {
+               // get_tokenised_unsubscribe_url() will validate the user object.
+               $user       = get_user_by( 'email', $email_parts['to'] );
+               $unsub_link = $this->get_tokenised_unsubscribe_url( $user, $this->term );
+
+               if ( ! $unsub_link ) {
+                       return $email_parts;
+               }
+
+               $email_parts['message'] = str_replace(
+                       '####UNSUB_LINK####',
+                       '<' . esc_url_raw( $unsub_link ) . '>',
+                       $email_parts['message']
+               );
+
+               $email_parts['headers'][] = 'List-Unsubscribe: <' . esc_url_raw( $unsub_link ) . '>';
+               $email_parts['headers'][] = 'List-Unsubscribe-Post: List-Unsubscribe=One-Click';
+
+               return $email_parts;
+       }
+
+       /**
+        * Get the WP_Term instance for the currently displayed item.
+        */
+       protected function get_current_term() {
+               if ( $this->term ) {
+                       return $this->term;
+               }
+
+               // The currently queried tag.
+               if (
+                       bbp_is_topic_tag() &&
+                       ( $term = get_queried_object() ) &&
+                       ( $term instanceof \WP_Term )
+               ) {
+                       return $term;
+               }
+
+               // The current directory loaded.
+               if (
+                       $this->directory &&
+                       bbp_is_single_view() &&
+                       $this->directory->compat() == bbp_get_view_id()
+               ) {
+                       return $this->directory->term;
+               }
+
+               return false;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Add a term subscription block to the user's profile.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function user_subscriptions() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -464,7 +616,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( $terms ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                echo '<p id="bbp-term-' . esc_attr( $this->taxonomy ) . '">' . "\n";
</span><span class="cx" style="display: block; padding: 0 10px">                                foreach ( $terms as $term ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        echo '<a href="' . esc_url( get_term_link( $term->term_id ) ) . '">' . esc_html( $term->slug ) . '</a>';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 echo '<a href="' . esc_url( get_term_link( $term->term_id ) ) . '">' . esc_html( $term->name ) . '</a>';
</ins><span class="cx" style="display: block; padding: 0 10px">                                         if ( get_current_user_id() == $user_id ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                $url = self::get_subscription_url( $user_id, $term->term_id, $this->taxonomy );
</span><span class="cx" style="display: block; padding: 0 10px">                                                echo ' (<a href="' . esc_url( $url ) . '">' . esc_html( 'Unsubscribe', 'wporg-forums' ) . '</a>)';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -494,22 +646,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array|bool Results if user has subscriptions, otherwise false
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public static function get_user_taxonomy_subscriptions( $user_id = 0, $taxonomy = 'topic-tag' ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $retval = false;
-
</del><span class="cx" style="display: block; padding: 0 10px">                 if ( empty( $user_id ) || empty( $taxonomy ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return false;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $terms = get_terms( array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $subscriptions = get_terms( array(
</ins><span class="cx" style="display: block; padding: 0 10px">                         'taxonomy'   => $taxonomy,
</span><span class="cx" style="display: block; padding: 0 10px">                        'meta_key'   => self::META_KEY,
</span><span class="cx" style="display: block; padding: 0 10px">                        'meta_value' => $user_id,
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! empty( $terms ) ) {
-                       $retval = $terms;
-               }
-               return apply_filters( 'wporg_bbp_get_user_taxonomy_subscriptions', $retval, $user_id, $taxonomy );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Default to false if empty.
+               $subscriptions = $subscriptions ?: false;
+
+               return apply_filters( 'wporg_bbp_get_user_taxonomy_subscriptions', $subscriptions, $user_id, $taxonomy );
</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">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -528,6 +678,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $subscribers = get_term_meta( $term_id, self::META_KEY );
</span><span class="cx" style="display: block; padding: 0 10px">                        wp_cache_set( 'wporg_bbp_get_term_subscribers_' . $term_id, $subscribers, 'bbpress_users' );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return apply_filters( 'wporg_bbp_get_term_subscribers', $subscribers, $term_id );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -618,9 +769,9 @@
</span><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 ( self::is_user_subscribed_to_term( $user_id, $term_id ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $query_args = array( 'action' => 'wporg_bbp_unsubscribe_term', 'term_id' => $term_id, 'taxonomy' => $taxonomy );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $action = 'wporg_bbp_unsubscribe_term';
</ins><span class="cx" style="display: block; padding: 0 10px">                 } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $query_args = array( 'action' => 'wporg_bbp_subscribe_term', 'term_id' => $term_id, 'taxonomy' => $taxonomy );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $action = 'wporg_bbp_subscribe_term';
</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 ( bbp_is_subscriptions() ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -629,15 +780,102 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $permalink = get_term_link( $term_id );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-term-subscription_' . $user_id . '_' . $term_id . '_' . $taxonomy ) );
-               return $url;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $url = wp_nonce_url(
+                       add_query_arg( compact( 'action', 'term_id' ), $permalink ),
+                       'toggle-term-subscription_' . $user_id . '_' . $term_id . '_' . $taxonomy
+               );
+
+               return esc_url( $url );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public static function get_subscription_link( $args ) {
-               if ( ! current_user_can( 'spectate' ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Generates a unsubscription token for a user.
+        *
+        * The token form is 'user_id|expiry|hash', and dependant upon the user email, password, and term.
+        *
+        * @param WP_Term $term   The user the token should be for.
+        * @param WP_User $user   The user the token should be for.
+        * @param int     $expiry The expiry of the token. Optional, only required for verifying tokens.
+        * @return string|bool The hashed token, false on failure.
+        */
+       protected static function generate_unsubscribe_token( $term, $user, $expiry = 0 ) {
+               if ( ! $term || ! $user ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return false;
</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 ( ! $expiry ) {
+                       $expiry = time() + self::UNSUBSCRIBE_LIFETIME;
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $expiry    = intval( $expiry );
+               $pass_frag = substr( $user->user_pass, 8, 4 ); // Password fragment used by cookie auth.
+               $key       = wp_hash( $term->term_id . '|' . $term->taxonomy . '|' . $user->user_email . '|' . $pass_frag . '|' . $expiry, 'forum_subcriptions' );
+               $hash      = hash_hmac( 'sha256',  $term->term_id . '|' . $term->taxonomy . '|' . $user->user_email . '|' . $expiry, $key );
+
+               return $user->ID . '|' . $expiry . '|' . $hash;
+       }
+
+       /**
+        * Validate if the current request has a valid tokenised unsubscription link.
+        * 
+        * @return bool|int User ID on success, false on failure.
+        */
+       protected function has_valid_unsubscription_token() {
+               if (
+                       ! isset( $_GET['token'] ) ||
+                       2 !== substr_count( $_GET['token'], '|' )
+               ) {
+                       return false;
+               }
+
+               $provided_token            = rtrim( $_GET['token'], '>' );
+               list( $user_id, $expiry, ) = explode( '|', $provided_token );
+               $term                      = $this->get_current_term();
+               $user                      = get_user_by( 'id', intval( $user_id ) );
+               $expected_token            = self::generate_unsubscribe_token( $term, $user, $expiry );
+
+               if (
+                       $expiry > time() &&
+                       hash_equals( $expected_token, $provided_token )
+               ) {
+                       return $user->ID;
+               }
+
+               return false;
+       }
+
+       /**
+        * Generate a tokenised unsubscription link for a given user & term.
+        * 
+        * This link can be used without being logged in.
+        * 
+        * @param \WP_User $user The user to generate the link for.
+        * @param \WP_Term $term The term to generate the link for.
+        * 
+        * @return bool|string The URL, or false upon failure.
+        */
+       public static function get_tokenised_unsubscribe_url( $user, $term ) {
+               $token  = self::generate_unsubscribe_token( $term, $user );
+               if ( ! $token ) {
+                       return false;
+               }
+
+               return add_query_arg(
+                       array(
+                               'action'   => 'wporg_bbp_unsubscribe_term',
+                               'token'    => $token,
+                       ),
+                       // We don't include the term_id in the URL, and instead rely upon the term coming from the URL
+                       get_term_link( $term )
+               );
+       }
+
+       /**
+        * Generate an unsubscription link for use in a Template.
+        * 
+        * @param array $args
+        * @return string
+        */
+       public static function get_subscription_link( $args ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $r = bbp_parse_args( $args, array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'user_id'     => get_current_user_id(),
</span><span class="cx" style="display: block; padding: 0 10px">                        'term_id'     => 0,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -647,16 +885,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'unsubscribe' => esc_html__( 'Unsubscribe from this topic tag', 'wporg-forums' ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'js_confirm'  => esc_html__( 'Are you sure you wish to subscribe by email to all future topics created in this tag?', 'wporg-forums' ),
</span><span class="cx" style="display: block; padding: 0 10px">                ), 'get_term_subscription_link' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( empty( $r['user_id'] ) || empty( $r['term_id'] ) || empty( $r['taxonomy'] ) ) {
-                       return false;
-               }
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $user_id  = $r['user_id'];
</span><span class="cx" style="display: block; padding: 0 10px">                $term_id  = $r['term_id'];
</span><span class="cx" style="display: block; padding: 0 10px">                $taxonomy = $r['taxonomy'];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $url = self::get_subscription_url( $r['user_id'], $r['term_id'], $r['taxonomy'] );
-               if ( self::is_user_subscribed_to_term( $r['user_id'], $r['term_id'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( empty( $user_id ) || empty( $term_id ) || empty( $taxonomy ) ) {
+                       return false;
+               }
+
+               if ( ! user_can( $user_id, 'spectate' ) ) {
+                       return false;
+               }
+
+               $url = self::get_subscription_url( $user_id, $term_id, $taxonomy );
+               if ( self::is_user_subscribed_to_term( $user_id, $term_id ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $text       = $r['unsubscribe'];
</span><span class="cx" style="display: block; padding: 0 10px">                        $js_confirm = '';
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -672,11 +915,4 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        esc_html( $text )
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-       public static function get_valid_actions() {
-               return array(
-                       'wporg_bbp_subscribe_term',
-                       'wporg_bbp_unsubscribe_term',
-               );
-       }
</del><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>