/*
Plugin Name: Forums Moderators
Plugin URI: http://www.adityanaik.com/projects/plugins/bb-forum-moderators/
Description: Give forum specific moderator priviledges
Author: Aditya Naik
Version: 1.0
Author URI: http://www.adityanaik.com/
Install Instructions:
- If you don't have a /my-plugins/ directory in your bbpress installaltion, create it on the same level as config.php.
- Menu can be access from Admin > User > Forum Moderators
Version History:
1.0 : Initial Release
Please note that the plugin will disable all priviledges for moderators and will need to be given
specific priviledges to each forum from the admin menu.
*/
function forum_moderators_add_admin_page() {
global $bb_submenu;
$bb_submenu['users.php'][] = array(__('Forum Moderators'), 'use_keys', 'forum_moderators_admin_page');
}
function forum_moderators_admin_page() {
$bb_user_search = new BB_Forum_Moderators_Search($_REQUEST['usersearch'], $_REQUEST['userspage']);
$bb_user_search->display();
}
function forum_moderators_process_post() {
if ($_POST['mod_user_id']) {
foreach($_POST['mod_user_id'] as $mod) {
$user_obj = new BB_User( $mod);
$user_obj->set_role('moderator');
bb_update_usermeta( $mod, 'forum_moderator', $_POST['mod_forums'][$mod] );
//print_r($_POST['mod_forums'][$mod]);
}
}
}
add_action( 'bb_admin-header.php','forum_moderators_process_post');
add_action( 'bb_admin_menu_generator', 'forum_moderators_add_admin_page' );
add_filter('bb_user_has_cap','forum_moderators_process_capacities',10,2);
function forum_moderators_process_capacities($allcaps, $caps){
global $bb_current_user, $forum,$topic_id;
if (!$bb_current_user || in_array('keymaster',$bb_current_user->roles) || in_array('administrator',$bb_current_user->roles))
return $allcaps;
if (empty($forum)) {
$topic = get_topic($topic_id);
$forum = get_forum($topic->forum_id);
}
$forum_moderator = ((!empty($bb_current_user->data->forum_moderator)) ? $bb_current_user->data->forum_moderator : array());
$filtering_caps = array(
'manage_topics' ,
'edit_closed' ,
'edit_deleted' ,
'browse_deleted' ,
'edit_others_tags' ,
'edit_others_topics' ,
'manage_posts' ,
'ignore_edit_lock' ,
'edit_others_posts'
);
if (!empty($forum) && !array_key_exists($forum->forum_id,$forum_moderator ) && array_intersect($filtering_caps,$caps)) {
foreach($filtering_caps as $filtering_cap) {
unset($allcaps[$filtering_cap]);
}
}
return $allcaps;
}
class BB_Forum_Moderators_Search {
var $results;
var $search_term;
var $page;
var $raw_page;
var $users_per_page = 50;
var $first_user;
var $last_user;
var $query_limit;
var $query_from_where;
var $total_users_for_query = 0;
var $search_errors;
function BB_Forum_Moderators_Search ($search_term = '', $page = '') { // constructor
$this->search_term = $search_term;
$this->raw_page = ( '' == $page ) ? false : (int) $page;
$this->page = (int) ( '' == $page ) ? 1 : $page;
$this->prepare_query();
$this->query();
$this->prepare_vars_for_template_usage();
$this->do_paging();
}
function prepare_query() {
global $bbdb;
$this->first_user = ($this->page - 1) * $this->users_per_page;
$this->query_limit = 'LIMIT ' . $this->first_user . ',' . $this->users_per_page;
if ( $this->search_term ) {
$searches = array();
$search_sql = 'AND (';
foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col )
$searches[] = $col . " LIKE '%$this->search_term%'";
$search_sql .= implode(' OR ', $searches);
$search_sql .= ')';
}
$this->query_from_where = "FROM $bbdb->users WHERE 1=1 $search_sql";
}
function query() {
global $bbdb;
$this->results = $bbdb->get_col('SELECT ID ' . $this->query_from_where . $this->query_limit);
$keymasters = get_ids_by_role( 'keymaster');
$administrators = get_ids_by_role( 'administrator');
$this->results = array_diff($this->results, $keymasters , $administrators );
if ( $this->results )
$this->total_users_for_query = $bbdb->get_var('SELECT COUNT(ID) ' . $this->query_from_where); // no limit
else
$this->search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!'));
}
function prepare_vars_for_template_usage() {
$this->search_term = stripslashes($this->search_term); // done with DB, from now on we want slashes gone
}
function do_paging() {
global $bb_current_submenu;
if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results
$pagenow = bb_get_admin_tab_link($bb_current_submenu);
$this->paging_text = paginate_links( array(
'total' => ceil($this->total_users_for_query / $this->users_per_page),
'current' => $this->page,
'prev_text' => '« Previous Page',
'next_text' => 'Next Page »',
'base' => $pagenow . ( false === strpos($pagenow, '?') ? '?%_%' : '&%_%' ),
'format' => 'userspage=%#%',
'add_args' => array( 'usersearch' => urlencode($this->search_term) )
) );
}
}
function get_results() {
return (array) $this->results;
}
function page_links() {
echo $this->paging_text;
}
function results_are_paged() {
if ( $this->paging_text )
return true;
return false;
}
function is_search() {
if ( $this->search_term )
return true;
return false;
}
function display( $show_search = true, $show_email = false ) {
global $bb_roles;
$r = '';
// Make the user objects
foreach ( $this->get_results() as $user_id ) {
$tmp_user = new BB_User($user_id);
$roles = $tmp_user->roles;
$role = array_shift($roles);
$roleclasses[$role][$tmp_user->data->user_login] = $tmp_user;
}
if ( isset($this->title) )
$title = $this->title;
elseif ( $this->is_search() )
$title = sprintf(__('Users Matching "%s" by Role'), wp_specialchars( $this->search_term ));
else
$title = __('User List by Role');
$r .= "
$title
\n";
if ( $show_search ) {
$r .= "\n\n";
}
if ( is_wp_error( $this->search_errors ) ) {
$r .= "\n";
$r .= "\t
\n";
foreach ( $this->search_errors->get_error_messages() as $message )
$r .= "\t\t- $message
\n";
$r .= "\t
\n
\n\n";
}
$forums = get_forums();
if ( $this->get_results() ) {
$colspan = 3;
$r .= '' . sprintf(__('%1$s – %2$s of %3$s shown below'), $this->first_user + 1, min($this->first_user + $this->users_per_page, $this->total_users_for_query), $this->total_users_for_query) . "
\n";
if ( $this->results_are_paged() )
$r .= "\n" . $this->paging_text . "
\n\n";
$r .= "\n\n";
if ( $this->results_are_paged() )
$r .= "\n" . $this->paging_text . "
\n\n";
}
echo $r;
}
}
function bb_forum_moderator_user_row( $user_id) {
$user = bb_get_user( $user_id );
$r = "\t\n";
$r .= "\t\t| $user_id | \n";
$r .= "\t\t" . get_user_name( $user_id ) . " | \n";
if ( $email )
$r .= "\t\t$user->user_email | \n";
$r .= "\t\t$user->user_registered | \n";
$r .= "\t\tEdit | \n\t
";
return $r;
}
?>
Statistics « « FOCUS Forums
| |
Registered Users
7,421
Posts
709
|
[ Time : 0.462s | 27 Queries ]