Skip to content

Commit

Permalink
Merge branch 'release-v1'
Browse files Browse the repository at this point in the history
  • Loading branch information
taija committed Jan 5, 2017
2 parents 936509d + 204142a commit 979140e
Show file tree
Hide file tree
Showing 7 changed files with 384 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Multisite Users by Role

This plugin is designed for WordPress Multisite installations, and is used to list all users who have a particular role on any site in the network.

An example of this would be to easily grab a list of all 'Administrator' users in a multisite network.

## Usage
1. Upload plugin files to /wp-content/plugins/
2. Network activate plugin
3. Menu will appear under **Users** on the Network Admin Dashboard


## Disclaimer
This plugin is in an 'alpha' state, and still has many missing features and best practices in implimentation. Use entirely at your own risk.

Pull requests are very welcome!
128 changes: 128 additions & 0 deletions classes/Admin_Interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
* Admin Interface Class
*
* Sets up Admin interface in WordPress
*/
class MUBR_Admin_Interface {
protected static $instance = NULL;
protected $action = 'gen_multisite_user_list';
protected $option_name = 'multisite_user_list_selected_role';
protected $page_id = NULL;

/**
* Access this plugin’s working instance
*
* @wp-hook wp_loaded
* @return object of this class
*/
public static function get_instance() {
NULL === self::$instance and self::$instance = new self;
return self::$instance;
}

/**
* Register menu pages
*/
public function register() {
add_action( 'network_admin_menu', array ( $this, 'add_menu' ) );
add_action( "admin_post_$this->action", array ( $this, 'admin_post' ) );
}

public function add_menu() {
$page_id = add_users_page(
'List All Multisite Users by Role',
'Network User List',
'manage_network_options',
'multisite_users_selected_role',
array ( $this, 'render_options_page' )
);
add_action( "load-$page_id", array ( $this, 'parse_message' ) );
}

public function parse_message() {
if ( ! isset ( $_GET['msg'] ) )
return;

$text = FALSE;

if ( 'updated' === $_GET['msg'] )
$this->msg_text = 'Updated!';

if ( 'deleted' === $_GET['msg'] )
$this->msg_text = 'Deleted!';

if ( $this->msg_text )
add_action( 'admin_notices', array ( $this, 'render_msg' ) );
}

public function render_msg() {
echo '<div class="' . esc_attr( $_GET['msg'] ) . '"><p>'
. $this->msg_text . '</p></div>';
}

/**
* Display content of network options page
*/
public function render_options_page() {
$option = esc_attr( stripslashes( get_site_option( $this->option_name ) ) );
$redirect = urlencode( remove_query_arg( 'msg', $_SERVER['REQUEST_URI'] ) );
$redirect = urlencode( $_SERVER['REQUEST_URI'] ); ?>

<div class="wrap">
<h1><?php echo $GLOBALS['title']; ?></h1>
<p>Select a role to generate a list of all users with that role, along with the sites to which they are assigned.</p>

<div class="tablenav top">
<div class="alignleft actions bulkactions">
<form action="<?php echo admin_url( 'admin-post.php' ); ?>" method="POST">
<input type="hidden" name="action" value="<?php echo $this->action; ?>">
<?php wp_nonce_field( $this->action, $this->option_name . '_nonce', FALSE ); ?>
<input type="hidden" name="_wp_http_referer" value="<?php echo $redirect; ?>">
<label for="<?php echo $this->option_name; ?>" class="screen-reader-text">Select role</label>
<select name="<?php echo $this->option_name; ?>" id="<?php echo $this->option_name; ?>">
<option value="-1">Select Role</option>
<?php wp_dropdown_roles( $option ); ?>
</select>
<?php submit_button( 'Create Report', 'action', 'submit', false ); ?>
</form>
</div>
</div>

<?php if ( get_site_option( $this->option_name ) && is_network_admin() ) {

$user_list = new MUBR_User_List();
$user_list->setRole( get_site_option( $this->option_name ) );
$user_list->loadUsers();

echo $user_list->output();
} else {
echo '<p>Please select a role to generate this report. If a role is already selected, please click generate.</p>';
} ?>
</div>
<?php }

public function admin_post() {
if ( ! wp_verify_nonce( $_POST[ $this->option_name . '_nonce' ], $this->action ) )
die( 'Invalid nonce.' . var_export( $_POST, true ) );

if ( isset ( $_POST[ $this->option_name ] ) ) {
update_site_option( $this->option_name, $_POST[ $this->option_name ] );
$msg = 'updated';
} else {
delete_site_option( $this->option_name );
$msg = 'deleted';
}

if ( ! isset ( $_POST['_wp_http_referer'] ) )
die( 'Missing target.' );

$url = add_query_arg( 'msg', $msg, urldecode( $_POST['_wp_http_referer'] ) );

wp_safe_redirect( $url );
exit;
}
}
29 changes: 29 additions & 0 deletions classes/Site.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
* Object to store site data
*/
class MUBR_Site {
//vars
protected $name;
protected $url;

public function __construct( $name, $url ) {
$this->name = $name;
$this->url = $url;
}

public function siteLink() {
return "<a href='$this->url'>$this->name</a>";
}

public function url() {
return $this->url;
}

public function name() {
return $this->name;
}
}
68 changes: 68 additions & 0 deletions classes/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
* Object to store user information
*/
class MUBR_User {

protected $id;
protected $email;
protected $first_name;
protected $last_name;
protected $sites;

public function __construct( $id, $email, $first_name, $last_name ) {
$this->id = $id;
$this->email = $email;
$this->first_name = $first_name;
$this->last_name = $last_name;

}

public function addSite( $site ) {
$info = get_blog_details( $site );
$this->sites[]= new MUBR_Site(
$info->blogname,
$info->siteurl
);
$this->sites = $this->sortSites( $this->sites );
}
public function email() {
return '<a href="mailto:' . $this->email . '">' . $this->email . '</a>';
}
public function last_name() {
return $this->last_name;
}
public function first_name() {
return $this->first_name;
}
public function nameLF() {
if ( $this->last_name && $this->first_name ) {
return '<a href="' . get_edit_user_link( $this->id ) . '">' . $this->last_name . ', ' . $this->first_name . '</a>';
} else {
return '<a href="' . get_edit_user_link( $this->id ) . '">' . '<i>undefined</i>' . '</a>';
}
}
public function sites() {
$output = '';
foreach ( $this->sites as $site ) {
$output .= $site->siteLink();
$output .= '<br>';
}
return $output;
}


private function sortSites( $data ) {
usort( $data, array( $this, 'sortBySiteName' ) );
return $data;
}

private function sortBySiteName( $a, $b) {
return strnatcmp( $a->name(), $b->name() );
}


}
104 changes: 104 additions & 0 deletions classes/User_List.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
* Object to store list of all users
*/
class MUBR_User_List {
//vars
protected $users;
protected $role;

public function __construct() {
$this->users = Array();
}

public function setRole( $role ) {
$this->role = $role;
}

public function output( ) {

$output = '';
$output .= '
<table class="wp-list-table widefat fixed posts">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Sites</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Email</th>
<th>Sites</th>
</tr>
</tfoot>
<tbody>';

if ( !empty( $this->users ) ) {

foreach( $this->users as $row ) {
$output .= '<tr><td>';
$output .= $row->nameLF();
$output .= '</td><td>';
$output .= $row->email();
$output .= '</td><td>';
$output .= $row->sites();
$output .= '</td></tr>';
}
} else {
$output .= '<tr>
<td colspan="3">No Data Found</td>
</tr>';
}
$output .= '</tbody>
</table>';
return $output;
}

private function sortUsers( $data ) {
usort( $data, array( $this, 'sortByLName' ) );
return $data;
}

private function sortByLName( $a, $b) {
return strnatcmp( $a->last_name(), $b->last_name() );
}

public function loadUsers( ) {
if ( isset( $this->role ) ) {
//Get array of public and private blogs
global $wpdb;
$blogs = get_sites( array(
'number' => 2048, // arbitrary large number
'archived' => 0,
'deleted' => 0,

) );
foreach ( $blogs as $blog ) {
$blog_id = $blog->blog_id;
$users = get_users( array(
'blog_id' => $blog_id,
'role' => $this->role
) );

foreach ( $users as $user ) {
if ( ! array_key_exists( $user->ID, $this->users ) ) {
$this->users[ $user->ID ] = new MUBR_User(
$user->ID,
$user->user_email,
get_user_meta($user->ID, 'first_name', true),
get_user_meta($user->ID, 'last_name', true)
);
}
$this->users[ $user->ID ]->addSite( $blog_id );
}

}
$this->users = $this->sortUsers( $this->users );
}
}
}
27 changes: 27 additions & 0 deletions multisite-users-by-role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/*
Plugin Name: Multisite Users by Role
Description: List all users with a certain role across a multisite network
Plugin URI: https://github.com/BellevueCollege/multisite-users-by-role/
Author: Taija Tevia-Clark
Version: 1
Author URI: http://www.bellevuecollege.edu
GitHub Plugin URI: BellevueCollege/multisite-users-by-role
Text Domain: mubr
*/

/**
* Based on the following sources:
* Shortcode to list all admins: http://wordpress.stackexchange.com/a/55997
* Building a settings page: http://wordpress.stackexchange.com/a/79899
* Sorting arrays of objects: http://www.the-art-of-web.com/php/sortarray/
*/

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

include( 'classes/Admin_Interface.php' );
include( 'classes/User_List.php' );
include( 'classes/User.php' );
include( 'classes/Site.php' );

add_action( 'wp_loaded', array ( MUBR_Admin_Interface::get_instance(), 'register' ) );
12 changes: 12 additions & 0 deletions uninstall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
// If uninstall is not called from WordPress, exit
if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit();
}

$option_name = 'multisite_user_list_selected_role';

delete_option( $option_name );

// For site options in Multisite
delete_site_option( $option_name );

0 comments on commit 979140e

Please sign in to comment.