Skip to content

Instantly share code, notes, and snippets.

@Dragooon
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dragooon/9674227 to your computer and use it in GitHub Desktop.
Save Dragooon/9674227 to your computer and use it in GitHub Desktop.
Proof of Concept for bbPress' per forum moderation
<?php
/**
* Proof of concept for per-forum moderation
*
* Place this under wp-content/plugins/poc, activate it via Plugins section of WP Admin
* Add a row in postmeta table having the post_id of the forum you want to assign the moderator(s) to,
* meta_key as "_bbp_forum_moderators" and a comma separated list of the user's IDs you want
* to make moderators for the said forum
*
* @author Shitiz "Dragooon" Garg <mail@dragooon.net>
*/
/**
* Plugin Name: Dragooon's Proof of Concept
* Description: POC for bbp's per-forum moderation
* Author: Shitiz Garg (Dragooon)
* Author URI: http://bbpress.org
* Version: 0.0.1
* Text Domain: poc
* Domain Path: /languages/
*/
add_filter( 'user_has_cap', 'poc_user_has_cap', 99, 3 );
/**
* Very crude proof of concept, grants the moderator role's capabilities
* to the specified user (see header for instructions)
*
* @todo: Take care of sub-forums
* @todo: Features?
*
* @param array $allcaps Current user's capabilities
* @param array $caps Capabilities being tested against
* @param array $args Additional arguments passed to has_cap
* @return array
*/
function poc_user_has_cap ( $allcaps, $caps, $args ) {
if ( empty( $args[2] ) )
return $allcaps;
$is_forum_check = false;
foreach ( $caps as $cap) {
if ( in_array( $cap, array( 'moderate', 'spectate', 'participate' ))
|| strpos( $cap, 'forum') > -1 || strpos( $cap, 'topic' ) > -1 ) {
$is_forum_check = true;
}
}
if ( !$is_forum_check )
return false;
$post = get_post( $args[2] );
// Make sure this post is either a forum or a topic
if ( $post->post_type != bbp_get_forum_post_type() && $post->post_type != bbp_get_topic_post_type()
&& $post->post_type != bbp_get_reply_post_type() ) {
return $allcaps;
}
// Find the forum's ID
if ( $post->post_type != bbp_get_forum_post_type() ) {
$postmeta = get_post_meta($post->ID);
if ( !empty( $postmeta['_bbp_forum_id'] ) ) {
$forum_id = (int) $postmeta['_bbp_forum_id'][0];
} else {
return $allcaps;
}
} else {
$forum_id = $post->ID;
}
$meta = get_post_meta( $forum_id );
if ( empty( $meta['_bbp_forum_moderators'] ) || !in_array( $args[1], explode(',', $meta['_bbp_forum_moderators'][0]) ) ) {
return $allcaps;
} else {
// This user is a moderator here, grant the additional permissions
$allcaps = array_merge( $allcaps, bbp_get_caps_for_role( bbp_get_moderator_role() ) );
return $allcaps;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment