Skip to content

Commit f5c5c63

Browse files
committed
Learn: Sync with git WordPress/Learn@702d7b7
git-svn-id: https://meta.svn.wordpress.org/sites/trunk@15187 74240141-8908-4e6f-9713-ba540dce6ec7
1 parent c1cf6f0 commit f5c5c63

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

‎wordpress.org/public_html/wp-content/plugins/wporg-learn/inc/capabilities.php‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
add_action( 'pre_get_posts', __NAMESPACE__ . '\scope_learn_content_list_to_author' );
1212
add_filter( 'user_has_cap', __NAMESPACE__ . '\set_caps_for_internal_notes' );
1313
add_filter( 'map_meta_cap', __NAMESPACE__ . '\map_meta_caps', 20, 4 ); // Needs to fire after meta caps in wporg-internal-notes.
14+
add_filter( 'editable_roles', __NAMESPACE__ . '\restrict_editable_roles' );
15+
add_action( 'load-user-new.php', __NAMESPACE__ . '\restrict_invited_user_role' );
1416
add_action( 'init', __NAMESPACE__ . '\add_or_update_lesson_plan_editor_role' );
1517
add_action( 'init', __NAMESPACE__ . '\add_or_update_workshop_reviewer_role' );
1618

@@ -170,6 +172,70 @@ function map_meta_caps( $required_caps, $current_cap, $user_id, $args ) {
170172
return $required_caps;
171173
}
172174

175+
/**
176+
* Limit role assignment to capabilities the current user already holds.
177+
*
178+
* Compare primitive grants, including dynamically assigned capabilities. Checking mapped
179+
* capabilities would exclude roles with grants such as `unfiltered_html` that multisite
180+
* restricts independently of the role. Multisite super admins inherently hold all capabilities.
181+
*
182+
* @param array[] $roles Array of arrays containing role information.
183+
*
184+
* @return array[]
185+
*/
186+
function restrict_editable_roles( $roles ) {
187+
$user = wp_get_current_user();
188+
$is_super_admin = is_multisite() && is_super_admin( $user->ID );
189+
190+
foreach ( $roles as $slug => $role ) {
191+
foreach ( array_keys( array_filter( $role['capabilities'] ) ) as $capability ) {
192+
if ( 'do_not_allow' === $capability ) {
193+
unset( $roles[ $slug ] );
194+
break;
195+
}
196+
197+
if ( $is_super_admin || 'exist' === $capability ) {
198+
continue;
199+
}
200+
201+
/** This filter is documented in wp-includes/class-wp-user.php */
202+
$user_caps = apply_filters(
203+
'user_has_cap',
204+
$user->allcaps,
205+
array( $capability ),
206+
array( $capability, $user->ID ),
207+
$user
208+
);
209+
210+
if ( empty( $user_caps[ $capability ] ) ) {
211+
unset( $roles[ $slug ] );
212+
break;
213+
}
214+
}
215+
}
216+
217+
return $roles;
218+
}
219+
220+
/**
221+
* Enforce editable roles before multisite stores an existing-user invitation.
222+
*
223+
* Core's confirmation flow stores the requested role without validating it against editable roles.
224+
*
225+
* @return void
226+
*/
227+
function restrict_invited_user_role() {
228+
if ( ! is_multisite() || ! isset( $_REQUEST['action'] ) || 'adduser' !== $_REQUEST['action'] ) {
229+
return;
230+
}
231+
232+
check_admin_referer( 'add-user', '_wpnonce_add-user' );
233+
234+
// Validate the exact value core persists; sanitizing it could validate a different role.
235+
$role = isset( $_REQUEST['role'] ) && is_string( $_REQUEST['role'] ) ? $_REQUEST['role'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
236+
wp_ensure_editable_role( $role );
237+
}
238+
173239
/**
174240
* Add the Lesson Plan Editor role if it doesn't exist yet, or ensure it has the correct capabilities.
175241
*
@@ -287,6 +353,7 @@ function add_or_update_workshop_reviewer_role() {
287353
*
288354
* The Workshop Reviewer should have all the same caps as the Editor role, with the addition of `promote_users`
289355
* (normally reserved for the Admin role), so that they can add workshop presenters as new users on the site.
356+
* `restrict_editable_roles` limits role assignment to capabilities the current user already holds.
290357
*
291358
* This also gives them the cap to manage internal notes on workshop posts. (See `set_caps_for_internal_notes` above.)
292359
*

‎wordpress.org/public_html/wp-content/themes/pub/wporg-learn-2024/style.css‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Author: WordPress.org
55
* Author URI: http://wordpress.org/
66
* Description: A theme for learn.wordpress.org, built in 2024.
7-
* Version: 1.0.0-7910bad
7+
* Version: 1.0.0-b0e7779
88
* License: GNU General Public License v2 or later
99
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
1010
* Text Domain: wporg-learn

0 commit comments

Comments
 (0)