Last active
April 14, 2022 16:26
-
-
Save stephanieleary/85979b0a355171a6f023cbea0172f444 to your computer and use it in GitHub Desktop.
Multisite Network Administration functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Texas A&M Multisite Network Administration | |
Version: 0.4.2 | |
Plugin URI: https://github.tamu.edu/COE-wordpress/coe-network-admin | |
Description: Adds columns to the Network Sites list screen; subsite user login redirects. | |
Author: Stephanie Leary | |
Author URI: http://stephanieleary.com | |
GitHub Plugin URI: https://github.tamu.edu/COE-wordpress/coe-network-admin | |
GitHub Enterprise: https://github.tamu.edu | |
*/ | |
// Default user roles | |
// Add new users to main site as subscriber | |
function tees_add_network_default_roles( $user_id ) { | |
global $current_site; // this is the main network site. Stupid variable name. | |
if ( !is_user_member_of_blog( $user_id, $current_site->blog_id ) ) | |
add_user_to_blog( $current_site->blog_id, $user_id, 'subscriber' ); | |
} | |
add_action( 'wpmu_activate_user', 'tees_add_network_default_roles', 10, 1 ); | |
add_action( 'wpmu_new_user', 'tees_add_network_default_roles', 10, 1 ); | |
add_action( 'user_register', 'tees_add_network_default_roles', 10, 1 ); | |
// Login redirect | |
// Redirect to top-level dashboard (instead of profile) if not superadmin or not allowed on this blog | |
add_filter( 'login_redirect', 'tees_subsite_login_redirect', 100, 3 ); | |
function tees_subsite_login_redirect( $redirect_to, $request_redirect_to, $user ) { | |
if ( !is_user_member_of_blog() || ( is_main_site() && !is_super_admin() ) ) | |
return network_admin_url(); | |
return $redirect_to; | |
} | |
// My Sites Dashboard widget | |
function tees_network_dashboard_widget_setup() { | |
add_meta_box( | |
'tees_dashboard_mysites_widget', | |
'My Sites', | |
'tees_dashboard_mysites_widget', | |
'dashboard', | |
'normal', | |
'high' | |
); | |
} | |
add_action( 'wp_dashboard_setup', 'tees_network_dashboard_widget_setup', 99 ); | |
function tees_dashboard_mysites_widget() { | |
echo '<ul>'; | |
$blogs = get_blogs_of_user( get_current_user_id() ); | |
foreach ( $blogs as $blog ) { | |
echo '<li><a href="'. esc_url( get_admin_url( $blog->userblog_id ) ) .'" title="'. sprintf( esc_html__( '%s Dashboard', 'tees' ), $blog->blogname ) .'">'. esc_html( $blog->blogname ) .'</a></li>'; | |
} | |
echo '</ul>'; | |
} | |
// Admin columns | |
add_filter( 'wpmu_blogs_columns', 'tees_sites_admin_column_headers' ); | |
add_action( 'manage_sites_custom_column', 'tees_sites_admin_add_columns', 10, 2 ); | |
add_action( 'manage_blogs_custom_column', 'tees_sites_admin_add_columns', 10, 2 ); | |
add_action( 'manage_sites-network_sortable_columns', 'tees_sites_admin_sort_columns' ); | |
function tees_sites_admin_query_args( $args ) { | |
$args['orderby'] = 'uploads'; | |
return $args; | |
} | |
function tees_sites_admin_sort_columns( $columns ) { | |
$columns['upload_size'] = 'uploads'; | |
return $columns; | |
} | |
add_action( 'plugins_loaded', 'tees_sites_admin_filter_site_query' ); | |
function tees_sites_admin_filter_site_query() { | |
global $pagenow; | |
if ( is_super_admin() && 'sites.php' == $pagenow && isset( $_REQUEST['orderby'] ) && 'uploads' == $_REQUEST['orderby'] ) | |
add_filter( 'sites_clauses', 'tees_sites_clauses' ); | |
} | |
function tees_sites_clauses( $query ) { | |
global $wpdb; | |
$query['fields'] = "*"; | |
$query['join'] = " as b, {$wpdb->sitemeta} as m"; | |
$query['where'] = "m.`meta_key` = 'upload_usage' AND b.`blog_id` = m.`site_id`"; | |
$query['orderby'] = "CAST(m.meta_value AS UNSIGNED) DESC"; | |
$query['groupby'] = 'b.`blog_id`'; | |
return $query; | |
} | |
function tees_sites_admin_add_columns( $column_name, $blog_id ) { | |
switch ( $column_name ) { | |
case 'upload_size': | |
$usage = get_metadata( 'site', $blog_id, 'upload_usage', true ); | |
if ( empty( $usage ) ) | |
echo '0'; | |
else | |
echo tees_sites_admin_filesize_format( $usage ); | |
default; break; | |
} | |
return $column_name; | |
} | |
function tees_sites_admin_column_headers( $columns ) { | |
$columns['upload_size'] = __( 'Uploads' ); | |
return $columns; | |
} | |
function tees_sites_admin_filesize_format( $size ) { | |
$units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); | |
$power = $size > 0 ? floor(log($size, 1024)) : 0; | |
return number_format($size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power]; | |
} | |
// Run cron task to update site options with upload directory file size | |
add_action( 'update_network_counts', 'tees_sites_admin_filesize_cron_exec' ); | |
function tees_sites_admin_filesize_cron_exec() { | |
global $wpdb; | |
$blogs = $wpdb->get_results( "SELECT blog_id FROM {$wpdb->blogs}", ARRAY_A ); | |
if ( empty( $blogs ) ) | |
return; | |
foreach ( (array) $blogs as $blogdetails ) { | |
$blog_id = absint( $blogdetails['blog_id'] ); | |
if ( is_main_site( $blog_id ) ) { | |
$upload_dir = wp_upload_dir(); | |
$upload_dir = $upload_dir['basedir']; | |
} | |
else | |
$upload_dir = ABSPATH . UPLOADBLOGSDIR . $blog_id . '/files'; | |
$usage = get_dirsize( $upload_dir ); | |
update_metadata( 'site', $blog_id, 'upload_usage', $usage ); | |
sleep(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment