diff --git a/bin/cache.php b/bin/cache.php index 08dfd48..7973430 100644 --- a/bin/cache.php +++ b/bin/cache.php @@ -7,6 +7,8 @@ set_time_limit( 0 ); +// Stats page + $cache_key = \PressbooksStats\Stats\get_admin_page_html_cache_key(); delete_site_transient( $cache_key ); @@ -20,3 +22,19 @@ } else { echo "Successfully cached the stats dashboard! \n"; } + +// Network storage + +$cache_key = 'pb_stats_network_storage'; + +delete_site_transient( $cache_key ); + +\PressbooksStats\Stats\cache_network_storage(); + +$storage = get_site_transient( $cache_key ); + +if ( empty( $storage ) ) { + echo "Failed to cache network storage data... \n"; +} else { + echo "Successfully cached network storage data! \n"; +} diff --git a/inc/stats/namespace.php b/inc/stats/namespace.php index c96a0c7..a225978 100644 --- a/inc/stats/namespace.php +++ b/inc/stats/namespace.php @@ -482,9 +482,24 @@ function users_with_x_or_more_books( $x ) { function display_network_storage() { $storage = get_site_transient( 'pb_stats_network_storage' ); - if ( empty( $storage ) ) { - $storage = format_bytes( recurse_dirsize( wp_upload_dir()['basedir'] ) ); - set_site_transient( 'pb_stats_network_storage', $storage, DAY_IN_SECONDS ); + if ( ! empty( $storage ) ) { + $cached = ''; + } else { + $cached = ''; + $storage = calculate_network_storage(); } - printf( '

%1$s: %2$s

', __( 'Network Storage', 'pressbooks-stats' ), $storage ); + printf( '%1$s

%2$s: %3$s

', $cached, __( 'Network Storage', 'pressbooks-stats' ), $storage ); +} + +function calculate_network_storage() { + $path = wp_upload_dir()['basedir']; + $storage = format_bytes( rtrim( str_replace( $path, '', `du -b -s $path` ) ) ); + return $storage; +} + +/** + * Cache the network storage level + */ +function cache_network_storage() { + set_site_transient( 'pb_stats_network_storage', calculate_network_storage() ); }