<!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>[BuddyPress][8942] trunk/src: Improvements to Blogs & capabilities:</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">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/8942">8942</a></dd>
<dt>Author</dt> <dd>johnjamesjacoby</dd>
<dt>Date</dt> <dd>2014-08-18 13:15:40 +0000 (Mon, 18 Aug 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>Improvements to Blogs & capabilities:
* Introduce `bp_blogs_get_allowed_roles()` to act as a blog-role whitelist
* Introduce `bp_get_current_blog_roles()` to avoid including an admin-only file theme-side
* Use these functions to clean-up the `add_user_to_blog` process in several ways:
** More accurately match the member-to-blog relationship specification
** No-subscriber users remains functionality intact
** Allow extending of existing user-to-blog behavior
See <a href="http://buddypress.trac.wordpress.org/ticket/5749">#5749</a>, <a href="http://buddypress.trac.wordpress.org/ticket/5819">#5819</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpblogsbpblogsfunctionsphp">trunk/src/bp-blogs/bp-blogs-functions.php</a></li>
<li><a href="#trunksrcbpblogsbpblogstemplatephp">trunk/src/bp-blogs/bp-blogs-template.php</a></li>
<li><a href="#trunksrcbpcorebpcorecapsphp">trunk/src/bp-core/bp-core-caps.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpblogsbpblogsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/src/bp-blogs/bp-blogs-functions.php (8941 => 8942)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/bp-blogs/bp-blogs-functions.php 2014-08-17 19:30:37 UTC (rev 8941)
+++ trunk/src/bp-blogs/bp-blogs-functions.php 2014-08-18 13:15:40 UTC (rev 8942)
</span><span class="lines">@@ -767,20 +767,19 @@
</span><span class="cx"> * set/changed ('add_user_to_blog', 'profile_update', 'user_register'). It
</span><span class="cx"> * parses the changes, and records them as necessary in the BP blog tracker.
</span><span class="cx"> *
</span><del>- * BuddyPress does not track blogs for Subscribers.
</del><ins>+ * BuddyPress does not track blogs for users with the 'subscriber' role by
+ * default, though as of 2.1.0 you can filter 'bp_blogs_get_allowed_roles' to
+ * modify this behavior.
</ins><span class="cx"> *
</span><del>- * @param int $user_id The ID of the user.
- * @param string|bool $role The WP role being assigned to the user
- * ('subscriber', 'contributor', 'author', 'editor', 'administrator', or
- * a custom role). Defaults to false.
- * @param int $blog_id Default: the current blog ID.
</del><ins>+ * @param int $user_id The ID of the user
+ * @param string|bool $role User's WordPress role for this blog ID
+ * @param int $blog_id Blog ID user is being added to
+ *
</ins><span class="cx"> * @return bool|null False on failure.
</span><span class="cx"> */
</span><span class="cx"> function bp_blogs_add_user_to_blog( $user_id, $role = false, $blog_id = 0 ) {
</span><span class="cx"> global $wpdb;
</span><span class="cx">
</span><del>- require_once( ABSPATH . '/wp-admin/includes/user.php' );
-
</del><span class="cx"> // If no blog ID was passed, use the root blog ID
</span><span class="cx"> if ( empty( $blog_id ) ) {
</span><span class="cx"> $blog_id = isset( $wpdb->blogid ) ? $wpdb->blogid : bp_get_root_blog_id();
</span><span class="lines">@@ -791,29 +790,29 @@
</span><span class="cx">
</span><span class="cx"> // Get user capabilities
</span><span class="cx"> $key = $wpdb->get_blog_prefix( $blog_id ). 'capabilities';
</span><del>- $user_roles = bp_get_user_meta( $user_id, $key, true );
</del><ins>+ $user_roles = array_keys( bp_get_user_meta( $user_id, $key, true ) );
</ins><span class="cx">
</span><span class="cx"> // User has roles so lets
</span><span class="cx"> if ( ! empty( $user_roles ) ) {
</span><span class="cx">
</span><del>- // Look for blog only roles
- $blog_roles = array_intersect(
- array_keys( $user_roles ),
- array_keys( get_editable_roles() )
- );
</del><ins>+ // Get blog roles
+ $blog_roles = array_keys( bp_get_current_blog_roles() );
</ins><span class="cx">
</span><ins>+ // Look for blog only roles of the user
+ $intersect_roles = array_intersect( $user_roles, $blog_roles );
+
</ins><span class="cx"> // If there's a role in the array, use the first one. This isn't
</span><span class="cx"> // very smart, but since roles aren't exactly hierarchical, and
</span><span class="cx"> // WordPress does not yet have a UI for multiple user roles, it's
</span><span class="cx"> // fine for now.
</span><del>- if ( ! empty( $blog_roles ) ) {
- $role = array_shift( $blog_roles );
</del><ins>+ if ( ! empty( $intersect_roles ) ) {
+ $role = array_shift( $intersect_roles );
</ins><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx">
</span><del>- // Bail if no role was found or user is a subscriber
- if ( empty( $role ) || ( $role === 'subscriber' ) ) {
</del><ins>+ // Bail if no role was found or role is not in the allowed roles array
+ if ( empty( $role ) || ! in_array( $role, bp_blogs_get_allowed_roles() ) ) {
</ins><span class="cx"> return false;
</span><span class="cx"> }
</span><span class="cx">
</span><span class="lines">@@ -825,6 +824,21 @@
</span><span class="cx"> add_action( 'user_register', 'bp_blogs_add_user_to_blog' );
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * The allowed blog roles a member must have to be recorded into the
+ * `bp_user_blogs` pointer table.
+ *
+ * This added and was made filterable in BuddyPress 2.1.0 to make it easier
+ * to extend the functionality of the Blogs component.
+ *
+ * @since BuddyPress (2.1.0)
+ *
+ * @return string
+ */
+function bp_blogs_get_allowed_roles() {
+ return apply_filters( 'bp_blogs_get_allowed_roles', array( 'contributor', 'author', 'editor', 'administrator' ) );
+}
+
+/**
</ins><span class="cx"> * Remove a blog-user pair from BP's blog tracker.
</span><span class="cx"> *
</span><span class="cx"> * @param int $user_id ID of the user whose blog is being removed.
</span></span></pre></div>
<a id="trunksrcbpblogsbpblogstemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/src/bp-blogs/bp-blogs-template.php (8941 => 8942)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/bp-blogs/bp-blogs-template.php 2014-08-17 19:30:37 UTC (rev 8941)
+++ trunk/src/bp-blogs/bp-blogs-template.php 2014-08-18 13:15:40 UTC (rev 8942)
</span><span class="lines">@@ -369,7 +369,7 @@
</span><span class="cx"> 'page' => 1,
</span><span class="cx"> 'per_page' => 20,
</span><span class="cx"> 'max' => false,
</span><del>- 'user_id' => bp_displayed_user_id(), // Pass a user_id to limit to only blogs this user has higher than subscriber access to
</del><ins>+ 'user_id' => bp_displayed_user_id(), // Pass a user_id to limit to only blogs this user is a member of
</ins><span class="cx"> 'include_blog_ids' => false,
</span><span class="cx"> 'search_terms' => $search_terms, // Pass search terms to filter on the blog title or description.
</span><span class="cx"> 'update_meta_cache' => true
</span></span></pre></div>
<a id="trunksrcbpcorebpcorecapsphp"></a>
<div class="modfile"><h4>Modified: trunk/src/bp-core/bp-core-caps.php (8941 => 8942)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/bp-core/bp-core-caps.php 2014-08-17 19:30:37 UTC (rev 8941)
+++ trunk/src/bp-core/bp-core-caps.php 2014-08-18 13:15:40 UTC (rev 8942)
</span><span class="lines">@@ -11,6 +11,33 @@
</span><span class="cx"> if ( !defined( 'ABSPATH' ) ) exit;
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Return an array of roles from the currently loaded blog
+ *
+ * WordPress roles are dynamically flipped when calls to switch_to_blog() and
+ * restore_current_blog() are made, so we use and trust WordPress core to have
+ * loaded the correct results for us here. As enhancements are made to
+ * WordPresss's RBAC, so should our capability functions here.
+ *
+ * @since BuddyPress (2.1.0)
+ *
+ * @return array
+ */
+function bp_get_current_blog_roles() {
+ global $wp_roles;
+
+ // Sanity check on roles global variable
+ $roles = isset( $wp_roles->roles )
+ ? $wp_roles->roles
+ : array();
+
+ // Apply WordPress core filter to editable roles
+ $roles = apply_filters( 'editable_roles', $roles );
+
+ // Return the editable roles
+ return apply_filters( 'bp_get_current_blog_roles', $roles, $wp_roles );
+}
+
+/**
</ins><span class="cx"> * Add capabilities to WordPress user roles.
</span><span class="cx"> *
</span><span class="cx"> * This is called on plugin activation.
</span></span></pre>
</div>
</div>
</body>
</html>