Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 66
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
wpcomsh_storage_notices
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
72
wpcomsh_display_disk_space_usage
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
20
wpcomsh_debug_information_disk_usage
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Storage notices file.
4 *
5 * @package wpcomsh
6 */
7
8/**
9 * Adds an admin notice if the site's space_used is 95% or higher of its space_quota.
10 */
11function wpcomsh_storage_notices() {
12    global $pagenow;
13    $site_info = wpcomsh_get_at_site_info();
14
15    if ( empty( $site_info['space_used'] ) || empty( $site_info['space_quota'] ) ) {
16        return;
17    }
18
19    $space_used  = intval( $site_info['space_used'] );
20    $space_quota = intval( $site_info['space_quota'] );
21
22    // Info (0-95% usage)
23    $notice_class = 'info';
24
25    // Warning (95%-99% usage)
26    if ( $space_used > $space_quota * 0.95 ) {
27        $notice_class = 'warning';
28    }
29
30    // Error (100%+ usage)
31    if ( $space_used > $space_quota ) {
32        $notice_class = 'error';
33    }
34
35    // Show the info notice only on the media library page.
36    if ( $notice_class === 'info' && ( $pagenow !== 'upload.php' || isset( $_GET['page'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
37        return;
38    }
39
40    $message = sprintf(
41        /* translators: 1: Upload space used; 2: Upload space allowed; 3: percentage of allowed space used */
42        __(
43            'You are currently using <strong>%1$s</strong> out of <strong>%2$s</strong> upload limit (%3$s%%).',
44            'wpcomsh'
45        ),
46        size_format( $space_used, 1 ),
47        size_format( $space_quota, 1 ),
48        number_format_i18n( ( $space_used / $space_quota ) * 100.0 )
49    );
50
51    printf(
52        '<div class="notice notice-%s">
53            <p>%s</p>
54        </div>',
55        esc_attr( $notice_class ),
56        wp_kses_post( $message )
57    );
58}
59add_action( 'admin_notices', 'wpcomsh_storage_notices' );
60
61/**
62 * Display disk space usage on the uploader
63 */
64function wpcomsh_display_disk_space_usage() {
65    global $pagenow;
66
67    if ( $pagenow === 'upload.php' ) {
68        return;
69    }
70
71    $site_info = wpcomsh_get_at_site_info();
72
73    if ( empty( $site_info['space_used'] ) || empty( $site_info['space_quota'] ) ) {
74        return;
75    }
76
77    $space_used  = intval( $site_info['space_used'] );
78    $space_quota = intval( $site_info['space_quota'] );
79
80    $message = sprintf(
81        /* translators: 1: Upload space used; 2: Upload space allowed; 3: percentage of allowed space used */
82        __(
83            'You are currently using <strong>%1$s</strong> out of <strong>%2$s</strong> upload limit (%3$s%%).',
84            'wpcomsh'
85        ),
86        size_format( $space_used, 1 ),
87        size_format( $space_quota, 1 ),
88        number_format_i18n( ( $space_used / $space_quota ) * 100.0 )
89    );
90
91    printf( '<p>%s</p>', wp_kses_post( $message ) );
92}
93add_action( 'pre-upload-ui', 'wpcomsh_display_disk_space_usage' );
94
95/**
96 * Debug information disk usage - /wp-admin/site-health.php?tab=debug
97 *
98 * @param array $args The arguments.
99 *
100 * @return array
101 */
102function wpcomsh_debug_information_disk_usage( $args ) {
103    if ( empty( $args['wp-paths-sizes']['fields'] ) ) {
104        return $args;
105    }
106
107    $site_info = wpcomsh_get_at_site_info();
108
109    if ( empty( $site_info['space_used'] ) || empty( $site_info['space_quota'] ) ) {
110        return $args;
111    }
112
113    $space_used  = $site_info['space_used'];
114    $space_quota = $site_info['space_quota'];
115
116    unset( $args['wp-paths-sizes']['fields']['total_size'] );
117    $args['wp-paths-sizes']['fields']['wpcomsh-disk-space-used']  = array(
118        'label' => __( 'Disk space used', 'wpcomsh' ),
119        'value' => size_format( $space_used, 1 ),
120    );
121    $args['wp-paths-sizes']['fields']['wpcomsh-disk-space-quota'] = array(
122        'label' => __( 'Disk space quota', 'wpcomsh' ),
123        'value' => size_format( $space_quota, 1 ),
124    );
125
126    return $args;
127}
128add_filter( 'debug_information', 'wpcomsh_debug_information_disk_usage' );