Skip to content

Instantly share code, notes, and snippets.

@glaubersilva
Last active April 10, 2020 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glaubersilva/1635201acd516e9b8c2bc601e6dc8b30 to your computer and use it in GitHub Desktop.
Save glaubersilva/1635201acd516e9b8c2bc601e6dc8b30 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: [Defender] Load Roles From All Child Sites
* Plugin URI: https://premium.wpmudev.org/
* Description: If the current url is from "advanced tools" configuration page and the site is multisite, this plugin load all roles from the multisite network. In this way, we make it possible to configure 2-factor authentication for any role present on any of the sites of the entire network.
* Author: Glauber Silva @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* Task: 0/11289012348292/1168965543535336
* License: GPLv2 or later
*
* @package Defender_Load_Roles_From_All_Child_Sites
*/
defined( 'ABSPATH' ) || exit;
/**
* Filters the list of editable roles.
*
* @since 2.8.0
*
* @param array[] $all_roles Array of arrays containing role information.
*/
function wpmudev_get_roles_from_all_sites( $all_roles ) {
if ( is_multisite() && isset( $_GET['page'] ) && 'wdf-advanced-tools' === $_GET['page'] ) { //phpcs:ignore
$sites = get_sites();
if ( ! empty( $sites ) ) {
foreach ( $sites as $site ) {
switch_to_blog( $site->blog_id );
$roles_current_site = wp_roles()->roles;
$all_roles = array_merge( $all_roles, $roles_current_site );
restore_current_blog();
}
}
}
return $all_roles;
}
add_filter( 'editable_roles', 'wpmudev_get_roles_from_all_sites' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment