<!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>[7958] sites/trunk/wordpress.org/public_html/wp-content/plugins/support-helphub: Support: Add role management for helphub users.</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/7958">7958</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/7958","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>Otto42</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-12-09 16:56:49 +0000 (Sun, 09 Dec 2018)</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: Add role management for helphub users. Fixes <a href="http://meta.trac.wordpress.org/ticket/3918">#3918</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginssupporthelphubsupporthelphubphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/support-helphub/support-helphub.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li>sites/trunk/wordpress.org/public_html/wp-content/plugins/support-helphub/inc/helphub-manager/</li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginssupporthelphubinchelphubmanagerclasshelphubmanagerphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/support-helphub/inc/helphub-manager/class-helphub-manager.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginssupporthelphubinchelphubmanagerclasshelphubmanagerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/support-helphub/inc/helphub-manager/class-helphub-manager.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-helphub/inc/helphub-manager/class-helphub-manager.php                            (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/support-helphub/inc/helphub-manager/class-helphub-manager.php      2018-12-09 16:56:49 UTC (rev 7958)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,322 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * This adds custom roles for the HelpHub project.
+ *
+ * @package HelpHub
+ */
+
+if ( ! defined( 'ABSPATH' ) ) {
+       exit;
+}
+
+class HelpHub_Manager {
+
+       /**
+        * The single instance of HelpHub_Custom_Roles.
+        *
+        * @var     object
+        * @access  private
+        * @since   1.0.0
+        */
+       private static $_instance = null;
+
+       /**
+        * Settings class object
+        *
+        * @var     object
+        * @access  public
+        * @since   1.0.0
+        */
+       public $settings = null;
+
+       /**
+        * The version number.
+        *
+        * @var     string
+        * @access  public
+        * @since   1.0.0
+        */
+       public $_version;
+
+       /**
+        * The token.
+        *
+        * @var     string
+        * @access  public
+        * @since   1.0.0
+        */
+       public $_token;
+
+       /**
+        * The main plugin file.
+        *
+        * @var     string
+        * @access  public
+        * @since   1.0.0
+        */
+       public $file;
+
+       /**
+        * The main plugin directory.
+        *
+        * @var     string
+        * @access  public
+        * @since   1.0.0
+        */
+       public $dir;
+
+       /**
+        * The plugin assets directory.
+        *
+        * @var     string
+        * @access  public
+        * @since   1.0.0
+        */
+       public $assets_dir;
+
+       /**
+        * The plugin assets URL.
+        *
+        * @var     string
+        * @access  public
+        * @since   1.0.0
+        */
+       public $assets_url;
+
+       /**
+        * Suffix for Javascripts.
+        *
+        * @var     string
+        * @access  public
+        * @since   1.0.0
+        */
+       public $script_suffix;
+
+       /**
+        * Custom roles Constructor.
+        *
+        * @param string $file    filename.
+        * @param string $version version.
+        */
+       public function __construct( $file = '', $version = '1.0.0' ) {
+               $this->_version = $version;
+               $this->_token   = 'helphub_manager';
+
+               $this->file = $file;
+               $this->dir  = dirname( $this->file );
+
+               $this->add_helphub_customrole();
+
+               add_action( 'bbp_template_after_user_profile', array( $this, 'helphub_profile_section' ) );
+
+               add_action( 'bbp_post_request', array( $this, 'helphub_profile_edits' ) );
+       } // End __construct ()
+
+       /**
+        * Main HelpHub_Manager Instance
+        *
+        * Ensures only one instance of HelpHub_Manager is loaded or can be loaded.
+        *
+        * @param string $file    Filename of site.
+        * @param string $version Version number.
+        * @since 1.0.0
+        * @static
+        * @see HelpHub_Custom_Roles()
+        * @return Main HelpHub_Manager instance
+        */
+       public static function instance( $file = '', $version = '1.0.0' ) {
+               if ( is_null( self::$_instance ) ) {
+                       self::$_instance = new self( $file, $version );
+               }
+               return self::$_instance;
+       } // End instance ()
+
+       /**
+        * Cloning is forbidden.
+        *
+        * @since 1.0.0
+        */
+       public function __clone() {
+               _doing_it_wrong( __FUNCTION__, esc_html( __( 'Sorry, this is not allowed.', 'wporg-forums' ) ), esc_html( $this->_version ) );
+       } // End __clone ()
+
+       /**
+        * Unserializing instances of this class is forbidden.
+        *
+        * @since 1.0.0
+        */
+       public function __wakeup() {
+               _doing_it_wrong( __FUNCTION__, esc_html( __( 'Sorry, this is not allowed.', 'wporg-forums' ) ), esc_html( $this->_version ) );
+       } // End __wakeup ()
+
+       public function get_helphub_roles() {
+               return array(
+                       'helphub_editor'  => esc_html__( 'HelpHub Editor', 'wporg-forums' ),
+                       'helphub_manager' => esc_html__( 'HelpHub Manager', 'wporg-forums' ),
+               );
+       }
+
+       /**
+        * Output markup for various HelpHub managements in the user profile section of bbPress.
+        */
+       public function helphub_profile_section() {
+               /*
+                * Don't process anything if the user in question is lacking the proper capabilities.
+                *
+                * For our use, that means HelpHub Managers, anyone with higher access can use the appropriate edit screens.
+                */
+               if ( ! current_user_can( 'manage_helphub' ) ) {
+                       return;
+               }
+
+               // Also don't allow editing your own user.
+               if ( bbp_get_displayed_user_id() === get_current_user_id() ) {
+                       return;
+               }
+
+               $helphub_roles = $this->get_helphub_roles();
+
+               // Get users current blog role.
+               $user_role = bbp_get_user_blog_role( bbp_get_displayed_user_id() );
+
+               /*
+                * Only allow changing roles of users that are HelpHub related, or do not already hold
+                * a role within the user hierarchy as is.
+                *
+                * This is to prevent overriding users with higher capabilities altogether.
+                */
+               if ( ! empty( $user_role ) && ! isset( $helphub_roles[ $user_role ] ) ) {
+                       return;
+               }
+
+               ?>
+
+               <div class="wporg-support-helphub">
+                       <h2 id="helphub" class="entry-title"><?php esc_html_e( 'HelpHub', 'wporg-forums' ); ?></h2>
+                       <div class="bbp-user-section">
+                               <form action="" method="post">
+                                       <fieldset class="bbp-form">
+                                               <label for="role"><?php esc_html_e( 'HelpHub Role', 'wporg-forums' ); ?></label>
+                                               <select name="role" id="role">
+                                                       <option value=""><?php esc_html_e( '&mdash; No role for this site &mdash;', 'wporg-forums' ); ?></option>
+
+                                                       <?php foreach ( $helphub_roles as $role => $label ) : ?>
+
+                                                               <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo $label; ?></option>
+
+                                                       <?php endforeach; ?>
+                                               </select>
+                                       </fieldset>
+
+                                       <fieldset style="padding: 20px 20px 0" class="submit">
+                                               <legend style="display:none;"><?php esc_html_e( 'Save Changes', 'wporg-forums' ); ?></legend>
+                                               <div style="margin-bottom: 20px;float: left;width: 100%;clear: left;">
+                                                       <input type="hidden" name="action" id="helphub_post_action" value="helphub-update-user">
+                                                       <input type="hidden" name="user_id" id="user_id" value="<?php echo esc_attr( bbp_get_displayed_user_id() ); ?>">
+
+                                                       <?php wp_nonce_field( 'helphub-change-user-role-' . bbp_get_displayed_user_id(), '_helphub_manage' ); ?>
+
+                                                       <button style="float:right;" type="submit" class="button submit user-submit"><?php esc_html_e( 'Update User', 'wporg-forums' ); ?></button>
+                                               </div>
+                                       </fieldset>
+                               </form>
+                       </div>
+               </div>
+
+               <?php
+       }
+
+       /**
+        * Capture and perform any profile edits initiated by a HelpHub Manager.
+        */
+       public function helphub_profile_edits() {
+               // Don't process anything if the post actions are invalid.
+               if ( ! isset( $_POST['action'] ) || 'helphub-update-user' !== $_POST['action'] ) {
+                       return;
+               }
+
+               // Get the displayed user ID.
+               $user_id = bbp_get_displayed_user_id();
+
+               // Ensure the proper user capabilities exist for changing user details.
+               if ( ! current_user_can( 'manage_helphub' ) ) {
+                       return;
+               }
+
+               // Double-check that nobody is trying to edit their own user.
+               if ( get_current_user_id() === $user_id ) {
+                       return;
+               }
+
+               // Check that the nonce is valid.
+               if ( ! wp_verify_nonce( $_POST['_helphub_manage'], 'helphub-change-user-role-' . $user_id ) ) {
+                       return;
+               }
+
+               // Make sure the new role is a HelpHub one, or is being reset to nothing.
+               $roles = $this->get_helphub_roles();
+               if ( ! empty( $_POST['role'] ) && ! isset( $roles[ $_POST['role'] ] ) ) {
+                       return;
+               }
+
+               $user_forum_role = bbp_get_user_role( $user_id );
+
+               $user = new stdClass();
+
+               $user->ID   = (int) $user_id;
+               $user->role = $_POST['role'];
+
+               $edit_user = wp_update_user( $user );
+
+               // Updating a user resets the forum role, so let's explicitly update that.
+               bbp_set_user_role( $user_id, $user_forum_role );
+
+               // Error(s) editng the user, so copy them into the global.
+               if ( is_wp_error( $edit_user ) ) {
+                       bbpress()->errors = $edit_user;
+
+                       // Successful edit to redirect.
+               } elseif ( is_integer( $edit_user ) ) {
+                       $redirect = add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_url( $edit_user ) );
+
+                       wp_safe_redirect( $redirect );
+                       exit;
+               }
+       }
+
+       /**
+        * Adds a HelpHub custom role.
+        */
+       public function add_helphub_customrole() {
+
+               // Load users library.
+               if ( ! function_exists( 'get_editable_roles' ) ) {
+                       require_once ABSPATH . 'wp-admin/includes/user.php';
+               }
+               get_editable_roles();
+               $role = 'helphub_manager';
+
+               // Check if the HelpHub Manager role is already added.
+               global $wp_roles;
+               $default_editorroles = $wp_roles->get_role( 'editor' );
+               if ( empty( $GLOBALS['wp_roles']->is_role( $role ) ) ) {
+                       $wp_roles->add_role( $role, __( 'HelpHub Manager', 'wporg-forums' ), $default_editorroles->capabilities );
+
+                       $wp_roles->add_cap( $role, 'edit_theme_options' );
+                       $wp_roles->add_cap( $role, 'manage_helphub' );
+               }
+       }
+}
+
+/**
+ * Returns the main instance of HelpHub_Manager to prevent the need to use globals.
+ *
+ * @since  1.0.0
+ * @return object HelpHub_Custom_Roles
+ */
+function helphub_manager() {
+       $instance = HelpHub_Manager::instance( __FILE__, '1.0.0' );
+       return $instance;
+}
+
+helphub_manager();
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: sites/trunk/wordpress.org/public_html/wp-content/plugins/support-helphub/inc/helphub-manager/class-helphub-manager.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginssupporthelphubsupporthelphubphp"></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-helphub/support-helphub.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-helphub/support-helphub.php      2018-12-08 11:42:41 UTC (rev 7957)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/support-helphub/support-helphub.php        2018-12-09 16:56:49 UTC (rev 7958)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -22,3 +22,5 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require_once( dirname( __FILE__ ) . '/inc/table-of-contents-lite/table-of-contents-lite.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require_once( dirname( __FILE__ ) . '/inc/helphub-front-page-blocks/helphub-front-page-blocks.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require_once( dirname( __FILE__ ) . '/inc/helphub-customroles/class-helphub-custom-roles.php' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require_once( dirname( __FILE__ ) . '/inc/helphub-manager/class-helphub-manager.php' );
+
</ins></span></pre>
</div>
</div>

</body>
</html>