Skip to content

Commit

Permalink
Merge branch 'release-v2'
Browse files Browse the repository at this point in the history
  • Loading branch information
angelavu committed Aug 28, 2018
2 parents 979140e + a2a57b2 commit 28235a1
Show file tree
Hide file tree
Showing 8 changed files with 371 additions and 35 deletions.
73 changes: 59 additions & 14 deletions classes/Admin_Interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,41 +64,86 @@ public function render_msg() {
. $this->msg_text . '</p></div>';
}

/*
* An edit of wp_dropdown_roles() for checkboxes and to allow remembering selections after submitting
*/

public function mubr_checkbox_roles( ) {
$r = '';
$editable_roles = array_reverse( get_editable_roles() );
$site_roles = get_site_option( $this->option_name );

foreach ( $editable_roles as $role => $details ) {
$name = translate_user_role($details['name'] );

$checked = '';
if ( is_array( $site_roles ) ) {
$checked = in_array( $role, $site_roles ) ? ' checked="checked"' : '';
}
$r .="\n\t<div>";
$r .= "\n\t\t<input type='checkbox' id='" . esc_attr( $role ) . "' name='" . $this->option_name . "[]" . "' value='" . esc_attr( $role ) . "'$checked>";
$r .= "\n\t\t<label for='" . esc_attr( $role ) . "'>$name</label>";
$r .="\n\t</div>";
}
echo $r;
}

/**
* Display content of network options page
*/
public function render_options_page() {
$option = esc_attr( stripslashes( get_site_option( $this->option_name ) ) );
//$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>
<p>Select a role or multiple roles 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">
<div class="mubr tablenav top">
<div class="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>
<fieldset name="<?php echo $this->option_name; ?>" id="<?php echo $this->option_name; ?>">
<legend>Select Role(s)</legend>
<?php $this->mubr_checkbox_roles( ); ?>
</fieldset>
<?php submit_button( 'Create Report', 'action', 'submit', false ); ?>
</form>
</div>
</div>

<?php if ( get_site_option( $this->option_name ) && is_network_admin() ) {
<?php $active_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : 'sort_by_user'; ?>

$user_list = new MUBR_User_List();
$user_list->setRole( get_site_option( $this->option_name ) );
$user_list->loadUsers();
<div id="mubr-nav">
<h2 class="nav-tab-wrapper">
<a href="?page=multisite_users_selected_role&tab=sort_by_user" class="nav-tab <?php echo $active_tab == 'sort_by_user' ? 'nav-tab-active' : ''; ?>">Sort By User</a>
<a href="?page=multisite_users_selected_role&tab=sort_by_site" class="nav-tab <?php echo $active_tab == 'sort_by_site' ? 'nav-tab-active' : ''; ?>">Sort By Site</a>
<a href="?page=multisite_users_selected_role&tab=emails" class="nav-tab <?php echo $active_tab == 'emails' ? 'nav-tab-active' : ''; ?>">Comma Separated Emails</a>
</h2>
</div>

echo $user_list->output();
<?php if ( get_site_option( $this->option_name ) && is_network_admin() ) {

if( $active_tab == 'sort_by_user' ) {
$user_list = new MUBR_User_List();
$user_list->setRoles( get_site_option( $this->option_name ) );
$user_list->loadUsers();
echo $user_list->output();
} elseif( $active_tab == 'sort_by_site' ) {
$site_list = new MUBR_Site_List();
$site_list->setRoles( get_site_option( $this->option_name ) );
$site_list->loadSites();
echo $site_list->output();
} else { // 'emails'
$user_list = new MUBR_User_List();
$user_list->setRoles( get_site_option( $this->option_name ) );
$user_list->loadUsers();
echo $user_list->email_output();
}
} else {
echo '<p>Please select a role to generate this report. If a role is already selected, please click generate.</p>';
} ?>
Expand All @@ -108,7 +153,7 @@ public function render_options_page() {
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';
Expand Down
53 changes: 53 additions & 0 deletions classes/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ class MUBR_Site {
//vars
protected $name;
protected $url;
protected $users;

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

public function siteLink() {
Expand All @@ -26,4 +28,55 @@ public function url() {
public function name() {
return $this->name;
}

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

public function addUser( $user ){
$this->users[] = new MUBR_User(
$user->ID,
$user->user_email,
get_user_meta($user->ID, 'first_name', true),
get_user_meta($user->ID, 'last_name', true),
$user->roles
);
$this->users = $this->sortUsers( $this->users );
}

public function usersEmail() {
$output = '';
foreach ( $this->users as $user ) {
$output .= $user->email();
$output .= '<br>';
}
return $output;
}

public function usersLF() {
$output = '';
foreach ( $this->users as $user ) {
$output .= $user->nameLF();
$output .= '<br>';
}
return $output;
}

public function usersRole() {
$output = '';
foreach ( $this->users as $user ) {
$output .= $user->role();
$output .= '<br>';
}
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() );
}
}
116 changes: 116 additions & 0 deletions classes/Site_List.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
* Object to store list of all sites and their users
*/
class MUBR_Site_List {
//vars
protected $sites;
protected $users;
protected $roles;

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

public function setRoles( $roles ) {
$this->roles = $roles;
}

public function output( ) {
$output = '';
$output .= '
<table class="mubr-table widefat fixed posts">
<thead>
<tr>
<th>Site</th>
<th>Name</th>
<th>Email</th>
<th>Role</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Site</th>
<th>Name</th>
<th>Email</th>
<th>Role</th>
</tr>
</tfoot>
<tbody>';

if ( !empty( $this->users ) ) {
foreach( $this->sites as $site ) {
$siteUsers = $site->users();

$output .= '<tr><td>';
$output .= $site->siteLink();
$output .= '</td><td>';
$output .= (!empty($siteUsers) ? $site->usersLF() : 'No Data');
$output .= '</td><td>';
$output .= (!empty($siteUsers) ? $site->usersEmail() : 'No Data');
$output .= '</td><td>';
$output .= (!empty($siteUsers) ? $site->usersRole() : 'No Data');
$output .= '</td></tr>';
}
} else {
$output .= '<tr>
<td colspan="4">No Data Found</td>
</tr>';
}
$output .= '</tbody>
</table>';
return $output;
}

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

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

public function loadSites( ) {
if ( isset( $this->roles ) ) {
//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;
$info = get_blog_details( $blog_id );
$this->sites[$blog_id] = new MUBR_Site(
$info->blogname,
$info->siteurl
);

$users = get_users( array(
'blog_id' => $blog_id,
'role__in' => $this->roles
) );

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),
$user->roles
);
}
$this->users[ $user->ID ]->addSite( $blog_id );
$this->sites[ $blog_id ]->addUser( $user );
}
}
$this->sites = $this->sortSites( $this->sites );
}
}
}
13 changes: 10 additions & 3 deletions classes/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ class MUBR_User {
protected $first_name;
protected $last_name;
protected $sites;
protected $role;

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

$this->role = $role;
}

public function addSite( $site ) {
Expand All @@ -32,12 +33,19 @@ public function addSite( $site ) {
public function email() {
return '<a href="mailto:' . $this->email . '">' . $this->email . '</a>';
}
public function emailOnly() {
return $this->email;
}
public function last_name() {
return $this->last_name;
}
public function first_name() {
return $this->first_name;
}
public function role() {
$this->role = reset($this->role);
return ucfirst($this->role);
}
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>';
Expand All @@ -54,7 +62,6 @@ public function sites() {
return $output;
}


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

0 comments on commit 28235a1

Please sign in to comment.