Skip to:
Content

bbPress.org

Ticket #459: 459.5.diff

File 459.5.diff, 19.1 KB (added by netweb, 11 years ago)
  • src/bbpress.php

     
    223223
    224224                // Post type identifiers
    225225                $this->forum_post_type   = apply_filters( 'bbp_forum_post_type',  'forum'     );
     226                $this->forum_mod_tax_id  = apply_filters( 'bbp_forum_mod_tax_id', 'forum-mod' );
    226227                $this->topic_post_type   = apply_filters( 'bbp_topic_post_type',  'topic'     );
     228                $this->topic_tag_tax_id  = apply_filters( 'bbp_topic_tag_tax_id', 'topic-tag' );
    227229                $this->reply_post_type   = apply_filters( 'bbp_reply_post_type',  'reply'     );
    228                 $this->topic_tag_tax_id  = apply_filters( 'bbp_topic_tag_tax_id', 'topic-tag' );
    229230
    230231                // Status identifiers
    231232                $this->spam_status_id    = apply_filters( 'bbp_spam_post_status',    'spam'    );
     
    647648        }
    648649
    649650        /**
    650          * Register the topic tag taxonomy
     651         * Register the topic tag and forum moderator taxonomies
    651652         *
    652653         * @since bbPress (r2464)
    653654         * @uses register_taxonomy() To register the taxonomy
     
    671672                                'show_ui'               => bbp_allow_topic_tags() && current_user_can( 'bbp_topic_tags_admin' )
    672673                        )
    673674                ) );
     675
     676                // Register the forum-mod taxonomy
     677                register_taxonomy(
     678                        bbp_get_forum_mod_tax_id(),
     679                        bbp_get_forum_post_type(),
     680                        apply_filters( 'bbp_register_forum_moderator_taxonomy', array(
     681                                'labels'                => bbp_get_forum_mod_tax_labels(),
     682                                'capabilities'          => bbp_get_forum_mod_caps(),
     683                                'update_count_callback' => '_update_post_term_count',
     684                                'query_var'             => false,
     685                                'show_tagcloud'         => true,
     686                                'hierarchical'          => false,
     687                                'show_in_nav_menus'     => false,
     688                                'public'                => false,
     689                                'show_ui'               => bbp_allow_forum_mods() && current_user_can( 'bbp_forum_mods_admin' )
     690                        )
     691                ) );
    674692        }
    675693
    676694        /**
  • src/includes/admin/forums.php

     
    5858        private function setup_actions() {
    5959
    6060                // Add some general styling to the admin area
    61                 add_action( 'bbp_admin_head',        array( $this, 'admin_head'       ) );
     61                add_action( 'bbp_admin_head',          array( $this, 'admin_head'              ) );
    6262
    6363                // Messages
    64                 add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) );
     64                add_filter( 'post_updated_messages',   array( $this, 'updated_messages'        ) );
    6565
    6666                // Metabox actions
    67                 add_action( 'add_meta_boxes',        array( $this, 'attributes_metabox'      ) );
    68                 add_action( 'save_post',             array( $this, 'attributes_metabox_save' ) );
     67                add_action( 'add_meta_boxes',          array( $this, 'attributes_metabox'      ) );
     68                add_action( 'save_post',               array( $this, 'attributes_metabox_save' ) );
    6969
     70                // Forum moderators AJAX; run at -1 to preempt built-in tag search
     71                add_action( 'wp_ajax_ajax-tag-search', array( $this, 'ajax_tag_search'         ), -1 );
     72
    7073                // Forum Column headers.
    7174                add_filter( 'manage_' . $this->post_type . '_posts_columns',        array( $this, 'column_headers' )        );
    7275
     
    262265        }
    263266
    264267        /**
     268         * Return user nicename suggestions instead of tag suggestions
     269         *
     270         * @since bbPress (rXXXX)
     271         *
     272         * @return Return early if not a request for forum moderators tax
     273         */
     274        public function ajax_tag_search() {
     275
     276                // Only do AJAX if this is a forum moderators tax search
     277                if ( ! isset( $_GET['tax'] ) || ( bbp_get_forum_mod_tax_id() !== $_GET['tax'] ) ) {
     278                        return;
     279                }
     280
     281                $taxonomy = sanitize_key( $_GET['tax'] );
     282                $tax      = get_taxonomy( $taxonomy );
     283                if ( empty( $tax ) ) {
     284                        wp_die( 0 );
     285                }
     286
     287                // Check permissions
     288                if ( ! current_user_can( $tax->cap->assign_terms ) ) {
     289                        wp_die( -1 );
     290                }
     291
     292                $s = stripslashes( $_GET['q'] );
     293
     294                // Replace tag delimiter with a comma if needed
     295                $comma = _x( ',', 'tag delimiter', 'bbpress' );
     296                if ( ',' !== $comma ) {
     297                        $s = str_replace( $comma, ',', $s );
     298                }
     299
     300                if ( false !== strpos( $s, ',' ) ) {
     301                        $s = explode( ',', $s );
     302                        $s = $s[ count( $s ) - 1 ];
     303                }
     304
     305                // Search on at least 2 characters
     306                $s = trim( $s );
     307                if ( strlen( $s ) < 2 ) {
     308                        wp_die(); // require 2 chars for matching
     309                }
     310
     311                // Get users
     312                $results = array();
     313                $users   = get_users( array(
     314                        'blog_id'        => 0, // All users
     315                        'fields'         => array( 'user_nicename' ),
     316                        'search'         => '*' . $s . '*',
     317                        'search_columns' => array( 'user_nicename' ),
     318                        'orderby'        => 'user_nicename'
     319                ) );
     320
     321                // Format the users into a nice array
     322                if ( ! empty( $users ) ) {
     323                        foreach( array_values( $users ) as $details ) {
     324                                $results[] = $details->user_nicename;
     325                        }
     326                }
     327
     328                // Echo results for AJAX
     329                echo join( $results, "\n" );
     330                wp_die();
     331        }
     332
     333        /**
    265334         * Pass the forum attributes for processing
    266335         *
    267336         * @since bbPress (r2746)
  • src/includes/core/filters.php

     
    239239// Capabilities
    240240add_filter( 'bbp_map_meta_caps', 'bbp_map_primary_meta_caps',   10, 4 ); // Primary caps
    241241add_filter( 'bbp_map_meta_caps', 'bbp_map_forum_meta_caps',     10, 4 ); // Forums
     242add_filter( 'bbp_map_meta_caps', 'bbp_map_forum_mod_meta_caps', 10, 4 ); // Forum moderator
    242243add_filter( 'bbp_map_meta_caps', 'bbp_map_topic_meta_caps',     10, 4 ); // Topics
     244add_filter( 'bbp_map_meta_caps', 'bbp_map_topic_tag_meta_caps', 10, 4 ); // Topic tags
    243245add_filter( 'bbp_map_meta_caps', 'bbp_map_reply_meta_caps',     10, 4 ); // Replies
    244 add_filter( 'bbp_map_meta_caps', 'bbp_map_topic_tag_meta_caps', 10, 4 ); // Topic tags
    245246
    246247/** Deprecated ****************************************************************/
    247248
  • src/includes/core/options.php

     
    235235}
    236236
    237237/**
     238 * Are per-forum moderators allowed
     239 *
     240 * @since bbPress (rXXXX)
     241 * @param $default bool Optional. Default value true
     242 * @uses get_option() To get the allow tags
     243 * @return bool Are per-forum moderators allowed?
     244 */
     245function bbp_allow_forum_mods( $default = 1 ) {
     246        return (bool) apply_filters( 'bbp_allow_forum_mods', (bool) get_option( '_bbp_allow_forum_mods', $default ) );
     247}
     248
     249/**
    238250 * Is forum-wide searching allowed
    239251 *
    240252 * @since bbPress (r4970)
  • src/includes/forums/capabilities.php

     
    179179
    180180                /** Admin *************************************************************/
    181181
     182                // Forum admin area
    182183                case 'bbp_forums_admin' :
    183184                        $caps = array( 'keep_gate' );
    184185                        break;
     186
     187                // Forum moderator admin area
     188                case 'bbp_forum_mods_admin' :
     189                        $caps = array( 'keep_gate' );
     190                        break;
    185191        }
    186192
    187193        return apply_filters( 'bbp_map_forum_meta_caps', $caps, $cap, $user_id, $args );
    188194}
     195
     196/**
     197 * Return forum moderator capabilities
     198 *
     199 * @since bbPress (rXXXX)
     200 *
     201 * @uses apply_filters() Calls 'bbp_get_forum_mod_caps' with the capabilities
     202 * @return array Forum mod capabilities
     203 */
     204function bbp_get_forum_mod_caps() {
     205        return apply_filters( 'bbp_get_forum_mod_caps', array(
     206                'manage_terms' => 'keep_gate',
     207                'edit_terms'   => 'keep_gate',
     208                'delete_terms' => 'keep_gate',
     209                'assign_terms' => 'keep_gate'
     210        ) );
     211}
     212
     213/**
     214 * Maps forum moderator capabilities
     215 *
     216 * @since bbPress (rXXXX)
     217 *
     218 * @param array $caps Capabilities for meta capability
     219 * @param string $cap Capability name
     220 * @param int $user_id User id
     221 * @param mixed $args Arguments
     222 * @uses apply_filters() Filter capabilities map results
     223 * @return array Actual capabilities for meta capability
     224 */
     225function bbp_map_forum_mod_meta_caps( $caps, $cap, $user_id, $args ) {
     226
     227        // What capability is being checked?
     228        switch( $cap ) {
     229                case 'manage_forum_mods'    :
     230                case 'edit_forum_mods'      :
     231                case 'delete_forum_mods'    :
     232                case 'assign_forum_mods'    :
     233                case 'bbp_forum_mods_admin' :
     234
     235                        // Key Masters can always edit
     236                        if ( user_can( $user_id, 'keep_gate' ) ) {
     237                                $caps = array( 'keep_gate' );
     238                        }
     239        }
     240
     241        return apply_filters( 'bbp_map_forum_mod_meta_caps', $caps, $cap, $user_id, $args );
     242}
     243
     244/**
     245 * Get moderators of a forum
     246 *
     247 * @since bbPress (rXXXX)
     248 *
     249 * @param $forum_id Forum id
     250 * @uses bbp_is_forum() To make sure it is a forum
     251 * @uses bbp_get_forum_mod_tax_id() To get the forum moderator taxonomy
     252 * @uses wp_get_object_terms() To get the forum's moderator terms
     253 * @uses is_wp_error() To check for errors
     254 * @uses bbp_get_term_taxonomy_user_id() To convert terms to user ids
     255 * @return boolean Return false early, or if no moderator terms set, or
     256 * @return array User ids
     257 */
     258function bbp_get_forum_mod_ids( $forum_id = 0 ) {
     259
     260        // Bail if no forum ID
     261        $forum_id = bbp_get_forum_id( $forum_id );
     262        if ( empty( $forum_id ) ) {
     263                return false;
     264        }
     265
     266        // Bail if forum does not exist
     267        if ( ! bbp_is_forum( $forum_id ) ) {
     268                return false;
     269        }
     270
     271        // Get forum taxonomy terms
     272        $taxonomy = bbp_get_forum_mod_tax_id();
     273        $terms    = wp_get_object_terms( $forum_id, $taxonomy, array(
     274                'fields' => 'ids'
     275        ) );
     276
     277        // Bail if no terms found
     278        if ( empty( $terms ) || is_wp_error( $terms ) ) {
     279                return false;
     280        }
     281
     282        $moderators = array();
     283
     284        // Convert term ids to user ids
     285        foreach ( $terms as $term ) {
     286                $user_id = bbp_get_term_taxonomy_user_id( $term, $taxonomy );
     287                if ( !empty( $user_id ) ) {
     288                        $moderators[] = $user_id;
     289                }
     290        }
     291
     292        // Moderators found
     293        if ( !empty( $moderators ) ) {
     294                return $moderators;
     295        }
     296
     297        return false;
     298}
     299
     300/**
     301 * Get forums of a moderator
     302 *
     303 * @since bbPress (rXXXX)
     304 *
     305 * @param $user_id User id
     306 * @uses get_userdata() To get the user object
     307 * @uses bbp_get_forum_mod_tax_id() To get the forum moderator taxonomy
     308 * @uses get_term_by() To get the term id
     309 * @uses get_objects_in_term() Get the forums the user moderates
     310 * @uses is_wp_error() To check for errors
     311 * @uses bbp_is_forum() To make sure the objects are forums
     312 * @return boolean Return false early, or if user has no forums, or
     313 * @return array Forum ids
     314 */
     315function bbp_get_moderator_forum_ids( $user_id = 0 ) {
     316
     317        // Bail if no user ID
     318        if ( empty( $user_id ) ) {
     319                return false;
     320        }
     321
     322        // Bail if user does not eist
     323        $user = get_userdata( $user_id );
     324        if ( empty( $user ) ) {
     325                return false;
     326        }
     327
     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
     332        // Get moderator forums
     333        $forums = get_objects_in_term( $term_id, $taxonomy );
     334
     335        // Forums found
     336        if ( empty( $forums ) || is_wp_error( $forums ) ) {
     337                return false;
     338        }
     339
     340        // Make sure the ids returned are forums
     341        $forum_ids = array();
     342        foreach ( $forums as $forum_id ) {
     343                if ( bbp_is_forum( $forum_id ) ) {
     344                        $forum_ids[] = $forum_id;
     345                }
     346        }
     347
     348        return $forum_ids;
     349}
     350
     351/**
     352 * Can a user moderate a forum?
     353 *
     354 * @since bbPress (rXXXX)
     355 *
     356 * @param int $user_id
     357 * @param int $forum_id
     358 * @return bool
     359 */
     360function bbp_is_user_forum_mod( $user_id = 0, $forum_id = 0 ) {
     361
     362        // Assume user cannot moderate the forum
     363        $retval    = false;
     364
     365        // Validate user ID - fallback to current user if no ID passed
     366        $user_id   = bbp_get_user_id( $user_id, false, ! empty( $user_id ) );
     367        $forum_id  = bbp_get_forum_id( $forum_id );
     368
     369        // Get forums the user can moderate
     370        $forum_ids = bbp_get_moderator_forum_ids( $user_id );
     371
     372        // Is this forum ID in the users array of forum IDs?
     373        if ( ! empty( $forum_ids ) ) {
     374                $retval = in_array( $forum_id, $forum_ids );
     375        }
     376
     377        return (bool) apply_filters( 'bbp_is_user_forum_mod', $retval, $user_id, $forum_id, $forum_ids );
     378}
     379 No newline at end of file
  • src/includes/forums/template.php

     
    21602160                return apply_filters( 'bbp_get_single_forum_description', $retstr, $r );
    21612161        }
    21622162
     2163/** Moderators ****************************************************************/
     2164
     2165/**
     2166 * Output the unique id of the forum moderators taxonomy
     2167 *
     2168 * @since bbPress (rXXXX)
     2169 *
     2170 * @uses bbp_get_forum_mod_tax_id() To get the forum modorator taxonomy ID
     2171 */
     2172function bbp_forum_mod_tax_id() {
     2173        echo bbp_get_forum_mod_tax_id();
     2174}
     2175        /**
     2176         * Return the unique id of the forum moderators taxonomy
     2177         *
     2178         * @since bbPress (rXXXX)
     2179         *
     2180         * @uses apply_filters() Calls 'bbp_get_forum_mod_tax_id' with the forum
     2181         *                        moderator taxonomy id
     2182         * @return string The unique forum moderators taxonomy
     2183         */
     2184        function bbp_get_forum_mod_tax_id() {
     2185                return apply_filters( 'bbp_get_forum_mod_tax_id', bbpress()->forum_mod_tax_id );
     2186        }
     2187
     2188/**
     2189 * Return array of labels used by the forum-mod taxonomy
     2190 *
     2191 * @since bbPress (rXXXX)
     2192 *
     2193 * @return array
     2194 */
     2195function bbp_get_forum_mod_tax_labels() {
     2196        return apply_filters( 'bbp_get_topic_tag_tax_labels', array(
     2197                'name'                       => __( 'Forum Moderators',     'bbpress' ),
     2198                'singular_name'              => __( 'Forum Moderator',      'bbpress' ),
     2199                'search_items'               => __( 'Search Moderators',    'bbpress' ),
     2200                'popular_items'              => __( 'Popular Moderators',   'bbpress' ),
     2201                'all_items'                  => __( 'All Moderators',       'bbpress' ),
     2202                'edit_item'                  => __( 'Edit Moderator',       'bbpress' ),
     2203                'update_item'                => __( 'Update Moderator',     'bbpress' ),
     2204                'add_new_item'               => __( 'Add New Moderator',    'bbpress' ),
     2205                'new_item_name'              => __( 'New Moderator Name',   'bbpress' ),
     2206                'view_item'                  => __( 'View Forum Moderator', 'bbpress' ),
     2207                'separate_items_with_commas' => __( 'Separate moderator names with commas', 'bbpress' )
     2208        ) );
     2209}
     2210
    21632211/** Forms *********************************************************************/
    21642212
    21652213/**
  • src/includes/replies/capabilities.php

     
    131131
    132132                                // Unknown, so map to edit_others_posts
    133133                                } else {
    134                                         $caps[] = $post_type->cap->edit_others_posts;
     134
     135                                        // If user is a per-forum moderator, make sure they can spectate
     136                                        if ( bbp_is_user_forum_mod( $user_id, bbp_get_reply_forum_id( $_post->ID ) ) ) {
     137                                                $caps = array( 'spectate' );
     138
     139                                        // Fallback to edit_others_posts
     140                                        } else {
     141                                                $caps[] = $post_type->cap->edit_others_posts;
     142                                        }
    135143                                }
    136144                        }
    137145
  • src/includes/topics/capabilities.php

     
    149149
    150150                                // Unknown, so map to edit_others_posts
    151151                                } else {
    152                                         $caps[] = $post_type->cap->edit_others_posts;
     152
     153                                        // If user is a per-forum moderator, make sure they can spectate
     154                                        if ( bbp_is_user_forum_mod( $user_id, bbp_get_topic_forum_id( $_post->ID ) ) ) {
     155                                                $caps = array( 'spectate' );
     156
     157                                        // Fallback to edit_others_posts
     158                                        } else {
     159                                                $caps[] = $post_type->cap->edit_others_posts;
     160                                        }
    153161                                }
    154162                        }
    155163
  • src/includes/users/capabilities.php

     
    3535
    3636                        // Moderators are always participants
    3737                        } else {
     38
     39                                // Default to the current cap
    3840                                $caps = array( $cap );
     41
     42                                // Bail if no post to check
     43                                if ( ( 'moderate' !== $cap ) || empty( $args[0] ) ) {
     44                                        break;
     45                                }
     46
     47                                // Get the post
     48                                $_post = get_post( $args[0] );
     49                                if ( empty( $_post ) ) {
     50                                        break;
     51                                }
     52
     53                                // Get forum ID for specific type of post
     54                                switch ( $_post->post_type ) {
     55
     56                                        // Forum
     57                                        case bbp_get_forum_post_type() :
     58                                                $forum_id = $_post->ID;
     59                                                break;
     60
     61                                        // Topic
     62                                        case bbp_get_topic_post_type() :
     63                                                $forum_id = bbp_get_topic_forum_id( $_post->ID );
     64                                                break;
     65
     66                                        // Reply
     67                                        case bbp_get_reply_post_type() :
     68                                                $forum_id = bbp_get_reply_forum_id( $_post->ID );
     69                                                break;
     70
     71                                        // Any other post type defaults to 0
     72                                        default :
     73                                                $forum_id = 0;
     74                                                break;
     75                                }
     76
     77                                // Bail if no forum ID
     78                                if ( empty( $forum_id ) ) {
     79                                        break;
     80                                }
     81
     82                                // If user is a per-forum moderator, make sure they can spectate
     83                                if ( bbp_is_user_forum_mod( $user_id, $forum_id ) ) {
     84                                        $caps = array( 'spectate' );
     85                                }
    3986                        }
    4087
    4188                        break;
  • src/includes/users/functions.php

     
    16811681        return bbp_bump_user_reply_count( $user_id, -1 );
    16821682}
    16831683
     1684/** User Nicename Taxonomies **************************************************/
     1685
     1686/**
     1687 * Return the term id for a given user id and taxonomy
     1688 *
     1689 * @since bbPress (rXXXX)
     1690 *
     1691 * @param int $user_id User id
     1692 * @param string $taxonomy Taxonomy
     1693 * @uses get_userdata() To get the user data
     1694 * @uses taxonomy_exists() To make sure the taxonomy exists
     1695 * @uses get_term_by() To get the term by name
     1696 *
     1697 * @return boolean Return false early, or if not found, or
     1698 * @return int Term id
     1699 */
     1700function bbp_get_user_taxonomy_term_id( $user_id = 0, $taxonomy = '' ) {
     1701
     1702        // Bail if no user ID
     1703        if ( empty( $user_id ) ) {
     1704                return false;
     1705        }
     1706
     1707        // Bail if user does not exist
     1708        $user = get_userdata( $user_id );
     1709        if ( empty( $user ) ) {
     1710                return false;
     1711        }
     1712
     1713        // Bail if no taxonomy
     1714        if ( empty( $taxonomy ) || ! taxonomy_exists( $taxonomy ) ) {
     1715                return false;
     1716        }
     1717
     1718        // Get the term id
     1719        $term = get_term_by( 'name', $user->user_nicename, $taxonomy );
     1720        if ( ! empty( $term ) ) {
     1721                return $term->term_id;
     1722        }
     1723
     1724        return false;
     1725}
     1726
     1727/**
     1728 * Return the user id for a given term id and taxonomy
     1729 *
     1730 * @since bbPress (rXXXX)
     1731 *
     1732 * @param int $term_id Term id
     1733 * @param string $taxonomy
     1734 * @uses taxonomy_exists() To make sure the taxonomy exists
     1735 * @uses get_term() To get the term by term id
     1736 * @uses get_user_by() To get the user by nicename
     1737 *
     1738 * @return boolean Return false early, or if not found, or
     1739 * @return int User id
     1740 */
     1741function bbp_get_term_taxonomy_user_id( $term_id = 0, $taxonomy = '' ) {
     1742
     1743        // Bail if no user ID
     1744        if ( empty( $term_id ) ) {
     1745                return false;
     1746        }
     1747
     1748        // Bail if no taxonomy
     1749        if ( empty( $taxonomy ) || ! taxonomy_exists( $taxonomy ) ) {
     1750                return false;
     1751        }
     1752
     1753        // Bail if no term exists
     1754        $term = get_term( $term_id, $taxonomy );
     1755        if ( empty( $term ) ) {
     1756                return false;
     1757        }
     1758
     1759        // Get the user by nicename
     1760        $nicename = $term->name;
     1761        $user     = get_user_by( 'slug', $nicename );
     1762        if ( !empty( $user ) ) {
     1763                return $user->ID;
     1764        }
     1765
     1766        return false;
     1767}
     1768
    16841769/** Permissions ***************************************************************/
    16851770
    16861771/**