<!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>[4043] sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc: Support Forums: Allow plugin/theme authors to set topics as sticky on their support forum.</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 { 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/4043">4043</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/4043","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>jmdodd</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-09-13 01:49:12 +0000 (Tue, 13 Sep 2016)</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: Allow plugin/theme authors to set topics as sticky on their support forum.</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_htmlwpcontentpluginssupportforumsincclassstickiescompatphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-stickies-compat.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    2016-09-13 00:17:58 UTC (rev 4042)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-directory-compat.php      2016-09-13 01:49:12 UTC (rev 4043)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -185,6 +185,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        $this->ratings = new Ratings_Compat( $this->compat(), $this->slug(), $this->taxonomy(), $this->get_object( $this->slug() ) );
</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">+                                // Instantiate WPORG_Stickies mode for topic view.
+                               if ( class_exists( 'WordPressdotorg\Forums\Stickies_Compat' ) ) {
+                                       $this->stickies = new Stickies_Compat( $this->compat(), $this->slug(), $this->taxonomy(), $this->get_object( $this->slug() ), $this->term, $this->authors );
+                               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $this->loaded = true;
</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_htmlwpcontentpluginssupportforumsincclassstickiescompatphp"></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-stickies-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-stickies-compat.php     2016-09-13 00:17:58 UTC (rev 4042)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-stickies-compat.php       2016-09-13 01:49:12 UTC (rev 4043)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12,7 +12,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        var $object   = null;
</span><span class="cx" style="display: block; padding: 0 10px">        var $term     = null;
</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 __construct( $compat, $slug, $taxonomy, $object, $term ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function __construct( $compat, $slug, $taxonomy, $object, $term, $authors = array() ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( empty( $compat ) || empty( $slug ) || empty( $taxonomy ) || empty( $object ) || empty( $term ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -22,8 +22,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->taxonomy = $taxonomy;
</span><span class="cx" style="display: block; padding: 0 10px">                $this->object   = $object;
</span><span class="cx" style="display: block; padding: 0 10px">                $this->term     = $term;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->authors  = $authors;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Add compat stickies to sticky array.
</ins><span class="cx" style="display: block; padding: 0 10px">                 add_filter( 'bbp_get_stickies', array( $this, 'get_compat_stickies' ), 10, 2 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               // Add topic toggle for compat authors.
+               add_action( 'bbp_get_request', array( $this, 'sticky_handler' ) );
+
+               // Add link to topic admin.
+               add_filter( 'bbp_topic_admin_links', array( $this, 'admin_links' ), 10, 2 );
</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">@@ -41,6 +49,199 @@
</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">+         */
+       public function sticky_handler( $action = '' ) {
+               // Bail if the action isn't meant for this function.
+               if ( ! in_array( $action, array(
+                       'wporg_bbp_stick_topic',
+                       'wporg_bbp_unstick_topic',
+               ) ) ) {
+                       return;
+               }
+
+               // Bail if no topic id or term id are passed.
+               if ( empty( $_GET['topic_id'] ) || empty( $_GET['term_id'] ) ) {
+                       return;
+               }
+
+               // Get required data.
+               $topic   = get_post( absint( $_GET['topic_id'] ) );
+               $term    = get_term( absint( $_GET['term_id'] ) );
+               $user_id = get_current_user_id();
+
+               // Check for empty topic or term id.
+               if ( ! $topic || ! $term ) {
+                       bbp_add_error( 'wporg_bbp_sticky_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you sticking?', 'wporg-forums' ) );
+
+               // Check user.
+               } elseif ( ! $this->user_can_stick( $user_id, $term->term_id, $topic->ID ) ) {
+                       bbp_add_error( 'wporg_bbp_sticky_logged_in', __( '<strong>ERROR</strong>: You do not have permission to do this!', 'wporg-forums' ) );
+
+               // Check nonce.
+               } elseif( ! bbp_verify_nonce_request( 'toggle-topic-sticky_' . $topic->ID . '_' . $term->term_id ) ) {
+                       bbp_add_error( 'wporg_bbp_sticky_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'wporg-forums' ) );
+               }
+
+               if ( bbp_has_errors() ) {
+                       return;
+               }
+
+               $is_sticky = self::is_sticky( $term->term_id, $topic->ID );
+               $success = false;
+
+               // Stick/unstick the topic.
+               if ( 'wporg_bbp_stick_topic' == $action ) {
+                       $success = self::add_sticky( $term->term_id, $topic->ID );
+               } elseif ( 'wporg_bbp_unstick_topic' == $action ) {
+                       $success = self::remove_sticky( $term->term_id, $topic->ID );
+               }
+
+               $permalink = get_permalink( $topic->ID );
+
+               if ( $success && ! is_wp_error( $success ) ) {
+                       bbp_redirect( $permalink );
+               } elseif ( true === $is_sticky && 'wporg_bbp_stick_topic' == $action ) {
+                       bbp_add_error( 'wporg_bbp_stick_topic', __( '<strong>ERROR<strong>: There was a problem sticking that topic!', 'wporg-forums' ) );
+               } elseif ( false === $is_sticky && 'wporg_bbp_unstick_topic' == $action ) {
+                       bbp_add_error( 'wporg_bbp_unstick_topic', __( '<strong>ERROR</strong>: There was a problem unsticking that topic!', 'wporg-forums' ) );
+               }
+       }
+
+       /**
+        * Replace the bbPress stick link with the custom stick link in compat topics.
+        *
+        * @param array $r The array of admin links
+        * @param int $topic_id The topic id
+        * @return array The filtered array of admin links
+        */
+       public function admin_links( $r, $topic_id ) {
+               if ( ! bbp_is_single_topic() ) {
+                       return $r;
+               }
+               $user_id = get_current_user_id();
+               if ( $this->user_can_stick( $user_id, $this->term->term_id, $topic_id ) ) {
+                       $r['stick'] = self::get_stick_link( array( 'topic_id' => $topic_id, 'term_id' => $this->term->term_id ) );
+               } else {
+                       unset( $r['stick'] );
+               }
+               return $r;
+       }
+
+       /**
+        * Get the link to stick/unstick a compat topic.
+        *
+        * @param array $args The link arguments
+        * @return string The linked URL
+        */
+       public static function get_stick_link( $args = array() ) {
+               $user_id = get_current_user_id();
+
+               $r = bbp_parse_args( $args, array(
+                       'topic_id' => get_the_ID(),
+                       'term_id'  => 0,
+                       'stick'    => esc_html__( 'Stick', 'wporg-forums' ),
+                       'unstick'  => esc_html__( 'Unstick', 'wporg-forums' ),
+               ), 'get_topic_stick_link' );
+               if ( empty( $r['topic_id'] ) || empty( $r['term_id'] ) ) {
+                       return false;
+               }
+
+               $topic = get_post( $r['topic_id'] );
+               $term  = get_term( $r['term_id'] );
+               if ( ! $topic || ! $term ) {
+                       return false;
+               }
+
+               if ( self::is_sticky( $term->term_id, $topic->ID ) ) {
+                       $text = $r['unstick'];
+                       $query_args = array( 'action' => 'wporg_bbp_unstick_topic', 'topic_id' => $topic->ID, 'term_id' => $term->term_id );
+               } else {
+                       $text = $r['stick'];
+                       $query_args = array( 'action' => 'wporg_bbp_stick_topic', 'topic_id' => $topic->ID, 'term_id' => $term->term_id );
+               }
+
+               $permalink = get_permalink( $topic->ID );
+               $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-topic-sticky_' . $topic->ID . '_' . $term->term_id ) );
+               return sprintf( "<a href='%s'>%s</a>", $url, esc_html( $text ) );
+       }
+
+       /**
+        * Is a given user allowed to stick/unstick a topic?
+        *
+        * @param int $user_id The user id
+        * @param int $term_id The term id
+        * @param int $topic_id The topic id
+        * @return bool True if allowed, false if not
+        */
+       public function user_can_stick( $user_id = 0, $term_id = 0, $topic_id = 0 ) {
+               $retval = $topic = $term = false;
+
+               if ( empty( $user_id ) ) {
+                       $user_id = get_current_user_id();
+               }
+
+               if ( $topic_id ) {
+                       $topic = get_post( $topic_id );
+               }
+               if ( $term_id ) {
+                       $term = get_term( $term_id );
+               }
+
+               if ( $user_id && $topic && $term ) {
+                       // Moderators.
+                       if ( user_can( $user_id, 'moderate' ) ) {
+                               $retval = true;
+                       }
+
+                       // Compat authors.
+                       if ( $this->authors && in_array( $user_id, $this->authors ) ) {
+                               $retval = true;
+                       }
+               }
+               return $retval;
+       }
+
+       /**
+        * Is a topic sticky for this term?
+        *
+        * @param int $term_id The term id
+        * @param int $topic_id The topic id
+        * @return bool True if sticky, false if not
+        */
+       public static function is_sticky( $term_id, $topic_id ) {
+               $stickies = self::get_stickies( $term_id );
+               return in_array( $topic_id, $stickies );
+       }
+
+       /**
+        * Add a topic id to a term's list of sticky topic ids.
+        *
+        * @param int $term_id The term id
+        * @param int $topic_id The topic id
+        */
+       public static function add_sticky( $term_id, $topic_id ) {
+               $stickies = self::get_stickies( $term_id );
+               if ( ! in_array( $topic_id, $stickies ) ) {
+                       $stickies[] = $topic_id;
+               }
+               return self::set_stickies( $term_id, $stickies );
+       }
+
+       /**
+        * Remove a topic id from a term's list of sticky topic ids.
+        *
+        * @param int $topic_id The topic id
+        * @param int $term_id The term id
+        */
+       public static function remove_sticky( $term_id, $topic_id ) {
+               $stickies = self::get_stickies( $term_id );
+               if ( ( $key = array_search( $topic_id, $stickies ) ) !== false ) {
+                       unset( $stickies[ $key ] );
+               }
+               return self::set_stickies( $term_id, $stickies );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Return an array of topic sticky ids for a given term.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param int $term_id The term id
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -55,4 +256,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">                return $retval;
</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 topic sticky ids for a given term.
+        *
+        * @param int $term_id The term id
+        * @param array $stickies The sticky topic ids
+        */
+       public static function set_stickies( $term_id, $stickies = array() ) {
+               return update_term_meta( $term_id, self::META, implode( ',', $stickies ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>