Making WordPress.org

Changeset 12747


Ignore:
Timestamp:
07/18/2023 05:41:27 AM (14 months ago)
Author:
dd32
Message:

Support Forums: Moderators: Allow moderators on the English forums to block users.

Fixes #5707.

Location:
sites/trunk/wordpress.org/public_html/wp-content
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-moderators.php

    r12012 r12747  
    2121        add_filter( 'bbp_map_primary_meta_caps',        array( $this, 'map_meta_caps' ), 10, 4 );
    2222        add_action( 'bbp_post_request',                 array( $this, 'edit_user_handler' ), 0 );
     23
     24        // Allow moderators to manage user roles.
     25        add_filter( 'bbp_get_caps_for_role',            array( $this, 'bbp_get_caps_for_role' ), 10, 2 );
     26
     27        // Limit which roles a moderator can assign to a user. Before bbp_profile_update_role().
     28        add_action( 'bbp_profile_update',               array( $this, 'bbp_profile_update' ), 1 );
    2329
    2430        // Append 'view=all' to forum, topic, and reply URLs in moderator views.
     
    185191                    }
    186192
    187                     if ( 'promote_user' === $cap || 'promote_users' === $cap ) {
    188                         // Only keymasters can promote users.
    189                         $caps = array( 'keep_gate' );
    190                     } else {
    191                         $caps = array( 'moderate' );
    192                     }
     193                    $caps = array( 'moderate' );
    193194                }
    194195                break;
     
    196197
    197198        return $caps;
     199    }
     200
     201    /**
     202     * Allow (global) moderators to assign roles to users.
     203     *
     204     * @param array  $caps Role capabilities.
     205     * @param string $role Role name.
     206     * @return array
     207     */
     208    function bbp_get_caps_for_role( $caps, $role ) {
     209        if (
     210            $role === bbp_get_moderator_role() &&
     211            Plugin::get_instance()->is_main_forums
     212        ) {
     213            $caps['promote_users'] = true;
     214        }
     215   
     216        return $caps;
     217    }
     218
     219    /**
     220     * Limit the site/forum roles a moderator can set.
     221     */
     222    public function bbp_profile_update() {
     223        // Keymasters need no special handling.
     224        if ( bbp_is_user_keymaster( get_current_user_id() ) ) {
     225            return;
     226        }
     227
     228        $new_forum_role = sanitize_key( $_POST['bbp-forums-role'] ?? '' );
     229
     230        // Prevent setting any roles.
     231        unset( $_POST['role'], $_POST['bbp-forums-role'] );
     232
     233        $allowed_roles = array(
     234            bbp_get_participant_role(),
     235            bbp_get_spectator_role(),
     236            bbp_get_blocked_role()
     237        );
     238
     239        // If it's an allowed role, add it back so it can be processed by bbp_profile_update_role().
     240        if ( in_array( $new_forum_role, $allowed_roles, true ) ) {
     241            $_POST['bbp-forums-role'] = $new_forum_role;
     242        }
    198243    }
    199244
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-plugin.php

    r12292 r12747  
    4242
    4343    /**
     44     * Define whether this is the global forums, or a locale forum.
     45     * ie. https://wordpress.org/support/
     46     *
     47     * @var bool
     48     */
     49    public $is_main_forums = false;
     50
     51    /**
    4452     * Always return the same instance of this plugin.
    4553     *
     
    6775        $this->audit_log    = new Audit_Log;
    6876
     77        // Set a flag to indicate whether this is the global forums, or a locale forum.
     78        $this->is_main_forums = (
     79            defined( 'WPORG_SUPPORT_FORUMS_BLOGID' ) &&
     80            WPORG_SUPPORT_FORUMS_BLOGID == get_current_blog_id()
     81        );
     82
    6983        // These modifications are specific to https://wordpress.org/support/
    70         $blog_id = get_current_blog_id();
    71         if ( $blog_id && defined( 'WPORG_SUPPORT_FORUMS_BLOGID' ) && WPORG_SUPPORT_FORUMS_BLOGID == $blog_id ) {
     84        if ( $this->is_main_forums ) {
    7285            $this->dropin          = new Dropin;
    7386            $this->support_compat  = new Support_Compat;
    7487
    7588            // Only load Performance_Optimizations if necessary.
    76             $this->performance = new Performance_Optimizations;
     89            $this->performance     = new Performance_Optimizations;
    7790
    7891            // Ratings_Compat is loaded by Theme_Directory_Compat or
     
    8295        }
    8396
     97        // Only load the Block Support if the Blocks Everywhere plugin is available.
    8498        if ( class_exists( 'Automattic\Blocks_Everywhere\Blocks_Everywhere' ) ) {
    85             $this->blocks = new Blocks;
     99            $this->blocks          = new Blocks;
    86100        }
    87101
Note: See TracChangeset for help on using the changeset viewer.