Skip to:
Content

bbPress.org

Ticket #459: 459.8.diff

File 459.8.diff, 24.7 KB (added by tharsheblows, 10 years ago)

needs refactoring to not be so redundant

  • src/bbpress.php

     
    690690                        )
    691691                ) );
    692692
    693                 // Register the forum-mod taxonomy.
    694                 register_taxonomy(
    695                         bbp_get_forum_mod_tax_id(),
    696                         bbp_get_forum_post_type(),
    697                         apply_filters( 'bbp_register_forum_moderator_taxonomy', array(
    698                                 'labels'                => bbp_get_forum_mod_tax_labels(),
    699                                 'capabilities'          => bbp_get_forum_mod_caps(),
    700                                 'update_count_callback' => '_update_post_term_count',
    701                                 'query_var'             => false,
    702                                 'show_tagcloud'         => true,
    703                                 'hierarchical'          => false,
    704                                 'show_in_menu'          => true,
    705                                 'show_in_nav_menus'     => false,
    706                                 'public'                => false,
    707                                 'show_ui'               => bbp_allow_forum_mods() && current_user_can( 'bbp_forum_mods_admin' ),
    708                         )
    709                 ) );
    710693        }
    711694
    712695        /**
  • src/includes/admin/admin.php

     
    560560                // Get the version to use for JS
    561561                $version = bbp_get_version();
    562562
     563                // Enqueue the common JS
     564                wp_enqueue_script( 'bbp-admin-common-js', $this->js_url . 'common' . $suffix . '.js', array( 'jquery', 'suggest' ), $version );
     565
    563566                // Post type checker (only topics and replies)
    564567                if ( 'post' === get_current_screen()->base ) {
    565568                        switch( get_current_screen()->post_type ) {
     
    566569                                case bbp_get_reply_post_type() :
    567570                                case bbp_get_topic_post_type() :
    568571
    569                                         // Enqueue the common JS
    570                                         wp_enqueue_script( 'bbp-admin-common-js', $this->js_url . 'common' . $suffix . '.js', array( 'jquery', 'suggest' ), $version );
    571 
    572572                                        // Topics admin
    573573                                        if ( bbp_get_topic_post_type() === get_current_screen()->post_type ) {
    574574                                                wp_enqueue_script( 'bbp-admin-topics-js', $this->js_url . 'topics' . $suffix . '.js', array( 'jquery' ), $version );
     
    581581                                        break;
    582582                        }
    583583                }
     584
    584585        }
    585586
    586587        /**
  • src/includes/admin/forums.php

     
    7373                // Metabox actions
    7474                add_action( 'add_meta_boxes', array( $this, 'attributes_metabox'      ) );
    7575                add_action( 'save_post',      array( $this, 'attributes_metabox_save' ) );
     76                add_action( 'add_meta_boxes', array( $this, 'moderator_metabox'           ) );
     77                add_action( 'save_post',          array( $this, 'moderator_metabox_save'  ) );
    7678
    7779                // Check if there are any bbp_toggle_forum_* requests on admin_init, also have a message displayed
    7880                add_action( 'load-edit.php',  array( $this, 'toggle_forum'        ) );
     
    8082
    8183                // Forum moderators AJAX; run at -1 to preempt built-in tag search
    8284                add_action( 'wp_ajax_ajax-tag-search', array( $this, 'ajax_tag_search'         ), -1 );
     85                add_action( 'wp_ajax_bbp_ajax_user_search', array( $this, 'bbp_ajax_user_search' ) );
    8386
    8487                // Contextual Help
    8588                add_action( 'load-edit.php',     array( $this, 'edit_help' ) );
     
    264267                do_action( 'bbp_forum_attributes_metabox' );
    265268        }
    266269
     270        public function moderator_metabox() {
     271                if ( $this->bail() ) {
     272                        return;
     273                }
     274                add_meta_box (
     275                        'bbp_forum_moderators',
     276                        __( 'Forum Moderators', 'bbpress' ),
     277                        'bbp_forum_moderators_metabox',
     278                        $this->post_type,
     279                        'normal',
     280                        'default'
     281                );
     282        }
     283
    267284        /**
    268285         * Return user nicename suggestions instead of tag suggestions
    269286         *
     
    337354                wp_die();
    338355        }
    339356
     357        public function bbp_ajax_user_search(){
     358
     359                $user_query = '*' . esc_attr( $_POST['user_query'] ) . '*';
     360                $user_role = ( esc_attr( $_POST['role'] ) === 'mods' ) ? 'bbp_moderator' : '' ;
     361                $ignore_users = explode( ',',  $_POST['current_mods'] );
     362                $item_query = new WP_User_Query( array ( 'search' =>  $user_query, 'order' => 'ASC', 'orderby' => 'display_name', 'number' => 50, 'role' => $user_role, 'exclude' => $ignore_users ) );
     363                $items = $item_query->get_results();
     364                $users = array();
     365
     366                ob_start();
     367                if( !empty( $items ) ){
     368                        foreach( $items as $item ){
     369                                $user = $item->data;
     370                                printf( '<input type="checkbox" name="choose_forum_moderators[]" value="%1$d" /> %2$s (<a href="/wp-admin/user-edit.php?user_id=%1$d">user profile page</a>)<br />', $user->ID, $user->display_name );
     371                        }
     372                }
     373                else{
     374                        echo 'No results from search.';
     375                }
     376                $user_list = ob_get_contents();
     377                ob_end_clean();
     378
     379
     380
     381                $data = array('users' => $user_list, 'result' => 'success');
     382                echo json_encode($data);
     383                die;
     384        }
     385
    340386        /**
    341387         * Pass the forum attributes for processing
    342388         *
     
    404450                return $forum_id;
    405451        }
    406452
     453        // ha ha no documentation I am just playing wheeeeeee
     454        public function moderator_metabox_save( $forum_id ) {
     455
     456                if ( $this->bail() ) {
     457                        return $forum_id;
     458                }
     459
     460                // Bail if doing an autosave
     461                if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
     462                        return $forum_id;
     463                }
     464
     465                // Bail if not a post request
     466                if ( ! bbp_is_post_request() ) {
     467                        return $forum_id;
     468                }
     469
     470                // Nonce check - nope not now
     471                // if ( empty( $_POST['bbp_forum_metabox'] ) || ! wp_verify_nonce( $_POST['bbp_forum_metabox'], 'bbp_forum_metabox_save' ) ) {
     472                //      return $forum_id;
     473                // }
     474
     475                // Only save for forum post-types
     476                if ( ! bbp_is_forum( $forum_id ) ) {
     477                        return $forum_id;
     478                }
     479
     480                // Bail if current user cannot edit this forum
     481                if ( !current_user_can( 'edit_forum', $forum_id ) ) {
     482                        return $forum_id;
     483                }
     484
     485                // Parent ID
     486                $new_forum_mods = (array)$_POST['choose_forum_moderators'];
     487                $old_forum_mods = (array)$_POST['current_forum_moderators'];
     488
     489                foreach( $new_forum_mods as $new_forum_mod ){
     490                        $new_forum_mods_array[ get_userdata( $new_forum_mod )->user_login ] = $new_forum_mod;
     491                }
     492
     493                // Update the forum meta bidness
     494                update_post_meta( $forum_id, 'forum_mods', $new_forum_mods_array );
     495
     496                //which mods need their 'mod_for_forums' usermeta updated?  This is those that need to be deleted from this forum.
     497                $remove_forum_from_mods = array_diff( $old_forum_mods, $new_forum_mods );
     498
     499
     500                // for each individual mod in the removal list
     501                foreach( $remove_forum_from_mods as $remove_forum_from_mod ){
     502
     503                        // which forums do they currently mod?
     504                        $forums_modded = (array)get_user_meta( $remove_forum_from_mod, 'mod_for_forums', true );
     505                        // if this forum is in their forums modded list
     506                        if( $key = array_search( $forum_id, $forums_modded ) !== false ){
     507                                if( count( $forums_modded ) == 1 ){
     508                                        // delete if it's the last one
     509                                        delete_user_meta( $remove_forum_from_mod, 'mod_for_forums' );
     510                                }
     511                                else{
     512                                        // remove if they have more than one
     513                                        unset( $forums_modded[$key] );
     514                                        update_user_meta( $remove_forum_from_mod, 'mod_for_forums', $forums_modded );
     515                                }
     516                        }
     517                }
     518
     519
     520                // Now add the user to the forums selected that they're not currently modding. These are in the $new_forum_mods but not in $old_forum_mods
     521                $add_forum_to_mods = array_diff( $new_forum_mods, $old_forum_mods );
     522
     523                foreach( $add_forum_to_mods as $add_forum_to_mod ){
     524
     525                        // get all the forums they mod
     526                        $get_forums_modded =  (array)get_user_meta( $add_forum_to_mod, 'mod_for_forums', true );
     527
     528                        $forums_modded = ( !empty( $get_forums_modded ) ) ? (array)$get_forums_modded : array();
     529
     530                        if( array_search( $forum_id, $forums_modded ) === false ){
     531                                $forums_modded[] = $forum_id;
     532                                update_user_meta( $add_forum_to_mod, 'mod_for_forums', $forums_modded );
     533                        }
     534                }
     535        }
     536
    407537        /**
    408538         * Add some general styling to the admin area
    409539         *
  • src/includes/admin/js/common.js

     
    1111                        }
    1212                }
    1313        );
    14 } );
    15  No newline at end of file
     14
     15        jQuery( '#bbp_ajax_user_query' ).on('click', function(e){
     16
     17        var input = {};
     18        jQuery.each( jQuery( '.user_query_form input' ), function() {
     19
     20                if( this.type == 'radio' ){
     21                        if( jQuery( this ).is( ':checked' ) ){
     22                                input[this.name] = this.value;
     23                        }
     24                }
     25                else if( this.type == 'hidden' && this.name == 'bbp_action' ){
     26                        input['action'] = this.value;
     27                }
     28                else{
     29                        input[this.name] = this.value;
     30                }
     31
     32        });
     33
     34         //posts form and handles responses
     35        jQuery.ajax({
     36                type: 'POST',
     37                url : ajaxurl,
     38                data : input,
     39                dataType : 'json',
     40                success : function(reply) {
     41                        if(reply.result == 'success') {
     42                                if( jQuery('.search_for_mod_list').length > 0 ){
     43                                         jQuery('.search_for_mod_list').remove();
     44                                }
     45                                jQuery( '.user_query_form' ).append( '<h4 class="search_for_mod_list">' + reply.users + '</h4>' );
     46                        }
     47                        else{
     48                                jQuery( '.user_query_form' ).append('<p>booo</p>');
     49                        }
     50                }
     51        });
     52
     53        return false;
     54
     55        });
     56
     57    jQuery( '#test' ).on('click', function(){
     58        alert('whoo');
     59    });
     60
     61    jQuery( '.view-all-mods' ).on('click', function(e){
     62
     63        e.preventDefault();
     64
     65        var input = {}
     66        input['action'] = 'bbp_list_mods';
     67        input['forum_id'] = jQuery(this).data('forum');
     68
     69        jQuery.ajax({
     70            type: 'POST',
     71            url : ajaxurl,
     72            data : input,
     73            dataType : 'json',
     74            success : function( mods_reply ) {
     75                if(mods_reply.result == 'success') {
     76                    if( jQuery( this ).find( '.mod_list' ).length > 0 ){
     77                         jQuery( this ).find( '.mod_list' ).remove();
     78                    }
     79                    jQuery( '*[data-forum="' + mods_reply.forum + '"]' ).after( '<h4 class="mod_list">' + mods_reply.mods + '</h4>' );
     80                }
     81                else{
     82                     jQuery( '#forums-to-moderate' ).append('<p>booo</p>');
     83                }
     84            }
     85        });
     86    });
     87 });
  • src/includes/admin/js/topics.js

     
    11jQuery( document ).ready( function() {
    22        jQuery( '#misc-publishing-actions' ).find( '.misc-pub-section' ).first().remove();
    33        jQuery( '#save-action' ).remove();
     4
     5        jQuery( '#test' ).on('click', function(){
     6        alert('whoo');
     7    });
     8
     9    jQuery( 'td a.view-all-mods' ).on('click', function(){
     10
     11        e.preventDefault();
     12        alert( 'whoo' );
     13        var input = {}
     14        input['action'] = 'bbp_list_mods';
     15        input['forum_id'] = jQuery(this).data('forum');
     16
     17        jQuery.ajax({
     18            type: 'POST',
     19            url : ajaxurl,
     20            data : input,
     21            dataType : 'json',
     22            success : function(reply) {
     23                if(reply.result == 'success') {
     24                    if( jQuery( this ).find( '.mod_list' ).length > 0 ){
     25                         jQuery( this ).find( '.mod_list' ).remove();
     26                    }
     27                    jQuery( this ).closest( 'td' ).append( '<h4 class="mod_list">' + reply.users + '</h4>' );
     28                }
     29                else{
     30                    jQuery( this ).closest( 'td' ).append('<p>booo</p>');
     31                }
     32            }
     33        });
     34    });
     35
    436} );
  • src/includes/admin/metaboxes.php

     
    375375        do_action( 'bbp_forum_metabox', $post_id );
    376376}
    377377
     378function bbp_forum_moderators_metabox(){
     379        $post_id = get_the_ID();
     380        $get_current_moderators = get_post_meta( $post_id, 'forum_mods', true );
     381        $current_moderators = ( !empty( $get_current_moderators ) ) ? (array)$get_current_moderators : false;
     382        if( !empty( $current_moderators ) ){
     383                printf( '<h3 style="text-decoration: underline;">Current moderators</h3>' );
     384                foreach( $current_moderators as $mod_name => $mod_id ){
     385                        printf( '<strong><input type="checkbox" name="choose_forum_moderators[]" checked value="%1$d" /> %2$s (<a href="/wp-admin/user-edit.php?user_id=%1$d">user profile page</a>)</strong><br />', $mod_id, $mod_name );
     386                        printf( '<input type="hidden" name="current_forum_moderators[]" value="%d" />', $mod_id );
     387                }
     388        }
     389        printf( '<br />' );
     390        printf( '<h3 style="text-decoration: underline;">Search users</h3>' );
     391        printf( '<div class="user_query_form">' );
     392        printf( '<input type="hidden" name="bbp_action" value="bbp_ajax_user_search" />');
     393        printf( '<input type="hidden" name="current_mods" value="%s" />', implode( ",", $current_moderators ) );
     394        printf( '<input type="text" name="user_query" />');
     395        printf( '<a class="button" id="bbp_ajax_user_query">search for users</a> (will retrieve 50 max)' );
     396        printf( '<br /><input type="radio" name="role" value="all" checked="checked" />Search all users' );
     397        printf( '<br /><input type="radio" name="role" value="mods"  />Search all users with moderator capability' );
     398        printf( '</div>' );
     399        // printf( '<p><strong>The search box does not work, it is just for show at the moment. The current mods will show up above the search box. You should go and add one via the users to see!</strong></p>');
     400}
     401
    378402/** Topics ********************************************************************/
    379403
    380404/**
  • src/includes/admin/users.php

     
    4949
    5050                // User profile edit/display actions
    5151                add_action( 'edit_user_profile', array( $this, 'secondary_role_display' ) );
     52                // Save individual forums user is allowed to moderate
     53                add_action('edit_user_profile_update', array( $this, 'update_moderator_to_forums' ) );
    5254
     55                // I'm lazy, just going to add secondary_role_display to add new user for show
     56                add_action( 'user_new_form', array( $this, 'secondary_role_display' ) );
     57                add_action( 'user_register', array( $this, 'update_moderator_to_forums' ) );
     58                add_action( 'wp_ajax_bbp_list_mods', array( $this, 'bbp_list_mods' ) );
     59
    5360                // WordPress user screen
    5461                add_action( 'restrict_manage_users',      array( $this, 'user_role_bulk_dropdown' )        );
    5562                add_filter( 'manage_users_columns',       array( $this, 'user_role_column'        )        );
     
    113120                                                </select>
    114121                                        </td>
    115122                                </tr>
     123                                <tr id="forums-to-moderate">
     124                                        <th> Choose forums to moderate: <br />
     125                                                <em>This could show and hide cleverly.</em>
     126                                                <a href="#" id="test">whoo</a>
     127                                        </th>
     128                                        <?php
    116129
     130                                                $mod_for_forums = (array)get_user_meta( $profileuser->ID, 'mod_for_forums', true );
     131
     132                                                if( !empty( $mod_for_forums ) ){
     133                                                        foreach( $mod_for_forums as $mod_for_forum ){
     134                                                                printf('<input type="hidden" name="current_mod_for_forums[]" value="%d" />', $mod_for_forum );
     135                                                        }
     136                                                }
     137
     138                                                $args = array(
     139                                                        'post_type' => 'forum',
     140                                                        'posts_per_page' => -1
     141                                                );
     142                                                $forums_query =  new WP_Query();
     143                                                $forums = $forums_query->query( $args );
     144                                                $i = 0;
     145
     146                                                foreach( $forums as $forum ){
     147                                                        $checked = ( in_array( $forum->ID, $mod_for_forums ) ) ? 'checked' : '';
     148                                                        $i++;
     149                                                        printf('<td><input name="mod_for_forums[]" type="checkbox" value="%1$d" %2$s /> %3$s <a href="/wp-admin/post.php?post=%1$d&action=edit">edit forum</a> &bull; <a class="view-all-mods" href="#" data-forum="%1$d">view all forum mods</a></td>', $forum->ID, $checked, $forum->post_title );
     150                                                        if( $i % 4 === 0 ){
     151                                                                echo '</tr><tr><th></th>';
     152                                                        }
     153                                                }
     154                                        ?>
     155                                </tr>
    117156                        </tbody>
    118157                </table>
    119158
     
    120159                <?php
    121160        }
    122161
     162        public function update_moderator_to_forums( $user_id ){
     163                if ( ! current_user_can( 'edit_user', $user_id ) ){
     164                        return;
     165                }
     166
     167                $user_login = get_userdata( $user_id )->user_login;
     168
     169                $new_forums = (array)$_POST['mod_for_forums'];
     170                $old_forums = (array)$_POST['current_mod_for_forums'];
     171
     172                // Whatever is ticked gets put in usermeta, easy peasy.
     173                update_user_meta( $user_id, 'mod_for_forums', $new_forums);
     174
     175                // Updating the forums is a little more convoluted.
     176                // First check to see in which forums the user needs to be removed. If it's in the old one but not the new one, remove it.
     177
     178                $remove_mod_from_forums = array_diff( $old_forums, $new_forums );
     179
     180                foreach( $remove_mod_from_forums as $remove_mod_from_forum ){
     181                        // get the forum mods for each forum. this is an array of user_ids or somesuch, maybe with user_login as a key
     182                        $forum_mods = (array)get_post_meta( $remove_mod_from_forum, 'forum_mods', true );
     183                        if( !empty( $forum_mods[ $user_login ] ) ){
     184                                if( count( $forum_mods ) == 1 ){
     185                                        delete_post_meta( $remove_mod_from_forum, 'forum_mods' );
     186                                }
     187                                else{
     188                                        unset( $forum_mods[ $user_login ] );
     189                                        update_post_meta( $remove_mod_from_forum, 'forum_mods', $forum_mods );
     190                                }
     191                        }
     192                }
     193
     194
     195                // Now add the user to the forums selected that they're not currently modding. These are in the $new_forums but not in $old_forums
     196                $add_mod_to_forums = array_diff( $new_forums, $old_forums );
     197
     198                foreach( $add_mod_to_forums as $add_mod_to_forum ){
     199                        $get_forum_mods =  (array)get_post_meta( $add_mod_to_forum, 'forum_mods', true );
     200                        $forum_mods = ( !empty( reset( $get_forum_mods  ) ) ) ? $get_forum_mods : array();
     201
     202                        if( array_search( $user_id, $forum_mods ) === false ){
     203                                print_r( $forum_mods );
     204                                $display_name = get_userdata( $user_id )->user_login;
     205                                $forum_mods[$display_name] = $user_id;
     206                                print_r( $forum_mods );
     207                                update_post_meta( $add_mod_to_forum, 'forum_mods', $forum_mods );
     208                        }
     209                }
     210        }
     211
     212        public function bbp_list_mods(){
     213                if ( ! current_user_can( 'edit_user' ) ){
     214                        return;
     215                }
     216
     217                $forum_id = intval( $_POST['forum_id'] );
     218                $forum_mods_arrays = get_post_meta( $forum_id, 'forum_mods', true );
     219                $forum_mods_ids = array_values( $forum_mods_arrays );
     220
     221
     222                $args = array(
     223                        'include' => $forum_mods_ids
     224                );
     225
     226                ob_start();
     227
     228                if( !empty( $forum_mods_ids ) ){
     229                        $forum_mods = get_users( $args );
     230                        foreach( $forum_mods as $forum_mod ){
     231                                printf( '%s<br />', $forum_mod->display_name );
     232                        }
     233                }
     234                else{
     235                        echo 'No current forum mods';
     236                }
     237
     238                $list_mods = ob_get_contents();
     239                ob_end_clean();
     240
     241                $data = array('mods' => $list_mods, 'result' => 'success', 'forum' => $forum_id );
     242                echo json_encode($data);
     243                die;
     244
     245        }
     246
    123247        /**
    124248         * Add bulk forums role dropdown to the WordPress users table
    125249         *
     
    127251         */
    128252        public static function user_role_bulk_dropdown() {
    129253
    130                 // Bail if current user cannot promote users 
     254                // Bail if current user cannot promote users
    131255                if ( ! current_user_can( 'promote_users' ) ) {
    132256                        return;
    133257                }
     
    182306                // Bail if nonce check fails
    183307                check_admin_referer( 'bbp-bulk-users', 'bbp-bulk-users-nonce' );
    184308
    185                 // Bail if current user cannot promote users 
     309                // Bail if current user cannot promote users
    186310                if ( ! current_user_can( 'promote_users' ) ) {
    187311                        return;
    188312                }
     
    200324                        }
    201325
    202326                        // Set up user and role data
    203                         $user_role = bbp_get_user_role( $user_id );                     
     327                        $user_role = bbp_get_user_role( $user_id );
    204328                        $new_role  = sanitize_text_field( $_REQUEST['bbp-new-role'] );
    205329
    206330                        // Only keymasters can set other keymasters
  • src/includes/common/classes.php

     
    530530                ob_start(); ?>
    531531
    532532                <option class="<?php echo esc_attr( $class ); ?>" value="<?php echo esc_attr( $value ); ?>"<?php selected( $args['selected'], $object->ID ); ?> <?php disabled( in_array( $reply_id, $ancestors ), true ); ?>><?php echo $pad . esc_html( $title ); ?></option>
    533                
     533
    534534                <?php
    535535
    536536                // Append the output buffer to the $output variable
  • src/includes/forums/capabilities.php

     
    278278                return false;
    279279        }
    280280
    281         // Setup default values
    282         $term_ids      = wp_parse_id_list( $terms );
    283         $taxonomy      = bbp_get_forum_mod_tax_id();
    284         $moderator_ids = array();
     281        $moderator_ids = array_values( (array)get_post_meta( $forum_id, 'forum_mods' ) );
    285282
    286         // Convert term ids to user ids.
    287         foreach ( $term_ids as $term_id ) {
    288                 $moderator_ids[] = bbp_get_term_taxonomy_user_id( $term_id, $taxonomy );
    289         }
    290 
    291         // Remove empties
    292         $retval = wp_parse_id_list( array_filter( $moderator_ids ) );
    293 
    294283        // Filter & return
    295         return apply_filters( 'bbp_get_forum_moderator_ids', $retval, $forum_id );
     284        return apply_filters( 'bbp_get_forum_moderator_ids', $moderator_ids, $forum_id );
    296285}
    297286
    298287/**
     
    325314                return false;
    326315        }
    327316
    328         // Convert user id to term id.
    329         $taxonomy = bbp_get_forum_mod_tax_id();
    330         $term_id  = bbp_get_user_taxonomy_term_id( $user_id, $taxonomy );
    331 
    332317        // Get moderator forums.
    333         $forums   = get_objects_in_term( $term_id, $taxonomy );
     318        $forums   = (array)$user->mod_for_forums;
    334319
    335320        // Forums found.
    336321        if ( empty( $forums ) || is_wp_error( $forums ) ) {
     
    345330                }
    346331        }
    347332
    348         // Remove empties
    349         $retval = wp_parse_id_list( array_filter( $forum_ids ) );
    350 
    351333        // Filter & return
    352         return apply_filters( 'bbp_get_moderator_forum_ids', $retval, $user_id );
     334        return apply_filters( 'bbp_get_moderator_forum_ids', $forum_ids, $user_id );
    353335}
    354336
    355337/**
  • src/includes/users/functions.php

     
    19051905        return bbp_bump_user_reply_count( $user_id, -1 );
    19061906}
    19071907
    1908 /** User Nicename Taxonomies **************************************************/
     1908/** User Forums Moderated **************************************************/
    19091909
    1910 /**
    1911  * Return the term id for a given user id and taxonomy
    1912  *
    1913  * @since bbPress (r5834)
    1914  *
    1915  * @param int    $user_id User id.
    1916  * @param string $taxonomy Taxonomy.
    1917  * @uses get_userdata() To get the user data
    1918  * @uses taxonomy_exists() To make sure the taxonomy exists
    1919  * @uses get_term_by() To get the term by name
    1920  *
    1921  * @return boolean|int Return false early, or if not found, or int term id
    1922  */
    1923 function bbp_get_user_taxonomy_term_id( $user_id = 0, $taxonomy = '' ) {
    19241910
    1925         // Bail if no user ID.
    1926         if ( empty( $user_id ) ) {
    1927                 return false;
    1928         }
    19291911
    1930         // Bail if user does not exist.
    1931         $user = get_userdata( $user_id );
    1932         if ( empty( $user ) ) {
    1933                 return false;
    1934         }
    1935 
    1936         // Bail if no taxonomy.
    1937         if ( empty( $taxonomy ) || ! taxonomy_exists( $taxonomy ) ) {
    1938                 return false;
    1939         }
    1940 
    1941         // Get the term id.
    1942         $term = get_term_by( 'name', $user->user_nicename, $taxonomy );
    1943         if ( ! empty( $term ) ) {
    1944                 return $term->term_id;
    1945         }
    1946 
    1947         return false;
    1948 }
    1949 
    1950 /**
    1951  * Return the user id for a given term id and taxonomy
    1952  *
    1953  * @since bbPress (r5834)
    1954  *
    1955  * @param int    $term_id Term id.
    1956  * @param string $taxonomy Taxonomy.
    1957  * @uses taxonomy_exists() To make sure the taxonomy exists
    1958  * @uses get_term() To get the term by term id
    1959  * @uses get_user_by() To get the user by nicename
    1960  *
    1961  * @return boolean|int Return false early, or if not found, or int user id
    1962  */
    1963 function bbp_get_term_taxonomy_user_id( $term_id = 0, $taxonomy = '' ) {
    1964 
    1965         // Bail if no user ID.
    1966         if ( empty( $term_id ) ) {
    1967                 return false;
    1968         }
    1969 
    1970         // Bail if no taxonomy.
    1971         if ( empty( $taxonomy ) || ! taxonomy_exists( $taxonomy ) ) {
    1972                 return false;
    1973         }
    1974 
    1975         // Bail if no term exists.
    1976         $term = get_term( $term_id, $taxonomy );
    1977         if ( empty( $term ) ) {
    1978                 return false;
    1979         }
    1980 
    1981         // Get the user by nicename.
    1982         $nicename = $term->name;
    1983         $user     = get_user_by( 'slug', $nicename );
    1984         if ( ! empty( $user ) ) {
    1985                 return $user->ID;
    1986         }
    1987 
    1988         return false;
    1989 }
    1990 
    1991 function bbp_filter_forum_mod_term_link( $termlink = '', $term = '', $taxonomy = '' ) {
    1992 
    1993         // Bail if taxonomy is not forum mod
    1994         if ( bbp_get_forum_mod_tax_id() !== $taxonomy ) {
    1995                 return $termlink;
    1996         }
    1997 
    1998         // Bail if forum mods is not allowed
    1999         if ( ! bbp_allow_forum_mods() ) {
    2000                 return $termlink;
    2001         }
    2002 
    2003         // Get user ID from taxonomy term
    2004         $user_id = bbp_get_term_taxonomy_user_id( $term->term_id, bbp_get_forum_mod_tax_id() );
    2005 
    2006         if ( is_admin() ) {
    2007 
    2008                 // Get the moderator's display name
    2009                 $display_name = get_userdata( $user_id )->display_name;
    2010                 $user_link    = get_edit_user_link( $user_id );
    2011 
    2012                 // Link or name only
    2013                 if ( ! empty( $user_link ) ) {
    2014                         $retval = '<a href="' . esc_url( $user_link ) . '">' . esc_html( $display_name ) . '</a>';
    2015 
    2016                 // Can't edit
    2017                 } else {
    2018                         $retval = $display_name;
    2019                 }
    2020 
    2021         // Theme side term link
    2022         } else {
    2023                 $retval = bbp_get_user_profile_link( $user_id );
    2024         }
    2025 
    2026         return $retval;
    2027 }
    2028 
    20291912/** Permissions ***************************************************************/
    20301913
    20311914/**