<!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>[3978] sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-user-badges: Support: User Badges initial commit.</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/3978">3978</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/3978","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>coffee2code</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-09-06 20:27:56 +0000 (Tue, 06 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: User Badges initial commit.

Adds badges to user replies. Currently assigns one of three potential badges:

* Plugin Author : replies made in the support/review forum for a plugin the reply author has commit access to
* Plugin Contributor : replies made in the support/review forum for a plugin the reply author is listed as a contributor
* Theme Author : replies made in the support/review forum for a theme the reply author has commit access to

See <a href="http://meta.trac.wordpress.org/ticket/1544">#1544</a>.
Fixes <a href="http://meta.trac.wordpress.org/ticket/1993">#1993</a>.</pre>

<h3>Added Paths</h3>
<ul>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-user-badges/inc/</li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporgbbpuserbadgesincclasspluginphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-user-badges/inc/class-plugin.php</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporgbbpuserbadgeswporgbbpuserbadgesphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-user-badges/wporg-bbp-user-badges.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporgbbpuserbadgesincclasspluginphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-user-badges/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-user-badges/inc/class-plugin.php                               (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-user-badges/inc/class-plugin.php 2016-09-06 20:27:56 UTC (rev 3978)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,253 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+namespace WordPressdotorg\Forums\User_Badges;
+
+defined( 'ABSPATH' ) or die();
+
+class Plugin {
+
+       /**
+        * @access private
+        * @var string The prefix for the plugin directory tables.
+        */
+       private static $plugins_table_prefix;
+
+       /**
+        * @access private
+        * @var string The prefix for the theme directory tables.
+        */
+       private static $themes_table_prefix;
+
+       /**
+        * @access private
+        * @var Plugin The singleton instance.
+        */
+       private static $instance;
+
+       /**
+        * Returns always the same instance of this plugin.
+        *
+        * @return Plugin
+        */
+       public static function get_instance() {
+               if ( ! ( self::$instance instanceof self ) ) {
+                       self::$instance = new self();
+               }
+               return self::$instance;
+       }
+
+       /**
+        * Instantiates a new Plugin object.
+        */
+       private function __construct() {
+               self::$plugins_table_prefix = 'wporg_' . WPORG_PLUGIN_DIRECTORY_BLOGID . '_';
+               self::$themes_table_prefix  = 'wporg_' . WPORG_THEME_DIRECTORY_BLOGID . '_';
+
+               add_action( 'bbp_loaded', array( $this, 'bbp_loaded' ) );
+       }
+
+       /**
+        * Initializes the plugin.
+        */
+       public function bbp_loaded() {
+               // Add class to div containing reply.
+               add_filter( 'bbp_get_reply_class', array( $this, 'bbp_get_reply_class' ), 10, 2 );
+
+               // Add badge before reply author info.
+               add_action( 'bbp_theme_before_reply_author_details', array( $this, 'wporg_support_add_author_badges' ) );
+       }
+
+       protected function get_author_badge_info() {
+               $badgeable_forums = array(
+                       \WordPressdotorg\Forums\Plugin::PLUGINS_FORUM_ID,
+                       \WordPressdotorg\Forums\Plugin::REVIEWS_FORUM_ID,
+                       \WordPressdotorg\Forums\Plugin::THEMES_FORUM_ID,
+               );
+
+               if ( ! in_array( bbp_get_forum_id(), $badgeable_forums ) ) {
+                       return;
+               }
+
+               $slug = $type = null;
+
+               $user_id = bbp_get_reply_author_id();
+               if ( ! $user_id ) {
+                       return;
+               }
+
+               $user_login = get_user_by( 'id', $user_id )->user_login;
+               $reply_id   = bbp_get_reply_id();
+               $topic_id   = bbp_get_reply_topic_id();
+
+               $support_forum = \WordPressdotorg\Forums\Plugin::get_instance();
+
+               // Check if the thread is associated with a plugin.
+               if ( $slug = $support_forum->plugins->slug ) {
+                       $type = 'plugin';
+               }
+               // Else check if the thread is associated with a theme.
+               elseif ( $slug = $support_forum->themes->slug ) {
+                       $type = 'theme';
+               }
+               // Else not a type of concern.
+               else {
+                       return;
+               }
+
+               return array(
+                       'type'       => $type,
+                       'slug'       => $slug,
+                       'user_login' => $user_login
+               );
+       }
+
+       public function bbp_get_reply_class( $classes, $reply_id ) {
+               // Class related to plugin and theme authors/contributors.
+               if ( $info = $this->get_author_badge_info() ) {
+                       if ( $this->is_user_author( $info['user_login'], $info['type'], $info['slug'] ) ) {
+                               $contrib_type = 'author';
+                       } elseif ( $this->is_user_contributor( $info['user_login'], $info['type'], $info['slug'] ) ) {
+                               $contrib_type = 'contributor';
+                       } else {
+                               $contrib_type = '';
+                       }
+
+                       if ( $contrib_type ) {
+                               $classes[] = 'by-' . $info['type'] . '-' . $contrib_type;
+                       }
+               }
+
+               return $classes;
+       }
+
+       /**
+        * Display author badge if reply author is in support or reviews forum for
+        * the plugin/theme they contribute to.
+        */
+       public function wporg_support_add_author_badges() {
+               if ( ! $info = $this->get_author_badge_info() ) {
+                       return;
+               }
+
+               $label = $help = null;
+
+               // Determine strings to use based on user being an author or contributor.
+               if ( $this->is_user_author( $info['user_login'], $info['type'], $info['slug'] ) ) {
+                       if ( 'plugin' == $info['type'] ) {
+                               $label = __( 'Plugin Author', 'wporg-forums' );
+                               $help  = __( 'This person is the author of this plugin', 'wporg-forums' );
+                       } else {
+                               $label = __( 'Theme Author', 'wporg-forums' );
+                               $help  = __( 'This person is the author of this theme', 'wporg-forums' );
+                       }
+               }
+               elseif ( $this->is_user_contributor( $info['user_login'], $info['type'], $info['slug'] ) ) {
+                       if ( 'plugin' == $info['type'] ) {
+                               $label = __( 'Plugin Contributor', 'wporg-forums' );
+                               $help  = __( 'This person is a contributor to this plugin', 'wporg-forums' );
+                       } else {
+                               $label = __( 'Theme Contributor', 'wporg-forums' );
+                               $help  = __( 'This person is a contributor to this theme', 'wporg-forums' );
+                       }
+               }
+
+               // Build the markup.
+               $output = '';
+               if ( $label ) {
+                       $output .= sprintf(
+                               '<span class="author-badge %s-author-badge" title="%s">%s</span>',
+                               esc_attr( $info['type'] ),
+                               esc_attr( $help ),
+                               $label
+                       );
+               }
+
+               // Output the markup.
+               echo $output;
+       }
+
+       /**
+        * Checks if the specified user is an author to the specified plugin/theme.
+        *
+        * An author is defined as someone who has commit access to a plugin, or is
+        * the designated author for a theme.
+        *
+        * @param string $user_login User login.
+        * @param string $type       Either 'plugin' or 'theme'.
+        * @param string $slug       Slug for the plugin or theme.
+        * @return bool              True if user is an author, false otherwise.
+        */
+       public function is_user_author( $user_login, $type, $slug ) {
+               global $wpdb;
+
+               $authors = wp_cache_get( $slug, $type . '_authors' );
+
+               if ( false === $authors ) {
+                       if ( 'plugin' === $type ) {
+                               // Get users who have commit access.
+                               $authors = $wpdb->get_col( $wpdb->prepare(
+                                       "SELECT user FROM plugin_2_svn_access WHERE `path` = %s",
+                                       '/' . $slug
+                               ) );
+                       }
+                       else {
+                               // TODO: Change this if themes support having more than one author.
+                               $author_id = $wpdb->get_var( $wpdb->prepare(
+                                       "SELECT post_author FROM " . self::$themes_table_prefix . "posts WHERE post_name = %s LIMIT 1",
+                                       $slug
+                               ) );
+                               if ( $author_id ) {
+                                       $author = get_user_by( 'id', $author_id );
+                                       $authors = array( $author->user_login );
+                               }
+                       }
+
+                       wp_cache_add( $slug, $authors, $type . '_authors', HOUR_IN_SECONDS );
+               }
+
+               return in_array( $user_login, $authors );
+       }
+
+       /**
+        * Checks if the specified user is a contributor to the specified plugin/theme.
+        *
+        * A plugin contributor is someone listed as a contributor in the plugin's readme.txt.
+        * Currently, themes do not support having contirbutors.
+        *
+        * @param string $user_login User login.
+        * @param string $type       Either 'plugin' or 'theme'.
+        * @param string $slug       Slug for the plugin or theme.
+        * @return bool              True if user is a contributor, false otherwise.
+        */
+       public function is_user_contributor( $user_login, $type, $slug ) {
+               global $wpdb;
+
+               $contributors = wp_cache_get( $slug, $type . '_contributors' );
+
+               if ( false === $contributors ) {
+                       if ( 'plugin' === $type ) {
+                               // TODO: Change this when the Plugin Directory switches over to WordPress.
+                               $contributors = $wpdb->get_var( $wpdb->prepare(
+                                       'SELECT meta_value FROM plugin_2_meta m LEFT JOIN plugin_2_topics t ON m.object_id = t.topic_id WHERE t.topic_slug = %s AND m.object_type = %s AND m.meta_key = %s',
+                                       $slug,
+                                       'bb_topic',
+                                       'contributors'
+                               ) );
+
+                               if ( $contributors ) {
+                                       $contributors = unserialize( $contributors );
+                               }
+                       }
+                       else {
+                               // Themes have no additional contributors at the moment.
+                               // TODO: Change this if themes support specifying contributors.
+                               $contributors = array();
+                       }
+
+                       wp_cache_add( $slug, $contributors, $type . '_contributors', HOUR_IN_SECONDS );
+               }
+
+               return in_array( $user_login, $contributors );
+       }
+
+}
</ins></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporgbbpuserbadgeswporgbbpuserbadgesphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-user-badges/wporg-bbp-user-badges.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-user-badges/wporg-bbp-user-badges.php                          (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-bbp-user-badges/wporg-bbp-user-badges.php    2016-09-06 20:27:56 UTC (rev 3978)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,23 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Plugin Name: bbPress: Show User Badges
+ * Description: Display a badge for plugin and theme authors and contributors for replies in their own support forums.
+ * Version:     1.0
+ * Author:      WordPress.org
+ * Author URI:  https://wordpress.org/
+ * License:     GPLv2 or later
+ * Text Domain: wporg-forums
+ */
+
+namespace WordPressdotorg\Forums\User_Badges;
+
+defined( 'ABSPATH' ) or die();
+
+// Store the root plugin file for usage with functions which use the plugin basename
+define( __NAMESPACE__ . '\PLUGIN_FILE', __FILE__ );
+
+// Includes
+include( dirname( __FILE__ ) . '/inc/class-plugin.php' );
+
+// Instantiate the Plugin
+Plugin::get_instance();
</ins></span></pre>
</div>
</div>

</body>
</html>